Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Mar 31, 2018
1 parent 87d09ef commit 4471f28
Show file tree
Hide file tree
Showing 18 changed files with 5,059 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"presets": [
[
"env",
{
"targets": {
"node": 6
},
"loose": true
}
]
],
"plugins": ["transform-class-properties"]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: 'smooth',
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
/lib/
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
!/lib
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js

node_js:
- 6
- 8

script:
- yarn ci

notifications:
email: false

cache:
yarn: true
directories:
- "node_modules"

services:
- postgresql
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Smooth Code

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# factory-girl-objection-adapter

[![travis][travis-image]][travis-url]
[![npm][npm-image]][npm-url]

[travis-image]: https://img.shields.io/travis/smooth-code/factory-girl-objection-adapter/master.svg
[travis-url]: https://travis-ci.org/smooth-code/factory-girl-objection-adapter
[npm-image]: https://img.shields.io/npm/v/factory-girl-objection-adapter.svg
[npm-url]: https://npmjs.org/package/factory-girl-objection-adapter

This module brings [objection](http://vincit.github.io/objection.js/) support to [factory-girl](https://github.com/aexmachina/factory-girl).

```
npm install factory-girl-objection-adapter
```

## Usage

```js
import { factory } from 'factory-girl'
import ObjectionAdapter from 'factory-girl-objection-adapter'
import User from './models/User'

factory.setAdapter(new ObjectionAdapter())

factory.define('user', User, {
name: factory.chance('name'),
email: factory.chance('email'),
})

export default factory
```

## License

MIT
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: '2'
services:
postgres:
image: postgres:10.2-alpine
ports:
- '5432:5432'
8 changes: 8 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
client: 'postgresql',
connection: {
user: 'postgres',
database: 'postgres',
timezone: 'utc',
},
}
8 changes: 8 additions & 0 deletions migrations/20180331112243_init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.up = knex =>
knex.schema.createTable('users', table => {
table.bigincrements('id').primary()
table.string('name').notNullable()
table.string('email').notNullable()
})

exports.down = knex => knex.schema.dropTableIfExists('users')
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "factory-girl-objection-adapter",
"description": "Objection adapter for factory girl.",
"keywords": [
"factory-girl",
"factory-girl-adapter",
"objection"
],
"version": "1.0.0",
"scripts": {
"build": "babel -d lib src",
"ci": "yarn build && yarn lint && yarn test --ci",
"db:migrate": "knex migrate:latest",
"dev": "yarn build --watch",
"lint": "eslint .",
"test": "jest"
},
"main": "lib/index.js",
"author": "Greg Bergé <berge.greg@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-config-prettier": "^2.9.0",
"eslint-config-smooth": "^1.1.1",
"eslint-plugin-import": "^2.10.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"factory-girl": "^5.0.2",
"jest": "^22.4.3",
"knex": "^0.14.4",
"objection": "^1.0.1",
"pg": "^7.4.1"
},
"peerDependencies": {
"factory-girl": ">=5.0.2"
}
}
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ObjectionAdapter {
build(Model, props) {
return Object.assign(new Model(), props)
}

async save(model, Model) {
return Model.query().insert(model)
}

async destroy(model, Model) {
return Model.query().deleteById(model.id)
}

get(model, attr) {
return model[attr]
}

set(props, model) {
return Object.assign(model, props)
}
}

module.exports = ObjectionAdapter
3 changes: 3 additions & 0 deletions test/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'smooth/dev',
}
17 changes: 17 additions & 0 deletions test/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Model } from 'objection'

export default class User extends Model {
static tableName = 'users'

static jsonSchema = {
type: 'object',
required: [],
properties: {
id: { type: 'string' },
name: { type: 'string' },
email: { type: 'string' },
},
}

static modelPaths = [__dirname]
}
12 changes: 12 additions & 0 deletions test/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { factory } from 'factory-girl'
import ObjectionAdapter from '../lib/index'
import User from './User'

factory.setAdapter(new ObjectionAdapter())

factory.define('user', User, {
name: factory.chance('name'),
email: factory.chance('email'),
})

export default factory
52 changes: 52 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Knex from 'knex'
import { Model } from 'objection'
import User from './User'
import factory from './factory'
import knexConfig from '../knexfile'

describe('ObjectionAdapter', () => {
let knex

beforeAll(async () => {
knex = Knex(knexConfig)
Model.knex(knex)
await knex.migrate.latest()
})

afterAll(
async () =>
new Promise((resolve, reject) =>
knex.destroy(error => {
if (error) {
reject(error)
} else {
resolve()
}
}),
),
)

it('should build model', async () => {
const model = await factory.build('user', { name: 'James' })
expect(model.name).toBe('James')
expect(model).toBeInstanceOf(User)
})

it('should create model', async () => {
const model = await factory.create('user', { name: 'James' })
expect(model.name).toBe('James')
expect(model.id).toBeDefined()
expect(model).toBeInstanceOf(User)
})

it('should remove model', async () => {
const model = await factory.create('user', { name: 'James' })
expect(model.name).toBe('James')
expect(model.id).toBeDefined()
expect(model).toBeInstanceOf(User)

expect(await User.query().where('id', model.id)).toHaveLength(1)
await factory.cleanUp()
expect(await User.query().where('id', model.id)).toHaveLength(0)
})
})
Loading

0 comments on commit 4471f28

Please sign in to comment.