@wxn0brp/vql-client 0.0.2 → 0.0.5

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
@@ -1,4 +1,5 @@
1
- export type VQLQuery = string | object;
1
+ import { VQLR } from "./vql.js";
2
+ export type VQLQuery = string | VQLR;
2
3
  export type VQLResult<T = any> = Promise<T>;
3
4
  export type VQLTransport = (query: VQLQuery) => VQLResult;
4
5
  export type VQLHooks = {
@@ -9,6 +10,7 @@ export type VQLHooks = {
9
10
  export declare function initVQLClient(config: {
10
11
  transport?: VQLTransport;
11
12
  hooks?: VQLHooks;
13
+ defaultFetchUrl?: string;
12
14
  }): void;
13
15
  export declare function fetchVQL<T = any>(query: VQLQuery): Promise<T>;
14
16
  export declare function resetVQLClient(): void;
package/dist/index.js CHANGED
@@ -1,10 +1,13 @@
1
1
  let transport = defaultFetchTransport;
2
2
  let hooks = {};
3
+ let defaultFetchUrl = "/VQL";
3
4
  export function initVQLClient(config) {
4
5
  if (config.transport)
5
6
  transport = config.transport;
6
7
  if (config.hooks)
7
8
  hooks = config.hooks;
9
+ if (config.defaultFetchUrl)
10
+ defaultFetchUrl = config.defaultFetchUrl;
8
11
  }
9
12
  export async function fetchVQL(query) {
10
13
  const start = Date.now();
@@ -30,7 +33,7 @@ export function resetVQLClient() {
30
33
  hooks = {};
31
34
  }
32
35
  export async function defaultFetchTransport(query) {
33
- const res = await fetch("/VQL", {
36
+ const res = await fetch(defaultFetchUrl, {
34
37
  method: "POST",
35
38
  headers: {
36
39
  "Content-Type": "application/json"
@@ -27,9 +27,11 @@ __export(index_exports, {
27
27
  module.exports = __toCommonJS(index_exports);
28
28
  var transport = defaultFetchTransport;
29
29
  var hooks = {};
30
+ var defaultFetchUrl = "/VQL";
30
31
  function initVQLClient(config) {
31
32
  if (config.transport) transport = config.transport;
32
33
  if (config.hooks) hooks = config.hooks;
34
+ if (config.defaultFetchUrl) defaultFetchUrl = config.defaultFetchUrl;
33
35
  }
34
36
  async function fetchVQL(query) {
35
37
  var _a, _b, _c, _d, _e;
@@ -55,7 +57,7 @@ function resetVQLClient() {
55
57
  hooks = {};
56
58
  }
57
59
  async function defaultFetchTransport(query) {
58
- const res = await fetch("/VQL", {
60
+ const res = await fetch(defaultFetchUrl, {
59
61
  method: "POST",
60
62
  headers: {
61
63
  "Content-Type": "application/json"
@@ -1 +1 @@
1
- var VQLClient=(()=>{var Q=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var x=(t,r)=>{for(var a in r)Q(t,a,{get:r[a],enumerable:!0})},E=(t,r,a,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of l(r))!T.call(t,o)&&o!==a&&Q(t,o,{get:()=>r[o],enumerable:!(i=f(r,o))||i.enumerable});return t};var h=t=>E(Q({},"__esModule",{value:!0}),t);var m={};x(m,{defaultFetchTransport:()=>s,fetchVQL:()=>d,initVQLClient:()=>V,resetVQLClient:()=>c});var u=s,n={};function V(t){t.transport&&(u=t.transport),t.hooks&&(n=t.hooks)}async function d(t){var a,i,o,p,y;let r=Date.now();try{(a=n.onStart)==null||a.call(n,t);let e=await u(t),w=Date.now()-r;if((i=n.onEnd)==null||i.call(n,t,w,e),e!=null&&e.err){let L=new Error(e.err);throw(o=n.onError)==null||o.call(n,t,L),L}return(p=e==null?void 0:e.result)!=null?p:e}catch(e){throw(y=n.onError)==null||y.call(n,t,e),e}}function c(){u=s,n={}}async function s(t){let r=await fetch("/VQL",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:t})});if(!r.ok)throw new Error(`VQL request failed: ${r.status}`);return await r.json()}typeof window!="undefined"&&(window.VQLClient={fetchVQL:d,initVQLClient:V,resetVQLClient:c,defaultFetchTransport:s});return h(m);})();
1
+ var VQLClient=(()=>{var Q=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var x=(t,r)=>{for(var a in r)Q(t,a,{get:r[a],enumerable:!0})},m=(t,r,a,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of T(r))!h.call(t,o)&&o!==a&&Q(t,o,{get:()=>r[o],enumerable:!(i=w(r,o))||i.enumerable});return t};var E=t=>m(Q({},"__esModule",{value:!0}),t);var F={};x(F,{defaultFetchTransport:()=>s,fetchVQL:()=>d,initVQLClient:()=>l,resetVQLClient:()=>f});var u=s,e={},V="/VQL";function l(t){t.transport&&(u=t.transport),t.hooks&&(e=t.hooks),t.defaultFetchUrl&&(V=t.defaultFetchUrl)}async function d(t){var a,i,o,p,y;let r=Date.now();try{(a=e.onStart)==null||a.call(e,t);let n=await u(t),c=Date.now()-r;if((i=e.onEnd)==null||i.call(e,t,c,n),n!=null&&n.err){let L=new Error(n.err);throw(o=e.onError)==null||o.call(e,t,L),L}return(p=n==null?void 0:n.result)!=null?p:n}catch(n){throw(y=e.onError)==null||y.call(e,t,n),n}}function f(){u=s,e={}}async function s(t){let r=await fetch(V,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:t})});if(!r.ok)throw new Error(`VQL request failed: ${r.status}`);return await r.json()}typeof window!="undefined"&&(window.VQLClient={fetchVQL:d,initVQLClient:l,resetVQLClient:f,defaultFetchTransport:s});return E(F);})();
package/dist/vql.d.ts ADDED
@@ -0,0 +1,275 @@
1
+ export interface Data {
2
+ [key: string]: any;
3
+ }
4
+ export interface Context {
5
+ [key: string]: any;
6
+ }
7
+ export type Id = string;
8
+ export type LogicalOperators = {
9
+ $and?: Array<SearchOptions>;
10
+ $or?: Array<SearchOptions>;
11
+ $not?: SearchOptions;
12
+ };
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, [
21
+ number,
22
+ number
23
+ ]>;
24
+ };
25
+ export type TypeAndExistenceOperators = {
26
+ $exists?: Record<string, boolean>;
27
+ $type?: Record<string, string>;
28
+ };
29
+ export type ArrayOperators = {
30
+ $arrinc?: Record<string, any[]>;
31
+ $arrincall?: Record<string, any[]>;
32
+ $size?: Record<string, number>;
33
+ };
34
+ export type StringOperators = {
35
+ $regex?: Record<string, RegExp>;
36
+ $startsWith?: Record<string, string>;
37
+ $endsWith?: Record<string, string>;
38
+ };
39
+ export type OtherOperators = {
40
+ $subset?: Record<string, any>;
41
+ };
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;
49
+ };
50
+ export type ObjectUpdater = {
51
+ $merge?: any;
52
+ };
53
+ export type ValueUpdater = {
54
+ $set?: any;
55
+ $inc?: any;
56
+ $dec?: any;
57
+ $unset?: any;
58
+ $rename?: any;
59
+ };
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 {
72
+ reverse?: boolean;
73
+ max?: number;
74
+ }
75
+ export interface FindOpts {
76
+ select?: string[];
77
+ exclude?: string[];
78
+ transform?: Function;
79
+ }
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;
87
+ }
88
+ export interface ValtheraCompatible {
89
+ c(collection: string): CollectionManager;
90
+ getCollections(): Promise<string[]>;
91
+ checkCollection(collection: string): Promise<boolean>;
92
+ 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
+ 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>;
118
+ }
119
+ declare namespace RelationTypes {
120
+ type Path = [
121
+ string,
122
+ string
123
+ ];
124
+ type FieldPath = string[];
125
+ interface DBS {
126
+ [key: string]: ValtheraCompatible;
127
+ }
128
+ interface Relation {
129
+ [key: string]: RelationConfig;
130
+ }
131
+ interface RelationConfig {
132
+ path: Path;
133
+ pk?: string;
134
+ fk?: string;
135
+ as?: string;
136
+ select?: string[];
137
+ findOpts?: DbFindOpts;
138
+ type?: "1" | "11" | "1n" | "nm";
139
+ relations?: Relation;
140
+ through?: {
141
+ table: string;
142
+ db?: string;
143
+ pk: string;
144
+ fk: string;
145
+ };
146
+ }
147
+ }
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;
158
+ removeCollection: VQLCollectionOperation;
159
+ checkCollection: VQLCollectionOperation;
160
+ issetCollection: VQLCollectionOperation;
161
+ getCollections: {};
162
+ };
163
+ export type VQLQueryData = {
164
+ find: VQLFind;
165
+ } | {
166
+ findOne: VQLFindOne;
167
+ } | {
168
+ f: VQLFindOne;
169
+ } | {
170
+ add: VQLAdd;
171
+ } | {
172
+ update: VQLUpdate;
173
+ } | {
174
+ updateOne: VQLUpdateOne;
175
+ } | {
176
+ remove: VQLRemove;
177
+ } | {
178
+ removeOne: VQLRemoveOne;
179
+ } | {
180
+ updateOneOrAdd: VQLUpdateOneOrAdd;
181
+ } | {
182
+ removeCollection: VQLCollectionOperation;
183
+ } | {
184
+ checkCollection: VQLCollectionOperation;
185
+ } | {
186
+ issetCollection: VQLCollectionOperation;
187
+ } | {
188
+ getCollections: {};
189
+ };
190
+ export interface VQLRequest {
191
+ db: string;
192
+ d: VQLQueryData;
193
+ }
194
+ export interface VQLFind {
195
+ collection: string;
196
+ search?: Search;
197
+ limit?: number;
198
+ fields?: VQLFields;
199
+ select?: VQLFields;
200
+ relations?: VQLRelations;
201
+ options?: DbFindOpts;
202
+ searchOpts?: FindOpts;
203
+ }
204
+ export interface VQLFindOne {
205
+ collection: string;
206
+ search: Search;
207
+ fields?: VQLFields;
208
+ select?: VQLFields;
209
+ relations?: VQLRelations;
210
+ searchOpts?: FindOpts;
211
+ }
212
+ export interface VQLAdd {
213
+ collection: string;
214
+ data: Arg;
215
+ id_gen?: boolean;
216
+ }
217
+ export interface VQLUpdate {
218
+ collection: string;
219
+ search: Search;
220
+ updater: UpdaterArg;
221
+ }
222
+ export interface VQLUpdateOne {
223
+ collection: string;
224
+ search: Search;
225
+ updater: UpdaterArg;
226
+ }
227
+ export interface VQLRemove {
228
+ collection: string;
229
+ search: Search;
230
+ }
231
+ export interface VQLRemoveOne {
232
+ collection: string;
233
+ search: Search;
234
+ }
235
+ export interface VQLUpdateOneOrAdd {
236
+ collection: string;
237
+ search: Search;
238
+ updater: UpdaterArg;
239
+ add_arg?: Arg;
240
+ id_gen?: boolean;
241
+ }
242
+ export interface VQLCollectionOperation {
243
+ collection: string;
244
+ }
245
+ export type VQLFields = Record<string, boolean | number> | string[];
246
+ export type VQLRelations = Record<string, VQLFind | VQLFindOne>;
247
+ export interface RelationQuery {
248
+ r: {
249
+ path: RelationTypes.Path;
250
+ search: Search;
251
+ relations: RelationTypes.Relation;
252
+ many?: boolean;
253
+ options?: DbFindOpts;
254
+ select?: RelationTypes.FieldPath[];
255
+ };
256
+ }
257
+ export interface VQLRef {
258
+ ref?: string;
259
+ var?: {
260
+ [k: string]: any;
261
+ };
262
+ }
263
+ export type VQLRefRequired = VQLRef & Required<Pick<VQLRef, "ref">>;
264
+ export type DeepPartial<T> = {
265
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
266
+ };
267
+ export type VQL = (VQLRequest | RelationQuery) & VQLRef;
268
+ export type VQLR = VQL | (DeepPartial<VQL> & VQLRefRequired) | VQLRefRequired;
269
+ export interface VQLError {
270
+ err: true;
271
+ msg: string;
272
+ c: number;
273
+ why?: string;
274
+ }
275
+ export {};
package/dist/vql.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/vql-client",
3
- "version": "0.0.2",
3
+ "version": "0.0.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",
@@ -20,6 +20,12 @@
20
20
  "import": "./dist/index.js",
21
21
  "require": "./dist/index.cjs",
22
22
  "default": "./dist/index.js"
23
+ },
24
+ "./*": {
25
+ "types": "./dist/*.d.ts",
26
+ "import": "./dist/*.js",
27
+ "require": "./dist/*.cjs",
28
+ "default": "./dist/*.js"
23
29
  }
24
30
  }
25
31
  }