Skip to content

Commit

Permalink
feat(lucid): add incrementing flag
Browse files Browse the repository at this point in the history
when primaryKey is not incrementing, one needs to define it as an explicit flag. Which will aware

lucid to setup correct value for primaryKey and make everything work as expected

Closes #89
  • Loading branch information
thetutlage committed Jul 16, 2017
1 parent 2e0ad5a commit f9f9410
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Lucid/Model/Mixins/Persistance.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ Peristance.insert = function * () {
}
const save = yield query.insertAttributes(values).returning(this.constructor.primaryKey)
if (save[0]) {
this.$primaryKeyValue = save[0]
/**
* Since {returning} statement does not work for Sqlite3, we need to
* make use of the returning value only when {incrementing} is set
* to true.
*/
this.$primaryKeyValue = this.constructor.incrementing ? save[0] : values[this.constructor.primaryKey]
this.exists = true
this.original = _.clone(this.attributes)
}
return !!save
Expand Down Expand Up @@ -100,6 +106,7 @@ Peristance.delete = function * () {
const affected = yield query.deleteAttributes(values)
if (affected > 0) {
_.merge(this.attributes, values)
this.exists = false
this.freeze()
}
return affected
Expand Down
5 changes: 4 additions & 1 deletion src/Lucid/QueryBuilder/Serializers/Base.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class BaseSerializer {
}

/**
* converts the final result set into a custom collection
* converts the final result set into a custom collection. This method
* should never be called on newly instantiated models, whereas is
* only used when fetching models from Database.
*
* @param {Array} values [description]
* @param {Array} eagerValues [description]
Expand All @@ -81,6 +83,7 @@ class BaseSerializer {
return helpers.toCollection(values).transform((result, value, index) => {
const modelInstance = new this.queryBuilder.HostModel()
modelInstance.attributes = value
modelInstance.exists = true
modelInstance.original = _.clone(modelInstance.attributes)
this.queryBuilder.eagerLoad.mapRelationsToRow(eagerValues, modelInstance, value)
result[index] = modelInstance
Expand Down
1 change: 1 addition & 0 deletions test/unit/fixtures/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
const tables = [
knex.schema.createTable('users', function (table) {
table.increments()
table.string('uuid').defaultTo(null)
table.string('username')
table.string('firstname')
table.string('lastname')
Expand Down
6 changes: 3 additions & 3 deletions test/unit/helpers/postgresConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
default: {
client: 'pg',
connection: {
user: '',
user: 'harmindervirk',
password: '',
database: 'default'
}
Expand All @@ -19,7 +19,7 @@ module.exports = {
alternateConnection: {
client: 'pg',
connection: {
user: '',
user: 'harmindervirk',
password: '',
database: 'alternate'
}
Expand All @@ -28,7 +28,7 @@ module.exports = {
defaultPrefix: {
client: 'pg',
connection: {
user: '',
user: 'harmindervirk',
password: '',
database: 'default'
},
Expand Down

0 comments on commit f9f9410

Please sign in to comment.