From 6950c1a78baa37c1b0f5e05caa721c2c83671d47 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Mon, 18 Jan 2016 19:08:21 +0530 Subject: [PATCH 1/2] style(migrations): Remove extra line before use strict in migration file template --- src/Commands/Make.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Commands/Make.js b/src/Commands/Make.js index b3c60b3c..81a9b831 100644 --- a/src/Commands/Make.js +++ b/src/Commands/Make.js @@ -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') From fe045294a6764de0f62ffe99a1416fe800528d40 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 27 Jan 2016 18:14:39 +0530 Subject: [PATCH 2/2] fix(model-create): fixed #16, where returning statement is required for postgres Postgres does not return id without defining returning id statement 16 --- package.json | 1 + src/Orm/Proxy/Static/index.js | 24 ++++- test/unit/blueprints/model-blueprint.js | 6 +- test/unit/model.spec.js | 127 ++++++++++-------------- 4 files changed, 80 insertions(+), 78 deletions(-) diff --git a/package.json b/package.json index d4212049..a26c2bc6 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/Orm/Proxy/Static/index.js b/src/Orm/Proxy/Static/index.js index 40e9faa6..cca85001 100644 --- a/src/Orm/Proxy/Static/index.js +++ b/src/Orm/Proxy/Static/index.js @@ -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) + }) } /** @@ -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) + }) } /** diff --git a/test/unit/blueprints/model-blueprint.js b/test/unit/blueprints/model-blueprint.js index 34e7fbde..77090ea1 100644 --- a/test/unit/blueprints/model-blueprint.js +++ b/test/unit/blueprints/model-blueprint.js @@ -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){ @@ -46,4 +48,4 @@ blueprint.seed = function(knex){ return Q.all([ knex.table('users').insert(users) ]) -} \ No newline at end of file +} diff --git a/test/unit/model.spec.js b/test/unit/model.spec.js index f4b3d775..c84b19fa 100644 --- a/test/unit/model.spec.js +++ b/test/unit/model.spec.js @@ -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 + } } } } @@ -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() { @@ -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) { @@ -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) { @@ -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) }) @@ -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 {