-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.ts
More file actions
165 lines (127 loc) · 4.9 KB
/
Copy pathmodel.ts
File metadata and controls
165 lines (127 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { EventEmitter } from 'events';
import Adapter from './adapter';
import InMemoryAdapter from './adapters/in-memory';
export type Diff<T, U> = T extends U ? never : T;
export type Filter<T, U> = T extends U ? T : never;
export type FunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? K : never
}[keyof T];
export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>>;
export type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends Function ? never : K
}[keyof T];
export type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;
export type Readonly<T> = { readonly [P in keyof T]: T[P] };
export type SchemaOf<T> = Pick<T, NonFunctionPropertyNames<T>>;
export type PartialSchemaOf<T> = Partial<SchemaOf<T>>;
// ==============================================================================================
//#region interfaces
type QueryWhereValue = string | boolean | number | null;
interface QueryWhereOperator {
/** Equivalence operator */
'=': QueryWhereValue;
/** Not equal (!=) */
neq: QueryWhereValue;
/** Numerical greater than (>); Valid only for numerical and date values. */
gt: number | Date;
/** Numerical greater than or equal (>=). ; Valid only for numerical and date values. */
gte: number | Date;
/** Numerical less than (<); Valid only for numerical and date values.*/
lt: number | Date;
/** Numerical less than or equal (<=); Valid only for numerical and date values.*/
lte: number | Date;
/**
* True if the value is between the two specified values: greater than or equal to first
* value and less than or equal to second value.
*/
between: [number, number];
/** In an array of values. */
inq: (string | boolean | number | null)[];
/** Not in an array of values. */
nin: (string | boolean | number | null)[];
/**
* For geolocations, return the closest points, sorted in order of distance.
* Use with limit to return the n closest points.
*/
near: (string | [number, number] | { lat: number, lng: number });
/** To use with `near`, `gt`, `gte`, `lt`, `lte`, `between` filter */
maxDistance: string;
/** To use with `near`, `gt`, `gte`, `lt`, `lte`, `between` filter */
unit: 'kilometers' | 'meters' | 'miles' | 'feet' | 'radians' | 'degrees';
/**
* LIKE operators for use with regular expressions.
* The regular expression format depends on the backend data source.
*/
like: string;
/**
* NOT LIKE operators for use with regular expressions.
* The regular expression format depends on the backend data source.
*/
nlike: string;
/**
* ILIKE operators for use with regular expressions.
* The operator is supported only by the memory and Postgresql connectors.
*/
ilike: string;
/**
* NOT ILIKE operators for use with regular expressions.
* The operator is supported only by the memory and Postgresql connectors.
*/
nilike: string;
/** Regular expression. */
regexp: string | RegExp;
}
export type QueryWhere<T = Model> = {
[name in ('id' | keyof SchemaOf<T>)]?: QueryWhereValue | Partial<QueryWhereOperator>
} & {
/** Logical AND operator. */
and?: QueryWhere<T>[]
/** Logical OR operator. */
or?: QueryWhere<T>[],
};
export interface QueryFilterInclude<T = Model> {
/** The relation to include */
relation: string;
/** The filter to use for the include data */
scope?: QueryFilterForManyThroughInclude<T>;
}
export interface QueryFilterMinimal<T = Model> {
/** Specify fields to include in or exclude from the response. */
fields?: string | string[] | { [name: string]: boolean };
/** Include results from related models, for relations such as belongsTo and hasMany. */
include?: string | string[] | QueryFilterInclude<T> | QueryFilterInclude<T>[];
}
export interface QueryFilter<T = Model> extends QueryFilterMinimal<T> {
/** Specify sort order: ascending or descending. */
order?: string | string[];
/** Skip the specified number of instances */
skip?: number;
/** Specify search criteria; similar to a WHERE clause in SQL. */
where?: QueryWhere<T>;
}
export interface QueryFilterAll<T = Model> extends QueryFilter<T> {
/** Limit the number of instances to return. */
limit?: number;
}
export interface QueryFilterForManyThroughInclude<T = Model> extends QueryFilterAll<T> {
with?: QueryWhere<T>;
}
//#endregion interfaces
// ----------------------------------------------------------------------------------------------
export class Model extends EventEmitter {
static adapter: Adapter = new InMemoryAdapter();
static collectionName: string = 'default';
static async find<T extends typeof Model>(
this: T,
filter?: QueryFilterAll<T>,
): Promise<InstanceType<T>[]> {
return this.adapter.find<T>(this, filter);
}
static async findOne<T extends typeof Model>(
this: T,
filter?: QueryFilterAll<T>,
): Promise<InstanceType<T>[]> {
return this.adapter.find(this, filter);
}
}
export default Model;