moleculer-db-adapter-macrometa
v0.1.0
Published
MacroMeta.io DB adapter for Moleculer DB service.
Downloads
3
Readme
moleculer-db-adapter-macrometa
Moleculer Data access service for MacroMeta.co.
Features
- auto creating collection
- raw C8QL queries
- save & execute named queries
- subscription to collection changes
Install
npm install moleculer-db-adapter-macrometa --save
Usage
"use strict";
const { ServiceBroker } = require("moleculer");
const DbService = require("moleculer-db");
const MacroMetaAdapter = require("moleculer-db-adapter-macrometa");
const Sequelize = require("sequelize");
const broker = new ServiceBroker();
// Create a Sequelize service for `post` entities
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));
Raw queries
You can reach the sequelize
instance via this.adapter.db
. To call Raw queries:
// posts.service.js
module.exports = {
name: "posts",
adapter: new MacroMetaAdapter(),
actions: {
findHello2() {
return this.adapter.db.query("SELECT * FROM posts WHERE title = 'Hello 2' LIMIT 1")
.then(([res, metadata]) => res);
}
}
}
Subscribe to changes
// posts.service.js
module.exports = {
name: "posts",
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();
}
}
Named queries
await this.adapter.saveQuery(name, query, parameters);
await this.adapter.executeSavedQuery(name,variables);
Test
$ npm test
In development with watching
$ npm run ci
Contribution
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.
License
The project is available under the MIT license.
Contact
Copyright (c) 2019 MoleculerJS