Closed yuzifu closed 5 years ago
Nothing extra, just merge both examples:
"use strict";
const { ServiceBroker } = require("moleculer");
const DbService = require("moleculer-db");
const SqlAdapter = require("moleculer-db-adapter-sequelize");
const Sequelize = require("sequelize");
const broker = new ServiceBroker();
// Create a Sequelize service for `post` entities
broker.createService({
name: "posts",
mixins: [DbService],
adapter: new SqlAdapter("sqlite://:memory:"),
model: {
name: "post",
define: {
title: Sequelize.STRING,
content: Sequelize.TEXT,
votes: Sequelize.INTEGER,
author: Sequelize.INTEGER,
status: Sequelize.BOOLEAN
},
options: {
// Options from http://docs.sequelizejs.com/manual/tutorial/models-definition.html
}
},
settings: {
fields: ["_id", "title", "content", "votes"]
},
actions: {
// Increment `votes` field by post ID
vote(ctx) {
return this.adapter.updateById(ctx.params.id, { $inc: { votes: 1 } });
},
// List posts of an author
byAuthors(ctx) {
return this.find({
query: {
author: ctx.params.authorID
},
limit: ctx.params.limit || 10,
sort: "-createdAt"
});
}
}
});
broker.start()
// Create a new post
.then(() => broker.call("posts.vote", {
id: 1
}))
// Get all posts
.then(() => broker.call("posts.byAuthors", { authorID: 1 }).then(console.log));
By the way, this is not the correct repo for this issue
@icebob Thanks I found an example here: https://github.com/moleculerjs/moleculer-db/blob/master/packages/moleculer-db-adapter-sequelize/examples/integration/index.js
Examples of extend with custom actions in moleculer manual is:
Examples of Sequelize Adapter in moleculer manual is:
If I want extend with custom actions use sequelize adapter, what should we do?