modli-postgres

3.3.0 • Public • Published

Modli - PostgreSQL Adapter

This module provides adapter for the PostgreSQL datasource for integration with Modli.

Installation

npm install modli-postgres --save

Config and Usage

When defining a property which will utilize the adapter it is required that a tableName be supplied:

const { model, adapter, obey, use } = require('modli')
const postgres = require('modli-postgres')
 
model.add({
  name: 'foo',
  version: 1,
  tableName: 'tblFoo'
  schema: obey.model({
    id: { type: 'number' },
    fname: { type: 'string', min: 3, max: 30 },
    lname: { type: 'string', min: 3, max: 30 },
    email: { type: 'email', min: 3, max: 254, required: true }
  })
})

Then add the adapter as per usual with the following config object structure:

adapter.add({
  name: 'postgresFoo',
  source: postgres
  config: {
    host: {HOST_IP},
    user: {USERNAME},
    password: {PASSWORD},
    database: {DATABASE}
  }
})

You can then use the adapter with a model via:

// Use(MODEL, ADAPTER)
const postgresTest = use('foo', 'postgresFoo')

Methods

The following methods exist natively on the PostgreSQL adapter:

query

Allows for passing standard PostgreSQL queries:

postgresTest.query('SELECT * FROM tblFoo')
  .then(/*...*/)
  .catch(/*...*/)

createTable

Creates (IF NOT EXISTS) a table based on params:

postgresTest.createTable({
    'id': [ 'serial', 'NOT NULL', 'PRIMARY KEY'],
    'fname': [ 'varchar(255)' ],
    'lname': [ 'varchar(255)' ],
    'email': [ 'varchar(255)' ]
  })
  .then(/*...*/)
  .catch(/*...*/)

create

Creates a new record based on object passed:

postgresTest.create({
    fname: 'John',
    lname: 'Smith',
    email: 'jsmith@gmail.com'
  })
  .then(/*...*/)
  .catch(/*...*/)

read

Runs a SELECT with optional WHERE:

postgresTest.read('fname=\'John\'')
  .then(/*...*/)
  .catch(/*...*/)

update

Updates record(s) based on query and body:

postgresTest.update('fname=\'John\'', {
    fname: 'Bob',
    email: 'bsmith@gmail.com'
  })
  .then(/*...*/)
  .catch(/*...*/)

delete

Deletes record(s) based on query:

postgresTest.delete('fname=\'Bob\'')
  .then(/*...*/)
  .catch(/*...*/)

extend

Extends the adapter to allow for custom methods:

postgresTest.extend('myMethod', () => {
  /*...*/
})

Development

The PostgreSQL adapter requires the following environment variables to be set for running the tests. These should be associated with the PostgreSQL instance running locally.

MODLI_POSTGRES_HOST,
MODLI_POSTGRES_USERNAME,
MODLI_POSTGRES_PASSWORD,
MODLI_POSTGRES_DATABASE

This repository includes a base container config for running locally which is located in the /docker directory.

License

Modli-Postgres is licensed under the MIT license. Please see LICENSE.txt for full details.

Credits

Modli-Postgres was designed and created at TechnologyAdvice.

Package Sidebar

Install

npm i modli-postgres

Weekly Downloads

0

Version

3.3.0

License

MIT

Last publish

Collaborators

  • tadeploy