2021-08-03 16:55:52 +00:00
|
|
|
//
|
|
|
|
// 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'
|
2022-01-31 09:06:30 +00:00
|
|
|
import type { AttachedDoc, Class, Doc, Ref } from './classes'
|
2021-08-05 06:47:54 +00:00
|
|
|
import type { Tx } from './tx'
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
2021-10-01 13:07:09 +00:00
|
|
|
export type QuerySelector<T> = { // TODO: refactor this shit
|
2021-08-03 16:55:52 +00:00
|
|
|
$in?: T[]
|
|
|
|
$like?: string
|
2021-10-01 13:07:09 +00:00
|
|
|
$regex?: string
|
|
|
|
$options?: string
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export type ObjQueryType<T> = T | QuerySelector<T>
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export type DocumentQuery<T extends Doc> = {
|
|
|
|
[P in keyof T]?: ObjQueryType<T[P]>
|
2021-09-13 19:50:43 +00:00
|
|
|
} & {
|
|
|
|
$search?: string
|
2021-11-18 12:48:05 +00:00
|
|
|
// support nested queries e.g. 'user.friends.name'
|
|
|
|
// this will mark all unrecognized properties as any (including nested queries)
|
|
|
|
[key: string]: any
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-01-17 09:59:33 +00:00
|
|
|
export type ToClassRef<T extends object> = {
|
2022-01-31 09:06:30 +00:00
|
|
|
[P in keyof T]?: T[P] extends Ref<infer X> | null ? Ref<Class<X>> | [Ref<Class<X>>, Lookup<X>] : never
|
2022-01-17 09:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-01-31 09:06:30 +00:00
|
|
|
export type RefKeys<T extends Doc> = Pick<T, KeysByType<T, NullableRef>>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type NullableRef = Ref<Doc> | null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type Refs<T extends Doc> = ToClassRef<RefKeys<T>>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface ReverseLookups {
|
|
|
|
_id?: ReverseLookup
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export interface ReverseLookup {
|
|
|
|
[key: string]: Ref<Class<AttachedDoc>>
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type Lookup<T extends Doc> = Refs<T> | ReverseLookups | (Refs<T> & ReverseLookups)
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
|
|
export type FindOptions<T extends Doc> = {
|
|
|
|
limit?: number
|
|
|
|
sort?: SortingQuery<T>
|
2022-01-31 09:06:30 +00:00
|
|
|
lookup?: Lookup<T>
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export type SortingQuery<T extends Doc> = {
|
2021-08-20 16:20:22 +00:00
|
|
|
[P in keyof T]?: SortingOrder
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export enum SortingOrder {
|
|
|
|
Ascending = 1,
|
|
|
|
Descending = -1
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-01-31 09:06:30 +00:00
|
|
|
export type RefsAsDocs<T> = {
|
|
|
|
[P in keyof T]: T[P] extends Ref<infer X> ? (T extends X ? X : X | WithLookup<X>) : never
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type RemoveNever<T extends object> = Omit<T, KeysByType<T, never>>
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2022-01-31 09:06:30 +00:00
|
|
|
export type LookupData<T extends Doc> = Partial<RemoveNever<RefsAsDocs<T>>> | RemoveNever<{
|
|
|
|
[key: string]: Doc[]
|
|
|
|
}>
|
2021-08-03 16:55:52 +00:00
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export type WithLookup<T extends Doc> = T & {
|
|
|
|
$lookup?: LookupData<T>
|
|
|
|
}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export type FindResult<T extends Doc> = WithLookup<T>[]
|
|
|
|
|
2021-10-13 16:46:48 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
|
|
export interface TxResult {}
|
|
|
|
|
2021-08-04 20:24:30 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
2021-08-03 16:55:52 +00:00
|
|
|
export interface Storage {
|
|
|
|
findAll: <T extends Doc>(
|
|
|
|
_class: Ref<Class<T>>,
|
|
|
|
query: DocumentQuery<T>,
|
|
|
|
options?: FindOptions<T>
|
|
|
|
) => Promise<FindResult<T>>
|
2021-10-13 16:46:48 +00:00
|
|
|
tx: (tx: Tx) => Promise<TxResult>
|
2021-08-03 16:55:52 +00:00
|
|
|
}
|