codehooks-js 1.2.15 → 1.2.16
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/crudlify/index.mjs +14 -10
- package/crudlify/lib/schema/yup/index.mjs +3 -3
- package/index.js +1 -1
- package/package.json +1 -1
- package/types/index.d.ts +68 -2
package/crudlify/index.mjs
CHANGED
|
@@ -52,15 +52,19 @@ export default async function crudlify(app, schema = {}, options = { schema: "yu
|
|
|
52
52
|
// schema provider other than Yup?
|
|
53
53
|
for (const property in schema) {
|
|
54
54
|
//console.log(`${property}: ${schema[property].toString()}`);
|
|
55
|
-
|
|
56
|
-
if (schema[property]
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
55
|
+
try {
|
|
56
|
+
if (schema[property] !== undefined) {
|
|
57
|
+
if (schema[property].parse) {
|
|
58
|
+
_opt.schema = 'zod';
|
|
59
|
+
} else if (schema[property].properties) {
|
|
60
|
+
_opt.schema = 'json-schema';
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
console.error(`${property} is undefined`)
|
|
64
|
+
//schema[property] = null;
|
|
60
65
|
}
|
|
61
|
-
}
|
|
62
|
-
console.error(
|
|
63
|
-
//schema[property] = null;
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error("Unknown schema validator", error.message)
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
70
|
|
|
@@ -137,7 +141,7 @@ async function createFunc(req, res) {
|
|
|
137
141
|
}
|
|
138
142
|
}
|
|
139
143
|
} else {
|
|
140
|
-
if (Object.keys(_schema).length === 0) {
|
|
144
|
+
if (_schema[collection] === null || Object.keys(_schema).length === 0) {
|
|
141
145
|
console.debug("data", collection, document)
|
|
142
146
|
// insert any collection name no schema definitions, anything goes
|
|
143
147
|
try {
|
|
@@ -150,7 +154,7 @@ async function createFunc(req, res) {
|
|
|
150
154
|
res.status(400).send(ex);
|
|
151
155
|
}
|
|
152
156
|
} else {
|
|
153
|
-
console.error('schema for' ,collection, 'is not
|
|
157
|
+
console.error('schema for' ,collection, 'is not found:', _schema[collection])
|
|
154
158
|
return res.status(400).json({ "error": `Collection schema not found: ${collection}` });
|
|
155
159
|
}
|
|
156
160
|
|
|
@@ -4,9 +4,9 @@ export const validate = (schema, document) => {
|
|
|
4
4
|
|
|
5
5
|
return new Promise(async (resolve, reject) => {
|
|
6
6
|
debug('Validate Yup', document)
|
|
7
|
-
if (schema
|
|
7
|
+
if (schema === null || (schema && !schema.validate)) {
|
|
8
8
|
debug('Null validator')
|
|
9
|
-
resolve(document)
|
|
9
|
+
return resolve(document)
|
|
10
10
|
}
|
|
11
11
|
try {
|
|
12
12
|
const valid = await schema.validate(document);
|
|
@@ -27,7 +27,7 @@ export const validate = (schema, document) => {
|
|
|
27
27
|
|
|
28
28
|
export const cast = (schema, document) => {
|
|
29
29
|
debug('Cast', document, schema)
|
|
30
|
-
return schema.cast(document)
|
|
30
|
+
return schema.cast ? schema.cast(document) : document;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
export const prepare = (schemas) => {
|
package/index.js
CHANGED
package/package.json
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -262,6 +262,34 @@ export type NoSQLAPI = {
|
|
|
262
262
|
* - Remove multiple data objects in a collection in the current Datastore
|
|
263
263
|
*/
|
|
264
264
|
removeMany: (query: object) => Promise<object>;
|
|
265
|
+
/**
|
|
266
|
+
* Validate database colletion data agains JSON-Schema
|
|
267
|
+
* @param schema JSON-schema
|
|
268
|
+
* @returns Promise with result
|
|
269
|
+
*/
|
|
270
|
+
createSchema: (schema: object) => Promise<object>;
|
|
271
|
+
/**
|
|
272
|
+
* Validate database colletion data agains JSON-Schema
|
|
273
|
+
* @param schema JSON-schema
|
|
274
|
+
* @returns Promise with result
|
|
275
|
+
*/
|
|
276
|
+
setSchema: (schema: object) => Promise<object>;
|
|
277
|
+
/**
|
|
278
|
+
* Remove JSON-schema for database collection
|
|
279
|
+
* @param collection string
|
|
280
|
+
* @returns Promise with result
|
|
281
|
+
*/
|
|
282
|
+
removeSchema: () => Promise<object>;
|
|
283
|
+
/**
|
|
284
|
+
* Get JSON-schema for database collection
|
|
285
|
+
* @returns Promise with result
|
|
286
|
+
*/
|
|
287
|
+
getSchema: () => Promise<object>;
|
|
288
|
+
/**
|
|
289
|
+
* Count database collection items
|
|
290
|
+
* @returns Promise result
|
|
291
|
+
*/
|
|
292
|
+
count: () => Promise<object>;
|
|
265
293
|
};
|
|
266
294
|
/**
|
|
267
295
|
* Database API
|
|
@@ -400,6 +428,37 @@ export type DatastoreAPI = {
|
|
|
400
428
|
topic: string,
|
|
401
429
|
options?: object
|
|
402
430
|
) => Promise<object>;
|
|
431
|
+
/**
|
|
432
|
+
* Validate database colletion data agains JSON-Schema
|
|
433
|
+
* @param collection string
|
|
434
|
+
* @param schema JSON-schema
|
|
435
|
+
* @returns Promise with result
|
|
436
|
+
*/
|
|
437
|
+
createSchema: (collection: string, schema: object) => Promise<object>;
|
|
438
|
+
/**
|
|
439
|
+
* Validate database colletion data agains JSON-Schema
|
|
440
|
+
* @param collection string
|
|
441
|
+
* @param schema JSON-schema
|
|
442
|
+
* @returns Promise with result
|
|
443
|
+
*/
|
|
444
|
+
setSchema: (collection: string, schema: object) => Promise<object>;
|
|
445
|
+
/**
|
|
446
|
+
* Remove JSON-schema for database collection
|
|
447
|
+
* @param collection string
|
|
448
|
+
* @returns Promise with result
|
|
449
|
+
*/
|
|
450
|
+
removeSchema: (collection: string) => Promise<object>;
|
|
451
|
+
/**
|
|
452
|
+
* Get JSON-schema for database collection
|
|
453
|
+
* @param collection string
|
|
454
|
+
* @returns Promise with result
|
|
455
|
+
*/
|
|
456
|
+
getSchema: (collection: string) => Promise<object>;
|
|
457
|
+
/**
|
|
458
|
+
* Count database collection items
|
|
459
|
+
* @returns Promise result
|
|
460
|
+
*/
|
|
461
|
+
count: (collection: string) => Promise<object>;
|
|
403
462
|
};
|
|
404
463
|
/**
|
|
405
464
|
* Persistent NoSql and Kev-Value datastore
|
|
@@ -811,7 +870,7 @@ declare class Codehooks {
|
|
|
811
870
|
) => void;
|
|
812
871
|
/**
|
|
813
872
|
* Add application worker function
|
|
814
|
-
* @param {
|
|
873
|
+
* @param {object} name a unique worker name or JSON configuration
|
|
815
874
|
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
816
875
|
* @example
|
|
817
876
|
* app.worker('myworker', (data, job) => {
|
|
@@ -820,9 +879,16 @@ declare class Codehooks {
|
|
|
820
879
|
* // do stuff with payload data
|
|
821
880
|
* job.end()
|
|
822
881
|
*})
|
|
882
|
+
* @example
|
|
883
|
+
* app.worker({name: 'myworker', workers: 5}, (data, job) => {
|
|
884
|
+
* const {body:{payload}} = data
|
|
885
|
+
* //console.debug('worker payload data', payload)
|
|
886
|
+
* // do stuff with payload data
|
|
887
|
+
* job.end()
|
|
888
|
+
*})
|
|
823
889
|
*/
|
|
824
890
|
worker: (
|
|
825
|
-
name: string,
|
|
891
|
+
name: string | object,
|
|
826
892
|
...hook: ((
|
|
827
893
|
request: httpRequest,
|
|
828
894
|
response: httpResponse,
|