Skip to content

Commit

Permalink
Merge pull request #17 from adonisjs/develop
Browse files Browse the repository at this point in the history
Fix for issue #16
  • Loading branch information
thetutlage committed Jan 27, 2016
2 parents e7c621d + fe04529 commit fa33bd4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 80 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"mocha": "^2.3.0",
"mocha-lcov-reporter": "0.0.2",
"mysql": "^2.9.0",
"pg": "^4.4.3",
"sqlite3": "^3.1.1",
"standard": "^5.4.1"
},
Expand Down
3 changes: 1 addition & 2 deletions src/Commands/Make.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ const i = require('i')()
* @description migration file startup code
* @type {String}
*/
const migrationContent = `
'use strict'
const migrationContent = `'use strict'
const Schema = use('Schema')
Expand Down
24 changes: 22 additions & 2 deletions src/Orm/Proxy/Static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,17 @@ class StaticProxy {
values[index] = self._foreignKey[index]
})
}
return query.create(this, values, isMutated, connection)
return new Promise((resolve, reject) => {
query
.create(this, values, isMutated, connection)
.returning(this.primaryKey)
.then(function (response) {
if (response && typeof(response.indexOf) !== 'function') {
response = [response]
}
resolve(response)
}).catch(reject)
})
}

/**
Expand Down Expand Up @@ -168,7 +178,17 @@ class StaticProxy {
})
}

return query.update(this, values, isMutated, connection)
return new Promise((resolve, reject) => {
query
.update(this, values, isMutated, connection)
.returning(this.primaryKey)
.then(function (response) {
if (response && typeof(response.indexOf) !== 'function') {
response = [response]
}
resolve(response)
}).catch(reject)
})
}

/**
Expand Down
6 changes: 4 additions & 2 deletions test/unit/blueprints/model-blueprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ blueprint.setup = function(knex) {

table.increments()
table.string('username')
table.string('displayName')
table.integer('age')
table.string('status')
table.timestamps()
table.timestamp('deleted_at')

})
])
])
}

blueprint.seed = function(knex){
Expand All @@ -46,4 +48,4 @@ blueprint.seed = function(knex){
return Q.all([
knex.table('users').insert(users)
])
}
}
127 changes: 53 additions & 74 deletions test/unit/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,31 @@ const Database = require('../../src/Database')
const Model = require('../../src/Orm/Proxy/Model')
const StaticProxy = require('../../src/Orm/Proxy/Static')

process.env.TEST_PG = false
const queryAppender = process.env.TEST_PG === 'true' ? ' returning "id"' : ''

let Config = {
get: function (name) {
return {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, './storage/model.sqlite3')
},
debug: false
if (process.env.TEST_PG === 'true') {
return {
client: 'pg',
connection: {
host : process.env.PG_HOST || 'localhost',
user : process.env.PG_USER || 'postgres',
password : process.env.PG_PASSWORD || 'postgres',
database : process.env.PG_DB || 'db',
charset : 'utf8'
}
}
}
else {
return {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, './storage/model.sqlite3')
},
debug: false
}
}
}
}
Expand Down Expand Up @@ -420,7 +437,7 @@ describe('Model', function () {
})
})

it('should insert mutated values inside database using static create method', function () {
it('should insert mutated values inside database using static create method', function (done) {
class User extends Model {

static get table() {
Expand All @@ -438,8 +455,16 @@ describe('Model', function () {
}

User.database = db; User = User.extend()
let create = User.create([{username: 'FOO'}, {username: 'BAR'}])
expect(create.toSQL().bindings).deep.equal(['foo', 'bar'])
User
.create([{username: 'UNICORN'}, {username: 'SPARK'}])
.then(function () {
return User.where('username', 'unicorn').orWhere('username', 'spark').fetch()
})
.then(function (users) {
expect(users.size()).to.equal(2)
done()
})
.catch(done)
})

it('should return instance of model , when using static find method' , function (done) {
Expand Down Expand Up @@ -475,61 +500,35 @@ describe('Model', function () {
.find(1)
.then(function (user) {
user.username = 'amanvirk'
let bindings = user.update().toSQL().bindings
let hasMatched = false
bindings.forEach(function (binding) {
if (binding === 'amanvirk') {
hasMatched = true
}
})
if (!hasMatched) {
done(new Error('Unable to update name'))
} else {
done()
}
return user.update()
})
.then(function (response) {
expect(parseInt(response[0])).to.equal(1)
done()
})
.catch(done)
})

it('should be able to update rows using static update method' , function () {
class User extends Model {

static get table() {
return 'users'
}

static get timestamps() {
return false
}
}

User.database = db; User = User.extend()
let update = User.update({displayName: 'foo'})
expect(update.toSQL().sql).to.equal('update "users" set "displayName" = ?')
expect(update.toSQL().bindings).deep.equal(['foo'])
})

it('should be able to bulk update rows using static update method and use setter method return value' , function () {
it('should return row primaryKey after update' , function (done) {
class User extends Model {

static get table() {
return 'users'
}

setDisplayName( value) {
return value.toUpperCase()
}

static get timestamps() {
return false
}

}

User.database = db; User = User.extend()
let update = User.update({displayName: 'foo'})
expect(update.toSQL().sql).to.equal('update "users" set "displayName" = ?')
expect(update.toSQL().bindings).deep.equal(['FOO'])
User
.update({username: 'foo'})
.then(function (user) {
expect(user).to.be.an('array')
done()
})
.catch(done)
})

it('should be able to update values when using model instance and should not re mutate values' , function (done) {
Expand All @@ -549,19 +548,13 @@ describe('Model', function () {
User
.find(2)
.then(function (user) {
console.log(user.attributes)
user.displayName = 'baz'
const bindings = user.update().toSQL().bindings
let hasMatched = false
bindings.forEach(function (binding) {
if (binding === 'bar-baz') {
hasMatched = true
}
})
if (!hasMatched) {
done(new Error('Unable to update value , mutation cycle took place for couple of times'))
} else {
done()
}
return user.update()
})
.then(function (response) {
expect(parseInt(response[0])).to.be.a('number')
done()
}).catch(done)
})

Expand Down Expand Up @@ -778,20 +771,6 @@ describe('Model', function () {
}).catch(done)
})

it('should add created_at and updated_at timestamps when timestamps are enabled', function () {
class User extends Model {

static get table() {
return 'users'
}
}

User.database = db; User = User.extend()

const createQuery = User.create({username: 'foo'}).toSQL()
expect(createQuery.sql).to.equal('insert into "users" ("created_at", "updated_at", "username") values (?, ?, ?)')
})

it('should be able to make table name when not mentioned', function () {
class User extends Model {

Expand Down

0 comments on commit fa33bd4

Please sign in to comment.