// // Copyright © 2021 Anticrm Platform Contributors. // // Licensed under the Eclipse Public License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. You may // obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // // See the License for the specific language governing permissions and // limitations under the License. // import type { KeysByType } from 'simplytyped' import type { AttachedDoc, Class, Doc, Ref } from './classes' import type { Tx } from './tx' /** * @public */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export type QuerySelector = { $in?: T[] $nin?: T[] $ne?: T $gt?: T extends number ? number : never $gte?: T extends number ? number : never $lt?: T extends number ? number : never $lte?: T extends number ? number : never $exists?: boolean $like?: string $regex?: string $options?: string } /** * @public */ export type ObjQueryType = (T extends Array ? U | U[] : T) | QuerySelector /** * @public */ export type DocumentQuery = { [P in keyof T]?: ObjQueryType } & { $search?: string // support nested queries e.g. 'user.friends.name' // this will mark all unrecognized properties as any (including nested queries) [key: string]: any } /** * @public */ export type ToClassRefT = T[P] extends Ref | null | undefined ? Ref> | [Ref>, Lookup] : never /** * @public */ export type ToClassRefTA = T[P] extends Array> | null | undefined ? Ref> | [Ref>, Lookup] : never /** * @public */ export type ToClassRef = { [P in keyof T]?: ToClassRefT | ToClassRefTA } /** * @public */ export type NullableRef = Ref | Array> | null | undefined /** * @public */ export type RefKeys = Pick> /** * @public */ export type Refs = ToClassRef> /** * @public */ export interface ReverseLookups { _id?: ReverseLookup } /** * @public */ export interface ReverseLookup { [key: string]: Ref> | [Ref>, string] } /** * @public */ export type Lookup = Refs | ReverseLookups | (Refs & ReverseLookups) /** * @public */ export type Projection = { [P in keyof T]?: 0 | 1 } /** * @public */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export type FindOptions = { limit?: number sort?: SortingQuery lookup?: Lookup projection?: Projection } /** * @public */ // eslint-disable-next-line @typescript-eslint/consistent-type-definitions export type SortQuerySelector = { $in?: T[] $nin?: T[] $ne?: T } /** * @public */ export type SortRuleQueryType = (T extends Array ? U | U[] : T) | SortQuerySelector /** * @public */ export interface SortingRules { order: SortingOrder default?: string | number cases: { query: SortRuleQueryType index: string | number }[] } /** * @public */ export type SortingQuery = { [P in keyof T]?: SortingOrder | SortingRules } & { // support nested queries e.g. 'user.friends.name' // this will mark all unrecognized properties as any (including nested queries) [key: string]: SortingOrder | SortingRules } /** * @public */ export enum SortingOrder { Ascending = 1, Descending = -1 } /** * @public */ export type RefsAsDocs = { [P in keyof T]: T[P] extends Ref | null | undefined ? (T extends X ? X : X | WithLookup) : AttachedDoc[] } /** * @public */ export type LookupData = Partial> /** * @public */ export type WithLookup = T & { $lookup?: LookupData $source?: { $score: number // Score for document result [key: string]: any } } /** * @public */ export type FindResult = WithLookup[] & { total: number } /** * @public */ // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface TxResult {} /** * @public */ export interface Storage { findAll: ( _class: Ref>, query: DocumentQuery, options?: FindOptions ) => Promise> tx: (tx: Tx) => Promise }