0

I have created the following db_postgres service which works fine.

db_postgres.service.js

"use strict";

const Promise = require('bluebird');

const initOptions = {
    promiseLib: Promise,
    capSQL:     true
};

const pgp = require('pg-promise')(initOptions);

const cn_postgres = {
    host:     'localhost',
    port:      5432,
    database: 'db_name',
    user:     'user_name',
    password: 'user_password',
    ...
};

var db_postgres = null;

module.exports = {
    name: "db_postgres",
    version: "",
    mixins: [],
    hooks: {},
    settings: {},
    metadata: {},
    dependencies: [],

    actions: {
        getUsers() {
            db_postgres.any('SELECT * FROM user')
                .then(data => {
                    return console.log(data);
                })
                .catch(error => {
                    return console.log(error);
                });
        },
    },

    methods: {},
    events: {},
    created() {},

    async started() {
        return db_postgres = await pgp(cn_postgres);
    },

    async stopped() {
        if (db_postgres) {
            return db_postgres.$pool.end();
        }
    }
}

Case:

  • I have a large number of actions.
  • I would like to create more instances of this service, but unfortunately, it is not possible since I get a warning/error about establishing a new connection that already exists.
  • I do not wish to use any ORM-based solution.
  • I wish to avoid using any wrapper function(s) and maintain a DRY manner as much as possible.

Question: What is the simplest and easiest way to do that?

My effort:

  • I removed the getUsers() action from db_postgres service.
  • I created a new queries service as follows:

queries.service.js

"use strict";

module.exports = {
    name: "queries",
    version: "",
    mixins: [db_postgres],
    hooks: {},
    settings: {},
    metadata: {},
    dependencies: ["db_postgres"],

    actions: {
        getUsers() {
            db_postgres.any('SELECT * FROM user')
                .then(data => {
                    return console.log(data);
                })
                .catch(error => {
                    return console.log(error);
                });
        },
    },

    methods: {},
    events: {},
    created() {},
    async started() {},
    async stopped() {}
}

Problems:

  • When I add "db_postgres" in dependencies and run npm run dev ie moleculer-runner --env --hot --repl services, the db_postgres service is being registered normally. But, queries service is never. The flow in terminal freezes and cursor is blinking. Why?
  • Next, when I also add db_postgres in mixins I get the following error Failed to load service '.../services/queries.service.js' ReferenceError: db_postgres is not defined Why?

Tia

SONewbiee
  • 363
  • 2
  • 15

1 Answers1

0

You should load db_postgres with require and use as a mixin, not as dependency.

Icebob
  • 1,132
  • 7
  • 14
  • In this case, when I run `call queries.getUsers` I get: `db_postgres.any is not a function...`. I suspect that by saying "... use as a mixin", you mean "forget **service schema** syntax and use the **classic** way" as it is done in `db.mixin.js` in **conduit** example. Correct? – SONewbiee Sep 21 '18 at 17:01