From 104285113c55e961b202565c74dc7dcb8f6b33f9 Mon Sep 17 00:00:00 2001 From: Zach Silveira Date: Tue, 8 Nov 2016 10:28:47 -0500 Subject: [PATCH] Add migration / model cli commands --- .gitignore | 3 ++- .npmignore | 3 ++- README.md | 25 ++++++++++++++++++++++--- modules/cli/index.js | 13 +++++++++++++ modules/cli/models/make.js | 21 +++++++++++++++++++++ modules/{orm => }/global/get-name.js | 2 +- modules/migrations/cli.js | 3 --- modules/orm/adapters/mysql/index.js | 4 ++-- modules/orm/model.js | 4 ++-- package.json | 5 +++-- tests/setup/local-setup.js | 3 ++- tests/setup/setup.js | 4 +++- tests/unit/cli/models/make.js | 27 +++++++++++++++++++++++++++ tests/unit/general/get-name.js | 4 ++-- 14 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 modules/cli/index.js create mode 100644 modules/cli/models/make.js rename modules/{orm => }/global/get-name.js (91%) delete mode 100644 modules/migrations/cli.js create mode 100644 tests/unit/cli/models/make.js diff --git a/.gitignore b/.gitignore index 3f6fff7..cb267dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist -coverage \ No newline at end of file +coverage +migrations diff --git a/.npmignore b/.npmignore index 4321cc1..7b98df3 100644 --- a/.npmignore +++ b/.npmignore @@ -3,4 +3,5 @@ modules .babelrc .gitignore circle.yml -coverage \ No newline at end of file +coverage +migrations diff --git a/README.md b/README.md index dcefe9b..1a4406a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -103,7 +103,7 @@ import { Model } from 'relation' export default class User extends Model { - + } export default class Chat extends Model { @@ -134,7 +134,7 @@ export default class User extends Model { } export default class Chat extends Model { - + } let user = await User.first() @@ -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 diff --git a/modules/cli/index.js b/modules/cli/index.js new file mode 100644 index 0000000..34c89d1 --- /dev/null +++ b/modules/cli/index.js @@ -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 +}) diff --git a/modules/cli/models/make.js b/modules/cli/models/make.js new file mode 100644 index 0000000..3a51ae2 --- /dev/null +++ b/modules/cli/models/make.js @@ -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) +} diff --git a/modules/orm/global/get-name.js b/modules/global/get-name.js similarity index 91% rename from modules/orm/global/get-name.js rename to modules/global/get-name.js index c8919e4..ec26934 100644 --- a/modules/orm/global/get-name.js +++ b/modules/global/get-name.js @@ -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 } diff --git a/modules/migrations/cli.js b/modules/migrations/cli.js deleted file mode 100644 index 5479700..0000000 --- a/modules/migrations/cli.js +++ /dev/null @@ -1,3 +0,0 @@ -#! /usr/bin/env node -console.log("console.log output") - \ No newline at end of file diff --git a/modules/orm/adapters/mysql/index.js b/modules/orm/adapters/mysql/index.js index b3cfbda..54a118a 100644 --- a/modules/orm/adapters/mysql/index.js +++ b/modules/orm/adapters/mysql/index.js @@ -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) { @@ -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 } diff --git a/modules/orm/model.js b/modules/orm/model.js index b578595..197d50d 100644 --- a/modules/orm/model.js +++ b/modules/orm/model.js @@ -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.') @@ -25,7 +25,7 @@ export default class Model { } /* query builder methods - * + * * these methods return a builder instance so that you can chain methods * */ diff --git a/package.json b/package.json index 1c81265..a18e0b7 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" } } diff --git a/tests/setup/local-setup.js b/tests/setup/local-setup.js index d5de558..cc27b0a 100644 --- a/tests/setup/local-setup.js +++ b/tests/setup/local-setup.js @@ -1,6 +1,7 @@ require('dotenv').config() require('babel-polyfill') import { expect } from 'chai' +import sinon from 'sinon' global.expect = expect - +global.sinon = sinon diff --git a/tests/setup/setup.js b/tests/setup/setup.js index 1f5242b..d46edef 100644 --- a/tests/setup/setup.js +++ b/tests/setup/setup.js @@ -1,4 +1,6 @@ require('babel-polyfill') import { expect } from 'chai' +import sinon from 'sinon' -global.expect = expect \ No newline at end of file +global.expect = expect +global.sinon = sinon diff --git a/tests/unit/cli/models/make.js b/tests/unit/cli/models/make.js new file mode 100644 index 0000000..4f5f216 --- /dev/null +++ b/tests/unit/cli/models/make.js @@ -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') + } + }) +}) diff --git a/tests/unit/general/get-name.js b/tests/unit/general/get-name.js index 6d7291f..a0dbd97 100644 --- a/tests/unit/general/get-name.js +++ b/tests/unit/general/get-name.js @@ -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', () => { @@ -13,4 +13,4 @@ describe('get-name', () => { expect(getFieldName({})).to.equal(null) expect(getTableName({})).to.equal(null) }) -}) \ No newline at end of file +})