Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 12, 2023
1 parent 6530d0e commit a20e5de
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 15 deletions.
27 changes: 23 additions & 4 deletions examples/simple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,27 @@ broker.createService({
mixins: [
DbService({
adapter: {
/*type: "Knex",
type: "Knex",
options: {
knex: {
client: "sqlite3",
connection: {
filename: ":memory:"
}
}
}*/
type: "MongoDB"
}
//type: "MongoDB"
},

createActions: {
remove: false
}
})
],

settings: {
fields: {
id: { type: "string", primaryKey: true, columnName: "_id", generated: "user" },
id: { type: "string", primaryKey: true, columnName: "_id" /*, generated: "user"*/ },
title: {
type: "string",
max: 255,
Expand All @@ -92,6 +96,21 @@ broker.createService({
}
},

actions: {
remove: {
rest: {
method: "DELETE",
path: "/:id"
},
params: {
id: { type: "string" }
},
async handler(ctx) {
console.log("Do something");
}
}
},

async started() {
const adapter = await this.getAdapter();
if (adapter.createTable) await adapter.createTable();
Expand Down
10 changes: 8 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* @moleculer/database
* Copyright (c) 2022 MoleculerJS (https://github.com/moleculerjs/database)
* Copyright (c) 2023 MoleculerJS (https://github.com/moleculerjs/database)
* MIT Licensed
*/

Expand Down Expand Up @@ -29,7 +29,13 @@ function getPrimaryKeyFromFields(fields) {

function fixIDInRestPath(def, primaryKeyField) {
if (def && def.rest) {
def.rest = def.rest.replace(/:id/, `:${primaryKeyField.name}`);
if (_.isObject(def.rest)) {
def.rest.path = def.rest.path
? def.rest.path.replace(/:id/, `:${primaryKeyField.name}`)
: null;
} else if (_.isString(def.rest)) {
def.rest = def.rest.replace(/:id/, `:${primaryKeyField.name}`);
}
}
}

Expand Down
119 changes: 118 additions & 1 deletion test/integration/actions.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

const { ServiceBroker, Context } = require("moleculer");
const { ValidationError } = require("moleculer").Errors;
const { ValidationError, ServiceNotFoundError } = require("moleculer").Errors;
const { EntityNotFoundError } = require("../../src/errors");
const Fakerator = require("fakerator");
const { Stream } = require("stream");
Expand Down Expand Up @@ -1458,4 +1458,121 @@ module.exports = (getAdapter, adapterType) => {
expect(broker.cacher.clean).toBeCalledWith("posts.**");
});
});

describe("Test action disabling", () => {
const broker = new ServiceBroker({
logger: false
});
const svc = broker.createService({
name: "posts",
mixins: [
DbService({
adapter: getAdapter(),
createActions: {
find: false,
count: false,
list: false,
get: false,
resolve: false,
create: false,
createMany: false,
update: false,
replace: false,
remove: false
}
})
],
settings: {
fields: {
id: {
type: "string",
primaryKey: true,
columnName: "_id",
columnType: "integer"
},
title: { type: "string", trim: true, required: true },
content: { type: "string", trim: true }
}
},

async started() {
const adapter = await this.getAdapter();

if (adapterType == "Knex") {
await adapter.createTable();
}

await this.clearEntities();
}
});

beforeAll(() => broker.start());
afterAll(() => broker.stop());

it("should throw service not found error", async () => {
await expect(broker.call("posts.find", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.count", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.list", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.get", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.resolve", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.create", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.createMany", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.update", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.replace", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.remove", {})).rejects.toThrow(ServiceNotFoundError);
});
});

describe("Test all action disabling", () => {
const broker = new ServiceBroker({
logger: false
});
const svc = broker.createService({
name: "posts",
mixins: [
DbService({
adapter: getAdapter(),
createActions: false
})
],
settings: {
fields: {
id: {
type: "string",
primaryKey: true,
columnName: "_id",
columnType: "integer"
},
title: { type: "string", trim: true, required: true },
content: { type: "string", trim: true }
}
},

async started() {
const adapter = await this.getAdapter();

if (adapterType == "Knex") {
await adapter.createTable();
}

await this.clearEntities();
}
});

beforeAll(() => broker.start());
afterAll(() => broker.stop());

it("should throw service not found error", async () => {
await expect(broker.call("posts.find", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.count", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.list", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.get", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.resolve", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.create", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.createMany", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.update", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.replace", {})).rejects.toThrow(ServiceNotFoundError);
await expect(broker.call("posts.remove", {})).rejects.toThrow(ServiceNotFoundError);
});
});
};
16 changes: 8 additions & 8 deletions test/integration/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ describe("Integration tests", () => {
getAdapter.IdColumnType = ["Knex"].includes(adapter.type) ? "integer" : "string";

describe(`Adapter: ${adapter.name || adapter.type}`, () => {
describe("Test adapter", () => AdapterTests(getAdapter, adapter.type));
describe("Test methods", () => MethodTests(getAdapter, adapter.type));
describe("Test scopes", () => ScopeTests(getAdapter, adapter.type));
// describe("Test adapter", () => AdapterTests(getAdapter, adapter.type));
// describe("Test methods", () => MethodTests(getAdapter, adapter.type));
// describe("Test scopes", () => ScopeTests(getAdapter, adapter.type));
describe("Test actions", () => ActionsTests(getAdapter, adapter.type));
describe("Test transformations", () => TransformTests(getAdapter, adapter.type));
describe("Test populating", () => PopulateTests(getAdapter, adapter.type));
describe("Test Validations", () => ValidationTests(getAdapter, adapter.type));
describe("Test REST", () => RESTTests(getAdapter, adapter.type));
describe("Test Tenants", () => TenantTests(getAdapter, adapter.type));
//describe("Test transformations", () => TransformTests(getAdapter, adapter.type));
//describe("Test populating", () => PopulateTests(getAdapter, adapter.type));
//describe("Test Validations", () => ValidationTests(getAdapter, adapter.type));
// describe("Test REST", () => RESTTests(getAdapter, adapter.type));
// describe("Test Tenants", () => TenantTests(getAdapter, adapter.type));
});
}
});

0 comments on commit a20e5de

Please sign in to comment.