/* * MIT License * * Copyright (c) 2018-2020 Ardalan Amini * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import * as Odin from ".."; import Query from "../base/Query"; import DB, { Operator, JoinQuery, Order, Callback, Iterator } from "../DB"; import Filter from "../DB/Filter"; import Join from "../DB/Join"; declare namespace Relation { interface Relation { name: string; relations: Relation[]; } interface RelationCount { name: string; relation?: RelationCount; } } declare abstract class Relation { readonly model: Odin; readonly relation: typeof Odin; readonly localKey: string; readonly foreignKey: string; protected readonly filter: (q: Filter) => Filter; readonly as: string; constructor(model: Odin, relation: typeof Odin, localKey: string, foreignKey: string, filter: (q: Filter) => Filter, caller: (...args: any[]) => any); protected _query(relations?: string[]): Query; abstract load(query: DB | Join, relations: Relation.Relation[], withTrashed?: boolean, filter?: (q: Filter) => Filter): DB | Join; abstract loadCount(query: DB | Join, relations: string[], withTrashed?: boolean, filter?: (q: Filter) => Filter): DB | Join; /******************************* With Trashed *******************************/ withTrashed>(): Query; /*********************************** Lean ***********************************/ lean>(): Query; /****************************** With Relations ******************************/ with(...relations: string[]): Query; /*********************************** Joins **********************************/ join(collection: string | typeof Odin, query?: JoinQuery, as?: string): Query; /******************************* Where Clauses ******************************/ where(field: string, value: any): Query; where(field: string, operator: Operator, value: any): Query; whereIn(field: string, values: any[]): Query; whereNotIn(field: string, values: any[]): Query; whereBetween(field: string, start: any, end: any): Query; whereNotBetween(field: string, start: any, end: any): Query; whereNull(field: string): Query; whereNotNull(field: string): Query; /******************** Ordering, Grouping, Limit & Offset ********************/ orderBy(field: string, order?: Order): Query; skip(offset: number): Query; offset(offset: number): Query; limit(limit: number): Query; take(limit: number): Query; /*********************************** Read ***********************************/ exists(): Promise; exists(callback: Callback): void; count(): Promise; count(callback: Callback): void; iterate>(): Iterator; get(): Promise; get(callback: Callback): void; first(): Promise; first(callback: Callback): void; value(field: string): Promise; value(field: string, callback: Callback): void; pluck(field: string): Promise; pluck(field: string, callback: Callback): void; max(field: string): Promise; max(field: string, callback: Callback): void; min(field: string): Promise; min(field: string, callback: Callback): void; /********************************** Inserts *********************************/ insert(items: T[]): Promise; insert(items: T[], callback: Callback): void; create(item: T): Promise; create(item: T, callback: Callback): void; save(model: T): Promise; save(model: T, callback: Callback): void; /********************************** Updates *********************************/ update(update: T): Promise; update(update: T, callback: Callback): void; increment(field: string, count?: number): Promise; increment(field: string, callback: Callback): void; increment(field: string, count: number, callback: Callback): void; decrement(field: string, count?: number): Promise; decrement(field: string, callback: Callback): void; decrement(field: string, count: number, callback: Callback): void; /********************************** Deletes *********************************/ delete(): Promise; delete(callback: Callback): void; } export default Relation;