@wxn0brp/vql 0.6.2 → 0.6.4

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.
@@ -45,17 +45,20 @@ export interface ValtheraResolver {
45
45
  export declare function createValtheraAdapter(resolver: ValtheraResolver, extendedFind?: boolean): ValtheraCompatible;
46
46
  export type Operation = "add" | "find" | "findOne" | "update" | "updateOne" | "updateOneOrAdd" | "remove" | "removeOne" | "removeCollection";
47
47
  export declare class AdapterBuilder {
48
+ private catchCb;
48
49
  private handlers;
49
50
  private collections;
50
- register(op: "add", collection: string, fn: ResolverFn<[collection: string, data: any, id_gen?: boolean], any>): any;
51
- register(op: "find", collection: string, fn: ResolverFn<[collection: string, search: any, context?: any, options?: any, findOpts?: any], any[]>): any;
52
- register(op: "findOne", collection: string, fn: ResolverFn<[collection: string, search: any, context?: any, findOpts?: any], any | null>): any;
53
- register(op: "update", collection: string, fn: ResolverFn<[collection: string, search: any, updater: any], boolean>): any;
54
- register(op: "updateOne", collection: string, fn: ResolverFn<[collection: string, search: any, updater: any], boolean>): any;
55
- register(op: "updateOneOrAdd", collection: string, fn: ResolverFn<[collection: string, search: any, updater: any, add_arg?: any, context?: any, id_gen?: boolean], boolean>): any;
56
- register(op: "remove", collection: string, fn: ResolverFn<[collection: string, search: any], boolean>): any;
57
- register(op: "removeOne", collection: string, fn: ResolverFn<[collection: string, search: any], boolean>): any;
58
- register(op: "removeCollection", collection: string, fn: ResolverFn<[collection: string], boolean>): any;
51
+ constructor(catchCb?: (e: any, op: string, args: any[]) => void);
52
+ register(op: Operation, collection: string, fn: Function): this;
53
+ add(collection: string, fn: ResolverFn<[data: any, id_gen?: boolean], any>): this;
54
+ find(collection: string, fn: ResolverFn<[search: any, context?: any, options?: any, findOpts?: any], any[]>): this;
55
+ findOne(collection: string, fn: ResolverFn<[search: any, context?: any, findOpts?: any], any | null>): this;
56
+ update(collection: string, fn: ResolverFn<[collection: string, search: any, updater: any], boolean>): this;
57
+ updateOne(collection: string, fn: ResolverFn<[search: any, updater: any], boolean>): this;
58
+ updateOneOrAdd(collection: string, fn: ResolverFn<[search: any, updater: any, add_arg?: any, context?: any, id_gen?: boolean], boolean>): this;
59
+ remove(collection: string, fn: ResolverFn<[search: any], boolean>): this;
60
+ removeOne(collection: string, fn: ResolverFn<[search: any], boolean>): this;
61
+ removeCollection(collection: string, fn: ResolverFn<[], boolean>): this;
59
62
  getAdapter(extendedFind?: boolean): ValtheraCompatible;
60
63
  }
61
64
  export {};
@@ -1,5 +1,12 @@
1
1
  import CollectionManager from "@wxn0brp/db-core/helpers/CollectionManager";
2
2
  import updateFindObject from "@wxn0brp/db-core/utils/updateFindObject";
3
+ const list = [
4
+ "ensureCollection", "issetCollection", "getCollections", "removeCollection",
5
+ "add",
6
+ "find", "findOne",
7
+ "update", "updateOne", "updateOneOrAdd",
8
+ "remove", "removeOne"
9
+ ];
3
10
  export function createValtheraAdapter(resolver, extendedFind = false) {
4
11
  const safe = (fn) => {
5
12
  if (!fn)
@@ -9,21 +16,11 @@ export function createValtheraAdapter(resolver, extendedFind = false) {
9
16
  const adapter = {
10
17
  // @ts-ignore
11
18
  meta: resolver.meta ?? { type: "api", version: "0.0.1" },
12
- c: null,
13
- getCollections: () => safe(resolver.getCollections)(),
14
- issetCollection: (c) => safe(resolver.issetCollection)(c),
15
- ensureCollection: (c) => safe(resolver.ensureCollection)(c),
16
- add: (col, data, id_gen) => safe(resolver.add)(col, data, id_gen),
17
- find: (col, search, context, options, findOpts) => safe(resolver.find)(col, search, context, options, findOpts),
18
- findOne: (col, search, context, findOpts) => safe(resolver.findOne)(col, search, context, findOpts),
19
- update: (col, search, up) => safe(resolver.update)(col, search, up),
20
- updateOne: (col, search, up) => safe(resolver.updateOne)(col, search, up),
21
- updateOneOrAdd: (col, search, up, add_data, ctx, id_gen) => safe(resolver.updateOneOrAdd)(col, search, up, add_data, ctx, id_gen),
22
- remove: (col, search) => safe(resolver.remove)(col, search),
23
- removeOne: (col, search) => safe(resolver.removeOne)(col, search),
24
- removeCollection: (col) => safe(resolver.removeCollection)(col),
25
19
  };
26
20
  adapter.c = (collection) => new CollectionManager(adapter, collection);
21
+ for (const name of list) {
22
+ adapter[name] = (...args) => safe(resolver[name])(...args);
23
+ }
27
24
  if (extendedFind) {
28
25
  adapter.find = async (col, search, context, options, findOpts) => {
29
26
  let data = await safe(resolver.find)(col, search, context, options, findOpts);
@@ -44,19 +41,58 @@ export function createValtheraAdapter(resolver, extendedFind = false) {
44
41
  return adapter;
45
42
  }
46
43
  export class AdapterBuilder {
44
+ catchCb;
47
45
  handlers = new Map();
48
46
  collections = new Set();
47
+ constructor(catchCb = (e) => { console.log(e); }) {
48
+ this.catchCb = catchCb;
49
+ }
49
50
  register(op, collection, fn) {
50
51
  this.handlers.set(`${op}:${collection}`, fn);
51
52
  this.collections.add(collection);
52
53
  return this;
53
54
  }
55
+ add(collection, fn) {
56
+ return this.register("add", collection, fn);
57
+ }
58
+ find(collection, fn) {
59
+ return this.register("find", collection, fn);
60
+ }
61
+ findOne(collection, fn) {
62
+ return this.register("findOne", collection, fn);
63
+ }
64
+ update(collection, fn) {
65
+ return this.register("update", collection, fn);
66
+ }
67
+ updateOne(collection, fn) {
68
+ return this.register("updateOne", collection, fn);
69
+ }
70
+ updateOneOrAdd(collection, fn) {
71
+ return this.register("updateOneOrAdd", collection, fn);
72
+ }
73
+ remove(collection, fn) {
74
+ return this.register("remove", collection, fn);
75
+ }
76
+ removeOne(collection, fn) {
77
+ return this.register("removeOne", collection, fn);
78
+ }
79
+ removeCollection(collection, fn) {
80
+ return this.register("removeCollection", collection, fn);
81
+ }
54
82
  getAdapter(extendedFind = true) {
55
- const resolve = async (op, col, ...args) => {
83
+ const resolve = async (op, ...args) => {
84
+ const col = args.shift();
56
85
  const handler = this.handlers.get(`${op}:${col}`) || this.handlers.get(`${op}:*`) || null;
57
86
  if (!handler)
58
87
  throw new Error(`Unimplemented method: ${op}:${col}`);
59
- return handler(...args);
88
+ if (col === "*")
89
+ args.unshift(col);
90
+ try {
91
+ return await handler(...args);
92
+ }
93
+ catch (e) {
94
+ this.catchCb(e, op, args);
95
+ }
60
96
  };
61
97
  const adapter = createValtheraAdapter({
62
98
  getCollections: async () => Array.from(this.collections),
@@ -66,16 +102,10 @@ export class AdapterBuilder {
66
102
  this.collections.add(col);
67
103
  return true;
68
104
  },
69
- add: (col, data, id_gen) => resolve("add", col, data, id_gen),
70
- find: (col, search, context, options, findOpts) => resolve("find", col, search, context, options, findOpts),
71
- findOne: (col, search, context, findOpts) => resolve("findOne", col, search, context, findOpts),
72
- update: (col, search, up) => resolve("update", col, search, up),
73
- updateOne: (col, search, up) => resolve("updateOne", col, search, up),
74
- updateOneOrAdd: (col, search, up, add_data, ctx, id_gen) => resolve("updateOneOrAdd", col, search, up, add_data, ctx, id_gen),
75
- remove: (col, search) => resolve("remove", col, search),
76
- removeOne: (col, search) => resolve("removeOne", col, search),
77
- removeCollection: (col) => resolve("removeCollection", col),
78
105
  }, extendedFind);
106
+ for (const name of ["add", "find", "findOne", "update", "updateOne", "updateOneOrAdd", "remove", "removeOne", "removeCollection"]) {
107
+ adapter[name] = (...args) => resolve(name, ...args);
108
+ }
79
109
  return adapter;
80
110
  }
81
111
  }
@@ -1,8 +1,7 @@
1
1
  import { checkRelationPermission } from "../permissions/index.js";
2
- import { parseObjectSelect, parseSelect } from "./utils.js";
2
+ import { parseSelect } from "./utils.js";
3
3
  function standardizeRelationRequest(config, req) {
4
- const select = parseObjectSelect(req.select) || [];
5
- req.select = parseSelect(config, select);
4
+ req.select = parseSelect(config, req.select);
6
5
  }
7
6
  function checkDBsExist(cpu, req) {
8
7
  const db = req.path[0];
@@ -1,3 +1,2 @@
1
1
  import { VQLConfig } from "../config.js";
2
2
  export declare function parseSelect(config: VQLConfig, select: object | object[]): any[];
3
- export declare function parseObjectSelect(obj: object): any[];
package/dist/cpu/utils.js CHANGED
@@ -4,27 +4,13 @@ export function parseSelect(config, select) {
4
4
  return undefined;
5
5
  return select;
6
6
  }
7
- else {
7
+ else if (typeof select === "object") {
8
8
  const keys = Object.keys(select);
9
9
  if (!config.strictSelect && keys.length === 0)
10
10
  return undefined;
11
11
  return keys.filter(k => !!select[k]);
12
12
  }
13
- }
14
- export function parseObjectSelect(obj) {
15
- if (Array.isArray(obj))
16
- return obj;
17
- let result = [];
18
- function walk(o, path = []) {
19
- if (o !== null && typeof o === "object") {
20
- for (const k of Object.keys(o)) {
21
- walk(o[k], [...path, k]);
22
- }
23
- }
24
- else {
25
- result.push(path);
26
- }
13
+ else {
14
+ return config.strictSelect ? [] : undefined;
27
15
  }
28
- walk(obj);
29
- return result;
30
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/vql",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "main": "dist/index.js",
5
5
  "author": "wxn0brP",
6
6
  "license": "MIT",