1.0.6 • Published 3 years ago

@nsfx/pg v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Postgresql nodes

  • Base - provide default init function and dsn prop
  • ArrayQuery - execute query by sql, values and push rows to out port

Usage

ArrayQuery

'use strict'

const def = `
ticks(@nsfx/time/Ticks) -> tasks(@nsfx/pg/ArrayQuery) -> dispatch(./Dummy)
`

const props = {
    ticks: {
        interval: 5000
    },
    tasks: {
        dsn: 'pg://u:p@host/dbname',
        sql: `select * from generate_series(1,5,1) as t(id) limit $1`,
        values: message => [ 2 ],
        shiftBy: 2,
        out: 'task'
    }
}

build(def, props, {}, (err, flow) => {

    flow.ticks.start.write()
})

Custom query node

// custom/SimpleQuery.node.js

module.exports = (args, env) => ({
    base: '@nsfx/pg/Base',
    out_: 1,
    _in(message, done) {

        let sql = `select * from generate_series(1,5,1) as t(id) limit $1`
        let values = [ 5 ]

        this.__pool.query(sql, values, (error, res) => {

            if (err) this.send('errors', { error, ...message })
            else this.send('out', { rows: res.rows, ...message })

            done()
        })
    }
})

// exp.flow.js

const def = `
ticks(@nsfx/time/Ticks) -> tasks(./custom/SimpleQuery) -> dispatch(./Dummy)
`

const props = {
    ticks: {
        interval: 5000
    },
    tasks: {
        dsn: 'pg://u:p@host/dbname',
    }
}

build(def, props, {}, (err, flow) => {

    flow.ticks.start.write()
})