ngyv / re-modelr

A simple-to-use pure javascript object-relational mapping to manage data.
MIT License
0 stars 0 forks source link

[FEATURE] Base model #2

Closed ngyv closed 6 years ago

ngyv commented 6 years ago

How to use ?

As the name suggests, this is a base model that you can use to create other models to map the json fetched from servers.

import { types } from '@ngyv/prop-utils';

class User extends BaseModel {
  _attributes() {
    const defaultAttributes = super._attributes();
    const userAttributes = {
      name: {
        type: types.string,
        validate: [types.undefined, types.null, types.emptyString] // optional. Validation will run because `name` is an object
      },
      favouriteFood: types.array,
    };
    return Object.assign({}, defaultAttributes, userAttributes);
  }
}

class UserStore extends DomainStore {}  // DomainStore not implemented yet
const userStore = new UserStore();

// `id`, `createdAt`, and `updatedAt` are default attributes defined in the `BaseModel` class
let singer = new User(userStore, {
  id: 1,
  name: 'Yuna',
  createdAt: new Date(),
  updatedAt: new Date(),
  favouriteFood: ['nasi lemak'],
});

singer.set('name', 'Zee Avi');

singer.changedAttributes();
//=> { name: ['Yuna', 'Zee Avi'] }

singer.isDirty();
//=> true

singer.discardChanges();
singer.get('name');
//=> 'Yuna'

For more on the api, please look at the test file 😃

codecov-io commented 6 years ago

Codecov Report

:exclamation: No coverage uploaded for pull request base (master@85ab570). Click here to learn what that means. The diff coverage is 91.66%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master       #2   +/-   ##
=========================================
  Coverage          ?   92.04%           
=========================================
  Files             ?        3           
  Lines             ?       88           
  Branches          ?        0           
=========================================
  Hits              ?       81           
  Misses            ?        7           
  Partials          ?        0
Impacted Files Coverage Δ
lib/index.js 100% <100%> (ø)
lib/base-model.js 88.7% <85.71%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 85ab570...ef8dcab. Read the comment docs.