lawson
Yet another Couchbase Orm
Install
$ npm install --save lawson
Setup
orm.js
import couchbase from 'couchbase';
import lawson from 'lawson';
const cluster = new couchbase.Cluster();
const bucket = cluster.openBucket();
const lawsonInstance = lawson(bucket);
export const model = lawsonInstance.defineModel;
models/user.js
import {model} from '../orm';
export const default = model('user', {
username: 'string',
email: 'string',
password: 'string'
});
Usage
import user from './models/user';
user.create({
username: 'test',
email: 'test@test.com',
password: 'secret'
})
.then(createdUser => {
return user.get(createdUser.id);
})
.then(newUser => {
newUser.username = 'bobby';
return user.update(newUser.id, newUser);
})
.then(updatedUser => {
console.log('user updated');
})
.then(() => {
return user.query({
where: {
username: 'bobby'
}
});
})
.then(users => {
console.log(user.length);
return user.delete(users[0].id);
})
.catch(e => {
console.warn(e);
});
API
lawsonInstance = lawson(couchbaseBucket)
Create a new instance of lawson
couchbaseBucket
Type: Bucket
required
The bucket the library is storing documents
model = lawsonInstance.defineModel(modelName, modelDefinition)
Define a new model
modelName
Type: string
required
the type of the document
modelDefinition
Type: object
required
the structure the document is following
model.get(documentId)
Returns a Promise, that resolve to the requested document
documentId
Type: string
required
model.update(documentId, document)
Returns a Promise, that resolve when the document is updated
documentId
Type: string
required
the id of the document to update
document
Type: object
required
the new version of the document that will be updated
model.create(document)
Returns a Promise, that resolve to the created document
document
Type: object
required
The document that will be created
model.delete(documentId)
Returns a Promise when the document is deleted
documentId
Type: string
required
The id of the document that will be deleted
model.query(options)
Returns a Promise, that resolve to all the documents
options.where
Type: object
Used to filter out results of the query
License
MIT © Thomas Sileghem