@naturalcycles/backend-lib 9.46.2 → 9.47.0

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,22 +1,22 @@
1
1
  import { DBQuery } from '@naturalcycles/db-lib';
2
2
  import { InMemoryDB } from '@naturalcycles/db-lib/inmemory';
3
3
  import { commonDBOptionsSchema, commonDBSaveOptionsSchema, dbQuerySchema, } from '@naturalcycles/db-lib/validation';
4
- import { anyObjectSchema, arraySchema, objectSchema, stringSchema, } from '@naturalcycles/nodejs-lib/joi';
4
+ import { j } from '@naturalcycles/nodejs-lib/ajv';
5
5
  import { getDefaultRouter } from '../express/getDefaultRouter.js';
6
- import { validateRequest } from '../validation/joi/joiValidateRequest.js';
7
- const getByIdsInputSchema = objectSchema({
8
- table: stringSchema,
9
- ids: arraySchema(stringSchema),
10
- opt: commonDBOptionsSchema.optional(),
6
+ import { ajvValidateRequest } from '../validation/ajv/ajvValidateRequest.js';
7
+ const getByIdsInputSchema = j.object({
8
+ table: j.string(),
9
+ ids: j.array(j.string()),
10
+ opt: commonDBOptionsSchema().optional(),
11
11
  });
12
- const runQueryInputSchema = objectSchema({
13
- query: dbQuerySchema,
14
- opt: commonDBOptionsSchema.optional(),
12
+ const runQueryInputSchema = j.object({
13
+ query: dbQuerySchema(),
14
+ opt: commonDBOptionsSchema().optional(),
15
15
  });
16
- const saveBatchInputSchema = objectSchema({
17
- table: stringSchema,
18
- rows: arraySchema(anyObjectSchema()),
19
- opt: commonDBSaveOptionsSchema.optional(),
16
+ const saveBatchInputSchema = j.object({
17
+ table: j.string(),
18
+ rows: j.array(j.object({ id: j.string() }).allowAdditionalProperties()),
19
+ opt: commonDBSaveOptionsSchema().optional(),
20
20
  });
21
21
  /**
22
22
  * Exposes CommonDB interface from provided CommonDB as HTTP endpoint (Express RequestHandler).
@@ -49,24 +49,24 @@ export function httpDBRequestHandler(db) {
49
49
  // })
50
50
  // getByIds
51
51
  router.put('/getByIds', async (req, res) => {
52
- const { table, ids, opt } = validateRequest.body(req, getByIdsInputSchema);
52
+ const { table, ids, opt } = ajvValidateRequest.body(req, getByIdsInputSchema);
53
53
  res.json(await db.getByIds(table, ids, opt));
54
54
  });
55
55
  // runQuery
56
56
  router.put('/runQuery', async (req, res) => {
57
- const { query, opt } = validateRequest.body(req, runQueryInputSchema);
57
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
58
58
  const q = DBQuery.fromPlainObject(query);
59
59
  res.json(await db.runQuery(q, opt));
60
60
  });
61
61
  // runQueryCount
62
62
  router.put('/runQueryCount', async (req, res) => {
63
- const { query, opt } = validateRequest.body(req, runQueryInputSchema);
63
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
64
64
  const q = DBQuery.fromPlainObject(query);
65
65
  res.json(await db.runQueryCount(q, opt));
66
66
  });
67
67
  // saveBatch
68
68
  router.put('/saveBatch', async (req, res) => {
69
- const { table, rows, opt } = validateRequest.body(req, saveBatchInputSchema);
69
+ const { table, rows, opt } = ajvValidateRequest.body(req, saveBatchInputSchema);
70
70
  await db.saveBatch(table, rows, opt);
71
71
  res.end();
72
72
  });
@@ -77,7 +77,7 @@ export function httpDBRequestHandler(db) {
77
77
  // })
78
78
  // deleteByQuery
79
79
  router.put('/deleteByQuery', async (req, res) => {
80
- const { query, opt } = validateRequest.body(req, runQueryInputSchema);
80
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema);
81
81
  const q = DBQuery.fromPlainObject(query);
82
82
  res.json(await db.deleteByQuery(q, opt));
83
83
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/backend-lib",
3
3
  "type": "module",
4
- "version": "9.46.2",
4
+ "version": "9.47.0",
5
5
  "peerDependencies": {
6
6
  "@sentry/node": "^10"
7
7
  },
@@ -7,15 +7,10 @@ import {
7
7
  dbQuerySchema,
8
8
  } from '@naturalcycles/db-lib/validation'
9
9
  import type { ObjectWithId } from '@naturalcycles/js-lib/types'
10
- import {
11
- anyObjectSchema,
12
- arraySchema,
13
- objectSchema,
14
- stringSchema,
15
- } from '@naturalcycles/nodejs-lib/joi'
10
+ import { j } from '@naturalcycles/nodejs-lib/ajv'
16
11
  import { getDefaultRouter } from '../express/getDefaultRouter.js'
17
12
  import type { BackendRouter } from '../server/server.model.js'
18
- import { validateRequest } from '../validation/joi/joiValidateRequest.js'
13
+ import { ajvValidateRequest } from '../validation/ajv/ajvValidateRequest.js'
19
14
 
20
15
  export interface GetByIdsInput {
21
16
  table: string
@@ -23,10 +18,10 @@ export interface GetByIdsInput {
23
18
  opt?: CommonDBOptions
24
19
  }
25
20
 
26
- const getByIdsInputSchema = objectSchema<GetByIdsInput>({
27
- table: stringSchema,
28
- ids: arraySchema(stringSchema),
29
- opt: commonDBOptionsSchema.optional(),
21
+ const getByIdsInputSchema = j.object<GetByIdsInput>({
22
+ table: j.string(),
23
+ ids: j.array(j.string()),
24
+ opt: commonDBOptionsSchema().optional(),
30
25
  })
31
26
 
32
27
  export interface RunQueryInput {
@@ -34,9 +29,9 @@ export interface RunQueryInput {
34
29
  opt?: CommonDBOptions
35
30
  }
36
31
 
37
- const runQueryInputSchema = objectSchema<RunQueryInput>({
38
- query: dbQuerySchema,
39
- opt: commonDBOptionsSchema.optional(),
32
+ const runQueryInputSchema = j.object<RunQueryInput>({
33
+ query: dbQuerySchema(),
34
+ opt: commonDBOptionsSchema().optional(),
40
35
  })
41
36
 
42
37
  export interface SaveBatchInput {
@@ -45,10 +40,10 @@ export interface SaveBatchInput {
45
40
  opt?: CommonDBSaveOptions<any>
46
41
  }
47
42
 
48
- const saveBatchInputSchema = objectSchema<SaveBatchInput>({
49
- table: stringSchema,
50
- rows: arraySchema(anyObjectSchema()),
51
- opt: commonDBSaveOptionsSchema.optional(),
43
+ const saveBatchInputSchema = j.object<SaveBatchInput>({
44
+ table: j.string(),
45
+ rows: j.array(j.object<ObjectWithId>({ id: j.string() }).allowAdditionalProperties()),
46
+ opt: commonDBSaveOptionsSchema().optional(),
52
47
  })
53
48
 
54
49
  /**
@@ -88,27 +83,27 @@ export function httpDBRequestHandler(db: CommonDB): BackendRouter {
88
83
 
89
84
  // getByIds
90
85
  router.put('/getByIds', async (req, res) => {
91
- const { table, ids, opt } = validateRequest.body(req, getByIdsInputSchema)
86
+ const { table, ids, opt } = ajvValidateRequest.body(req, getByIdsInputSchema)
92
87
  res.json(await db.getByIds(table, ids, opt))
93
88
  })
94
89
 
95
90
  // runQuery
96
91
  router.put('/runQuery', async (req, res) => {
97
- const { query, opt } = validateRequest.body(req, runQueryInputSchema)
92
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
98
93
  const q = DBQuery.fromPlainObject(query)
99
94
  res.json(await db.runQuery(q, opt))
100
95
  })
101
96
 
102
97
  // runQueryCount
103
98
  router.put('/runQueryCount', async (req, res) => {
104
- const { query, opt } = validateRequest.body(req, runQueryInputSchema)
99
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
105
100
  const q = DBQuery.fromPlainObject(query)
106
101
  res.json(await db.runQueryCount(q, opt))
107
102
  })
108
103
 
109
104
  // saveBatch
110
105
  router.put('/saveBatch', async (req, res) => {
111
- const { table, rows, opt } = validateRequest.body(req, saveBatchInputSchema)
106
+ const { table, rows, opt } = ajvValidateRequest.body(req, saveBatchInputSchema)
112
107
  await db.saveBatch(table, rows, opt)
113
108
  res.end()
114
109
  })
@@ -121,7 +116,7 @@ export function httpDBRequestHandler(db: CommonDB): BackendRouter {
121
116
 
122
117
  // deleteByQuery
123
118
  router.put('/deleteByQuery', async (req, res) => {
124
- const { query, opt } = validateRequest.body(req, runQueryInputSchema)
119
+ const { query, opt } = ajvValidateRequest.body(req, runQueryInputSchema)
125
120
  const q = DBQuery.fromPlainObject(query)
126
121
  res.json(await db.deleteByQuery(q, opt))
127
122
  })