Skip to content

Commit

Permalink
Version 2.0 - Compatible with Sails 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luislobo committed Jul 26, 2020
1 parent 70f63de commit bb49dca
Show file tree
Hide file tree
Showing 25 changed files with 617 additions and 790 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ node_modules
nbproject
.DS_Store
npm-debug.log
.tmp
package-lock.json
test/helpers/sampleApp/.tmp
test/helpers/sampleApp/package-lock.json
7 changes: 7 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
extension: ['js'],
reporter: 'spec',
timeout: 10000
};
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: node_js
node_js:
- "8"
- "10"
- "12"
- "14"
services:
- mongodb
before_install: npm install -g grunt-cli

62 changes: 0 additions & 62 deletions Gruntfile.js

This file was deleted.

32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
[![Build status][travis-image]][travis-url]
[![Dependency Status][daviddm-image]][daviddm-url]

Installable hook that injects fixtures into your sails ORM at runtime. With associations!
An installable hook that injects fixtures into your Sails ORM at runtime with associations!

## Installation
`npm i sails-hook-fixtures`

## Usage
When lifting your Sails app, any fixtures that are loaded are checked against the model tabels/collections in the database. If the database table/collection is empty, the fixtures for that table/collection are automatically installed. If you want to overwrite an existing table/collection each time you lift, use the `overwrite` option.
When lifting your Sails app, any fixtures that are loaded are checked against the model
tables/collections in the database. If the database table/collection is empty, the
fixtures for that table/collection are automatically installed. If you want to overwrite
an existing table/collection each time you lift, use the `overwrite` option.

Currently only tested against Waterline + MongoDB.
Currently, only tested against Waterline + MongoDB.

## Configuration
Define your `fixtures` object so that it is loaded as `sails.config.fixtures`. For instance, export it in [sails/config/local.js](http://sailsjs.org/#!/documentation/anatomy/myApp/config/local.js.html):
Define your `fixtures` object so that it is loaded as `sails.config.fixtures`.
For instance, export it in [sails/config/local.js](http://sailsjs.org/#!/documentation/anatomy/myApp/config/local.js.html):

```javascript
// sails/config/local.js
Expand All @@ -22,7 +26,8 @@ module.exports = {
}
```

The fixtures object should have an attribute `order` specifying the order in which to populate your models. This is important if you want to associate models.
The fixtures object should have an attribute `order` specifying the order in which to populate your models.
This is important if you want to associate models.

The fixtures for a model are configured as an array of objects. Each object will be made into a document in the database.

Expand Down Expand Up @@ -55,7 +60,8 @@ fixtures: {

For an example, take a look at [the sample fixtures used in testing](https://github.com/arryon/sails-hook-fixtures/blob/master/test/helpers/fixtures.js)

Each attribute other than those of the options below is assumed to be the capitalized name of your model. Inability to find the model will silently fail and continue.
Each attribute other than those of the options below is assumed to be the capitalized name of your model.
Inability to find the model will silently fail and continue.

## Options
| attribute name | usage | example |
Expand All @@ -65,15 +71,17 @@ Each attribute other than those of the options below is assumed to be the capita
| empty | Specify which model documents to empty before installing any fixtures | `empty: ['Pet']` |

## Associations
The hook can automatically add associations between your models if you specify them in the right way. This only works if you use Waterline!
The hook can automatically add associations between your models if you specify them in the right way.
This only works if you use Waterline!

### Terminology
* **Association/relation**: relation between two models
* **Fixture**: object that is injected as a document into the database
* **Document**: an entry in the database

### General Usage
Define associations as you would look up documents in an existing sails application. Want to add user `jason` to group `testusers`? Define a relation in the fixture object that says `user: {username: 'jason'}`. You can add every [*where* query from sails](http://sailsjs.org/#!/documentation/concepts/ORM/Querylanguage.html) to the relation definition.
Define associations as you would look up documents in an existing sails application.
Want to add user `jason` to group `testusers`? Define a relation in the fixture object that says `user: {username: 'jason'}`. You can add every [*where* query from sails](http://sailsjs.org/#!/documentation/concepts/ORM/Querylanguage.html) to the relation definition.

Does the model you want to add have an attribute `name`? Then as added bonus you can directly input an array into the relation definition. If you want to associate `jason` with groups `user` and `editor`, and your `Group` model has an attribute `name`, you can just say `group: ['user', 'editor']`.

Expand Down Expand Up @@ -167,6 +175,14 @@ fixtures: {
}
```

## Changelog

### Version 2.0.0
- Compatible with Sails 1.x/Waterline 0.13

### Version 1.0.2
- Compatible with Sails 0.12.x

## Development
Want to contribute? Clone the repository, install the dependencies with `npm install`, and get going. You can test using the command `grunt`, which will also run JSHint against your changes to see if they are syntactically correct.

Expand Down

0 comments on commit bb49dca

Please sign in to comment.