codehooks-js 1.2.15 → 1.2.17
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 +71 -3
- package/webserver.mjs +3 -3
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
|
@@ -225,6 +225,7 @@ export type NoSQLAPI = {
|
|
|
225
225
|
updateOne: (
|
|
226
226
|
query: string | object,
|
|
227
227
|
document: object,
|
|
228
|
+
updateoperators?: object,
|
|
228
229
|
options?: object
|
|
229
230
|
) => Promise<object>;
|
|
230
231
|
/**
|
|
@@ -262,6 +263,34 @@ export type NoSQLAPI = {
|
|
|
262
263
|
* - Remove multiple data objects in a collection in the current Datastore
|
|
263
264
|
*/
|
|
264
265
|
removeMany: (query: object) => Promise<object>;
|
|
266
|
+
/**
|
|
267
|
+
* Validate database colletion data agains JSON-Schema
|
|
268
|
+
* @param schema JSON-schema
|
|
269
|
+
* @returns Promise with result
|
|
270
|
+
*/
|
|
271
|
+
createSchema: (schema: object) => Promise<object>;
|
|
272
|
+
/**
|
|
273
|
+
* Validate database colletion data agains JSON-Schema
|
|
274
|
+
* @param schema JSON-schema
|
|
275
|
+
* @returns Promise with result
|
|
276
|
+
*/
|
|
277
|
+
setSchema: (schema: object) => Promise<object>;
|
|
278
|
+
/**
|
|
279
|
+
* Remove JSON-schema for database collection
|
|
280
|
+
* @param collection string
|
|
281
|
+
* @returns Promise with result
|
|
282
|
+
*/
|
|
283
|
+
removeSchema: () => Promise<object>;
|
|
284
|
+
/**
|
|
285
|
+
* Get JSON-schema for database collection
|
|
286
|
+
* @returns Promise with result
|
|
287
|
+
*/
|
|
288
|
+
getSchema: () => Promise<object>;
|
|
289
|
+
/**
|
|
290
|
+
* Count database collection items
|
|
291
|
+
* @returns Promise result
|
|
292
|
+
*/
|
|
293
|
+
count: () => Promise<object>;
|
|
265
294
|
};
|
|
266
295
|
/**
|
|
267
296
|
* Database API
|
|
@@ -300,6 +329,7 @@ export type DatastoreAPI = {
|
|
|
300
329
|
updateOne: (
|
|
301
330
|
collection: string,
|
|
302
331
|
query: string | object,
|
|
332
|
+
updateoperators?: object,
|
|
303
333
|
options?: object
|
|
304
334
|
) => Promise<any>;
|
|
305
335
|
/**
|
|
@@ -310,7 +340,7 @@ export type DatastoreAPI = {
|
|
|
310
340
|
collection: string,
|
|
311
341
|
query: object,
|
|
312
342
|
document: object,
|
|
313
|
-
|
|
343
|
+
updateoperators?: object
|
|
314
344
|
) => Promise<any>;
|
|
315
345
|
/**
|
|
316
346
|
* - Replace one data object by ID in a datastore collection.
|
|
@@ -400,6 +430,37 @@ export type DatastoreAPI = {
|
|
|
400
430
|
topic: string,
|
|
401
431
|
options?: object
|
|
402
432
|
) => Promise<object>;
|
|
433
|
+
/**
|
|
434
|
+
* Validate database colletion data agains JSON-Schema
|
|
435
|
+
* @param collection string
|
|
436
|
+
* @param schema JSON-schema
|
|
437
|
+
* @returns Promise with result
|
|
438
|
+
*/
|
|
439
|
+
createSchema: (collection: string, schema: object) => Promise<object>;
|
|
440
|
+
/**
|
|
441
|
+
* Validate database colletion data agains JSON-Schema
|
|
442
|
+
* @param collection string
|
|
443
|
+
* @param schema JSON-schema
|
|
444
|
+
* @returns Promise with result
|
|
445
|
+
*/
|
|
446
|
+
setSchema: (collection: string, schema: object) => Promise<object>;
|
|
447
|
+
/**
|
|
448
|
+
* Remove JSON-schema for database collection
|
|
449
|
+
* @param collection string
|
|
450
|
+
* @returns Promise with result
|
|
451
|
+
*/
|
|
452
|
+
removeSchema: (collection: string) => Promise<object>;
|
|
453
|
+
/**
|
|
454
|
+
* Get JSON-schema for database collection
|
|
455
|
+
* @param collection string
|
|
456
|
+
* @returns Promise with result
|
|
457
|
+
*/
|
|
458
|
+
getSchema: (collection: string) => Promise<object>;
|
|
459
|
+
/**
|
|
460
|
+
* Count database collection items
|
|
461
|
+
* @returns Promise result
|
|
462
|
+
*/
|
|
463
|
+
count: (collection: string) => Promise<object>;
|
|
403
464
|
};
|
|
404
465
|
/**
|
|
405
466
|
* Persistent NoSql and Kev-Value datastore
|
|
@@ -811,7 +872,7 @@ declare class Codehooks {
|
|
|
811
872
|
) => void;
|
|
812
873
|
/**
|
|
813
874
|
* Add application worker function
|
|
814
|
-
* @param {
|
|
875
|
+
* @param {object} name a unique worker name or JSON configuration
|
|
815
876
|
* @param {...function(httpRequest, httpResponse, function(string):void)} hook - callback function(s) with parameters (req, res, [next])
|
|
816
877
|
* @example
|
|
817
878
|
* app.worker('myworker', (data, job) => {
|
|
@@ -820,9 +881,16 @@ declare class Codehooks {
|
|
|
820
881
|
* // do stuff with payload data
|
|
821
882
|
* job.end()
|
|
822
883
|
*})
|
|
884
|
+
* @example
|
|
885
|
+
* app.worker({name: 'myworker', workers: 5}, (data, job) => {
|
|
886
|
+
* const {body:{payload}} = data
|
|
887
|
+
* //console.debug('worker payload data', payload)
|
|
888
|
+
* // do stuff with payload data
|
|
889
|
+
* job.end()
|
|
890
|
+
*})
|
|
823
891
|
*/
|
|
824
892
|
worker: (
|
|
825
|
-
name: string,
|
|
893
|
+
name: string | object,
|
|
826
894
|
...hook: ((
|
|
827
895
|
request: httpRequest,
|
|
828
896
|
response: httpResponse,
|
package/webserver.mjs
CHANGED
|
@@ -86,9 +86,9 @@ export function serveStatic(options, app, filestore, hook, fsoptions) {
|
|
|
86
86
|
})
|
|
87
87
|
|
|
88
88
|
} catch (error) {
|
|
89
|
-
console.
|
|
90
|
-
console.
|
|
91
|
-
res.status(404).end(
|
|
89
|
+
console.debug("Error message", error.message)
|
|
90
|
+
console.debug(error)
|
|
91
|
+
res.status(404).end(`No file here: ${filePath} (${error.message})`)
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
const farr = [readFunc]
|