@wxn0brp/vql-client 0.0.11 → 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,112 +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
74
  offset?: number;
75
- sortBy?: string;
75
+ sortBy?: KeysMatching<T, any>;
76
76
  sortAsc?: boolean;
77
77
  }
78
- export interface FindOpts {
79
- select?: string[];
80
- exclude?: string[];
78
+ export interface FindOpts<T = any> {
79
+ select?: KeysMatching<T, any>[];
80
+ exclude?: KeysMatching<T, any>[];
81
81
  transform?: Function;
82
82
  }
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>;
95
+ }
83
96
  export interface ValtheraCompatible {
84
97
  c(collection: string): CollectionManager;
85
98
  getCollections(): Promise<string[]>;
86
99
  checkCollection(collection: string): Promise<boolean>;
87
100
  issetCollection(collection: string): Promise<boolean>;
88
- add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
89
- find<T = Data>(collection: string, search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
90
- findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T | null>;
91
- update(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
92
- updateOne(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
93
- remove(collection: string, search: Search, context?: Context): Promise<boolean>;
94
- 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>;
95
108
  removeCollection(collection: string): Promise<boolean>;
96
- updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
97
- }
98
- declare class CollectionManager {
99
- private db;
100
- private collection;
101
- constructor(db: ValtheraCompatible, collection: string);
102
- add<T = Data>(data: Arg, id_gen?: boolean): Promise<T>;
103
- find<T = Data>(search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
104
- findOne<T = Data>(search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
105
- update(search: Search, updater: Updater, context?: Context): Promise<boolean>;
106
- updateOne(search: Search, updater: Updater, context?: Context): Promise<boolean>;
107
- remove(search: Search, context?: Context): Promise<boolean>;
108
- removeOne(search: Search, context?: Context): Promise<boolean>;
109
- 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>;
110
110
  }
111
111
  declare namespace RelationTypes {
112
112
  type Path = [
@@ -137,39 +137,39 @@ declare namespace RelationTypes {
137
137
  };
138
138
  }
139
139
  }
140
- export type VQLQuery = {
141
- find: VQLFind;
142
- findOne: VQLFindOne;
143
- f: VQLFindOne;
144
- add: VQLAdd;
145
- update: VQLUpdate;
146
- updateOne: VQLUpdateOne;
147
- remove: VQLRemove;
148
- removeOne: VQLRemoveOne;
149
- 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>;
150
150
  removeCollection: VQLCollectionOperation;
151
151
  checkCollection: VQLCollectionOperation;
152
152
  issetCollection: VQLCollectionOperation;
153
153
  getCollections: {};
154
- };
155
- export type VQLQueryData = {
156
- find: VQLFind;
154
+ }
155
+ export type VQLQueryData<T = any> = {
156
+ find: VQLFind<T>;
157
157
  } | {
158
- findOne: VQLFindOne;
158
+ findOne: VQLFindOne<T>;
159
159
  } | {
160
- f: VQLFindOne;
160
+ f: VQLFindOne<T>;
161
161
  } | {
162
- add: VQLAdd;
162
+ add: VQLAdd<T>;
163
163
  } | {
164
- update: VQLUpdate;
164
+ update: VQLUpdate<T>;
165
165
  } | {
166
- updateOne: VQLUpdateOne;
166
+ updateOne: VQLUpdateOne<T>;
167
167
  } | {
168
- remove: VQLRemove;
168
+ remove: VQLRemove<T>;
169
169
  } | {
170
- removeOne: VQLRemoveOne;
170
+ removeOne: VQLRemoveOne<T>;
171
171
  } | {
172
- updateOneOrAdd: VQLUpdateOneOrAdd;
172
+ updateOneOrAdd: VQLUpdateOneOrAdd<T>;
173
173
  } | {
174
174
  removeCollection: VQLCollectionOperation;
175
175
  } | {
@@ -179,56 +179,56 @@ export type VQLQueryData = {
179
179
  } | {
180
180
  getCollections: {};
181
181
  };
182
- export interface VQLRequest {
182
+ export interface VQLRequest<T = any> {
183
183
  db: string;
184
- d: VQLQueryData;
184
+ d: VQLQueryData<T>;
185
185
  }
186
- export interface VQLFind {
186
+ export interface VQLFind<T = any> {
187
187
  collection: string;
188
- search?: Search;
188
+ search?: Search<T>;
189
189
  limit?: number;
190
190
  fields?: VQLFields;
191
191
  select?: VQLFields;
192
192
  relations?: VQLRelations;
193
- options?: DbFindOpts;
194
- searchOpts?: FindOpts;
193
+ options?: DbFindOpts<T>;
194
+ searchOpts?: FindOpts<T>;
195
195
  }
196
- export interface VQLFindOne {
196
+ export interface VQLFindOne<T = any> {
197
197
  collection: string;
198
- search: Search;
198
+ search: Search<T>;
199
199
  fields?: VQLFields;
200
200
  select?: VQLFields;
201
201
  relations?: VQLRelations;
202
- searchOpts?: FindOpts;
202
+ searchOpts?: FindOpts<T>;
203
203
  }
204
- export interface VQLAdd {
204
+ export interface VQLAdd<T = any> {
205
205
  collection: string;
206
- data: Arg;
206
+ data: Arg<T>;
207
207
  id_gen?: boolean;
208
208
  }
209
- export interface VQLUpdate {
209
+ export interface VQLUpdate<T = any> {
210
210
  collection: string;
211
- search: Search;
212
- updater: UpdaterArg;
211
+ search: Search<T>;
212
+ updater: UpdaterArg<T>;
213
213
  }
214
- export interface VQLUpdateOne {
214
+ export interface VQLUpdateOne<T = any> {
215
215
  collection: string;
216
- search: Search;
217
- updater: UpdaterArg;
216
+ search: Search<T>;
217
+ updater: UpdaterArg<T>;
218
218
  }
219
- export interface VQLRemove {
219
+ export interface VQLRemove<T = any> {
220
220
  collection: string;
221
- search: Search;
221
+ search: Search<T>;
222
222
  }
223
- export interface VQLRemoveOne {
223
+ export interface VQLRemoveOne<T = any> {
224
224
  collection: string;
225
- search: Search;
225
+ search: Search<T>;
226
226
  }
227
- export interface VQLUpdateOneOrAdd {
227
+ export interface VQLUpdateOneOrAdd<T = any> {
228
228
  collection: string;
229
- search: Search;
230
- updater: UpdaterArg;
231
- add_arg?: Arg;
229
+ search: Search<T>;
230
+ updater: UpdaterArg<T>;
231
+ add_arg?: Arg<T>;
232
232
  id_gen?: boolean;
233
233
  }
234
234
  export interface VQLCollectionOperation {
@@ -256,15 +256,15 @@ export type VQLRefRequired = VQLRef & Required<Pick<VQLRef, "ref">>;
256
256
  export type DeepPartial<T> = {
257
257
  [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
258
258
  };
259
- export type VQL = (VQLRequest | RelationQuery) & VQLRef;
260
- 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;
261
261
  export interface VQLError {
262
262
  err: true;
263
263
  msg: string;
264
264
  c: number;
265
265
  why?: string;
266
266
  }
267
- export type VqlQueryRaw = VQLR | string | {
267
+ export type VqlQueryRaw<T = any> = VQLR<T> | string | {
268
268
  query: string;
269
269
  } & VQLRef;
270
270
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/vql-client",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",