Skip to content

Commit

Permalink
Added a way to get new query chain when previous query chain is pending
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 8, 2015
1 parent 28ac51d commit a43759c
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/Orm/Proxy/Model/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,8 @@ helpers.rowTimeStamp = function (row, keys) {
})
return row
}


helpers.isFetched = function (target) {
return _.size(target.connection._statements) > 0
}
9 changes: 9 additions & 0 deletions src/Orm/Proxy/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class Model {
* @return {Promise}
*/
update (values) {
if(!helpers.isFetched(this)){
throw new Error(`You cannot update a fresh model instance , trying fetching one using find method`)
}
let isMutated = !values
values = values || this.attributes
return this.constructor.update(values, isMutated, this.connection)
Expand All @@ -75,6 +78,9 @@ class Model {
* @return {Promise}
*/
delete () {
if(!helpers.isFetched(this)){
throw new Error(`You cannot delete a fresh model instance , trying fetching one using find method`)
}
return this.constructor.delete(this.connection)
}

Expand All @@ -85,6 +91,9 @@ class Model {
* @return {Promise}
*/
forceDelete () {
if(!helpers.isFetched(this)){
throw new Error(`You cannot delete a fresh model instance , trying fetching one using find method`)
}
return this.constructor.forceDelete(this.connection)
}

Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Proxy/Static/database.temporary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ let Config = {
client: 'sqlite3',
connection: {
filename: path.join(__dirname, '../../../../test/unit/storage/test.sqlite3')
}
},
debug: false
}
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/Orm/Proxy/Static/hijacker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ hijacker.then = function (target,name,cb) {

return target.activeConnection[name](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.
Expand All @@ -39,17 +46,7 @@ hijacker.then = function (target,name,cb) {
*/
values = helpers.mutateValues(target, values)

cb(values)

}).finally(function () {

/**
* 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()
return cb(values)

})
}
Expand Down
13 changes: 7 additions & 6 deletions src/Orm/Proxy/Static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ class StaticProxy {
* @see addons.create
*/
Model.create = function (values, isMutated, connection) {
return addons.create(Model, values, isMutated, connection)
return addons.create(this, values, isMutated, connection)
}

/**
* @function update
* @see addons.update
*/
Model.update = function (values, isMutated, connection) {
return addons.update(Model, values, isMutated, connection)
return addons.update(this, values, isMutated, connection)
}

/**
* @function delete
* @see addons.delete
*/
Model.delete = function (connection) {
return addons.delete(Model, connection)
return addons.delete(this, connection)
}

/**
* @function forceDelete
* @see addons.forceDelete
*/
Model.forceDelete = function (connection) {
return addons.forceDelete(Model, connection)
return addons.forceDelete(this, connection)
}

/**
Expand All @@ -55,8 +55,9 @@ class StaticProxy {
* chain will we prepended.
*/
Model.new = function () {
Model.disableSoftDeletes = false
Model.activeConnection._statements = []
this.disableSoftDeletes = false
this.activeConnection._statements = []
return this
}

return new Proxy(Model, mapper)
Expand Down
103 changes: 103 additions & 0 deletions test/unit/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,5 +820,108 @@ describe('Model', function () {

})

it('should not be able to update a model if it was not fetched ', function () {

class User extends Model{
static extend(){
return new StaticProxy(this)
}
}

User = User.extend()
const user = new User()

const fn = function () {
return user.update()
}

expect(fn).to.throw(/You cannot update/)

})

it('should not be able to delete a model if it was not fetched ', function () {

class User extends Model{
static extend(){
return new StaticProxy(this)
}
}

User = User.extend()
const user = new User()

const fn = function () {
return user.delete()
}

expect(fn).to.throw(/You cannot delete/)

})


it('should not be able to forceDelete a model if it was not fetched ', function () {

class User extends Model{
static extend(){
return new StaticProxy(this)
}
}

User = User.extend()
const user = new User()

const fn = function () {
return user.forceDelete()
}

expect(fn).to.throw(/You cannot delete/)

})

it('should not use existing query chain , when values for one is fetched', function (done) {

class User extends Model{
static extend(){
return new StaticProxy(this)
}
}

let user1 = []

User = User.extend()

User
.where('id',1)
.then (function (user) {
user1 = user
return User.where('id',2)
})
.then (function (user) {
expect(user1.first().id).to.equal(1)
expect(user.first().id).to.equal(2)
done()
})
.catch(done)

})

it('should not use existing query chain , when new function is used while building another query' , function () {

class User extends Model{
static extend(){
return new StaticProxy(this)
}
}

User = User.extend()

let user1 = User.where('id',1)
let user2 = User.new().where('id',2)

expect(user1.toSQL().sql).to.equal('select * from "users" where "id" = ?')
expect(user2.toSQL().sql).to.equal('select * from "users" where "id" = ?')

})


})

0 comments on commit a43759c

Please sign in to comment.