@ampatspell/sofa-models

0.0.3 • Public • Published

sofa-models

Model library for CouchDB

Declare model

import { Model, name, attr, belongsTo } from '@ampatspell/sofa-models';

@name('thing')
export default class Thing extends Model {

  @attr('string')
  name

  @attr('string')
  email

  @belongsTo('something', { inverse: 'thing' })
  something

  update(props) {
    _.merge(this, props);
  }

  static byName(name) {
    return this.database('main').load('thing', name);
  }

}

Register model

import Models from '@ampatspell/sofa-models';
import Thing from './models/thing';

let models = new Models();
models.register(Thing);

Use

let session = models.session({ url, database: (name) => 'test' });

const Thing = session.modelClass('thing');

Thing.byName('yellow').then((thing) => {
  thing.update({ name: 'greenish' });
  return thing.save();
});

Package Sidebar

Install

npm i @ampatspell/sofa-models

Weekly Downloads

3

Version

0.0.3

License

ISC

Last publish

Collaborators

  • ampatspell