Skip to content

Commit a43759c

Browse files
committed
Added a way to get new query chain when previous query chain is pending
1 parent 28ac51d commit a43759c

File tree

6 files changed

+134
-18
lines changed

6 files changed

+134
-18
lines changed

src/Orm/Proxy/Model/helpers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ helpers.rowTimeStamp = function (row, keys) {
8989
})
9090
return row
9191
}
92+
93+
94+
helpers.isFetched = function (target) {
95+
return _.size(target.connection._statements) > 0
96+
}

src/Orm/Proxy/Model/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class Model {
6363
* @return {Promise}
6464
*/
6565
update (values) {
66+
if(!helpers.isFetched(this)){
67+
throw new Error(`You cannot update a fresh model instance , trying fetching one using find method`)
68+
}
6669
let isMutated = !values
6770
values = values || this.attributes
6871
return this.constructor.update(values, isMutated, this.connection)
@@ -75,6 +78,9 @@ class Model {
7578
* @return {Promise}
7679
*/
7780
delete () {
81+
if(!helpers.isFetched(this)){
82+
throw new Error(`You cannot delete a fresh model instance , trying fetching one using find method`)
83+
}
7884
return this.constructor.delete(this.connection)
7985
}
8086

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

src/Orm/Proxy/Static/database.temporary.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ let Config = {
1616
client: 'sqlite3',
1717
connection: {
1818
filename: path.join(__dirname, '../../../../test/unit/storage/test.sqlite3')
19-
}
19+
},
20+
debug: false
2021
}
2122
}
2223
}

src/Orm/Proxy/Static/hijacker.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ hijacker.then = function (target,name,cb) {
2727

2828
return target.activeConnection[name](function (values) {
2929

30+
/**
31+
* here we empty query chain after returning
32+
* all data, it is required otherwise old
33+
* methods will be called while making a
34+
* new query
35+
*/
36+
target.new()
3037
/**
3138
* here we set visibility of values fetched
3239
* from model query.
@@ -39,17 +46,7 @@ hijacker.then = function (target,name,cb) {
3946
*/
4047
values = helpers.mutateValues(target, values)
4148

42-
cb(values)
43-
44-
}).finally(function () {
45-
46-
/**
47-
* here we empty query chain after returning
48-
* all data, it is required otherwise old
49-
* methods will be called while making a
50-
* new query
51-
*/
52-
target.new()
49+
return cb(values)
5350

5451
})
5552
}

src/Orm/Proxy/Static/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ class StaticProxy {
2121
* @see addons.create
2222
*/
2323
Model.create = function (values, isMutated, connection) {
24-
return addons.create(Model, values, isMutated, connection)
24+
return addons.create(this, values, isMutated, connection)
2525
}
2626

2727
/**
2828
* @function update
2929
* @see addons.update
3030
*/
3131
Model.update = function (values, isMutated, connection) {
32-
return addons.update(Model, values, isMutated, connection)
32+
return addons.update(this, values, isMutated, connection)
3333
}
3434

3535
/**
3636
* @function delete
3737
* @see addons.delete
3838
*/
3939
Model.delete = function (connection) {
40-
return addons.delete(Model, connection)
40+
return addons.delete(this, connection)
4141
}
4242

4343
/**
4444
* @function forceDelete
4545
* @see addons.forceDelete
4646
*/
4747
Model.forceDelete = function (connection) {
48-
return addons.forceDelete(Model, connection)
48+
return addons.forceDelete(this, connection)
4949
}
5050

5151
/**
@@ -55,8 +55,9 @@ class StaticProxy {
5555
* chain will we prepended.
5656
*/
5757
Model.new = function () {
58-
Model.disableSoftDeletes = false
59-
Model.activeConnection._statements = []
58+
this.disableSoftDeletes = false
59+
this.activeConnection._statements = []
60+
return this
6061
}
6162

6263
return new Proxy(Model, mapper)

test/unit/model.spec.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,5 +820,108 @@ describe('Model', function () {
820820

821821
})
822822

823+
it('should not be able to update a model if it was not fetched ', function () {
824+
825+
class User extends Model{
826+
static extend(){
827+
return new StaticProxy(this)
828+
}
829+
}
830+
831+
User = User.extend()
832+
const user = new User()
833+
834+
const fn = function () {
835+
return user.update()
836+
}
837+
838+
expect(fn).to.throw(/You cannot update/)
839+
840+
})
841+
842+
it('should not be able to delete a model if it was not fetched ', function () {
843+
844+
class User extends Model{
845+
static extend(){
846+
return new StaticProxy(this)
847+
}
848+
}
849+
850+
User = User.extend()
851+
const user = new User()
852+
853+
const fn = function () {
854+
return user.delete()
855+
}
856+
857+
expect(fn).to.throw(/You cannot delete/)
858+
859+
})
860+
861+
862+
it('should not be able to forceDelete a model if it was not fetched ', function () {
863+
864+
class User extends Model{
865+
static extend(){
866+
return new StaticProxy(this)
867+
}
868+
}
869+
870+
User = User.extend()
871+
const user = new User()
872+
873+
const fn = function () {
874+
return user.forceDelete()
875+
}
876+
877+
expect(fn).to.throw(/You cannot delete/)
878+
879+
})
880+
881+
it('should not use existing query chain , when values for one is fetched', function (done) {
882+
883+
class User extends Model{
884+
static extend(){
885+
return new StaticProxy(this)
886+
}
887+
}
888+
889+
let user1 = []
890+
891+
User = User.extend()
892+
893+
User
894+
.where('id',1)
895+
.then (function (user) {
896+
user1 = user
897+
return User.where('id',2)
898+
})
899+
.then (function (user) {
900+
expect(user1.first().id).to.equal(1)
901+
expect(user.first().id).to.equal(2)
902+
done()
903+
})
904+
.catch(done)
905+
906+
})
907+
908+
it('should not use existing query chain , when new function is used while building another query' , function () {
909+
910+
class User extends Model{
911+
static extend(){
912+
return new StaticProxy(this)
913+
}
914+
}
915+
916+
User = User.extend()
917+
918+
let user1 = User.where('id',1)
919+
let user2 = User.new().where('id',2)
920+
921+
expect(user1.toSQL().sql).to.equal('select * from "users" where "id" = ?')
922+
expect(user2.toSQL().sql).to.equal('select * from "users" where "id" = ?')
923+
924+
})
925+
823926

824927
})

0 commit comments

Comments
 (0)