Skip to content

Commit

Permalink
not hijacking then method anymore , it causes issues while creating r…
Browse files Browse the repository at this point in the history
…ecords
  • Loading branch information
thetutlage committed Sep 8, 2015
1 parent 49538bc commit 9fd225c
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 36 deletions.
25 changes: 23 additions & 2 deletions src/Orm/Proxy/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Model {
/**
* creating an isoloted database instance using Database provider
*/
this.connection = Database.table(this.constructor.table)
this.connection = this.constructor.database.table(this.constructor.table)

/**
* returning proxied model instance , it helps in having
Expand Down Expand Up @@ -156,8 +156,29 @@ class Model {
return 'id'
}

/**
* @function extend
* @description extending static interface of class via StaticProxy
* @return {Object}
*/
static extend(){
return new StaticProxy(this,Database)
return new StaticProxy(this,this.database)
}

/**
* @getter
* database instance for this model
*/
static get database(){
return this._database || Database;
}

/**
* @setter
* database instance for this model
*/
static set database(database){
this._database = database
}

}
Expand Down
25 changes: 19 additions & 6 deletions src/Orm/Proxy/Static/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ let addons = exports = module.exports = {}
*/
addons.create = function (target, values, isMutated, connection) {

let insertQuery = {}
connection = connection || target.activeConnection

/**
* here we use given connection or falls back to
* default connection on model static interface
*/
connection = connection || target.activeConnection

if (!isMutated) {
values = modelHelpers.mutateSetters(target.prototype, values)
Expand All @@ -34,7 +36,9 @@ addons.create = function (target, values, isMutated, connection) {
values = modelHelpers.addTimeStamps(values, ['created_at', 'updated_at'])
}

return connection.insert(values)
insertQuery = connection.insert(values)
target.new()
return connection
}

/**
Expand All @@ -49,14 +53,17 @@ addons.create = function (target, values, isMutated, connection) {
*/
addons.update = function (target, values, isMutated, connection) {
connection = connection || target.activeConnection
let updateQuery = {}

if (!isMutated) {
values = modelHelpers.mutateSetters(target.prototype, values)
}
if (target.timestamps) {
values = modelHelpers.addTimeStamps(values, ['updated_at'])
}
return connection.update(values)
updateQuery = connection.update(values)
target.new()
return updateQuery
}

/**
Expand All @@ -69,12 +76,15 @@ addons.update = function (target, values, isMutated, connection) {
*/
addons.delete = function (target, connection) {
connection = connection || target.activeConnection
let deleteQuery = {}

if (target.softDeletes) {
return connection.update(target.softDeletes, new Date())
deleteQuery = connection.update(target.softDeletes, new Date())
} else {
return connection.del()
deleteQuery = connection.del()
}
target.new()
return deleteQuery
}

/**
Expand All @@ -87,5 +97,8 @@ addons.delete = function (target, connection) {
*/
addons.forceDelete = function (target, connection) {
connection = connection || target.activeConnection
return connection.del()
let deleteQuery = {}
deleteQuery = connection.del()
target.new()
return deleteQuery
}
42 changes: 22 additions & 20 deletions src/Orm/Proxy/Static/hijacker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let hijacker = exports = module.exports = {}
* @param {String} name
* @param {Function} cb
*/
hijacker.then = function (target,name,cb) {
hijacker.fetch = function (target,name) {

/**
* checking if soft deletes are enabled and user has not
Expand All @@ -25,29 +25,31 @@ hijacker.then = function (target,name,cb) {
target.activeConnection.where(target.softDeletes, null)
}

return target.activeConnection[name](function (values) {
return new Promise (function (resolve,reject) {
target.activeConnection.then(function (values) {

/**
* here we empty query chain after returning
* all data, it is required otherwise old
* methods will be called while making a
* new query
*/
target.new()
/**
* here we set visibility of values fetched
* from model query.
*/
values = helpers.setVisibility(target, values)
/**
* here we empty query chain after returning
* all data, it is required otherwise old
* methods will be called while making a
* new query
*/
target.new()
/**
* here we set visibility of values fetched
* from model query.
*/
values = helpers.setVisibility(target, values)

/**
* finally before returning we need to mutate values
* by calling getters defined on model
*/
values = helpers.mutateValues(target, values)
/**
* finally before returning we need to mutate values
* by calling getters defined on model
*/
values = helpers.mutateValues(target, values)

return cb(values)
resolve(values)

}).catch(reject)
})
}

Expand Down
3 changes: 1 addition & 2 deletions src/Orm/Proxy/Static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class StaticProxy {
* chain will we prepended.
*/
Model.new = function () {
this.disableSoftDeletes = false
this.activeConnection._statements = []
this.activeConnection = this.database.table(this.table)
return this
}

Expand Down
8 changes: 4 additions & 4 deletions src/Orm/Proxy/Static/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ mapper.get = function (target, name) {
}

/**
* hijack then method here to return values as
* implement fetch method here to return values as
* instance of collection class
*/
if (name === 'then') {
return function (cb) {
return hijacker.then(target,name,cb)
if (name === 'fetch') {
return function () {
return hijacker.fetch(target,name)
}
}

Expand Down
74 changes: 74 additions & 0 deletions test/implementation/model.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict'

const path = require('path')
const chai = require('chai')
const expect = chai.expect
const Database = require('../../src/Database')
const Model = require('../../src/Orm/Proxy/Model')

let Env = {
get: function(){
return 'sqlite'
}
}

let Config = {
get: function(name){
return {
client: 'sqlite3',
connection: {
filename: path.join(__dirname,'./storage/blog.sqlite3')
},
debug: false
}
}
}

const db = new Database(Env,Config)

class User extends Model{

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

}

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


class Posts extends Model{
}

describe('Database Implementation', function () {

context('Users', function () {

it('should be able to create a new user using static create method', function (done) {

const userData = {
username: 'virk',
email: 'virk@adonisjs.com',
password: 'foobar'
}

let user_id = null

User
.create(userData)
.then (function (user) {
user_id = user[0]
return User.where('id',user_id).fetch()
})
.then (function (user){
expect(user.first().username).to.equal('VIRK')
done()
})
.catch(done)

})

})

})
Binary file added test/implementation/storage/blog.sqlite3
Binary file not shown.
1 change: 0 additions & 1 deletion test/unit/implementation.spec.js

This file was deleted.

11 changes: 10 additions & 1 deletion test/unit/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('Model', function () {

User
.where('id',1)
.fetch()
.then (function (values){
expect(values.first().username).to.equal(username)
done()
Expand Down Expand Up @@ -128,6 +129,7 @@ describe('Model', function () {

User
.where('id',1)
.fetch()
.then (function (values){
expect(values.first().username).to.equal(username)
done()
Expand Down Expand Up @@ -403,6 +405,7 @@ describe('Model', function () {

User
.where('id',10)
.fetch()
.then (function (result) {
expect(result.first().deleted_at).to.equal(null)
done()
Expand All @@ -428,6 +431,7 @@ describe('Model', function () {

User
.where('id',11)
.fetch()
.then (function (result) {
expect(result.size()).to.equal(0)
done()
Expand All @@ -453,6 +457,7 @@ describe('Model', function () {

User
.where('id',11)
.fetch()
.then (function (result) {
expect(result.first().id).to.equal(11)
done()
Expand Down Expand Up @@ -480,6 +485,7 @@ describe('Model', function () {
User
.withTrashed()
.where('id',11)
.fetch()
.then (function (result) {
expect(result.first().id).to.equal(11)
done()
Expand Down Expand Up @@ -675,6 +681,7 @@ describe('Model', function () {

User
.where('id',1)
.fetch()
.then (function (user){
expect(user.first().email).to.equal(undefined)
done()
Expand All @@ -701,6 +708,7 @@ describe('Model', function () {

User
.where('id',1)
.fetch()
.then (function (user){
expect(user.first().username).to.equal(undefined)
expect(user.first().age).to.be.a('number')
Expand Down Expand Up @@ -769,9 +777,10 @@ describe('Model', function () {

User
.where('id',1)
.fetch()
.then (function (user) {
user1 = user
return User.where('id',2)
return User.where('id',2).fetch()
})
.then (function (user) {
expect(user1.first().id).to.equal(1)
Expand Down
13 changes: 13 additions & 0 deletions test/unit/staticProxy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,31 @@ describe('StaticProxy', function () {
it('should return an instance of collection on values fetched from model queries', function(done) {

class User{

static extend(){
return new StaticProxy(this,Database);
}

static get database(){
return this._database
}

static set database(value){
this._database = value
}

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

User.database = Database
User = User.extend()

User
.select('*')
.first()
.fetch()
.then(function (users) {
expect(users.__actions__).deep.equal([])
done()
Expand Down

0 comments on commit 9fd225c

Please sign in to comment.