pouchdb-model
v0.4.0
Published
[![Build Status](https://travis-ci.org/ewnd9/pouchdb-model.svg?branch=master)](https://travis-ci.org/ewnd9/pouchdb-model) [![Coverage Status](https://coveralls.io/repos/github/ewnd9/pouchdb-model/badge.svg?branch=master)](https://coveralls.io/github/ewnd9
Downloads
9
Readme
pouchdb-model
Convenient Wrapper for PouchDB
Install
$ npm install --save pouchdb-model
Examples
- examples/simple (inlined below) - no schema / no validation
- examples/tcomb - schema and validation by tcomb
import PouchDB from 'pouchdb';
import init from 'pouchdb-model/dist/init';
// if you don't use migrations, you could omit this import
import MigratePlugin from 'pouchdb-migrate';
PouchDB.plugin(MigratePlugin);
const File = {
createId: ({ name }) => name,
migrations: [
function(doc) {
if (doc.foo) {
return;
}
doc.foo = true;
return [doc];
}
],
indexes: [
{
name: 'updatedAtIndex',
fn: function(doc) {
emit(doc.updatedAt + '$' + doc._id);
}
}
]
};
async function main(models) {
const { File } = await init(models, name => new PouchDB(name.toLowerCase()))
const docA = await File.put({ name: 'user-a' });
/*
{ name: 'user-a',
_id: 'user-a',
updatedAt: '2016-11-03T19:35:19.869Z',
_rev: '1-1ccfb26dd920cbf8ac11c965efac73d9' }
*/
const docB = await File.put({ name: 'user-b' });
/*
{ name: 'user-b',
_id: 'user-b',
updatedAt: '2016-11-03T19:36:16.827Z',
_rev: '1-7ce9e72b2c2ed18fd1753e0c6627e6b0' }
*/
const docs = await File.findAll();
/*
[ { name: 'user-a',
updatedAt: '2016-11-03T19:38:44.765Z',
_id: 'user-a',
_rev: '1-722dd9fc99ef1f04334544bc01e0639a',
_key: 'user-a' },
{ name: 'user-b',
updatedAt: '2016-11-03T19:38:44.766Z',
_id: 'user-b',
_rev: '1-b3b0f835f60044a68faf04e80a91ced7',
_key: 'user-b' } ]
*/
const docsByUpdateAt = await File.findByIndex('updatedAtIndex', { descending: true });
/*
[ { name: 'user-b',
updatedAt: '2016-11-03T19:45:27.729Z',
_id: 'user-b',
_rev: '1-ab63623e330d68e060bc2f93ddab0279',
_key: '2016-11-03T19:39:27.403Z$user-b' },
{ name: 'user-a',
updatedAt: '2016-11-03T19:45:27.728Z',
_id: 'user-a',
_rev: '1-c6da589a48e47402198e145ace61155f',
_key: '2016-11-03T19:39:27.403Z$user-a' } ]
*/
}
try {
main({ File });
} catch (e) {
console.error(e.stack || e);
}
Methods
#createId(data: Object)
Returns the _id
field in data
or new id generated by a function provided in an initializer
#findById(id: String)
#findOne(data: Object)
#findAll(options: Object?)
#findByIndex(options: Object?)
#put(data: Object)
#update(data: Object)
Overwrites the previous object if existed with a correct _rev
or insert a new doc
#bulk(data: Object[])
Returns an array of the same length containing corresponding doc (has _id
and _rev
)
or error (has error
, name
, status
and, in case of validation error - err
)
#sync(db: PouchDB | String, options: Object?, notify: Function)
Changelog
v0.4.0
createdAt
property
Related
License
MIT © ewnd9