Skip to content

Commit

Permalink
check action enabled in schema fixing codes
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob authored Dec 18, 2024
1 parent a72349a commit 9f50ea4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
31 changes: 13 additions & 18 deletions src/actions.js
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down Expand Up @@ -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.
*
Expand All @@ -77,7 +72,7 @@ module.exports = function (mixinOpts) {
*
* @returns {Array<Object>} List of found entities.
*/
if (actionEnabled("find")) {
if (isActionEnabled(mixinOpts, "find")) {
res.find = {
visibility: mixinOpts.actionVisibility,
rest: mixinOpts.rest ? "GET /all" : null,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -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,
Expand All @@ -325,7 +320,7 @@ module.exports = function (mixinOpts) {
*
* @returns {Array<Object>} Saved entities.
*/
if (actionEnabled("createMany")) {
if (isActionEnabled(mixinOpts, "createMany")) {
res.createMany = {
visibility: mixinOpts.actionVisibility,
rest: null,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
43 changes: 28 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
*/

Expand All @@ -18,7 +18,8 @@ const {
generateValidatorSchemaFromFields,
getPrimaryKeyFromFields,
fixIDInRestPath,
fixIDInCacheKeys
fixIDInCacheKeys,
isActionEnabled
} = require("./schema");
const pkg = require("../package.json");

Expand Down Expand Up @@ -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,
{
Expand All @@ -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,
Expand All @@ -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,
{
Expand All @@ -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,
{
Expand All @@ -246,34 +247,46 @@ 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
};
}

// 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);
}
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -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
*/

Expand Down Expand Up @@ -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 = {};

Expand Down Expand Up @@ -141,5 +148,6 @@ module.exports = {
fixIDInRestPath,
fixIDInCacheKeys,
generateValidatorSchemaFromFields,
generateFieldValidatorSchema
generateFieldValidatorSchema,
actionEnabled
};

0 comments on commit 9f50ea4

Please sign in to comment.