Skip to content

Commit 9f50ea4

Browse files
authored
check action enabled in schema fixing codes
1 parent a72349a commit 9f50ea4

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

src/actions.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/*
22
* @moleculer/database
3-
* Copyright (c) 2022 MoleculerJS (https://github.com/moleculerjs/database)
3+
* Copyright (c) 2024 MoleculerJS (https://github.com/moleculerjs/database)
44
* MIT Licensed
55
*/
66

77
"use strict";
88

9+
const { isisActionEnabled } = require("./schema");
10+
911
const PARAMS_FIELDS = [
1012
{ type: "string", optional: true },
1113
{ type: "array", optional: true, items: "string" }
@@ -52,13 +54,6 @@ module.exports = function (mixinOpts) {
5254
return null;
5355
};
5456

55-
const actionEnabled = name => {
56-
if (typeof mixinOpts.createActions == "object") {
57-
return mixinOpts.createActions[name] !== false;
58-
}
59-
return mixinOpts.createActions !== false;
60-
};
61-
6257
/**
6358
* Find entities by query.
6459
*
@@ -77,7 +72,7 @@ module.exports = function (mixinOpts) {
7772
*
7873
* @returns {Array<Object>} List of found entities.
7974
*/
80-
if (actionEnabled("find")) {
75+
if (isActionEnabled(mixinOpts, "find")) {
8176
res.find = {
8277
visibility: mixinOpts.actionVisibility,
8378
rest: mixinOpts.rest ? "GET /all" : null,
@@ -131,7 +126,7 @@ module.exports = function (mixinOpts) {
131126
*
132127
* @returns {Number} Count of found entities.
133128
*/
134-
if (actionEnabled("count")) {
129+
if (isActionEnabled(mixinOpts, "count")) {
135130
res.count = {
136131
visibility: mixinOpts.actionVisibility,
137132
rest: mixinOpts.rest ? "GET /count" : null,
@@ -166,7 +161,7 @@ module.exports = function (mixinOpts) {
166161
*
167162
* @returns {Object} List of found entities and total count.
168163
*/
169-
if (actionEnabled("list")) {
164+
if (isActionEnabled(mixinOpts, "list")) {
170165
res.list = {
171166
visibility: mixinOpts.actionVisibility,
172167
rest: mixinOpts.rest ? "GET /" : null,
@@ -237,7 +232,7 @@ module.exports = function (mixinOpts) {
237232
*
238233
* @throws {EntityNotFoundError} - 404 Entity not found
239234
*/
240-
if (actionEnabled("get")) {
235+
if (isActionEnabled(mixinOpts, "get")) {
241236
res.get = {
242237
visibility: mixinOpts.actionVisibility,
243238
rest: mixinOpts.rest ? "GET /:id" : null,
@@ -270,7 +265,7 @@ module.exports = function (mixinOpts) {
270265
*
271266
* @throws {EntityNotFoundError} - 404 Entity not found
272267
*/
273-
if (actionEnabled("resolve")) {
268+
if (isActionEnabled(mixinOpts, "resolve")) {
274269
res.resolve = {
275270
visibility: mixinOpts.actionVisibility,
276271
cache: generateCacheOptions([
@@ -307,7 +302,7 @@ module.exports = function (mixinOpts) {
307302
*
308303
* @returns {Object} Saved entity.
309304
*/
310-
if (actionEnabled("create")) {
305+
if (isActionEnabled(mixinOpts, "create")) {
311306
res.create = {
312307
visibility: mixinOpts.actionVisibility,
313308
rest: mixinOpts.rest ? "POST /" : null,
@@ -325,7 +320,7 @@ module.exports = function (mixinOpts) {
325320
*
326321
* @returns {Array<Object>} Saved entities.
327322
*/
328-
if (actionEnabled("createMany")) {
323+
if (isActionEnabled(mixinOpts, "createMany")) {
329324
res.createMany = {
330325
visibility: mixinOpts.actionVisibility,
331326
rest: null,
@@ -345,7 +340,7 @@ module.exports = function (mixinOpts) {
345340
*
346341
* @throws {EntityNotFoundError} - 404 Entity not found
347342
*/
348-
if (actionEnabled("update")) {
343+
if (isActionEnabled(mixinOpts, "update")) {
349344
res.update = {
350345
visibility: mixinOpts.actionVisibility,
351346
rest: mixinOpts.rest ? "PATCH /:id" : null,
@@ -365,7 +360,7 @@ module.exports = function (mixinOpts) {
365360
*
366361
* @throws {EntityNotFoundError} - 404 Entity not found
367362
*/
368-
if (actionEnabled("replace")) {
363+
if (isActionEnabled(mixinOpts, "replace")) {
369364
res.replace = {
370365
visibility: mixinOpts.actionVisibility,
371366
rest: mixinOpts.rest ? "PUT /:id" : null,
@@ -386,7 +381,7 @@ module.exports = function (mixinOpts) {
386381
*
387382
* @throws {EntityNotFoundError} - 404 Entity not found
388383
*/
389-
if (actionEnabled("remove")) {
384+
if (isActionEnabled(mixinOpts, "remove")) {
390385
res.remove = {
391386
visibility: mixinOpts.actionVisibility,
392387
rest: mixinOpts.rest ? "DELETE /:id" : null,

src/index.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
/*
33
* @moleculer/database
4-
* Copyright (c) 2022 MoleculerJS (https://github.com/moleculerjs/database)
4+
* Copyright (c) 2024 MoleculerJS (https://github.com/moleculerjs/database)
55
* MIT Licensed
66
*/
77

@@ -18,7 +18,8 @@ const {
1818
generateValidatorSchemaFromFields,
1919
getPrimaryKeyFromFields,
2020
fixIDInRestPath,
21-
fixIDInCacheKeys
21+
fixIDInCacheKeys,
22+
isActionEnabled
2223
} = require("./schema");
2324
const pkg = require("../package.json");
2425

@@ -190,7 +191,7 @@ module.exports = function DatabaseMixin(mixinOpts) {
190191
if (mixinOpts.generateActionParams) {
191192
// Generate action params
192193
if (Object.keys(fields).length > 0) {
193-
if (schema.actions.create) {
194+
if (isActionEnabled(mixinOpts, "create") && schema.actions.create) {
194195
schema.actions.create.params = generateValidatorSchemaFromFields(
195196
fields,
196197
{
@@ -201,7 +202,7 @@ module.exports = function DatabaseMixin(mixinOpts) {
201202
);
202203
}
203204

204-
if (schema.actions.createMany) {
205+
if (isActionEnabled(mixinOpts, "createMany") && schema.actions.createMany) {
205206
schema.actions.createMany.params = {
206207
// TODO!
207208
$$root: true,
@@ -220,7 +221,7 @@ module.exports = function DatabaseMixin(mixinOpts) {
220221
};
221222
}
222223

223-
if (schema.actions.update) {
224+
if (isActionEnabled(mixinOpts, "update") && schema.actions.update) {
224225
schema.actions.update.params = generateValidatorSchemaFromFields(
225226
fields,
226227
{
@@ -231,7 +232,7 @@ module.exports = function DatabaseMixin(mixinOpts) {
231232
);
232233
}
233234

234-
if (schema.actions.replace) {
235+
if (isActionEnabled(mixinOpts, "replace") && schema.actions.replace) {
235236
schema.actions.replace.params = generateValidatorSchemaFromFields(
236237
fields,
237238
{
@@ -246,34 +247,46 @@ module.exports = function DatabaseMixin(mixinOpts) {
246247

247248
if (primaryKeyField) {
248249
// Set `id` field name & type in `get`, `resolve` and `remove` actions
249-
if (schema.actions.get && schema.actions.get.params) {
250+
if (isActionEnabled(mixinOpts, "get") && schema.actions.get && schema.actions.get.params) {
250251
schema.actions.get.params[primaryKeyField.name] = {
251252
type: primaryKeyField.type,
252253
convert: true
253254
};
254255
}
255-
if (schema.actions.resolve && schema.actions.resolve.params) {
256+
if (isActionEnabled(mixinOpts, "resolve") && schema.actions.resolve && schema.actions.resolve.params) {
256257
schema.actions.resolve.params[primaryKeyField.name] = [
257258
{ type: "array", items: { type: primaryKeyField.type, convert: true } },
258259
{ type: primaryKeyField.type, convert: true }
259260
];
260261
}
261-
if (schema.actions.remove && schema.actions.remove.params) {
262+
if (isActionEnabled(mixinOpts, "remove") && schema.actions.remove && schema.actions.remove.params) {
262263
schema.actions.remove.params[primaryKeyField.name] = {
263264
type: primaryKeyField.type,
264265
convert: true
265266
};
266267
}
267268

268269
// Fix the ":id" variable name in the actions
269-
fixIDInRestPath(schema.actions.get, primaryKeyField);
270-
fixIDInRestPath(schema.actions.update, primaryKeyField);
271-
fixIDInRestPath(schema.actions.replace, primaryKeyField);
272-
fixIDInRestPath(schema.actions.remove, primaryKeyField);
270+
if (isActionEnabled(mixinOpts, "create")) {
271+
fixIDInRestPath(schema.actions.get, primaryKeyField);
272+
}
273+
if (isActionEnabled(mixinOpts, "update")) {
274+
fixIDInRestPath(schema.actions.update, primaryKeyField);
275+
}
276+
if (isActionEnabled(mixinOpts, "replace")) {
277+
fixIDInRestPath(schema.actions.replace, primaryKeyField);
278+
}
279+
if (isActionEnabled(mixinOpts, "remove")) {
280+
fixIDInRestPath(schema.actions.remove, primaryKeyField);
281+
}
273282

274283
// Fix the "id" key name in the cache keys
275-
fixIDInCacheKeys(schema.actions.get, primaryKeyField);
276-
fixIDInCacheKeys(schema.actions.resolve, primaryKeyField);
284+
if (isActionEnabled(mixinOpts, "get")) {
285+
fixIDInCacheKeys(schema.actions.get, primaryKeyField);
286+
}
287+
if (isActionEnabled(mixinOpts, "resolve")) {
288+
fixIDInCacheKeys(schema.actions.resolve, primaryKeyField);
289+
}
277290
}
278291
}
279292

src/schema.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @moleculer/database
3-
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/database)
3+
* Copyright (c) 2024 MoleculerJS (https://github.com/moleculerjs/database)
44
* MIT Licensed
55
*/
66

@@ -45,6 +45,13 @@ function fixIDInCacheKeys(def, primaryKeyField) {
4545
}
4646
}
4747

48+
function actionEnabled(mixinOpts, actionName) {
49+
if (typeof mixinOpts.createActions == "object") {
50+
return mixinOpts.createActions[actionName] !== false;
51+
}
52+
return mixinOpts.createActions !== false;
53+
};
54+
4855
function generateValidatorSchemaFromFields(fields, opts) {
4956
const res = {};
5057

@@ -141,5 +148,6 @@ module.exports = {
141148
fixIDInRestPath,
142149
fixIDInCacheKeys,
143150
generateValidatorSchemaFromFields,
144-
generateFieldValidatorSchema
151+
generateFieldValidatorSchema,
152+
actionEnabled
145153
};

0 commit comments

Comments
 (0)