NoSQL Library for PostgreSQL


NoSQL Client Module for PostgreSQL databases.

var nopg = require('nor-nopg');

Before using the database you should initialize it by calling: nor-nopg --pg=psql://localhost:5432/test init

We use Trello board to organize development and keep track on things to do.

NoPgNoPg modulevar NoPg = require('nor-nopg');
dbNoPg instanceNoPg.start(...).then(function(db) { ... });
docNoPg.Document instancedb.create()(...).shift().then(function(doc) { ... });
typeNoPg.Type instancedb.declareType("name")().shift().then(function(type) { ... });
Short usageDescriptionTested at
NoPg.start(...)Get connection and start transactionL42
db.init()Initialize databaseL15
db.create()({"hello":"world"})Create document without typeL41
db.create("MyType")({"hello":"world"})Create document with type as stringL57
db.create(type)({"hello":"world"})Create document with type as objectL306{"$id": "b58e402e-6b39-11e3-99c7-0800279ca880"})Search documents by idL156{"hello": "world"})Search documents by valuesL130 { return doc.hello === 'world'; })Search documents by custom function"Foobar")()Search documents by type stringL185"Foobar")({"name":"hello"})Search documents by type string with valuesL219 documents by type{"name":"hello"})Search documents by type as string with valuesL254
db.update(doc)Edit document by instance of NoPg.DocumentL93
db.update(doc, {"hello": "world"})Edit document by plain documentL74
n/aEdit documents by type
db.del(doc)Delete document by instance of NoPg.DocumentL113
n/aDelete documents by instance of NoPg.Type
db.del(type)Delete type by instance of NoPg.TypeL400
db.del(attachment)Delete attachment
db.declareType("Product")({"$schema":{"type":"object"}})Create or replace type with name as string
db.createType("Product")({"$schema":{"type":"object"}})Create type with name as string
db.createType()({"$schema":{"type":"object"}})Create type without name
db.update(type)Edit type by instance of NoPg.TypeYes
db.update(type, {$schema:{...}})Edit type by plain objectYes
db.searchTypes({"$id": "b58e402e-6b39-11e3-99c7-0800279ca880"})Search types
doc.createAttachment(data, {"content-type": "image/png"})Create attachments
doc.searchAttachments()Search attachments
doc.getAttachment("b58e402e-6b39-11e3-99c7-0800279ca880")Search attachments
db.import('/path/to/tv4.js', {'$name': 'tv4'})Import or upgrade module in database
idobj.$idProperty with leading $ is mapped to the actual database table column
content->>'name' or content->'name'obj.nameProperty without leading $ is mapped to the property of the primary JSON data variable. It's content for NoPg.Documents and meta for other objects. The string or number operator is detected automatically from the type of the value.
nopg.start('postgres://user:pass@localhost/dbname').then(function(db) {
    /* ... */
    return db.commit();

You must call

  • db.commit() to actually save any changes to the database; or
  • db.rollback() to cancel the transaction

The required table structures and initial settings and data can be created or upgraded by calling db.init():

nopg.start(PGCONFIG).init().then(function(db) {

We use an extended promise implementation which allows chaining of multiple methods together.

Under the hood we are using q promises which are extended using nor-extend.

However these extended features are not required. You may use our promises just like any other q-promises.

NoPg.start(...).create("Group")({"name":"Bar"}).create("User")({"name":"Foo"}).then(function(db) {
    var group = db.fetch();
    var user = db.fetch();
    // ... do your magic at this point 
    return db.commit();
}).fail(function(err) {

The module has simple ORM mappings for all of our PostgreSQL tables.

JavaScript constructorPostgreSQL tableDefault JSON column

These constructors will take an object and convert it to JavaScript instance of that PostgreSQL table row.

Example object:

    "name": "Hello",
    "foo": "bar",
    "age": 10
    "$id": "8a567836-72be-11e3-be5d-0800279ca880",
    "$created": "",
    "$updated": ""

The special $ in the name makes it possible to point directly to a column in PostgreSQL row.

Any other property points to the column in default JSON column.

For example a obj.$ in NoPg.Type instance has the same value as unless the ORM instance has been changed by the user.

db.create()({"hello":"world"}).then(function(db) {
    var doc = db.fetch();
    console.log("Successfully created new document: " + util.inspect(doc) );
db.create("MyType")({"hello":"world"}).then(function(db) {
    var doc = db.fetch();
    console.log("Successfully created new document: " + util.inspect(doc) );

Tested at test-nopg.js:57.

db.create(type)({"hello":"world"}).then(function(db) {
    var doc = db.fetch();
    console.log("Successfully created new document: " + util.inspect(doc) );
});{"$id": "b58e402e-6b39-11e3-99c7-0800279ca880"}).then(function(db) {
    var list = db.fetch();
    console.log("Found documents: " + util.inspect(list) );
});{"hello": "world"}).then(function(db) {
    var list = db.fetch();
    console.log("Found documents: " + util.inspect(list) );
}); {
    return doc.hello === 'world';
}).then(function(db) {
    var list = db.fetch();
    console.log("Found documents: " + util.inspect(list) );
});"Foobar")().then(function(db) {
    var list = db.fetch();
    console.log("Found documents: " + util.inspect(list) );
});"Foobar")({"name":"hello"}).then(function(db) {
    var list = db.fetch();
    console.log("Found documents: " + util.inspect(list) );
}); {
    var list = db.fetch();
    console.log("Found documents: " + util.inspect(list) );
doc.hello = "world";
db.update(doc).then(function(db) {
    console.log("Successfully edited document: " + util.inspect(doc) );

Tested at test-nopg.js:93.

db.update(doc, {"hello": "world"}).then(function(db) {
    console.log("Successfully edited document: " + util.inspect(doc) );

Tested at test-nopg.js:74.

/* n/a */
db.del(doc).then(function(db) {
    console.log("Document deleted succesfully.");

Tested at test-nopg.js:113.

// n/a 
db.del(type).then(function(db) {
    console.log("Type deleted succesfully.");
db.del(attachment).then(function(db) {
    console.log("Attachment deleted succesfully.");
db.declareType("Product")({"schema":{"type":"object"}}).then(function(db) {
    var type = db.fetch();
    console.log("Successfully fetched a type: " + util.inspect(type) );
db.createType("Product")({"schema":{"type":"object"}}).then(function(db) {
    var type = db.fetch();
    console.log("Successfully created new type: " + util.inspect(type) );
db.createType()({"schema":{"type":"object"}}).then(function(db) {
    var product_type = db.fetch();
    console.log("Successfully created new type: " + util.inspect(product_type) );
type.schema = {..};
db.update(type).then(function(db) {
    console.log("Successfully edited type: " + util.inspect(type) );
db.update(type, {schema:{...}}).then(function(db) {
    console.log("Successfully edited type: " + util.inspect(type) );
db.searchTypes({"$id": "b58e402e-6b39-11e3-99c7-0800279ca880"}).then(function(db) {
    var list = db.fetch();
    console.log("Found types: " + util.inspect(list) );
nopg.createAttachment(doc)(file, {"content-type": "image/png"}).then(function(db) {
    var file = db.fetch();
    console.log("Successfully created new attachment: " + util.inspect(file) );
  • If doc is undefined then document is looked from previous value in the buffer which must by nopg.Attachment or nopg.Document.
doc.searchAttachments(doc)(opts).then(function(db) {
    var list = db.fetch();
    console.log("Found attachments: " + util.inspect(list) );
  • If you omit doc, the last element in the queue will be used.
  • If you omit opts, then all attachments are listed otherwise only matching.
doc.getAttachment("b58e402e-6b39-11e3-99c7-0800279ca880").then(function(db) {
    var attachment = db.fetch();
    console.log("Found attachment: " + util.inspect(attachment) );
db.import('/path/to/tv4.js', {'name': 'tv4'}).then(function(db) {
    console.log("Library imported succesfully.");

Database configurations can be set using PGCONFIG:

export PGCONFIG='pg://user:password@localhost/db'

The actual test can be run: npm test

You must delete the data if you need to run the test suite again for the same database:

psql -q db < scripts/cleanup.sql

Please note: psql does not follow PGCONFIG environment variable!

npm run lint