Skip to content
Compare
Choose a tag to compare
@icebob icebob released this 17 Jul 12:53
· 275 commits to master since this release

New

Encoding & decoding IDs

There are two new encodeID and decodeID methods. You can use them if you want to encode & decode database ID (for example with hashids)

const Hashids = require("hashids");
const hashids = new Hashids("secret salt");

broker.createService({
    name: "posts",
    mixins: [DbService],
    methods: {
        encodeID(id) {
            return hashids.encodeHex(id);
        },
        decodeID(id) {
            return hashids.decodeHex(id);
        }
    }
});

Entity lifecycle events

There are 3 entity lifecycle events which are called when entities are manipulated.

broker.createService({
    name: "posts",
    mixins: [DbService],
    settings: {},

	afterConnected() {
		this.logger.info("Connected successfully");
	},

	entityCreated(json, ctx) {
		this.logger.info("New entity created!");
	},

	entityUpdated(json, ctx) {
        // You can also access to Context
		this.logger.info(`Entity updated by '${ctx.meta.user.name}' user!`);
	},

	entityRemoved(json, ctx) {
		this.logger.info("Entity removed", json);
	},    
});

Better fields filtering

A new fields filtering method is implemented. It can also handle nested properties.

const DbService = require("moleculer-db");

module.exports = {
    name: "users",
    mixins: [DbService],

    settings: {
        fields: ["name", "address.city", "address.country", "bio"]
    }
}

broker.call("users.get", { id: 5, fields: ["name", "address", "bio.height", "bio.hair", "password"] }).then(console.log);
/* The returned object contains only the following fields:
{
    name: "Walter",
    address: {
        city: "Albuquerque",
        country: "USA"
    },
    bio: {
        height: 185,
        hair: "bald"
    }
}
*/

Changes

  • deprecated fields as space-separated String in settings. Enabled only Array<String>.

  • deprecated fields as space-separated String in fields of settings.populates. Enabled only Array<String>.

  • BREAKING: model action & method is removed! Use get action instead.

  • moleculer-db-adapter-mongoose returns with Mongoose objects instead of native object. But it will be converted to native JS object in [moleculer-db].

        customAction(ctx) {
            return this.adapter.find({}).then(docs => {
                // You can access the Mongoose virtual methods & getters of `docs` here
            });
        }