Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leobalter/model-golem
Model Factory libraray
https://github.com/leobalter/model-golem
Last synced: 3 months ago
JSON representation
Model Factory libraray
- Host: GitHub
- URL: https://github.com/leobalter/model-golem
- Owner: leobalter
- License: mit
- Created: 2015-12-10T21:39:45.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-11T15:32:46.000Z (about 9 years ago)
- Last Synced: 2024-04-26T10:21:02.864Z (9 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# model-golem
A model factory JS library to generate data mocks
---
## Installing
```
npm install --save-dev model-golem
```## Using
Load the Factory:
```js
// NodeJS / CommonJS Modules
var Factory = require('model-golem');// ES2015 Modules
import Factory from 'model-golem';
```Define a model:
```js
var Person = {
id: function() {// this.sequence is autoincremented on every creation step, starts from 0
return this.sequence;
},// Define the attributes giving their default values
name: '',
charisma: 42,// Reuse attributes
username: function() {
return this.item.name.toLowerCase().replace(/ /g, '');
}
};
```Instantiate the Factory with your model:
```js
var peopleFactory = new Factory(Person);
```Create your mocks!
```js
var person = peopleFactory({
name: 'Leo Balter',
});/*
person === {
id: 0,
name: 'Leo Balter',
charisma: 42,
username: 'leobalter'
}
*/
```Create your mocks from arrays!
```js
var people = peopleFactory([
{
name: 'Green Avocados',// replace default values!
charisma: 0
},
{
name: 'Spicy Mustard',
id: 100,
charisma: 37,
username: 'mryellow'
}
]);/*
people === [
{
id: 2,
name: 'Green Avocados',
charisma: 0,
username: 'greenavocados'
},
{
id: 100,
name: 'Spicy Mustard',
charisma: 37,
username: 'mryellow'
}
]
*/
```Get the stored data!
```js
var stored = peopleFactory.store;/*
stored === [
{
id: 1,
name: 'Leo Balter',
charisma: 42,
username: 'leobalter'
},
{
id: 2,
name: 'Green Avocados',
charisma: 0,
username: 'greenavocados'
},
{
id: 100,
name: 'Spicy Mustard',
charisma: 37,
username: 'mryellow'
}
]
*/
```---
## MIT Licensed