From 9f50ea4a2403a5d5e8a062c66cbf1aaa9dbed4cd Mon Sep 17 00:00:00 2001 From: Icebob Date: Wed, 18 Dec 2024 09:40:19 +0100 Subject: [PATCH] check action enabled in schema fixing codes --- src/actions.js | 31 +++++++++++++------------------ src/index.js | 43 ++++++++++++++++++++++++++++--------------- src/schema.js | 12 ++++++++++-- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/actions.js b/src/actions.js index 2f32ffc..49970fd 100644 --- a/src/actions.js +++ b/src/actions.js @@ -1,11 +1,13 @@ /* * @moleculer/database - * Copyright (c) 2022 MoleculerJS (https://github.com/moleculerjs/database) + * Copyright (c) 2024 MoleculerJS (https://github.com/moleculerjs/database) * MIT Licensed */ "use strict"; +const { isisActionEnabled } = require("./schema"); + const PARAMS_FIELDS = [ { type: "string", optional: true }, { type: "array", optional: true, items: "string" } @@ -52,13 +54,6 @@ module.exports = function (mixinOpts) { return null; }; - const actionEnabled = name => { - if (typeof mixinOpts.createActions == "object") { - return mixinOpts.createActions[name] !== false; - } - return mixinOpts.createActions !== false; - }; - /** * Find entities by query. * @@ -77,7 +72,7 @@ module.exports = function (mixinOpts) { * * @returns {Array} List of found entities. */ - if (actionEnabled("find")) { + if (isActionEnabled(mixinOpts, "find")) { res.find = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "GET /all" : null, @@ -131,7 +126,7 @@ module.exports = function (mixinOpts) { * * @returns {Number} Count of found entities. */ - if (actionEnabled("count")) { + if (isActionEnabled(mixinOpts, "count")) { res.count = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "GET /count" : null, @@ -166,7 +161,7 @@ module.exports = function (mixinOpts) { * * @returns {Object} List of found entities and total count. */ - if (actionEnabled("list")) { + if (isActionEnabled(mixinOpts, "list")) { res.list = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "GET /" : null, @@ -237,7 +232,7 @@ module.exports = function (mixinOpts) { * * @throws {EntityNotFoundError} - 404 Entity not found */ - if (actionEnabled("get")) { + if (isActionEnabled(mixinOpts, "get")) { res.get = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "GET /:id" : null, @@ -270,7 +265,7 @@ module.exports = function (mixinOpts) { * * @throws {EntityNotFoundError} - 404 Entity not found */ - if (actionEnabled("resolve")) { + if (isActionEnabled(mixinOpts, "resolve")) { res.resolve = { visibility: mixinOpts.actionVisibility, cache: generateCacheOptions([ @@ -307,7 +302,7 @@ module.exports = function (mixinOpts) { * * @returns {Object} Saved entity. */ - if (actionEnabled("create")) { + if (isActionEnabled(mixinOpts, "create")) { res.create = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "POST /" : null, @@ -325,7 +320,7 @@ module.exports = function (mixinOpts) { * * @returns {Array} Saved entities. */ - if (actionEnabled("createMany")) { + if (isActionEnabled(mixinOpts, "createMany")) { res.createMany = { visibility: mixinOpts.actionVisibility, rest: null, @@ -345,7 +340,7 @@ module.exports = function (mixinOpts) { * * @throws {EntityNotFoundError} - 404 Entity not found */ - if (actionEnabled("update")) { + if (isActionEnabled(mixinOpts, "update")) { res.update = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "PATCH /:id" : null, @@ -365,7 +360,7 @@ module.exports = function (mixinOpts) { * * @throws {EntityNotFoundError} - 404 Entity not found */ - if (actionEnabled("replace")) { + if (isActionEnabled(mixinOpts, "replace")) { res.replace = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "PUT /:id" : null, @@ -386,7 +381,7 @@ module.exports = function (mixinOpts) { * * @throws {EntityNotFoundError} - 404 Entity not found */ - if (actionEnabled("remove")) { + if (isActionEnabled(mixinOpts, "remove")) { res.remove = { visibility: mixinOpts.actionVisibility, rest: mixinOpts.rest ? "DELETE /:id" : null, diff --git a/src/index.js b/src/index.js index c640b0a..f25a42e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ /* /* * @moleculer/database - * Copyright (c) 2022 MoleculerJS (https://github.com/moleculerjs/database) + * Copyright (c) 2024 MoleculerJS (https://github.com/moleculerjs/database) * MIT Licensed */ @@ -18,7 +18,8 @@ const { generateValidatorSchemaFromFields, getPrimaryKeyFromFields, fixIDInRestPath, - fixIDInCacheKeys + fixIDInCacheKeys, + isActionEnabled } = require("./schema"); const pkg = require("../package.json"); @@ -190,7 +191,7 @@ module.exports = function DatabaseMixin(mixinOpts) { if (mixinOpts.generateActionParams) { // Generate action params if (Object.keys(fields).length > 0) { - if (schema.actions.create) { + if (isActionEnabled(mixinOpts, "create") && schema.actions.create) { schema.actions.create.params = generateValidatorSchemaFromFields( fields, { @@ -201,7 +202,7 @@ module.exports = function DatabaseMixin(mixinOpts) { ); } - if (schema.actions.createMany) { + if (isActionEnabled(mixinOpts, "createMany") && schema.actions.createMany) { schema.actions.createMany.params = { // TODO! $$root: true, @@ -220,7 +221,7 @@ module.exports = function DatabaseMixin(mixinOpts) { }; } - if (schema.actions.update) { + if (isActionEnabled(mixinOpts, "update") && schema.actions.update) { schema.actions.update.params = generateValidatorSchemaFromFields( fields, { @@ -231,7 +232,7 @@ module.exports = function DatabaseMixin(mixinOpts) { ); } - if (schema.actions.replace) { + if (isActionEnabled(mixinOpts, "replace") && schema.actions.replace) { schema.actions.replace.params = generateValidatorSchemaFromFields( fields, { @@ -246,19 +247,19 @@ module.exports = function DatabaseMixin(mixinOpts) { if (primaryKeyField) { // Set `id` field name & type in `get`, `resolve` and `remove` actions - if (schema.actions.get && schema.actions.get.params) { + if (isActionEnabled(mixinOpts, "get") && schema.actions.get && schema.actions.get.params) { schema.actions.get.params[primaryKeyField.name] = { type: primaryKeyField.type, convert: true }; } - if (schema.actions.resolve && schema.actions.resolve.params) { + if (isActionEnabled(mixinOpts, "resolve") && schema.actions.resolve && schema.actions.resolve.params) { schema.actions.resolve.params[primaryKeyField.name] = [ { type: "array", items: { type: primaryKeyField.type, convert: true } }, { type: primaryKeyField.type, convert: true } ]; } - if (schema.actions.remove && schema.actions.remove.params) { + if (isActionEnabled(mixinOpts, "remove") && schema.actions.remove && schema.actions.remove.params) { schema.actions.remove.params[primaryKeyField.name] = { type: primaryKeyField.type, convert: true @@ -266,14 +267,26 @@ module.exports = function DatabaseMixin(mixinOpts) { } // Fix the ":id" variable name in the actions - fixIDInRestPath(schema.actions.get, primaryKeyField); - fixIDInRestPath(schema.actions.update, primaryKeyField); - fixIDInRestPath(schema.actions.replace, primaryKeyField); - fixIDInRestPath(schema.actions.remove, primaryKeyField); + if (isActionEnabled(mixinOpts, "create")) { + fixIDInRestPath(schema.actions.get, primaryKeyField); + } + if (isActionEnabled(mixinOpts, "update")) { + fixIDInRestPath(schema.actions.update, primaryKeyField); + } + if (isActionEnabled(mixinOpts, "replace")) { + fixIDInRestPath(schema.actions.replace, primaryKeyField); + } + if (isActionEnabled(mixinOpts, "remove")) { + fixIDInRestPath(schema.actions.remove, primaryKeyField); + } // Fix the "id" key name in the cache keys - fixIDInCacheKeys(schema.actions.get, primaryKeyField); - fixIDInCacheKeys(schema.actions.resolve, primaryKeyField); + if (isActionEnabled(mixinOpts, "get")) { + fixIDInCacheKeys(schema.actions.get, primaryKeyField); + } + if (isActionEnabled(mixinOpts, "resolve")) { + fixIDInCacheKeys(schema.actions.resolve, primaryKeyField); + } } } diff --git a/src/schema.js b/src/schema.js index 883c15d..5c42ece 100644 --- a/src/schema.js +++ b/src/schema.js @@ -1,6 +1,6 @@ /* * @moleculer/database - * Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/database) + * Copyright (c) 2024 MoleculerJS (https://github.com/moleculerjs/database) * MIT Licensed */ @@ -45,6 +45,13 @@ function fixIDInCacheKeys(def, primaryKeyField) { } } +function actionEnabled(mixinOpts, actionName) { + if (typeof mixinOpts.createActions == "object") { + return mixinOpts.createActions[actionName] !== false; + } + return mixinOpts.createActions !== false; +}; + function generateValidatorSchemaFromFields(fields, opts) { const res = {}; @@ -141,5 +148,6 @@ module.exports = { fixIDInRestPath, fixIDInCacheKeys, generateValidatorSchemaFromFields, - generateFieldValidatorSchema + generateFieldValidatorSchema, + actionEnabled };