@wxn0brp/vql-client 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -11,6 +11,6 @@ export declare function initVQLClient(config: {
11
11
  hooks?: VQLHooks;
12
12
  defaultFetchUrl?: string;
13
13
  }): void;
14
- export declare function fetchVQL<T = any>(query: VqlQueryRaw, hookContext?: any): Promise<T>;
14
+ export declare function fetchVQL<T = any>(query: VqlQueryRaw<T>, hookContext?: any): Promise<T>;
15
15
  export declare function resetVQLClient(): void;
16
16
  export declare function defaultFetchTransport(query: VqlQueryRaw): Promise<any>;
package/dist/vql.d.ts CHANGED
@@ -1,120 +1,112 @@
1
1
  export interface Data {
2
2
  [key: string]: any;
3
3
  }
4
- export interface Context {
4
+ export interface VContext {
5
5
  [key: string]: any;
6
6
  }
7
- export type Id = string;
8
- export type LogicalOperators = {
9
- $and?: Array<SearchOptions>;
10
- $or?: Array<SearchOptions>;
11
- $not?: SearchOptions;
7
+ export type KeysMatching<T, V, C = V> = {
8
+ [K in keyof T]-?: T[K] extends C ? K : never;
9
+ }[keyof T];
10
+ export type PartialOfType<T, V, C = V> = Partial<Record<KeysMatching<T, V, C>, V>>;
11
+ export type PartialPickMatching<T, V, C = V> = Partial<Pick<T, KeysMatching<T, V, C>>>;
12
+ export type LogicalOperators<T = any> = {
13
+ $and?: Array<SearchOptions<T>>;
14
+ $or?: Array<SearchOptions<T>>;
15
+ $not?: SearchOptions<T>;
12
16
  };
13
- export type ComparisonOperators = {
14
- $gt?: Record<string, number>;
15
- $lt?: Record<string, number>;
16
- $gte?: Record<string, number>;
17
- $lte?: Record<string, number>;
18
- $in?: Record<string, any[]>;
19
- $nin?: Record<string, any[]>;
20
- $between?: Record<string, [
17
+ export type ComparisonOperators<T = any> = {
18
+ $gt?: PartialOfType<T, number>;
19
+ $lt?: PartialOfType<T, number>;
20
+ $gte?: PartialOfType<T, number>;
21
+ $lte?: PartialOfType<T, number>;
22
+ $between?: PartialOfType<T, [
21
23
  number,
22
24
  number
23
- ]>;
25
+ ], number>;
26
+ $in?: Partial<Record<keyof T, T[keyof T][]>>;
27
+ $nin?: Partial<Record<keyof T, T[keyof T][]>>;
24
28
  };
25
- export type TypeAndExistenceOperators = {
26
- $exists?: Record<string, boolean>;
27
- $type?: Record<string, string>;
29
+ export type TypeAndExistenceOperators<T = any> = {
30
+ $exists?: PartialOfType<T, boolean, any>;
31
+ $type?: PartialOfType<T, string>;
28
32
  };
29
- export type ArrayOperators = {
30
- $arrinc?: Record<string, any[]>;
31
- $arrincall?: Record<string, any[]>;
32
- $size?: Record<string, number>;
33
+ export type ArrayOperators<T = any> = {
34
+ $arrinc?: PartialPickMatching<T, any[]>;
35
+ $arrincall?: PartialPickMatching<T, any[]>;
36
+ $size?: PartialOfType<T, number>;
33
37
  };
34
- export type StringOperators = {
35
- $regex?: Record<string, RegExp>;
36
- $startsWith?: Record<string, string>;
37
- $endsWith?: Record<string, string>;
38
+ export type StringOperators<T = any> = {
39
+ $regex?: PartialOfType<T, RegExp, string>;
40
+ $startsWith?: PartialOfType<T, string>;
41
+ $endsWith?: PartialOfType<T, string>;
38
42
  };
39
- export type OtherOperators = {
40
- $subset?: Record<string, any>;
43
+ export type OtherOperators<T = any> = {
44
+ $subset?: Partial<Record<keyof T, T[keyof T]>>;
41
45
  };
42
- export type PredefinedSearchOperators = LogicalOperators & ComparisonOperators & TypeAndExistenceOperators & ArrayOperators & StringOperators & OtherOperators;
43
- export type SearchOptions = PredefinedSearchOperators & Arg;
44
- export type ArrayUpdater = {
45
- $push?: any;
46
- $pushset?: any;
47
- $pull?: any;
48
- $pullall?: any;
46
+ export type PredefinedSearchOperators<T = any> = LogicalOperators<T> & ComparisonOperators<T> & TypeAndExistenceOperators<T> & ArrayOperators<T> & StringOperators<T> & OtherOperators<T>;
47
+ export type SearchOptions<T = any> = PredefinedSearchOperators<T> & Arg<T>;
48
+ export type ArrayUpdater<T = any> = {
49
+ $push?: PartialOfType<T, any>;
50
+ $pushset?: PartialOfType<T, any>;
51
+ $pull?: PartialOfType<T, any>;
52
+ $pullall?: PartialOfType<T, any>;
49
53
  };
50
- export type ObjectUpdater = {
51
- $merge?: any;
54
+ export type ObjectUpdater<T = any> = {
55
+ $merge?: PartialOfType<T, any[]>;
52
56
  };
53
- export type ValueUpdater = {
54
- $set?: any;
55
- $inc?: any;
56
- $dec?: any;
57
- $unset?: any;
58
- $rename?: any;
57
+ export type ValueUpdater<T = any> = {
58
+ $inc?: PartialOfType<T, number>;
59
+ $dec?: PartialOfType<T, number>;
60
+ $unset?: PartialOfType<T, any>;
61
+ $rename?: PartialOfType<T, any>;
59
62
  };
60
- export type UpdaterArg = ArrayUpdater & ObjectUpdater & ValueUpdater & {
61
- [key: string]: any;
62
- };
63
- export interface Arg {
64
- _id?: Id;
65
- [key: string]: any;
66
- }
67
- export type SearchFunc<T = any> = (data: T, context: Context) => boolean;
68
- export type UpdaterFunc<T = any> = (data: T, context: Context) => boolean;
69
- export type Search<T = any> = SearchOptions | SearchFunc<T>;
70
- export type Updater<T = any> = UpdaterArg | UpdaterArg[] | UpdaterFunc<T>;
71
- export interface DbFindOpts {
63
+ export type UpdaterArg<T = any> = ArrayUpdater<T> & ObjectUpdater<T> & ValueUpdater<T> & Arg<T>;
64
+ export type Arg<T = any> = {
65
+ [K in keyof T]?: any;
66
+ } & Record<string, any>;
67
+ export type SearchFunc<T = any> = (data: T, context: VContext) => boolean;
68
+ export type UpdaterFunc<T = any> = (data: T, context: VContext) => boolean;
69
+ export type Search<T = any> = SearchOptions<T> | SearchFunc<T>;
70
+ export type Updater<T = any> = UpdaterArg<T> | UpdaterArg<T>[] | UpdaterFunc<T>;
71
+ export interface DbFindOpts<T = any> {
72
72
  reverse?: boolean;
73
73
  max?: number;
74
+ offset?: number;
75
+ sortBy?: KeysMatching<T, any>;
76
+ sortAsc?: boolean;
74
77
  }
75
- export interface FindOpts {
76
- select?: string[];
77
- exclude?: string[];
78
+ export interface FindOpts<T = any> {
79
+ select?: KeysMatching<T, any>[];
80
+ exclude?: KeysMatching<T, any>[];
78
81
  transform?: Function;
79
82
  }
80
- export interface Transaction {
81
- type: "update" | "updateOne" | "updateOneOrAdd" | "remove" | "removeOne";
82
- search: Search;
83
- updater?: Updater;
84
- addArg?: Arg;
85
- idGen?: boolean;
86
- context?: Context;
83
+ declare class CollectionManager {
84
+ private db;
85
+ private collection;
86
+ constructor(db: ValtheraCompatible, collection: string);
87
+ add<T = Data>(data: Arg, id_gen?: boolean): Promise<T>;
88
+ find<T = Data>(search: Search, context?: VContext, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
89
+ findOne<T = Data>(search: Search, context?: VContext, findOpts?: FindOpts): Promise<T>;
90
+ update(search: Search, updater: Updater, context?: VContext): Promise<boolean>;
91
+ updateOne(search: Search, updater: Updater, context?: VContext): Promise<boolean>;
92
+ remove(search: Search, context?: VContext): Promise<boolean>;
93
+ removeOne(search: Search, context?: VContext): Promise<boolean>;
94
+ updateOneOrAdd(search: Search, updater: Updater, add_arg?: Arg, context?: VContext, id_gen?: boolean): Promise<boolean>;
87
95
  }
88
96
  export interface ValtheraCompatible {
89
97
  c(collection: string): CollectionManager;
90
98
  getCollections(): Promise<string[]>;
91
99
  checkCollection(collection: string): Promise<boolean>;
92
100
  issetCollection(collection: string): Promise<boolean>;
93
- add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
94
- find<T = Data>(collection: string, search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
95
- findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T | null>;
96
- findStream<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts, limit?: number): Promise<AsyncGenerator<T>>;
97
- update(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
98
- updateOne(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
99
- remove(collection: string, search: Search, context?: Context): Promise<boolean>;
100
- removeOne(collection: string, search: Search, context?: Context): Promise<boolean>;
101
+ add<T = Data>(collection: string, data: Arg<T>, id_gen?: boolean): Promise<T>;
102
+ find<T = Data>(collection: string, search: Search<T>, context?: VContext, options?: DbFindOpts<T>, findOpts?: FindOpts<T>): Promise<T[]>;
103
+ findOne<T = Data>(collection: string, search: Search<T>, context?: VContext, findOpts?: FindOpts<T>): Promise<T | null>;
104
+ update<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
105
+ updateOne<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
106
+ remove<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
107
+ removeOne<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
101
108
  removeCollection(collection: string): Promise<boolean>;
102
- transaction(collection: string, transaction: Transaction[]): Promise<boolean>;
103
- updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
104
- }
105
- declare class CollectionManager {
106
- private db;
107
- private collection;
108
- constructor(db: ValtheraCompatible, collection: string);
109
- add<T = Data>(data: Arg, id_gen?: boolean): Promise<T>;
110
- find<T = Data>(search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
111
- findOne<T = Data>(search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
112
- findStream<T = Data>(search: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<T>;
113
- update(search: Search, updater: Updater, context?: Context): Promise<boolean>;
114
- updateOne(search: Search, updater: Updater, context?: Context): Promise<boolean>;
115
- remove(search: Search, context?: Context): Promise<boolean>;
116
- removeOne(search: Search, context?: Context): Promise<boolean>;
117
- updateOneOrAdd(search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
109
+ updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, add_arg?: Arg<T>, context?: VContext, id_gen?: boolean): Promise<boolean>;
118
110
  }
119
111
  declare namespace RelationTypes {
120
112
  type Path = [
@@ -145,39 +137,39 @@ declare namespace RelationTypes {
145
137
  };
146
138
  }
147
139
  }
148
- export type VQLQuery = {
149
- find: VQLFind;
150
- findOne: VQLFindOne;
151
- f: VQLFindOne;
152
- add: VQLAdd;
153
- update: VQLUpdate;
154
- updateOne: VQLUpdateOne;
155
- remove: VQLRemove;
156
- removeOne: VQLRemoveOne;
157
- updateOneOrAdd: VQLUpdateOneOrAdd;
140
+ export interface VQLQuery<T = any> {
141
+ find: VQLFind<T>;
142
+ findOne: VQLFindOne<T>;
143
+ f: VQLFindOne<T>;
144
+ add: VQLAdd<T>;
145
+ update: VQLUpdate<T>;
146
+ updateOne: VQLUpdateOne<T>;
147
+ remove: VQLRemove<T>;
148
+ removeOne: VQLRemoveOne<T>;
149
+ updateOneOrAdd: VQLUpdateOneOrAdd<T>;
158
150
  removeCollection: VQLCollectionOperation;
159
151
  checkCollection: VQLCollectionOperation;
160
152
  issetCollection: VQLCollectionOperation;
161
153
  getCollections: {};
162
- };
163
- export type VQLQueryData = {
164
- find: VQLFind;
154
+ }
155
+ export type VQLQueryData<T = any> = {
156
+ find: VQLFind<T>;
165
157
  } | {
166
- findOne: VQLFindOne;
158
+ findOne: VQLFindOne<T>;
167
159
  } | {
168
- f: VQLFindOne;
160
+ f: VQLFindOne<T>;
169
161
  } | {
170
- add: VQLAdd;
162
+ add: VQLAdd<T>;
171
163
  } | {
172
- update: VQLUpdate;
164
+ update: VQLUpdate<T>;
173
165
  } | {
174
- updateOne: VQLUpdateOne;
166
+ updateOne: VQLUpdateOne<T>;
175
167
  } | {
176
- remove: VQLRemove;
168
+ remove: VQLRemove<T>;
177
169
  } | {
178
- removeOne: VQLRemoveOne;
170
+ removeOne: VQLRemoveOne<T>;
179
171
  } | {
180
- updateOneOrAdd: VQLUpdateOneOrAdd;
172
+ updateOneOrAdd: VQLUpdateOneOrAdd<T>;
181
173
  } | {
182
174
  removeCollection: VQLCollectionOperation;
183
175
  } | {
@@ -187,56 +179,56 @@ export type VQLQueryData = {
187
179
  } | {
188
180
  getCollections: {};
189
181
  };
190
- export interface VQLRequest {
182
+ export interface VQLRequest<T = any> {
191
183
  db: string;
192
- d: VQLQueryData;
184
+ d: VQLQueryData<T>;
193
185
  }
194
- export interface VQLFind {
186
+ export interface VQLFind<T = any> {
195
187
  collection: string;
196
- search?: Search;
188
+ search?: Search<T>;
197
189
  limit?: number;
198
190
  fields?: VQLFields;
199
191
  select?: VQLFields;
200
192
  relations?: VQLRelations;
201
- options?: DbFindOpts;
202
- searchOpts?: FindOpts;
193
+ options?: DbFindOpts<T>;
194
+ searchOpts?: FindOpts<T>;
203
195
  }
204
- export interface VQLFindOne {
196
+ export interface VQLFindOne<T = any> {
205
197
  collection: string;
206
- search: Search;
198
+ search: Search<T>;
207
199
  fields?: VQLFields;
208
200
  select?: VQLFields;
209
201
  relations?: VQLRelations;
210
- searchOpts?: FindOpts;
202
+ searchOpts?: FindOpts<T>;
211
203
  }
212
- export interface VQLAdd {
204
+ export interface VQLAdd<T = any> {
213
205
  collection: string;
214
- data: Arg;
206
+ data: Arg<T>;
215
207
  id_gen?: boolean;
216
208
  }
217
- export interface VQLUpdate {
209
+ export interface VQLUpdate<T = any> {
218
210
  collection: string;
219
- search: Search;
220
- updater: UpdaterArg;
211
+ search: Search<T>;
212
+ updater: UpdaterArg<T>;
221
213
  }
222
- export interface VQLUpdateOne {
214
+ export interface VQLUpdateOne<T = any> {
223
215
  collection: string;
224
- search: Search;
225
- updater: UpdaterArg;
216
+ search: Search<T>;
217
+ updater: UpdaterArg<T>;
226
218
  }
227
- export interface VQLRemove {
219
+ export interface VQLRemove<T = any> {
228
220
  collection: string;
229
- search: Search;
221
+ search: Search<T>;
230
222
  }
231
- export interface VQLRemoveOne {
223
+ export interface VQLRemoveOne<T = any> {
232
224
  collection: string;
233
- search: Search;
225
+ search: Search<T>;
234
226
  }
235
- export interface VQLUpdateOneOrAdd {
227
+ export interface VQLUpdateOneOrAdd<T = any> {
236
228
  collection: string;
237
- search: Search;
238
- updater: UpdaterArg;
239
- add_arg?: Arg;
229
+ search: Search<T>;
230
+ updater: UpdaterArg<T>;
231
+ add_arg?: Arg<T>;
240
232
  id_gen?: boolean;
241
233
  }
242
234
  export interface VQLCollectionOperation {
@@ -264,15 +256,15 @@ export type VQLRefRequired = VQLRef & Required<Pick<VQLRef, "ref">>;
264
256
  export type DeepPartial<T> = {
265
257
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
266
258
  };
267
- export type VQL = (VQLRequest | RelationQuery) & VQLRef;
268
- export type VQLR = VQL | (DeepPartial<VQL> & VQLRefRequired) | VQLRefRequired;
259
+ export type VQL<T = any> = (VQLRequest<T> | RelationQuery) & VQLRef;
260
+ export type VQLR<T = any> = VQL<T> | (DeepPartial<VQL<T>> & VQLRefRequired) | VQLRefRequired;
269
261
  export interface VQLError {
270
262
  err: true;
271
263
  msg: string;
272
264
  c: number;
273
265
  why?: string;
274
266
  }
275
- export type VqlQueryRaw = VQLR | string | {
267
+ export type VqlQueryRaw<T = any> = VQLR<T> | string | {
276
268
  query: string;
277
269
  } & VQLRef;
278
270
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/vql-client",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",