electrodb 2.10.0 → 2.10.2

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/src/where.js CHANGED
@@ -1,147 +1,169 @@
1
- const {MethodTypes, ExpressionTypes, BuilderTypes} = require("./types");
2
- const {AttributeOperationProxy, ExpressionState, FilterOperations} = require("./operations");
1
+ const { MethodTypes, ExpressionTypes, BuilderTypes } = require("./types");
2
+ const {
3
+ AttributeOperationProxy,
4
+ ExpressionState,
5
+ FilterOperations,
6
+ } = require("./operations");
3
7
  const e = require("./errors");
4
8
 
5
9
  class FilterExpression extends ExpressionState {
6
- constructor(props) {
7
- super(props);
8
- this.expression = "";
9
- this.type = BuilderTypes.filter;
10
- }
10
+ constructor(props) {
11
+ super(props);
12
+ this.expression = "";
13
+ this.type = BuilderTypes.filter;
14
+ }
11
15
 
12
- _trim(expression) {
13
- if (typeof expression === "string" && expression.length > 0) {
14
- return expression.replace(/\n|\r/g, "").trim();
15
- }
16
- return "";
17
- }
16
+ _trim(expression) {
17
+ if (typeof expression === "string" && expression.length > 0) {
18
+ return expression.replace(/\n|\r/g, "").trim();
19
+ }
20
+ return "";
21
+ }
18
22
 
19
- _isEmpty(expression) {
20
- if (typeof expression !== "string") {
21
- throw new Error("Invalid expression value type. Expected type string.");
22
- }
23
- return !expression.replace(/\n|\r|\w/g, "").trim();
24
- }
23
+ _isEmpty(expression) {
24
+ if (typeof expression !== "string") {
25
+ throw new Error("Invalid expression value type. Expected type string.");
26
+ }
27
+ return !expression.replace(/\n|\r|\w/g, "").trim();
28
+ }
25
29
 
26
- add(newExpression) {
27
- let expression = "";
28
- let existingExpression = this.expression;
29
- if (typeof existingExpression === "string" && existingExpression.length > 0) {
30
- newExpression = this._trim(newExpression);
31
- let isEmpty = this._isEmpty(newExpression);
32
- if (isEmpty) {
33
- return existingExpression;
34
- }
35
- let existingNeedsParens = !existingExpression.startsWith("(") && !existingExpression.endsWith(")");
36
- if (existingNeedsParens) {
37
- existingExpression = `(${existingExpression})`;
38
- }
39
- expression = `${existingExpression} AND ${newExpression}`;
40
- } else {
41
- expression = this._trim(newExpression);
42
- }
43
- this.expression = expression;
44
- }
30
+ add(newExpression) {
31
+ let expression = "";
32
+ let existingExpression = this.expression;
33
+ if (
34
+ typeof existingExpression === "string" &&
35
+ existingExpression.length > 0
36
+ ) {
37
+ newExpression = this._trim(newExpression);
38
+ let isEmpty = this._isEmpty(newExpression);
39
+ if (isEmpty) {
40
+ return existingExpression;
41
+ }
42
+ let existingNeedsParens =
43
+ !existingExpression.startsWith("(") &&
44
+ !existingExpression.endsWith(")");
45
+ if (existingNeedsParens) {
46
+ existingExpression = `(${existingExpression})`;
47
+ }
48
+ expression = `${existingExpression} AND ${newExpression}`;
49
+ } else {
50
+ expression = this._trim(newExpression);
51
+ }
52
+ this.expression = expression;
53
+ }
45
54
 
46
- // applies operations without verifying them against known attributes. Used internally for key conditions.
47
- unsafeSet(operation, name, ...values) {
48
- const {template} = FilterOperations[operation] || {};
49
- if (template === undefined) {
50
- throw new Error(`Invalid operation: "${operation}". Please report this issue via a bug ticket.`);
51
- }
52
- const names = this.setName({}, name, name);
53
- const valueExpressions = values.map(value => this.setValue(name, value));
54
- const condition = template({}, names.expression, names.prop, ...valueExpressions);
55
- this.add(condition);
56
- }
55
+ // applies operations without verifying them against known attributes. Used internally for key conditions.
56
+ unsafeSet(operation, name, ...values) {
57
+ const { template } = FilterOperations[operation] || {};
58
+ if (template === undefined) {
59
+ throw new Error(
60
+ `Invalid operation: "${operation}". Please report this issue via a bug ticket.`,
61
+ );
62
+ }
63
+ const names = this.setName({}, name, name);
64
+ const valueExpressions = values.map((value) => this.setValue(name, value));
65
+ const condition = template(
66
+ {},
67
+ names.expression,
68
+ names.prop,
69
+ ...valueExpressions,
70
+ );
71
+ this.add(condition);
72
+ }
57
73
 
58
- build() {
59
- return this.expression;
60
- }
74
+ build() {
75
+ return this.expression;
76
+ }
61
77
  }
62
78
 
63
79
  class WhereFactory {
64
80
  constructor(attributes = {}, operations = {}) {
65
- this.attributes = {...attributes};
66
- this.operations = {...operations};
81
+ this.attributes = { ...attributes };
82
+ this.operations = { ...operations };
67
83
  }
68
84
 
69
85
  getExpressionType(methodType) {
70
- switch (methodType) {
71
- case MethodTypes.put:
72
- case MethodTypes.create:
73
- case MethodTypes.update:
74
- case MethodTypes.patch:
75
- case MethodTypes.delete:
76
- case MethodTypes.remove:
77
- case MethodTypes.upsert:
78
- case MethodTypes.get:
79
- case MethodTypes.check:
80
- return ExpressionTypes.ConditionExpression
81
- default:
82
- return ExpressionTypes.FilterExpression
83
- }
86
+ switch (methodType) {
87
+ case MethodTypes.put:
88
+ case MethodTypes.create:
89
+ case MethodTypes.update:
90
+ case MethodTypes.patch:
91
+ case MethodTypes.delete:
92
+ case MethodTypes.remove:
93
+ case MethodTypes.upsert:
94
+ case MethodTypes.get:
95
+ case MethodTypes.check:
96
+ return ExpressionTypes.ConditionExpression;
97
+ default:
98
+ return ExpressionTypes.FilterExpression;
99
+ }
84
100
  }
85
101
 
86
- buildClause(cb) {
87
- if (typeof cb !== "function") {
88
- throw new e.ElectroError(e.ErrorCodes.InvalidWhere, 'Where callback must be of type "function"');
89
- }
90
- return (entity, state, ...params) => {
91
- const type = this.getExpressionType(state.query.method);
92
- const builder = state.query.filter[type];
93
- const proxy = new AttributeOperationProxy({
94
- builder,
95
- attributes: this.attributes,
96
- operations: this.operations
97
- });
98
- const expression = proxy.invokeCallback(cb, ...params);
99
- if (typeof expression !== "string") {
100
- throw new e.ElectroError(e.ErrorCodes.InvalidWhere, "Invalid response from where clause callback. Expected return result to be of type string");
101
- }
102
- builder.add(expression);
103
- return state;
104
- };
105
- }
102
+ buildClause(cb) {
103
+ if (typeof cb !== "function") {
104
+ throw new e.ElectroError(
105
+ e.ErrorCodes.InvalidWhere,
106
+ 'Where callback must be of type "function"',
107
+ );
108
+ }
109
+ return (entity, state, ...params) => {
110
+ const type = this.getExpressionType(state.query.method);
111
+ const builder = state.query.filter[type];
112
+ const proxy = new AttributeOperationProxy({
113
+ builder,
114
+ attributes: this.attributes,
115
+ operations: this.operations,
116
+ });
117
+ const expression = proxy.invokeCallback(cb, ...params);
118
+ if (typeof expression !== "string") {
119
+ throw new e.ElectroError(
120
+ e.ErrorCodes.InvalidWhere,
121
+ "Invalid response from where clause callback. Expected return result to be of type string",
122
+ );
123
+ }
124
+ builder.add(expression);
125
+ return state;
126
+ };
127
+ }
106
128
 
107
129
  injectWhereClauses(clauses = {}, filters = {}) {
108
- let injected = { ...clauses };
109
- let filterParents = Object.entries(injected)
110
- .filter(clause => {
111
- let [name, { children }] = clause;
112
- return children.find(child => ['go', 'commit'].includes(child));
113
- })
114
- .map(([name]) => name);
115
- let modelFilters = Object.keys(filters);
116
- let filterChildren = [];
117
- for (let [name, filter] of Object.entries(filters)) {
118
- filterChildren.push(name);
119
- injected[name] = {
120
- name,
121
- action: this.buildClause(filter),
122
- children: ["params", "go", "commit", "where", ...modelFilters],
123
- };
124
- }
125
- filterChildren.push("where");
126
- injected["where"] = {
127
- name: "where",
128
- action: (entity, state, fn) => {
129
- return this.buildClause(fn)(entity, state);
130
- },
131
- children: ["params", "go", "commit", "where", ...modelFilters],
132
- };
133
- for (let parent of filterParents) {
134
- injected[parent] = { ...injected[parent] };
135
- injected[parent].children = [
136
- ...filterChildren,
137
- ...injected[parent].children,
138
- ];
139
- }
140
- return injected;
141
- }
130
+ let injected = { ...clauses };
131
+ let filterParents = Object.entries(injected)
132
+ .filter((clause) => {
133
+ let [name, { children }] = clause;
134
+ return children.find((child) => ["go", "commit"].includes(child));
135
+ })
136
+ .map(([name]) => name);
137
+ let modelFilters = Object.keys(filters);
138
+ let filterChildren = [];
139
+ for (let [name, filter] of Object.entries(filters)) {
140
+ filterChildren.push(name);
141
+ injected[name] = {
142
+ name,
143
+ action: this.buildClause(filter),
144
+ children: ["params", "go", "commit", "where", ...modelFilters],
145
+ };
146
+ }
147
+ filterChildren.push("where");
148
+ injected["where"] = {
149
+ name: "where",
150
+ action: (entity, state, fn) => {
151
+ return this.buildClause(fn)(entity, state);
152
+ },
153
+ children: ["params", "go", "commit", "where", ...modelFilters],
154
+ };
155
+ for (let parent of filterParents) {
156
+ injected[parent] = { ...injected[parent] };
157
+ injected[parent].children = [
158
+ ...filterChildren,
159
+ ...injected[parent].children,
160
+ ];
161
+ }
162
+ return injected;
163
+ }
142
164
  }
143
165
 
144
166
  module.exports = {
145
- WhereFactory,
146
- FilterExpression
167
+ WhereFactory,
168
+ FilterExpression,
147
169
  };
package/tsconfig.json CHANGED
@@ -4,17 +4,18 @@
4
4
 
5
5
  /* Basic Options */
6
6
  // "incremental": true, /* Enable incremental compilation */
7
- "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8
- "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9
- // "lib": [], /* Specify library files to be included in the compilation. */
7
+ "target": "ESNext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
8
+ "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
9
+ "lib": ["ESNext"], /* Specify library files to be included in the compilation. */
10
+ "resolveJsonModule": true,
10
11
  // "allowJs": true, /* Allow javascript files to be compiled. */
11
12
  // "checkJs": true, /* Report errors in .js files. */
12
13
  // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
13
14
  // "declaration": true, /* Generates corresponding '.d.ts' file. */
14
15
  // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15
- "sourceMap": true, /* Generates corresponding '.map' file. */
16
+ "sourceMap": true /* Generates corresponding '.map' file. */,
16
17
  // "outFile": "./", /* Concatenate and emit output to single file. */
17
- "outDir": "./dist", /* Redirect output structure to the directory. */
18
+ "outDir": "./dist" /* Redirect output structure to the directory. */,
18
19
  // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19
20
  // "composite": true, /* Enable project compilation */
20
21
  // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
@@ -25,8 +26,8 @@
25
26
  // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26
27
 
27
28
  /* Strict Type-Checking Options */
28
- "strict": true, /* Enable all strict type-checking options. */
29
- "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
29
+ "strict": true /* Enable all strict type-checking options. */,
30
+ "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
30
31
  // "strictNullChecks": true, /* Enable strict null checks. */
31
32
  // "strictFunctionTypes": true, /* Enable strict checking of function types. */
32
33
  // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
@@ -43,14 +44,14 @@
43
44
  // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
44
45
 
45
46
  /* Module Resolution Options */
46
- // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
47
+ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
47
48
  // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
48
49
  // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
49
50
  // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
50
51
  // "typeRoots": [], /* List of folders to include type definitions from. */
51
52
  // "types": [], /* Type declaration files to be included in compilation. */
52
53
  // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
53
- "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
54
+ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
54
55
  // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
55
56
  // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
56
57
 
@@ -65,7 +66,7 @@
65
66
  // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
66
67
 
67
68
  /* Advanced Options */
68
- "skipLibCheck": true, /* Skip type checking of declaration files. */
69
- "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
69
+ "skipLibCheck": true /* Skip type checking of declaration files. */,
70
+ "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
70
71
  }
71
72
  }