Skip to content

Commit

Permalink
Merge branch 'release-2.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jan 11, 2016
2 parents 0e8156e + 389f9c9 commit 2bbd87f
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adonis-lucid",
"version": "2.0.1",
"version": "2.0.2",
"description": "Beautifully crafted sql ORM for adonis framework",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,7 +28,7 @@
"adonis-fold": "^2.0.0"
},
"dependencies": {
"auto-loader": "^0.2.0",
"auto-loader": "git+https://github.com/thetutlage/node-auto-loader.git",
"cat-log": "^1.0.0",
"change-case": "^2.3.0",
"harmony-reflect": "^1.4.2",
Expand Down
28 changes: 25 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
# Lucid

![travis-build](https://img.shields.io/travis/adonisjs/adonis-lucid.svg)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
[![Coverage Status](https://coveralls.io/repos/adonisjs/adonis-lucid/badge.svg?branch=master&service=github)](https://coveralls.io/github/adonisjs/adonis-lucid?branch=master)
![travis-build](https://img.shields.io/travis/adonisjs/adonis-lucid.svg?style=flat-square)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![Coverage Status](https://img.shields.io/coveralls/adonisjs/adonis-lucid/master.svg?style=flat-square)](https://coveralls.io/github/adonisjs/adonis-lucid?branch=master)
[![License](https://img.shields.io/npm/l/adonis-lucid.svg?style=flat-square)](https://opensource.org/licenses/MIT)

Lucid is adonis official ORM for Sql database and is the most expressive ORM for NodeJs.

## The MIT License
Copyright (c) 2016 Harminder Virk

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.
86 changes: 80 additions & 6 deletions src/Commands/Make.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,71 @@

const fs = require('fs')
const Ioc = require('adonis-fold').Ioc
const i = require('i')()

/**
* @description migration file startup code
* @type {String}
*/
const migrationContent = `
'use strict'
const Schema = use('Schema')
class NewSchema extends Schema {
class {{Schema}} extends Schema {
up () {
{{up}}
}
down () {
{{down}}
}
}
module.exports = NewSchema
module.exports = {{Schema}}
`

let Make = exports = module.exports = {}

Make.description = 'Create a new migration file'
Make.signature = '{name}'
Make.signature = '{name} {--table?:Name of the table you want to modify} {--create?:Name of the table you want to create}'

/**
* @description returns code block for create or
* update table.
* @method _up
* @param {String} table [description]
* @param {Boolean} createTable [description]
* @return {String} [description]
* @public
*/
Make._up = function (table, createTable) {
if (createTable) {
return `this.create('${table}', function (table) {
table.increments('id')
table.timestamps()
table.timestamp('deleted_at')
})`
}
return `this.table('${table}', function (table) {
})`
}

/**
* @description returns code block for drop or
* update table
* @method _down
* @param {String} table [description]
* @param {Boolean} createTable [description]
* @return {String} [description]
*/
Make._down = function (table, createTable) {
if (createTable) {
return `this.drop('${table}')`
}
return ''
}

/**
* @description writes file with content to a given path
Expand Down Expand Up @@ -59,13 +101,45 @@ Make.writeFile = function (migrationPath, migrationContent) {
* @return {Object}
* @public
*/
Make.handle = function * (options) {
Make.handle = function *(options, flags) {
const helpers = Ioc.make('Adonis/Src/Helpers')
const Ansi = Ioc.use('Adonis/Src/Ansi')
let className = 'NewSchema'
let isCreate = true
let tableName = '<table>'

/**
* if create flag is passed, mark the process as creating
* a table
*/
if (typeof (flags.create) === 'string') {
tableName = flags.create
className = i.camelize(tableName)
}

/**
* if update flag is passed, mark the process as updating
* a table
*/
if (typeof (flags.table) === 'string') {
tableName = flags.table
className = i.camelize(tableName)
isCreate = false
}

/**
* @description replaces dynamic
* code blocks with values
* @type {String}
*/
const formattedContent = migrationContent
.replace(/{{Schema}}/g, className)
.replace('{{up}}', Make._up(tableName, isCreate))
.replace('{{down}}', Make._down(tableName, isCreate))

const name = `${new Date().getTime()}_${options.name}.js`
const migrationPath = helpers.migrationsPath(name)

yield Make.writeFile(migrationPath, migrationContent)
yield Make.writeFile(migrationPath, formattedContent)
Ansi.success(Ansi.icon('success') + ' created %s migration successfully', name)
}
15 changes: 12 additions & 3 deletions src/Commands/Rollback.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const autoLoader = require('auto-loader')
const Ioc = require('adonis-fold').Ioc
const _ = require('lodash')

let Rollback = exports = module.exports = {}

Expand All @@ -23,7 +24,7 @@ Rollback.signature = '{--force?}'
* @return {Object}
* @public
*/
Rollback.handle = function * (options, flags) {
Rollback.handle = function *(options, flags) {
const Helpers = Ioc.make('Adonis/Src/Helpers')
const Runner = Ioc.make('Adonis/Src/Runner')
const Ansi = Ioc.use('Adonis/Src/Ansi')
Expand All @@ -32,9 +33,17 @@ Rollback.handle = function * (options, flags) {
if (process.env.NODE_ENV === 'production' && !flags.force) {
throw new Error('Cannot run migrations in production')
}
const migrationsFiles = autoLoader.load(migrations)
const response = yield Runner.down(migrationsFiles)

/**
* filters only files ending with .js
*/
const migrationsFiles = _.object(_.compact(_.map(autoLoader.load(migrations), function (file, name) {
if (name.endsWith('.js')) {
return [name.replace('.js', ''), file]
}
})))

const response = yield Runner.down(migrationsFiles)
if (response.status === 'completed') {
Ansi.success(Ansi.icon('success') + ' latest migrations batch has been rolled back')
}
Expand Down
15 changes: 12 additions & 3 deletions src/Commands/Run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const autoLoader = require('auto-loader')
const Ioc = require('adonis-fold').Ioc
const _ = require('lodash')

let Run = exports = module.exports = {}

Expand All @@ -23,7 +24,7 @@ Run.signature = '{--force?}'
* @return {Object}
* @public
*/
Run.handle = function * (options, flags) {
Run.handle = function *(options, flags) {
const Helpers = Ioc.make('Adonis/Src/Helpers')
const Runner = Ioc.make('Adonis/Src/Runner')
const Ansi = Ioc.use('Adonis/Src/Ansi')
Expand All @@ -32,9 +33,17 @@ Run.handle = function * (options, flags) {
if (process.env.NODE_ENV === 'production' && !flags.force) {
throw new Error('Cannot run migrations in production')
}
const migrationsFiles = autoLoader.load(migrations)
const response = yield Runner.up(migrationsFiles)

/**
* filters only files ending with .js
*/
const migrationsFiles = _.object(_.compact(_.map(autoLoader.load(migrations), function (file, name) {
if (name.endsWith('.js')) {
return [name.replace('.js', ''), file]
}
})))

const response = yield Runner.up(migrationsFiles)
if (response.status === 'completed') {
Ansi.success(Ansi.icon('success') + ' database migrated successfully')
}
Expand Down
Loading

0 comments on commit 2bbd87f

Please sign in to comment.