Skip to content

Commit

Permalink
Models with static and instance methods are working
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 8, 2015
1 parent 9fd225c commit 4d5467b
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "iojs ./node_modules/mocha/bin/mocha --harmony_proxies test/unit --bail"
"test": "iojs ./node_modules/mocha/bin/mocha --harmony_proxies test/implementation --bail"
},
"author": "",
"license": "ISC",
Expand Down
13 changes: 12 additions & 1 deletion src/Orm/Proxy/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,21 @@ class Model {
* @return {Promise}
*/
forceDelete () {
let self = this
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)
return new Promise(function (resolve,reject) {
self
.constructor
.forceDelete(self.connection)
.then (function (response) {
self.attributes = {}
self.connection = self.constructor.database.table(self.constructor.table)
resolve(response)
})
.catch(reject)
})
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Orm/Proxy/Model/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ mapper.get = function (target, name) {
* @param {*} value
*/
mapper.set = function (target, name, value) {
if(name === 'attributes'){
target[name] = value
return true
}
const setter = helpers.mutateField(target, name)
target.attributes[name] = setter ? setter(value) : value
}
4 changes: 0 additions & 4 deletions src/Orm/Proxy/Static/hijacker.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,19 @@ hijacker.find = function (target,id) {
.where(target.primaryKey, id)
.first()
.then(function (values) {

let instance = new target(values)
instance.connection.where(target.primaryKey, id)
resolve(instance)

})
.catch(reject)
.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()

})
})
}
1 change: 1 addition & 0 deletions src/Orm/Proxy/Static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class StaticProxy {
* chain will we prepended.
*/
Model.new = function () {
this.disableSoftDeletes = false
this.activeConnection = this.database.table(this.table)
return this
}
Expand Down
160 changes: 158 additions & 2 deletions test/implementation/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ User = User.extend()
class Posts extends Model{
}

let user_id = null
let instanceUserId = null

describe('Database Implementation', function () {

context('Users', function () {
Expand All @@ -53,8 +56,6 @@ describe('Database Implementation', function () {
password: 'foobar'
}

let user_id = null

User
.create(userData)
.then (function (user) {
Expand All @@ -69,6 +70,161 @@ describe('Database Implementation', function () {

})

it('should be able to fetch all users', function (done) {

User
.select('*')
.fetch()
.then (function (users) {
users.each(function (user){
expect(user.id).not.to.be('undefined')
})
done()
})
.catch(done)

})

it('should be able to update selected user', function (done) {

User
.where('id',user_id)
.update({username:'foo'})
.then (function () {
return User.where('id',user_id).fetch()
})
.then (function (user) {
expect(user.first().username).to.equal('FOO')
done()
}).catch(done)

})

it('should be able to soft delete a given user', function (done) {

User
.where('id',user_id)
.delete()
.then (function () {
return User.where('id',user_id).fetch()
})
.then (function (user){
expect(user.size()).to.equal(0)
done()
}).catch(done)

})

it('should be able to fetch soft deleted entries', function (done) {

User
.where('id',user_id)
.delete()
.then (function () {
return User.withTrashed().where('id',user_id).fetch()
})
.then (function (user){
expect(user.first().username).to.equal('FOO')
done()
}).catch(done)

})

it('should be able to forceDelete entires', function (done) {

User
.where('id',user_id)
.forceDelete()
.then (function () {
return User.withTrashed().where('id',user_id).fetch()
})
.then (function (user){
expect(user.size()).to.equal(0)
done()
}).catch(done)

})

})

context('Database instance', function () {

it('should be able to insert values using model instance', function (done){

const user = new User({username:'virk'})

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

})

it('should be able to update values using model instance', function (done){

User
.find(instanceUserId)
.then (function (user) {
user.username = 'bar'
return user.update()
})
.then (function () {
return User.where('id',instanceUserId).fetch()
}).then(function (user) {
expect(user.first().username).to.equal('BAR')
done()
}).catch(done)

})


it('should be able to soft delete row using model instance', function (done){

User
.find(instanceUserId)
.then (function (user) {
return user.delete()
})
.then (function () {
return User.where('id',instanceUserId).fetch()
}).then(function (user) {
expect(user.size()).to.equal(0)
done()
}).catch(done)

})

it('should be able to find soft deleted values and return true while checking is softDeleted', function (done) {

User
.find(instanceUserId)
.then (function (user) {
expect(user.isTrashed()).to.equal(true)
done()
}).catch(done)

})

it('should be able to forceDelete model instance', function (done) {

User
.find(instanceUserId)
.then (function (user){
return user.forceDelete()
}).then (function () {
return User.where('id',instanceUserId).fetch()
}).then (function (user) {
expect(user.size()).to.equal(0)
done()
}).catch(done)

})

})

})
Binary file modified test/implementation/storage/blog.sqlite3
Binary file not shown.

0 comments on commit 4d5467b

Please sign in to comment.