Skip to content

Commit 2822446

Browse files
committed
some mods
1 parent 20d6eb8 commit 2822446

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/base/model.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,13 @@ ModelBase.prototype.serialize = function(options = {}) {
251251

252252
if (!shallow) {
253253

254-
let relations = mapValues(this.relations, (relation, key) =>
255-
relation.toJSON != null
256-
? relation.toJSON(options)
257-
: relation
258-
);
254+
let relations = mapValues(this.relations, (relation, key) => {
255+
if (relation.toJSON != null) {
256+
const relationAttrs = relation.toJSON(options);
257+
return (_.isEmpty(relationAttrs) && !_.isArray(relationAttrs)) ? null : relationAttrs;
258+
}
259+
return relation;
260+
});
259261

260262
// Omit null relations from the omitNew option
261263
relations = _.omitBy(relations, _.isNull);

src/model.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -976,15 +976,20 @@ const BookshelfModel = ModelBase.extend({
976976
.then(function(resp) {
977977

978978
// After a successful database save, the id is updated if the model was created
979-
if (method === 'insert' && this.id == null) {
980-
const updatedCols = {};
981-
updatedCols[this.idAttribute] = this.id = resp[0];
982-
const updatedAttrs = this.parse(updatedCols);
983-
_.assign(this.attributes, updatedAttrs);
984-
} else if (method === 'update' && resp === 0) {
979+
980+
// zube modifications. This allows us to return the full model attrs on save and update since we're using postgres.
981+
982+
if (method === 'update' && resp === 0) {
985983
if (options.require !== false) {
986984
throw new this.constructor.NoRowsUpdatedError('No Rows Updated');
987985
}
986+
} else {
987+
const returningAttrs = resp[0];
988+
const updatedCols = {};
989+
updatedCols[this.idAttribute] = this.id = returningAttrs.id;
990+
delete returningAttrs.id;
991+
const updatedAttrs = this.parse(updatedCols);
992+
_.assign(this.attributes, updatedAttrs);
988993
}
989994

990995
// In case we need to reference the `previousAttributes` for the this

src/sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ _.extend(Sync.prototype, {
191191
// Issues an `insert` command on the query - only used by models.
192192
insert: Promise.method(function() {
193193
const syncing = this.syncing;
194-
return this.query.insert(syncing.format(_.extend(Object.create(null), syncing.attributes)), syncing.idAttribute);
194+
return this.query.insert(syncing.format(_.extend(Object.create(null), syncing.attributes)), '*'); // zube mod, return '*'
195195
}),
196196

197197
// Issues an `update` command on the query - only used by models.
@@ -201,7 +201,7 @@ _.extend(Sync.prototype, {
201201
if (_.filter(query._statements, {grouping: 'where'}).length === 0) {
202202
throw new Error('A model cannot be updated without a "where" clause or an idAttribute.');
203203
}
204-
return query.update(syncing.format(_.extend(Object.create(null), attrs)));
204+
return query.update(syncing.format(_.extend(Object.create(null), attrs)), '*'); // zube mod, return '*'
205205
}),
206206

207207
// Issues a `delete` command on the query.

0 commit comments

Comments
 (0)