Skip to content

Commit

Permalink
Add migration / model cli commands
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Nov 8, 2016
1 parent 08fa1d8 commit 1042851
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist
coverage
coverage
migrations
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ modules
.babelrc
.gitignore
circle.yml
coverage
coverage
migrations
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Chat.where({ messages: 'blah' }).limit(2).get()

###Relationships

This is a huge WIP, feel free to contribute :)
This is a huge WIP, feel free to contribute :)

Supported:
- One To One
Expand All @@ -103,7 +103,7 @@ import { Model } from 'relation'


export default class User extends Model {

}

export default class Chat extends Model {
Expand Down Expand Up @@ -134,7 +134,7 @@ export default class User extends Model {
}

export default class Chat extends Model {

}

let user = await User.first()
Expand All @@ -145,3 +145,22 @@ let chats = await user.chats.first()

```

###Migrations

Will go over this soon...

###CLI

If you install relation globally (`npm install relation -g`) you can access the CLI methods to help create migrations, models, etc.

####Migrations

`relation make:migration User -m` -m will create a model as well

This will create a migration file that will allow you to build out tables.

####Models

`relation make:model User`

Creates a file in your current directory `/models/user.js` with a default model
13 changes: 13 additions & 0 deletions modules/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /usr/bin/env node
import fs from 'fs'

let commands = {
'make:migration': require('./migrations/make').default,
'make:model': require('./models/make').default,
}

commands[process.argv[2]]({
args: process.argv.slice(3),
cwd: process.cwd(),
fs
})
21 changes: 21 additions & 0 deletions modules/cli/models/make.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default ({ args, cwd, fs }) => {
let filePath = `${cwd}/models/${args[0].toLowerCase()}.js`

const template = `import { Model } from 'relation'
export default class ${args[0]} extends Model {
}
`
try {
fs.accessSync(`${cwd}/models`, fs.F_OK)
} catch (e) {
fs.mkdirSync(`${cwd}/models`)
}

fs.writeFile(filePath, template, err => {
if (err) throw err;
})

console.log('Created model: ', filePath)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pluralize from 'pluralize'


module.exports = {
module.exports = {
getTableName: (model) => typeof model === 'string' ? pluralize(model).toLowerCase() : null,
getFieldName: (model) => typeof model === 'string' ? `${model.toLowerCase()}_id` : null
}
3 changes: 0 additions & 3 deletions modules/migrations/cli.js

This file was deleted.

4 changes: 2 additions & 2 deletions modules/orm/adapters/mysql/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import connection from './connection'
import Builder from './builder'
import { getTableName } from '../../global/get-name'
import { getTableName } from '../../../global/get-name'

const relatable = (result, model) => new Proxy(result, {
get(target, name) {
Expand All @@ -16,7 +16,7 @@ export default {
select({ model, select, where, limit, joins = [] }) {
return new Promise((resolve, reject) => {
const options = {
sql: `SELECT ${select ? select : '*'} FROM ${model.tableName()}${where ? ` WHERE ${connection.escape(where)}` : ''}${this.getJoins(joins)}${limit ? ` LIMIT ${connection.escape(limit)}` : ''}`,
sql: `SELECT ${select ? select : '*'} FROM ${model.tableName()}${where ? ` WHERE ${connection.escape(where)}` : ''}${this.getJoins(joins)}${limit ? ` LIMIT ${connection.escape(limit)}` : ''}`,
nestTables: joins.length > 0 ? true : false
}

Expand Down
4 changes: 2 additions & 2 deletions modules/orm/model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import adapter from './adapters'
import { getTableName, getFieldName } from './global/get-name'
import { getTableName, getFieldName } from '../global/get-name'

if(!adapter) throw new Error('You must specify process.env.DB_DRIVER before creating a model.')

Expand All @@ -25,7 +25,7 @@ export default class Model {
}

/* query builder methods
*
*
* these methods return a builder instance so that you can chain methods
*
*/
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.10",
"description": "",
"bin": {
"make:migration": "dist/migrations/cli.js"
"relation": "dist/cli/index.js"
},
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -33,6 +33,7 @@
"dotenv": "^2.0.0",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^3.1.2",
"mysql": "^2.11.1"
"mysql": "^2.11.1",
"sinon": "^1.17.6"
}
}
3 changes: 2 additions & 1 deletion tests/setup/local-setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config()
require('babel-polyfill')
import { expect } from 'chai'
import sinon from 'sinon'

global.expect = expect

global.sinon = sinon
4 changes: 3 additions & 1 deletion tests/setup/setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require('babel-polyfill')
import { expect } from 'chai'
import sinon from 'sinon'

global.expect = expect
global.expect = expect
global.sinon = sinon
27 changes: 27 additions & 0 deletions tests/unit/cli/models/make.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Make from '../../../../modules/cli/models/make'

describe('Models', () => {
it('makes a model', () => {

let fs = {
mkdirSync: sinon.spy(),
writeFile: sinon.spy(),
}

Make({ args: ['Test'], cwd: 'blah', fs })

expect(fs.mkdirSync.calledOnce).to.equal(true)
expect(fs.writeFile.calledOnce).to.equal(true)
expect(fs.mkdirSync.args[0][0]).to.equal('blah/models')
expect(fs.writeFile.args[0][0]).to.equal('blah/models/test.js')

let error = new Error('failed creating file')
try {
fs.writeFile.args[0][2](error)
}
catch(e) {
fs.writeFile.args[0][2]()
expect(e.message).to.equal('failed creating file')
}
})
})
4 changes: 2 additions & 2 deletions tests/unit/general/get-name.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTableName, getFieldName } from '../../../modules/orm/global/get-name'
import { getTableName, getFieldName } from '../../../modules/global/get-name'

describe('get-name', () => {
it('gets the plural table name', () => {
Expand All @@ -13,4 +13,4 @@ describe('get-name', () => {
expect(getFieldName({})).to.equal(null)
expect(getTableName({})).to.equal(null)
})
})
})

0 comments on commit 1042851

Please sign in to comment.