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.
@@ -1,267 +1,267 @@
1
1
  const e = require("./errors");
2
- const {KeyCasing} = require("./types")
2
+ const { KeyCasing } = require("./types");
3
3
 
4
4
  const Validator = require("jsonschema").Validator;
5
5
  Validator.prototype.customFormats.isFunction = function (input) {
6
- return typeof input === "function";
6
+ return typeof input === "function";
7
7
  };
8
8
  Validator.prototype.customFormats.isFunctionOrString = function (input) {
9
- return typeof input === "function" || typeof input === "string";
9
+ return typeof input === "function" || typeof input === "string";
10
10
  };
11
11
  Validator.prototype.customFormats.isFunctionOrRegexp = function (input) {
12
- return typeof input === "function" || input instanceof RegExp;
12
+ return typeof input === "function" || input instanceof RegExp;
13
13
  };
14
14
 
15
15
  let v = new Validator();
16
16
 
17
17
  const Attribute = {
18
- id: "/Attribute",
19
- type: ["object", "string", "array"],
20
- required: ["type"],
21
- properties: {
22
- type: {
23
- // todo: only specific values
24
- type: ["string", "array"],
25
- // enum: ["string", "number", "boolean", "enum"],
26
- },
27
- field: {
28
- type: "string",
29
- },
30
- hidden: {
31
- type: "boolean"
32
- },
33
- watch: {
34
- type: ["array", "string"],
35
- items: {
36
- type: "string",
37
- }
38
- },
39
- label: {
40
- type: "string",
41
- },
42
- readOnly: {
43
- type: "boolean",
44
- },
45
- required: {
46
- type: "boolean",
47
- },
48
- cast: {
49
- type: "string",
50
- enum: ["string", "number"],
51
- },
52
- default: {
53
- type: "any",
54
- },
55
- validate: {
56
- type: "any",
57
- format: "isFunctionOrRegexp",
58
- },
59
- get: {
60
- type: "any",
61
- format: "isFunction",
62
- },
63
- set: {
64
- type: "any",
65
- format: "isFunction",
66
- },
67
- padding: {
68
- type: "object",
69
- required: ['length', 'char'],
70
- properties: {
71
- length: {
72
- type: 'number'
73
- },
74
- char: {
75
- type: 'string',
76
- }
77
- }
78
- }
79
- },
18
+ id: "/Attribute",
19
+ type: ["object", "string", "array"],
20
+ required: ["type"],
21
+ properties: {
22
+ type: {
23
+ // todo: only specific values
24
+ type: ["string", "array"],
25
+ // enum: ["string", "number", "boolean", "enum"],
26
+ },
27
+ field: {
28
+ type: "string",
29
+ },
30
+ hidden: {
31
+ type: "boolean",
32
+ },
33
+ watch: {
34
+ type: ["array", "string"],
35
+ items: {
36
+ type: "string",
37
+ },
38
+ },
39
+ label: {
40
+ type: "string",
41
+ },
42
+ readOnly: {
43
+ type: "boolean",
44
+ },
45
+ required: {
46
+ type: "boolean",
47
+ },
48
+ cast: {
49
+ type: "string",
50
+ enum: ["string", "number"],
51
+ },
52
+ default: {
53
+ type: "any",
54
+ },
55
+ validate: {
56
+ type: "any",
57
+ format: "isFunctionOrRegexp",
58
+ },
59
+ get: {
60
+ type: "any",
61
+ format: "isFunction",
62
+ },
63
+ set: {
64
+ type: "any",
65
+ format: "isFunction",
66
+ },
67
+ padding: {
68
+ type: "object",
69
+ required: ["length", "char"],
70
+ properties: {
71
+ length: {
72
+ type: "number",
73
+ },
74
+ char: {
75
+ type: "string",
76
+ },
77
+ },
78
+ },
79
+ },
80
80
  };
81
81
 
82
82
  const Index = {
83
- id: "/Index",
84
- type: "object",
85
- properties: {
86
- pk: {
87
- type: "object",
88
- required: true,
89
- properties: {
90
- field: {
91
- type: "string",
92
- required: true,
93
- },
94
- facets: {
95
- type: ["array", "string"],
96
- items: {
97
- type: "string",
98
- },
99
- required: false,
100
- },
101
- composite: {
102
- type: ["array"],
103
- items: {
104
- type: "string",
105
- },
106
- required: false,
107
- },
108
- template: {
109
- type: "string",
110
- required: false,
111
- },
112
- casing: {
113
- type: "string",
114
- enum: ["upper", "lower", "none", "default"],
115
- required: false,
116
- },
117
- cast: {
118
- type: "string",
119
- enum: ["string", "number"],
120
- required: false,
121
- }
122
- },
123
- },
124
- sk: {
125
- type: "object",
126
- required: ["field"],
127
- properties: {
128
- field: {
129
- type: "string",
130
- required: true,
131
- },
132
- facets: {
133
- type: ["array", "string"],
134
- required: false,
135
- items: {
136
- type: "string",
137
- },
138
- },
139
- composite: {
140
- type: ["array"],
141
- required: false,
142
- items: {
143
- type: "string",
144
- },
145
- },
146
- template: {
147
- type: "string",
148
- required: false,
149
- },
150
- casing: {
151
- type: "string",
152
- enum: ["upper", "lower", "none", "default"],
153
- required: false,
154
- },
155
- cast: {
156
- type: "string",
157
- enum: ["string", "number"],
158
- required: false,
159
- }
160
- },
161
- },
162
- index: {
163
- type: "string",
164
- },
165
- collection: {
166
- type: ["array", "string"]
167
- },
168
- type: {
169
- type: 'string',
170
- enum: ['clustered', 'isolated'],
171
- required: false,
172
- }
173
- },
83
+ id: "/Index",
84
+ type: "object",
85
+ properties: {
86
+ pk: {
87
+ type: "object",
88
+ required: true,
89
+ properties: {
90
+ field: {
91
+ type: "string",
92
+ required: true,
93
+ },
94
+ facets: {
95
+ type: ["array", "string"],
96
+ items: {
97
+ type: "string",
98
+ },
99
+ required: false,
100
+ },
101
+ composite: {
102
+ type: ["array"],
103
+ items: {
104
+ type: "string",
105
+ },
106
+ required: false,
107
+ },
108
+ template: {
109
+ type: "string",
110
+ required: false,
111
+ },
112
+ casing: {
113
+ type: "string",
114
+ enum: ["upper", "lower", "none", "default"],
115
+ required: false,
116
+ },
117
+ cast: {
118
+ type: "string",
119
+ enum: ["string", "number"],
120
+ required: false,
121
+ },
122
+ },
123
+ },
124
+ sk: {
125
+ type: "object",
126
+ required: ["field"],
127
+ properties: {
128
+ field: {
129
+ type: "string",
130
+ required: true,
131
+ },
132
+ facets: {
133
+ type: ["array", "string"],
134
+ required: false,
135
+ items: {
136
+ type: "string",
137
+ },
138
+ },
139
+ composite: {
140
+ type: ["array"],
141
+ required: false,
142
+ items: {
143
+ type: "string",
144
+ },
145
+ },
146
+ template: {
147
+ type: "string",
148
+ required: false,
149
+ },
150
+ casing: {
151
+ type: "string",
152
+ enum: ["upper", "lower", "none", "default"],
153
+ required: false,
154
+ },
155
+ cast: {
156
+ type: "string",
157
+ enum: ["string", "number"],
158
+ required: false,
159
+ },
160
+ },
161
+ },
162
+ index: {
163
+ type: "string",
164
+ },
165
+ collection: {
166
+ type: ["array", "string"],
167
+ },
168
+ type: {
169
+ type: "string",
170
+ enum: ["clustered", "isolated"],
171
+ required: false,
172
+ },
173
+ },
174
174
  };
175
175
 
176
- const Modelv1= {
177
- type: "object",
178
- required: true,
179
- properties: {
180
- model: {
181
- type: "object",
182
- required: true,
183
- properties: {
184
- entity: {
185
- type: "string",
186
- required: true
187
- },
188
- version: {
189
- type: "string",
190
- required: true
191
- },
192
- service: {
193
- type: "string",
194
- required: true
195
- }
196
- }
197
- },
198
- table: {
199
- type: "string",
200
- },
201
- attributes: {
202
- type: "object",
203
- patternProperties: {
204
- ["."]: { $ref: "/Attribute" },
205
- },
206
- },
207
- indexes: {
208
- type: "object",
209
- minProperties: 1,
210
- patternProperties: {
211
- ["."]: { $ref: "/Index" },
212
- },
213
- },
214
- filters: { $ref: "/Filters" },
215
- },
216
- required: ["model", "attributes", "indexes"]
176
+ const Modelv1 = {
177
+ type: "object",
178
+ required: true,
179
+ properties: {
180
+ model: {
181
+ type: "object",
182
+ required: true,
183
+ properties: {
184
+ entity: {
185
+ type: "string",
186
+ required: true,
187
+ },
188
+ version: {
189
+ type: "string",
190
+ required: true,
191
+ },
192
+ service: {
193
+ type: "string",
194
+ required: true,
195
+ },
196
+ },
197
+ },
198
+ table: {
199
+ type: "string",
200
+ },
201
+ attributes: {
202
+ type: "object",
203
+ patternProperties: {
204
+ ["."]: { $ref: "/Attribute" },
205
+ },
206
+ },
207
+ indexes: {
208
+ type: "object",
209
+ minProperties: 1,
210
+ patternProperties: {
211
+ ["."]: { $ref: "/Index" },
212
+ },
213
+ },
214
+ filters: { $ref: "/Filters" },
215
+ },
216
+ required: ["model", "attributes", "indexes"],
217
217
  };
218
218
 
219
219
  const ModelBeta = {
220
- type: "object",
221
- required: true,
222
- properties: {
223
- service: {
224
- type: "string",
225
- required: true,
226
- },
227
- entity: {
228
- type: "string",
229
- required: true,
230
- },
231
- table: {
232
- type: "string",
233
- },
234
- version: {
235
- type: "string",
236
- },
237
- attributes: {
238
- type: "object",
239
- patternProperties: {
240
- ["."]: { $ref: "/Attribute" },
241
- },
242
- },
243
- indexes: {
244
- type: "object",
245
- minProperties: 1,
246
- patternProperties: {
247
- ["."]: { $ref: "/Index" },
248
- },
249
- },
250
- filters: { $ref: "/Filters" },
251
- },
252
- required: ["attributes", "indexes"]
220
+ type: "object",
221
+ required: true,
222
+ properties: {
223
+ service: {
224
+ type: "string",
225
+ required: true,
226
+ },
227
+ entity: {
228
+ type: "string",
229
+ required: true,
230
+ },
231
+ table: {
232
+ type: "string",
233
+ },
234
+ version: {
235
+ type: "string",
236
+ },
237
+ attributes: {
238
+ type: "object",
239
+ patternProperties: {
240
+ ["."]: { $ref: "/Attribute" },
241
+ },
242
+ },
243
+ indexes: {
244
+ type: "object",
245
+ minProperties: 1,
246
+ patternProperties: {
247
+ ["."]: { $ref: "/Index" },
248
+ },
249
+ },
250
+ filters: { $ref: "/Filters" },
251
+ },
252
+ required: ["attributes", "indexes"],
253
253
  };
254
254
 
255
255
  const Filters = {
256
- id: "/Filters",
257
- type: "object",
258
- patternProperties: {
259
- ["."]: {
260
- type: "any",
261
- format: "isFunction",
262
- message: "Requires function",
263
- },
264
- },
256
+ id: "/Filters",
257
+ type: "object",
258
+ patternProperties: {
259
+ ["."]: {
260
+ type: "any",
261
+ format: "isFunction",
262
+ message: "Requires function",
263
+ },
264
+ },
265
265
  };
266
266
 
267
267
  v.addSchema(Attribute, "/Attribute");
@@ -271,122 +271,134 @@ v.addSchema(ModelBeta, "/ModelBeta");
271
271
  v.addSchema(Modelv1, "/Modelv1");
272
272
 
273
273
  function validateModel(model = {}) {
274
- /** start beta/v1 condition **/
275
- let betaErrors = v.validate(model, "/ModelBeta").errors;
276
- if (betaErrors.length) {
277
- /** end/v1 condition **/
278
- let errors = v.validate(model, "/Modelv1").errors;
279
- if (errors.length) {
280
- throw new e.ElectroError(e.ErrorCodes.InvalidModel,
281
- errors
282
- .map((err) => {
283
- let message = `${err.property}`;
284
- switch (err.argument) {
285
- case "isFunction":
286
- return `${message} must be a function`;
287
- case "isFunctionOrString":
288
- return `${message} must be either a function or string`;
289
- case "isFunctionOrRegexp":
290
- return `${message} must be either a function or Regexp`;
291
- default:
292
- return `${message} ${err.message}`;
293
- }
294
- })
295
- .join(", "),
296
- );
297
- }
298
- }
274
+ /** start beta/v1 condition **/
275
+ let betaErrors = v.validate(model, "/ModelBeta").errors;
276
+ if (betaErrors.length) {
277
+ /** end/v1 condition **/
278
+ let errors = v.validate(model, "/Modelv1").errors;
279
+ if (errors.length) {
280
+ throw new e.ElectroError(
281
+ e.ErrorCodes.InvalidModel,
282
+ errors
283
+ .map((err) => {
284
+ let message = `${err.property}`;
285
+ switch (err.argument) {
286
+ case "isFunction":
287
+ return `${message} must be a function`;
288
+ case "isFunctionOrString":
289
+ return `${message} must be either a function or string`;
290
+ case "isFunctionOrRegexp":
291
+ return `${message} must be either a function or Regexp`;
292
+ default:
293
+ return `${message} ${err.message}`;
294
+ }
295
+ })
296
+ .join(", "),
297
+ );
298
+ }
299
+ }
299
300
  }
300
301
 
301
302
  function testModel(model) {
302
- let isModel = false;
303
- let error = "";
304
- try {
305
- validateModel(model);
306
- isModel = true;
307
- } catch(err) {
308
- error = err.message;
309
- }
310
- return [isModel, error];
303
+ let isModel = false;
304
+ let error = "";
305
+ try {
306
+ validateModel(model);
307
+ isModel = true;
308
+ } catch (err) {
309
+ error = err.message;
310
+ }
311
+ return [isModel, error];
311
312
  }
312
313
 
313
314
  function isStringHasLength(str) {
314
- return typeof str === "string" && str.length > 0;
315
+ return typeof str === "string" && str.length > 0;
315
316
  }
316
317
 
317
318
  function isObjectHasLength(obj) {
318
- return typeof obj === "object" && Object.keys(obj).length > 0;
319
+ return typeof obj === "object" && Object.keys(obj).length > 0;
319
320
  }
320
321
 
321
322
  function isArrayHasLength(arr) {
322
- return Array.isArray(arr) && arr.length > 0;
323
+ return Array.isArray(arr) && arr.length > 0;
323
324
  }
324
325
 
325
326
  function isNameEntityRecordType(entityRecord) {
326
- return isObjectHasLength(entityRecord) && Object.values(entityRecord).find(value => {
327
- return value._instance !== undefined;
328
- })
327
+ return (
328
+ isObjectHasLength(entityRecord) &&
329
+ Object.values(entityRecord).find((value) => {
330
+ return value._instance !== undefined;
331
+ })
332
+ );
329
333
  }
330
334
 
331
335
  function isNameModelRecordType(modelRecord) {
332
- return isObjectHasLength(modelRecord) && Object.values(modelRecord).find(value => {
333
- return value.model
334
- && isStringHasLength(value.model.entity)
335
- && isStringHasLength(value.model.version)
336
- && isStringHasLength(value.model.service)
337
- });
336
+ return (
337
+ isObjectHasLength(modelRecord) &&
338
+ Object.values(modelRecord).find((value) => {
339
+ return (
340
+ value.model &&
341
+ isStringHasLength(value.model.entity) &&
342
+ isStringHasLength(value.model.version) &&
343
+ isStringHasLength(value.model.service)
344
+ );
345
+ })
346
+ );
338
347
  }
339
348
 
340
349
  function isBetaServiceConfig(serviceConfig) {
341
- return isObjectHasLength(serviceConfig)
342
- && (isStringHasLength(serviceConfig.service) || isStringHasLength(serviceConfig.name))
343
- && isStringHasLength(serviceConfig.version)
350
+ return (
351
+ isObjectHasLength(serviceConfig) &&
352
+ (isStringHasLength(serviceConfig.service) ||
353
+ isStringHasLength(serviceConfig.name)) &&
354
+ isStringHasLength(serviceConfig.version)
355
+ );
344
356
  }
345
357
 
346
358
  function isFunction(value) {
347
- return typeof value === "function";
359
+ return typeof value === "function";
348
360
  }
349
361
 
350
362
  function stringArrayMatch(arr1, arr2) {
351
- let areArrays = Array.isArray(arr1) && Array.isArray(arr2);
352
- let match = areArrays && arr1.length === arr2.length;
353
- for (let i = 0; i < arr1.length; i++) {
354
- if (!match) {
355
- break;
356
- }
357
- match = isStringHasLength(arr1[i]) && arr1[i] === arr2[i];
358
- }
359
- return match;
363
+ let areArrays = Array.isArray(arr1) && Array.isArray(arr2);
364
+ let match = areArrays && arr1.length === arr2.length;
365
+ for (let i = 0; i < arr1.length; i++) {
366
+ if (!match) {
367
+ break;
368
+ }
369
+ match = isStringHasLength(arr1[i]) && arr1[i] === arr2[i];
370
+ }
371
+ return match;
360
372
  }
361
373
 
362
374
  function isMatchingCasing(casing1, casing2) {
363
- const equivalentCasings = [KeyCasing.default, KeyCasing.lower];
364
- if (isStringHasLength(casing1) && isStringHasLength(casing2)) {
365
- let isRealCase = KeyCasing[casing1.toLowerCase()] !== undefined;
366
- let casingsMatch = casing1 === casing2;
367
- let casingsAreEquivalent = [casing1, casing2].every(casing => {
368
- return casing === KeyCasing.lower || casing === KeyCasing.default;
369
- });
370
- return isRealCase && (casingsMatch || casingsAreEquivalent);
371
- } else if (isStringHasLength(casing1)) {
372
- return equivalentCasings.includes(casing1.toLowerCase());
373
- } else if (isStringHasLength(casing2)) {
374
- return equivalentCasings.includes(casing2.toLowerCase());
375
- } else {
376
- return casing1 === undefined && casing2 === undefined;
377
- }
375
+ const equivalentCasings = [KeyCasing.default, KeyCasing.lower];
376
+ if (isStringHasLength(casing1) && isStringHasLength(casing2)) {
377
+ let isRealCase = KeyCasing[casing1.toLowerCase()] !== undefined;
378
+ let casingsMatch = casing1 === casing2;
379
+ let casingsAreEquivalent = [casing1, casing2].every((casing) => {
380
+ return casing === KeyCasing.lower || casing === KeyCasing.default;
381
+ });
382
+ return isRealCase && (casingsMatch || casingsAreEquivalent);
383
+ } else if (isStringHasLength(casing1)) {
384
+ return equivalentCasings.includes(casing1.toLowerCase());
385
+ } else if (isStringHasLength(casing2)) {
386
+ return equivalentCasings.includes(casing2.toLowerCase());
387
+ } else {
388
+ return casing1 === undefined && casing2 === undefined;
389
+ }
378
390
  }
379
391
 
380
392
  module.exports = {
381
- testModel,
382
- isFunction,
383
- stringArrayMatch,
384
- isMatchingCasing,
385
- isArrayHasLength,
386
- isStringHasLength,
387
- isObjectHasLength,
388
- isBetaServiceConfig,
389
- isNameModelRecordType,
390
- isNameEntityRecordType,
391
- model: validateModel
393
+ testModel,
394
+ isFunction,
395
+ stringArrayMatch,
396
+ isMatchingCasing,
397
+ isArrayHasLength,
398
+ isStringHasLength,
399
+ isObjectHasLength,
400
+ isBetaServiceConfig,
401
+ isNameModelRecordType,
402
+ isNameEntityRecordType,
403
+ model: validateModel,
392
404
  };