data-modela
an application that uses sequelize methods to work with dummy-data when building appliication
Last updated 7 months ago by tutugodfrey .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install data-modela 
SYNC missed versions from official npm registry.

Modela

Modela is a module for working with In memory dummy-data while developing an application and allows you to use common ORM search methods in like create, bulkCreate, find, findById, findAll, update, and destroy, clear in your controller. The return value is a promise which you can use the ...then() ...catch() block on, or simple use async ...await in your function to get the return value. Example are given below

status


Build Status Coverage Status Maintainability Test Coverage


Usage


import the module into your project import DummyData from modela

if you are using es5 const Modela = require('data-modele').default

initialize your model instance const users = new DummyData('users' [ /* specify required fields */ ], [ /* specify unique fields */ ]);

Creating a new model

Creating a single model

!(function() {
  return  users
  .create({
    name: 'Johb doe',
    email: 'johndoe@email.com',
    username: 'johndoe',
  })
  .then(es => console.log(res))
}());

returns

{ name: 'Johb doe',
  email: 'johndoe@email.com',
  username: 'johndoe',
  id: 1 }

Create another record

!(function() {
  return users.create({
    name: 'Jane doe',
    email: 'janedoe@email.com',
    username: 'janedoe',
  })
  .then(res => console.log(res))
}());

Returns

{ name: 'Janet doe',
  email: 'Janet@email.com',
  username: 'janedoe',
  id: 2 }

Creating record with bulkCreate

To create new record with the bulkCreate method, simple pass an array of your record to the bulkCreate call.

!(function() {
  return  users
  .bulkCreate([{
    name: 'Johb doe',
    email: 'johndoe@email.com',
    username: 'johndoe',
  }])
  .then(es => console.log(res))
}());

Finding models

Find by Id

!(function() {
  return users
  .findById(1)
  .then(res => {
    console.log(res)
  })
}());

Returns

{ name: 'Johb doe',
  email: 'johndoe@email.com',
  username: 'johndoe',
  id: 1 }

FindAll

!(function() {
  return users
  .findAll()
  .then(res => {
    console.log(res)
  })
}());

Returns

    email: 'johndoe@email.com',
    username: 'johndoe',
    id: 1 },
  { name: 'Janet doe',
    email: 'Janet@email.com',
    username: 'janedoe',
    id: 2 } ]

Find by find values

!(function() {
  return users
  .find({where: {username: 'johndoe'}})
  .then(res => {
    console.log(res)
  })
}());

Returns

{ name: 'Johb doe',
  email: 'johndoe@email.com',
  username: 'johndoe',
  id: 1 }

Updating a model

!(function() {
  return users
    .update({
			where: {
      	id: 2,
				// you can specify other properties as well
			}
    }, {
    name: 'Janet doe',
    email: 'Janet@email.com',
    })
    .then(res => console.log(res))
}())

Returns

{ name: 'Janet doe',
  email: 'Janet@email.com',
  username: 'janedoe',
  id: 2 }

Deleting a model

!(function() {
  return users
  .destroy({ where: { id: 1 }})
  .then(res => console.log(res))
}())

Returns

{ message: 'user has been deleted' }

Clearing a model

!(function() {
  return users
  .clear({ where: { id: 1 }})
  .then(res => console.log(res))
}())

Returns

{ message: 'Successfully cleared users' }

Specifying conditional search with where and type

The where conditional search can be used with find, findAll, update and delete as shown below.

model.findAll({ where: { propsToSearch1: 'value' propsToSearch2: 'value2' }, type: "or" || "and" })

  • and is the default if type is not specified. This return result where all the props match.
  • or return result where any of the props match.

Links

npm data-modela

Author

Current Tags

  • 0.0.7                                ...           latest (7 months ago)

7 Versions

  • 0.0.7                                ...           7 months ago
  • 0.0.6                                ...           8 months ago
  • 0.0.5                                ...           8 months ago
  • 0.0.4                                ...           10 months ago
  • 0.0.3                                ...           a year ago
  • 0.0.2                                ...           a year ago
  • 0.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (10)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |