UNPKG

pinia-orm

Version:

The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.

1,503 lines (1,478 loc) 72.3 kB
import * as pinia from 'pinia'; import { DefineStoreOptionsBase, DefineSetupStoreOptions, Pinia, PiniaPlugin } from 'pinia'; import * as vue from 'vue'; import { schema, Schema as Schema$1 } from '@pinia-orm/normalizr'; import * as __composables from '@/composables'; type Element = Record<string, any>; interface Elements { [id: string]: Element; } interface NormalizedData { [entity: string]: Elements; } type Item<M extends Model = Model> = M | null; type Collection<M extends Model = Model> = M[]; type GroupedCollection<M extends Model = Model> = Record<string, M[]>; interface Constructor<T> { new (...args: any[]): T; } type Mutator<T> = (value: T) => T; interface MutatorFunctions<T> { get?: Mutator<T>; set?: Mutator<T>; } interface Mutators { [name: string]: MutatorFunctions<any> | Mutator<any>; } interface CacheConfig { key?: string; params?: Record<string, any>; } declare class WeakCache<K, V extends object> implements Map<K, V> { #private; readonly [Symbol.toStringTag]: string; has(key: K): boolean; get(key: K): V; set(key: K, value: V): this; get size(): number; clear(): void; delete(key: K): boolean; forEach(cb: (value: V, key: K, map: Map<K, V>) => void): void; [Symbol.iterator](): MapIterator<[K, V]>; entries(): MapIterator<[K, V]>; keys(): MapIterator<K>; values(): MapIterator<V>; } type SortFlags = 'SORT_REGULAR' | 'SORT_FLAG_CASE'; type SortComparator = SortFlags | Intl.Collator; declare class Database { /** * The list of registered models. */ models: Record<string, Model>; /** * Register the given model. */ register<M extends Model>(model: M): void; /** * Register all related models. */ private registerRelatedModels; /** * Get a model by the specified entity name. */ getModel<M extends Model>(name: string): M; } interface ModelConstructor<M extends Model> extends Constructor<M> { newRawInstance(): M; } type Schemas = Record<string, schema.Entity>; declare class Schema { /** * The list of generated schemas. */ private schemas; /** * The model instance. */ private model; /** * Create a new Schema instance. */ constructor(model: Model); /** * Create a single schema. */ one(model?: Model, parent?: Model): schema.Entity; /** * Create an array schema for the given model. */ many(model: Model, parent?: Model): schema.Array; /** * Create an union schema for the given models. */ union(models: Model[], callback: schema.SchemaFunction): schema.Union; /** * Create a new normalizr entity. */ private newEntity; /** * The `id` attribute option for the normalizr entity. * * Generates any missing primary keys declared by a Uid attribute. Missing * primary keys where the designated attributes do not exist will * throw an error. * * Note that this will only generate uids for primary key attributes since it * is required to generate the "index id" while the other attributes are not. * * It's especially important when attempting to "update" records since we'll * want to retain the missing attributes in-place to prevent them being * overridden by newly generated uid values. * * If uid primary keys are omitted, when invoking the "update" method, it will * fail because the uid values will never exist in the store. * * While it would be nice to throw an error in such a case, instead of * silently failing an update, we don't have a way to detect whether users * are trying to "update" records or "inserting" new records at this stage. * Something to consider for future revisions. */ private idAttribute; /** * Get all primary keys defined by the Uid attribute for the given model. */ private getUidPrimaryKeyPairs; /** * Create a definition for the given model. */ private definition; } declare abstract class Attribute { /** * The model instance. */ protected model: Model; /** * The field name */ protected key: string; /** * Create a new Attribute instance. */ constructor(model: Model); /** * Set the key name of the field */ setKey(key: string): this; /** * Make the value for the attribute. */ abstract make(value?: any): any; } interface Dictionary { [id: string]: Model[]; } type deleteModes = 'cascade' | 'set null'; declare abstract class Relation extends Attribute { /** * The parent model. */ parent: Model; /** * The related model. */ related: Model; /** * The delete mode */ onDeleteMode?: deleteModes; /** * Create a new relation instance. */ protected constructor(parent: Model, related: Model); /** * Get all related models for the relationship. */ abstract getRelateds(): Model[]; /** * Get the related model of the relation. */ getRelated(): Model; /** * Define the normalizr schema for the relation. */ abstract define(schema: Schema): Schema$1; /** * Attach the relational key to the given relation. */ abstract attach(record: Element, child: Element): void; /** * Set the constraints for an eager loading relation. */ abstract addEagerConstraints(query: Query<any>, models: Collection): void; /** * Match the eagerly loaded results to their parents. */ abstract match(relation: string, models: Collection, query: Query<any>): void; /** * Get all of the primary keys for an array of models. */ protected getKeys<M extends Model = Model>(models: Collection<any>, key: string): (string | number)[]; /** * Specify how this model should behave on delete */ onDelete(mode?: deleteModes): this; /** * Run a dictionary map over the items. */ protected mapToDictionary(models: Collection<any>, callback: (model: Model) => [string, Model]): Dictionary; /** * Call a function for a current key match */ protected compositeKeyMapper(foreignKey: PrimaryKey, localKey: PrimaryKey, call: (foreignKey: string, localKey: string) => void): void; /** * Get the index key defined by the primary key or keys (composite) */ protected getResolvedKey(model: Model, key: PrimaryKey): string; } declare class Interpreter { /** * The model object. */ model: Model; /** * Create a new Interpreter instance. */ constructor(model: Model); /** * Perform interpretation for the given data. */ process(data: Element): [Element, NormalizedData]; process(data: Element[]): [Element[], NormalizedData]; /** * Normalize the given data. */ private normalize; /** * Get the schema from the database. */ private getSchema; } declare function useDataStore<S extends DataStoreState, T extends DataStore = DataStore>(id: string, options: DefineStoreOptionsBase<S, T>, customOptions?: DefineSetupStoreOptions<string, S, T, any>, query?: Query<any>): pinia.SetupStoreDefinition<string, { save(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; insert(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; update(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; fresh(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; destroy(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; delete(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; flush(this: DataStore, _records?: Elements, triggerQueryAction?: boolean): void; data: vue.Ref<Record<string, any>, Record<string, any>>; }>; interface DataStoreState { data: Record<string, any>; [s: string]: any; } type DataStore = ReturnType<typeof __composables['useDataStore']>; declare function useStoreActions(query?: Query): { save(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; insert(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; update(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; fresh(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; destroy(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; /** * Commit `delete` change to the store. */ delete(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; flush(this: DataStore, _records?: Elements, triggerQueryAction?: boolean): void; }; type StoreActions = 'insert' | 'flush' | 'delete' | 'update' | 'destroy' | 'save' | 'fresh'; interface Where<T = Model> { field: WherePrimaryClosure<T> | NonMethodKeys<T> | string | string[]; value: WhereSecondaryClosure<T> | any; boolean: 'and' | 'or'; } type NonMethodKeys<T> = { [P in keyof T]: T[P] extends Function ? never : P; }[keyof T]; type GetElementType<T extends unknown[] | unknown> = T extends (infer U)[] ? U : T; type UltimateKeys<M> = { [T in keyof M]-?: NonNullable<M[T]> extends Model | Model[] ? GetElementType<NonNullable<M[T]>> : never; }; type WherePrimaryClosure<T> = (model: T) => boolean; type WhereSecondaryClosure<T> = (value: T) => boolean; interface WhereGroup { and?: Where[]; or?: Where[]; } interface Order { field: OrderBy; direction: OrderDirection; flags?: SortComparator; } interface Group { field: GroupBy; } type OrderBy = string | ((model: any) => any); type GroupBy = string; type GroupByFields = string[]; type OrderDirection = 'asc' | 'desc'; type EagerLoad<M extends Model> = { [K in keyof M]: EagerLoadConstraint<GetElementType<M[WithKeys<M>] extends Model ? M[WithKeys<M>] : never>>; }; type EagerLoadConstraint<M extends Model> = (query: Query<M>) => void; declare class Query<M extends Model = Model> { /** * The database instance. */ database: Database; /** * The model object. */ protected model: M; /** * The where constraints for the query. */ protected wheres: Where<M>[]; /** * The orderings for the query. */ protected orders: Order[]; /** * The orderings for the query. */ protected groups: Group[]; /** * The maximum number of records to return. */ protected take: number | null; /** * The number of records to skip. */ protected skip: number; /** * Fields that should be visible. */ protected visible: string[]; /** * Fields that should be hidden. */ protected hidden: string[]; /** * The cache object. */ protected cache?: WeakCache<string, Collection<M> | GroupedCollection<M>> | undefined; /** * The relationships that should be eager loaded. */ protected eagerLoad: EagerLoad<M> | object; /** * The pinia store. */ protected pinia?: Pinia; protected fromCache: boolean; protected cacheConfig: CacheConfig; protected getNewHydrated: boolean; protected hydrationKey?: string; /** * Hydrated models. They are stored to prevent rerendering of child components. */ hydratedDataCache: Map<string, M>; /** * Create a new query instance. */ constructor(database: Database, model: M, cache: WeakCache<string, Collection<M> | GroupedCollection<M>> | undefined, hydratedData: Map<string, M>, pinia?: Pinia, hydrationKey?: string); /** * Create a new query instance for the given model. */ newQuery(model: string): Query<M>; /** * Create a new query instance with constraints for the given model. */ newQueryWithConstraints(model: string): Query<M>; /** * Create a new query instance from the given relation. */ newQueryForRelation(relation: Relation): Query<M>; /** * Create a new interpreter instance. */ protected newInterpreter(): Interpreter; /** * Commit a store action and get the data */ protected commit(name: StoreActions | 'all' | 'get', payload?: any): Record<string, any>; /** * Make meta field visible */ withMeta(): this; /** * Make hidden fields visible */ makeVisible(fields: string[]): this; /** * Make visible fields hidden */ makeHidden(fields: string[]): this; /** * Add a basic where clause to the query. */ where<T extends WherePrimaryClosure<M> | NonMethodKeys<M> | string[] | (string & {})>(field: T, value?: T extends string[] ? string | number | (string | number)[] : WhereSecondaryClosure<M[T extends keyof M ? T : never]> | M[T extends keyof M ? T : never] | null): this; /** * Add a "where in" clause to the query. */ whereIn<T extends NonMethodKeys<M>>(field: T | string & {}, values: any[] | Set<any>): this; /** * Add a "where not in" clause to the query. */ whereNotIn(field: string, values: any[] | Set<any>): this; /** * Add a "where not in" clause to the query. */ orWhereIn(field: string, values: any[] | Set<any>): this; /** * Add a "where not in" clause to the query. */ orWhereNotIn(field: string, values: any[] | Set<any>): this; /** * Add a where clause on the primary key to the query. */ whereId(ids: string | number | (string | number)[]): this; /** * Add an "or where" clause to the query. */ orWhere<T extends WherePrimaryClosure<M> | NonMethodKeys<M> | string & {}>(field: T, value?: WhereSecondaryClosure<M[T extends keyof M ? T : never]> | M[T extends keyof M ? T : never]): this; /** * Add a "whereNULL" clause to the query. */ whereNull(field: string): this; /** * Add a "whereNotNULL" clause to the query. */ whereNotNull(field: string): this; /** * Add a "where like" clause to the query. The value may contain `%` as * a wildcard for any number of characters and `_` for a single character. */ whereLike(field: string, value: string | number, caseSensitive?: boolean): this; /** * Add an "or where like" clause to the query. */ orWhereLike(field: string, value: string | number, caseSensitive?: boolean): this; /** * Add a "where has" clause to the query. */ whereHas<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void, operator?: string | number, count?: number): this; /** * Add an "or where has" clause to the query. */ orWhereHas<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void, operator?: string | number, count?: number): this; /** * Add a "has" clause to the query. */ has(relation: string, operator?: string | number, count?: number): this; /** * Add an "or has" clause to the query. */ orHas(relation: string, operator?: string | number, count?: number): this; /** * Add a "doesn't have" clause to the query. */ doesntHave(relation: string): this; /** * Add a "doesn't have" clause to the query. */ orDoesntHave(relation: string): this; /** * Add a "where doesn't have" clause to the query. */ whereDoesntHave<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void): this; /** * Add an "or where doesn't have" clause to the query. */ orWhereDoesntHave<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void): this; /** * Add a "group by" clause to the query. */ groupBy(...fields: GroupByFields): this; /** * Add an "order by" clause to the query. */ orderBy(field: OrderBy, direction?: OrderDirection, flags?: SortComparator): this; /** * Set the "limit" value of the query. */ limit(value: number): this; /** * Set the "offset" value of the query. */ offset(value: number): this; /** * Set the relationships that should be eager loaded. */ with<T extends WithKeys<M>>(name: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void): this; /** * Set to eager load all top-level relationships. Constraint is set for all relationships. */ withAll(callback?: EagerLoadConstraint<any>): this; /** * Set to eager load all relationships recursively. */ withAllRecursive(depth?: number): this; /** * Define to use the cache for a query */ useCache(key?: string, params?: Record<string, any>): this; /** * Get where closure for relations */ protected getFieldWhereForRelations<T extends WithKeys<M>>(relation: T | (string & {}), callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void, operator?: string | number, count?: number): WherePrimaryClosure<M>; /** * Get all models by id from the store. The difference with the `get` is that this * method will not process any query chain. */ protected storeFind(ids?: string[]): Collection<M>; /** * Get all models from the store. The difference with the `get` is that this * method will not process any query chain. It'll always retrieve all models. */ all(): Collection<M>; /** * Retrieve models by processing whole query chain. */ get<T extends 'group' | 'collection' = 'collection'>(triggerHook?: boolean): T extends 'group' ? GroupedCollection<M> : Collection<M>; private internalGet; /** * Execute the query and get the first result. */ first(): Item<M>; /** * Find a model by its primary key. */ find(id: string | number): Item<M>; find(ids: (string | number)[]): Collection<M>; /** * Retrieve models by processing all filters set to the query chain. */ select(): Collection<M>; /** * Filter the given collection by the registered where clause. */ protected filterWhere(models: Collection<M>): Collection<M>; protected generateHydrationKey(): string; /** * Get comparator for the where clause. */ protected getWhereComparator(): (model: M) => boolean; /** * The function to compare where clause to the given model. */ protected whereComparator(model: M, where: Where<M>): boolean; /** * Filter the given collection by the registered order conditions. */ protected filterOrder(models: Collection<M>): Collection<M>; /** * Filter the given collection by the registered group conditions. */ protected filterGroup(models: Collection<M>): Record<string, Collection<M>>; /** * Filter the given collection by the registered limit and offset values. */ protected filterLimit(models: Collection<M>): Collection<M>; /** * Eager load relations on the model. */ load(models: Collection<M>): void; /** * Eager load the relationships for the models. */ protected eagerLoadRelations(models: Collection<M>): void; /** * Eagerly load the relationship on a set of models. */ protected eagerLoadRelation<T extends WithKeys<M>>(models: Collection<M>, name: T | string & {}, constraints: EagerLoadConstraint<M>): void; /** * Get the relation instance for the given relation name. */ protected getRelation(name: string): Relation; revive(schema: Element[]): Collection<M>; revive(schema: Element): Item<M>; /** * Revive single model from the given schema. */ reviveOne(schema: Element): Item<M>; /** * Revive multiple models from the given schema. */ reviveMany(schema: Element[]): Collection<M>; /** * Revive relations for the given schema and entity. */ protected reviveRelations(model: M, schema: Element): void; /** * Create and persist model with default values. */ new(persist?: boolean): M | null; /** * Save the given records to the store with data normalization. */ save(records: Element[]): M[]; save(record: Element): M; /** * Save the given elements to the store. */ saveElements(elements: Elements): void; /** * Insert the given record to the store. */ insert(records: Element[]): Collection<M>; insert(record: Element): M; /** * Insert the given records to the store by replacing any existing records. * The `saving`/`creating` and `saved`/`created` lifecycle hooks are fired * for every record unless `raw` is set to `true`. Records for which a * before hook returns `false` are not persisted. */ fresh(records: Element[], options?: { raw?: boolean; }): Collection<M>; fresh(record: Element, options?: { raw?: boolean; }): M; /** * Update the reocrd matching the query chain. */ update(record: Element): Collection<M>; /** * Destroy the models for the given id. */ destroy(ids: (string | number)[]): Collection<M>; destroy(id: string | number): Item<M>; protected destroyOne(id: string | number): Item<M>; protected destroyMany(ids: (string | number)[]): Collection<M>; /** * Delete records resolved by the query chain. */ delete(): M[]; /** * Delete all records in the store. */ flush(): Collection<M>; protected checkAndDeleteRelations(model: M): void; protected dispatchDeleteHooks(models: M | Collection<M>): [{ (): void; }[], string[]]; /** * Get an array of index ids from the given collection. */ protected getIndexIdsFromCollection(models: Collection<M>): string[]; /** * Instantiate new models with the given record. */ protected hydrate(record: Element, options?: ModelOptions): M; protected hydrate(records: Element[], options?: ModelOptions): Collection<M>; /** * Convert given models into an indexed object that is ready to be saved to * the store. */ protected compile(models: M | Collection<M>): Elements; /** * Save already existing models and return them if they exist to prevent * an update event trigger in vue if the object is used. */ protected getHydratedModel(record: Element, options?: ModelOptions): M; } interface Repository<M extends Model = Model> { /** * Add a where clause where `field` value is in values. */ whereIn(field: string, values: any[] | Set<any>): Query<M>; /** * Add a where clause where `field` value is in values or ... */ orWhereIn(field: string, values: any[] | Set<any>): Query<M>; /** * Add a where clause where `field` value is not in values or ... */ orWhereNotIn(field: string, values: any[] | Set<any>): Query<M>; /** * Add a where clause where `field` has not defined values */ whereNotIn(field: string, values: any[] | Set<any>): Query<M>; /** * Add a where clause to get all results where `field` is null */ whereNull(field: string): Query<M>; /** * Add a where clause to get all results where `field` is not null */ whereNotNull(field: string): Query<M>; /** * Add a where clause where `field` matches a SQL LIKE style pattern. * `%` matches any number of characters, `_` a single character. */ whereLike(field: string, value: string | number, caseSensitive?: boolean): Query<M>; /** * Add an "or where like" clause to the query. */ orWhereLike(field: string, value: string | number, caseSensitive?: boolean): Query<M>; /** * Find the model with the given id. */ find(id: string | number): Item<M>; find(ids: (string | number)[]): Collection<M>; } declare class Repository<M extends Model = Model> { [index: string]: any; /** * A special flag to indicate if this is the repository class or not. It's * used when retrieving repository instance from `store.$repo()` method to * determine whether the passed in class is either a repository or a model. */ static _isRepository: boolean; /** * The database instance. */ database: Database; /** * The model instance. */ protected model: M; /** * The pinia instance */ protected pinia?: Pinia; /** * The cache instance */ queryCache?: WeakCache<string, M[]>; /** * Hydrated models. They are stored to prevent rerendering of child components. */ hydratedDataCache: Map<string, M>; /** * The model object to be used for the custom repository. */ use?: typeof Model; /** * The model object to be used for the custom repository. */ static useModel?: typeof Model; /** * Global config */ config: FilledInstallOptions & { [key: string]: any; }; /** * Create a new Repository instance. */ constructor(database: Database, pinia?: Pinia); /** * Set the model */ static setModel(model: typeof Model): typeof Repository; /** * Set the global config */ setConfig(config: FilledInstallOptions): void; /** * Initialize the repository by setting the model instance. */ initialize(model?: ModelConstructor<M>): this; /** * Get the constructor for this model. */ $self(): typeof Repository; /** * Get the model instance. If the model is not registered to the repository, * it will throw an error. It happens when users use a custom repository * without setting `use` property. */ getModel(): M; /** * Returns the pinia store used with this model */ piniaStore<S extends DataStoreState = DataStoreState>(): pinia.Store<string, Pick<{ save(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; insert(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; update(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; fresh(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; destroy(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; delete(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; flush(this: DataStore, _records?: Elements, triggerQueryAction?: boolean): void; data: vue.Ref<Record<string, any>, Record<string, any>>; }, "data">, Pick<{ save(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; insert(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; update(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; fresh(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; destroy(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; delete(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; flush(this: DataStore, _records?: Elements, triggerQueryAction?: boolean): void; data: vue.Ref<Record<string, any>, Record<string, any>>; }, never>, Pick<{ save(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; insert(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; update(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; fresh(this: DataStore, records: Elements, triggerQueryAction?: boolean): void; destroy(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; delete(this: DataStore, ids: (string | number)[], triggerQueryAction?: boolean): void; flush(this: DataStore, _records?: Elements, triggerQueryAction?: boolean): void; data: vue.Ref<Record<string, any>, Record<string, any>>; }, "insert" | "flush" | "delete" | "update" | "destroy" | "save" | "fresh">>; /** * Create a new repository with the given model. */ repo<M extends Model>(model: Constructor<M>): Repository<M>; repo<R extends Repository<any>>(repository: Constructor<R>): R; /** * Create a new Query instance. */ query(): Query<M>; /** * Create a new Query instance. */ cache(): WeakCache<string, M[]> | undefined; /** * Add a basic where clause to the query. */ where<T extends WherePrimaryClosure<M> | NonMethodKeys<M> | string & {}>(field: T, value?: T extends string[] ? string | number | (string | number)[] : WhereSecondaryClosure<M[T extends keyof M ? T : never]> | M[T extends keyof M ? T : never]): Query<M>; /** * Add an "or where" clause to the query. */ orWhere<T extends WherePrimaryClosure<M> | NonMethodKeys<M> | string & {}>(field: T, value?: WhereSecondaryClosure<M[T extends keyof M ? T : never]> | M[T extends keyof M ? T : never]): Query<M>; /** * Add a "where has" clause to the query. */ whereHas<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void, operator?: string | number, count?: number): Query<M>; /** * Add an "or where has" clause to the query. */ orWhereHas<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void, operator?: string | number, count?: number): Query<M>; /** * Add a "has" clause to the query. */ has(relation: string, operator?: string | number, count?: number): Query<M>; /** * Add an "or has" clause to the query. */ orHas(relation: string, operator?: string | number, count?: number): Query<M>; /** * Add a "doesn't have" clause to the query. */ doesntHave(relation: string): Query<M>; /** * Add a "doesn't have" clause to the query. */ orDoesntHave(relation: string): Query<M>; /** * Add a "where doesn't have" clause to the query. */ whereDoesntHave<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void): Query<M>; /** * Add an "or where doesn't have" clause to the query. */ orWhereDoesntHave<T extends WithKeys<M>>(relation: T | string & {}, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : () => void): Query<M>; /** * Make meta field visible */ withMeta(): Query<M>; /** * Make hidden fields visible */ makeVisible(fields: string[]): Query<M>; /** * Make visible fields hidden */ makeHidden(fields: string[]): Query<M>; /** * Add a "group by" clause to the query. */ groupBy(...fields: GroupByFields): Query<M>; /** * Add an "order by" clause to the query. */ orderBy(field: OrderBy, direction?: OrderDirection, flags?: SortComparator): Query<M>; /** * Set the "limit" value of the query. */ limit(value: number): Query<M>; /** * Set the "offset" value of the query. */ offset(value: number): Query<M>; /** * Set the relationships that should be eager loaded. */ with<T extends WithKeys<M>>(name: string & {} | T, callback?: M[T] extends Model | Model[] | null | undefined ? EagerLoadConstraint<GetElementType<NonNullable<M[T]>>> : never): Query<M>; /** * Set to eager load all top-level relationships. Constraint is set for all relationships. */ withAll(callback?: EagerLoadConstraint<M>): Query<M>; /** * Set to eager load all top-level relationships. Constraint is set for all relationships. */ withAllRecursive(depth?: number): Query<M>; /** * Define to use the cache for a query */ useCache(key?: string, params?: Record<string, any>): Query<M>; /** * Get all models from the store. */ all(): Collection<M>; /** * Retrieves the models from the store by following the given * normalized schema. */ revive(schema: Element[]): Collection<M>; revive(schema: Element): Item<M>; /** * Create a new model instance. This method will not save the model to the * store. It's pretty much the alternative to `new Model()`, but it injects * the store instance to support model instance methods in SSR environment. */ make(records: Element[]): M[]; make(record?: Element): M; save(records: Element[]): M[]; save(record: Element): M; /** * Get the first record matching the given attributes or persist a new * record made from the merged attributes and values. */ firstOrCreate(attributes: Element, values?: Element): M; /** * Update the first record matching the given attributes with the given * values or persist a new record made from the merged attributes and values. */ updateOrCreate(attributes: Element, values?: Element): M; /** * Get the first record matching the given attributes. */ protected matching(attributes: Element): Item<M>; /** * Create and persist model with default values. */ new(persist?: boolean): M | null; /** * Insert the given records to the store. */ insert(records: Element[]): Collection<M>; insert(record: Element): M; /** * Insert the given records to the store by replacing any existing records. */ fresh(records: Element[], options?: { raw?: boolean; }): Collection<M>; fresh(record: Element, options?: { raw?: boolean; }): M; /** * Destroy the models for the given id. */ destroy(ids: (string | number)[]): Collection<M>; destroy(id: string | number): Item<M>; /** * Delete all records in the store. */ flush(): M[]; } declare class BelongsTo extends Relation { /** * The child model instance of the relation. */ child: Model; /** * The foreign key of the parent model. */ foreignKey: PrimaryKey; /** * The associated key on the parent model. */ ownerKey: PrimaryKey; /** * Create a new belongs-to relation instance. */ constructor(parent: Model, child: Model, foreignKey: PrimaryKey, ownerKey: PrimaryKey); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the relational key to the given relation. */ attach(record: Element, child: Element): void; /** * Set the constraints for an eager load of the relation. */ addEagerConstraints(query: Query, models: Collection): void; /** * Gather the keys from a collection of related models. */ protected getEagerModelKeys(models: Collection<any>, foreignKey: string): (string | number)[]; /** * Match the eagerly loaded results to their respective parents. */ match(relation: string, models: Collection<any>, query: Query): void; /** * Build model dictionary keyed by relation's parent key. */ protected buildDictionary(models: Collection<any>): Record<string, Model>; /** * Make a related model. */ make(element?: Element): Model | null; } declare class BelongsToMany extends Relation { /** * The pivot model. */ pivot: Model; /** * The foreign key of the parent model. */ foreignPivotKey: string; /** * The associated key of the relation. */ relatedPivotKey: string; /** * The key name of the parent model. */ parentKey: string; /** * The key name of the related model. */ relatedKey: string; /** * The key name of the pivot data. */ pivotKey: string; /** * Create a new belongs to instance. */ constructor(parent: Model, related: Model, pivot: Model, foreignPivotKey: string, relatedPivotKey: string, parentKey: string, relatedKey: string); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relationship. */ define(schema: Schema): Schema$1; /** * Attach the parent type and id to the given relation. */ attach(record: Element, child: Element): void; /** * Convert given value to the appropriate value for the attribute. */ make(elements?: Element[]): Model[]; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Set the constraints for the related relation. */ addEagerConstraints(_query: Query, _collection: Collection): void; /** * Specify the custom pivot accessor to use for the relationship. */ as(accessor: string): this; } declare class HasMany extends Relation { /** * The foreign key of the parent model. */ foreignKey: PrimaryKey; /** * The local key of the parent model. */ localKey: PrimaryKey; /** * Create a new has-many relation instance. */ constructor(parent: Model, related: Model, foreignKey: PrimaryKey, localKey: PrimaryKey); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the relational key to the given relation. */ attach(record: Element, child: Element): void; /** * Set the constraints for an eager load of the relation. */ addEagerConstraints(query: Query, models: Collection): void; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Build model dictionary keyed by the relation's foreign key. */ protected buildDictionary(results: Collection<any>): Dictionary; /** * Make related models. */ make(elements?: Element[]): Model[]; } declare class HasOne extends Relation { /** * The foreign key of the parent model. */ foreignKey: PrimaryKey; /** * The local key of the parent model. */ localKey: PrimaryKey; /** * Create a new has-one relation instance. */ constructor(parent: Model, related: Model, foreignKey: PrimaryKey, localKey: PrimaryKey); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the relational key to the given relation. */ attach(record: Element, child: Element): void; /** * Set the constraints for an eager load of the relation. */ addEagerConstraints(query: Query, models: Collection): void; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Build model dictionary keyed by the relation's foreign key. */ protected buildDictionary(results: Collection<any>): Dictionary; /** * Make a related model. */ make(element?: Element): Model | null; } declare class HasManyBy extends Relation { /** * The child model instance of the relation. */ child: Model; /** * The foreign key of the parent model. */ foreignKey: string; /** * The owner key of the parent model. */ ownerKey: string; /** * Create a new has-many-by relation instance. */ constructor(parent: Model, child: Model, foreignKey: string, ownerKey: string); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the relational key to the given relation. */ attach(record: Element, child: Element): void; /** * Push owner key to foregin key array if owner key doesn't exist in foreign * key array. */ protected attachIfMissing(foreignKey: (string | number)[], ownerKey: string | number): void; /** * Set the constraints for an eager load of the relation. */ addEagerConstraints(query: Query, models: Collection): void; /** * Gather the keys from a collection of related models. */ protected getEagerModelKeys(models: Collection<any>): (string | number)[]; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Build model dictionary keyed by the relation's foreign key. */ protected buildDictionary(models: Collection<any>): Record<string, Model>; /** * Get all related models from the given dictionary. */ protected getRelatedModels(dictionary: Record<string, Model>, keys: (string | number)[]): Model[]; /** * Make related models. */ make(elements?: Element[]): Model[]; } declare class HasManyThrough extends Relation { /** * The "through" parent model. */ through: Model; /** * The near key on the relationship. */ firstKey: string; /** * The far key on the relationship. */ secondKey: string; /** * The local key on the relationship. */ localKey: string; /** * The local key on the intermediary model. */ secondLocalKey: string; /** * Create a new has-many-through relation instance. */ constructor(parent: Model, related: Model, through: Model, firstKey: string, secondKey: string, localKey: string, secondLocalKey: string); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the relational key to the given data. Since has many through * relationship doesn't have any foreign key, it would do nothing. */ attach(_record: Element, _child: Element): void; /** * Only register missing through relation */ addEagerConstraints(_query: Query, _models: Collection): void; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Build model dictionary keyed by the relation's foreign key. */ protected buildDictionary(throughResults: Collection<any>, results: Collection<any>): Dictionary; /** * Make related models. */ make(elements?: Element[]): Model[]; } declare class MorphMany extends Relation { /** * The field name that contains id of the parent model. */ morphId: string; /** * The field name that contains type of the parent model. */ morphType: string; /** * The local key of the model. */ localKey: string; /** * Create a new morph-many relation instance. */ constructor(parent: Model, related: Model, morphId: string, morphType: string, localKey: string); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the parent type and id to the given relation. */ attach(record: Element, child: Element): void; /** * Set the constraints for an eager load of the relation. */ addEagerConstraints(query: Query<any>, models: Collection<any>): void; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Build model dictionary keyed by the relation's foreign key. */ protected buildDictionary(results: Collection<any>): Dictionary; /** * Make related models. */ make(elements?: Element[]): Model[]; } interface DictionaryByEntities { [entity: string]: { [id: string]: Model; }; } declare class MorphTo extends Relation { /** * The related models. */ relatedModels: Model[]; /** * The related model dictionary. */ relatedTypes: Record<string, Model>; /** * The field name that contains id of the parent model. */ morphId: string; /** * The field name that contains type of the parent model. */ morphType: string; /** * The associated key of the child model. */ ownerKey: string; /** * Create a new morph-to relation instance. */ constructor(parent: Model, relatedModels: Model[], morphId: string, morphType: string, ownerKey: string); /** * Create a dictionary of relations keyed by their entity. */ protected createRelatedTypes(models: Model[]): Record<string, Model>; /** * Get the type field name. */ getType(): string; /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the relational key to the given record. Since morph-to relationship * doesn't have any foreign key, it would do nothing. */ attach(_record: Element, _child: Element): void; /** * Add eager constraints. Since we do not know the related model ahead of time, * we cannot add any eager constraints. */ addEagerConstraints(_query: Query, _models: Collection): void; /** * Find and attach related children to their respective parents. */ match(relation: string, models: Collection, query: Query): void; /** * Make a related model. */ make(element?: Element, type?: string): Model | null; /** * Build model dictionary keyed by the owner key for each entity. */ protected buildDictionary(query: Query<any>, models: Collection<any>): DictionaryByEntities; /** * Get the relation's primary keys grouped by its entity. */ protected getKeysByEntity(models: Collection<any>): Record<string, (string | number)[]>; } declare class MorphToMany extends Relation { /** * The pivot model. */ pivot: Model; /** * The field name that contains id of the parent model. */ morphId: string; /** * The field name that contains type of the parent model. */ morphType: string; /** * The associated key of the relation. */ relatedId: string; /** * The key name of the parent model. */ parentKey: string; /** * The key name of the related model. */ relatedKey: string; /** * The key name of the pivot data. */ pivotKey: string; /** * Create a new morph to many to instance. */ constructor(parent: Model, related: Model, pivot: Model, relatedId: string, morphId: string, morphType: string, parentKey: string, relatedKey: string); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relationship. */ define(schema: Schema): Schema$1; /** * Attach the parent type and id to the given relation. */ attach(record: Element, child: Element): void; /** * Convert given value to the appropriate value for the attribute. */ make(elements?: Element[]): Model[]; /** * Match the eagerly loaded results to their parents. */ match(relation: string, models: Collection<any>, query: Query<any>): void; /** * Set the constraints for the related relation. */ addEagerConstraints(_query: Query, _collection: Collection): void; /** * Specify the custom pivot accessor to use for the relationship. */ as(accessor: string): this; } declare class MorphOne extends Relation { /** * The field name that contains id of the parent model. */ morphId: string; /** * The field name that contains type of the parent model. */ morphType: string; /** * The local key of the model. */ localKey: string; /** * Create a new morph-one relation instance. */ constructor(parent: Model, related: Model, morphId: string, morphType: string, localKey: string); /** * Get all related models for the relationship. */ getRelateds(): Model[]; /** * Define the normalizr schema for the relation. */ define(schema: Schema): Schema$1; /** * Attach the parent type and id to the given relation. */ attach(record: Element, child: Element