MacroMeta adapter for Moleculer DB service.
- auto creating collection
- raw C8QL queries
- save & execute named queries
- subscription to collection changes
npm install moleculer-db-adapter-macrometa --save
"use strict";
const { ServiceBroker } = require("moleculer");
const DbService = require("moleculer-db");
const MacroMetaAdapter = require("moleculer-db-adapter-macrometa");
const broker = new ServiceBroker();
// Create a service for `post` Macrometa collection
broker.createService({
name: "posts",
mixins: [DbService],
adapter: new MacroMetaAdapter({
config: "https://gdn1.macrometa.io",
auth: {
email: "[email protected]",
password: "secretpass"
},
tenant: null, // use default
fabric: null // use default
}),
collection: "posts" // Name of collection
});
broker.start()
// Create a new post
.then(() => broker.call("posts.create", {
title: "My first post",
content: "Lorem ipsum...",
votes: 0
}))
// Get all posts
.then(() => broker.call("posts.find").then(console.log));
// posts.service.js
module.exports = {
name: "posts",
mixins: [DbService],
adapter: new MacroMetaAdapter(),
actions: {
getMaxVotes() {
return this.adapter.rawQuery(`
FOR post IN posts
FILTER post.status == true && post.votes > @minVotes
SORT post.createdAt DESC
LIMIT 3
RETURN post._id
`, { minVotes: 2 }, {});
}
}
}
More info about C8QL: https://dev.macrometa.io/docs/introduction-1
You have direct access for the
this.collection
&this.fabric
instances inside the services.
// posts.service.js
module.exports = {
name: "posts",
mixins: [DbService],
adapter: new MacroMetaAdapter(),
methods: {
onChanges(payload) {
this.logger.info("Collection has been changed", payload);
}
},
async started() {
await this.adapter.subscribeToChanges((err msg) => {
if (err)
return this.logger.error("Subscription error", err);
this.onChanges(msg.payload);
});
},
async stopped() {
await this.adapter.unsubscribeFromChanges();
}
}
await this.adapter.saveQuery(name, query, parameters);
await this.adapter.executeSavedQuery(name,variables);
$ npm test
In development with watching
$ npm run ci
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important.
The project is available under the MIT license.
Copyright (c) 2019 MoleculerJS