gresshelf

0.1.1 • Public • Published

Gresshelf

Gressshelf is a nodejs wrapper around postgresql jsonb columns. That allows you to use postgres like you would use a nosql database like MongoDB in schema(less) format.

usage

initialize gresshelf with the postgress connection details, and your application schema

let gs = require('./gresshelf')
let db = new gs({
  postgres: {
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  },
  schema:{
    users: {
      email: { type: 'string'},
      password: { type: 'string' }
    },
    posts: {
      title: { type:'string' },
      created_at { type: 'date' }
    }
  }
})

The schema is for type safety, but can be changed easily at application level without touching the db

insert items

db.table(tableName).insert(data, options);

// after initializing gresshelf
// to insert a document into the users table
let promise = db.table('users').insert({
  name: 'Jason Bourne',
  tag: 'asset'
}).then(changes => {
  // item inserted
})
  • a random integer ID is generated for each inserted document

query items

db.table(tableName).filter(data, options);

// after initializing gresshelf
// to query documents from the users table
let promise = db.table('users').filter({
  name: 'Jason Bourne',
  tag: 'asset'
}).then(results => { // returns an array of values
  // results == []
})
 
// similar to saying, return fields where name == 'Jason Bourne' and tag === 'asset'

find a single item

  • if you have the ID, you can fetch that particular document db.table(tableName).getItem(id, options)
async function () {
 let one = await db.table('users').getItem('225669998854755');
 // {
 //  id: '225669998854755',
 //  data,
 //  deleted: false,
 //  create_at: date,
 //  updated_at: date
 // }
}

update item

db.table(tableName).updateItem(id, data, options);

[WARNING] updateItem replaces the whole document, therefore you should provide all the fields, even those that haven't changed.

// after initializing gresshelf
// to update a document in the users table
let promise = db.table('users').update('225669998854755', {
  name: 'Matt Damon',
  tag: 'nothing'
}).then(results => { // returns an array of values
  // results == integer showing number of rows changed
})

Readme

Keywords

Package Sidebar

Install

npm i gresshelf

Weekly Downloads

1

Version

0.1.1

License

MIT

Last publish

Collaborators

  • mofax