Comparing version 1.5.1 to 1.6.0
52
model.js
@@ -61,2 +61,8 @@ const Immutable = require('immutable'); | ||
relation.dependent = relation.dependent === true; | ||
} else if (relation.has_one) { | ||
relation.many = false; | ||
relation.type = 'has_one'; | ||
relation.table = relation.table || Inflect.pluralize(relation.has_one); | ||
relation.key = relation.foreign_key || `${Inflect.singularize(this.table_name)}_id`; | ||
relation.dependent = relation.dependent === true; | ||
} | ||
@@ -468,2 +474,4 @@ | ||
return this._saveBelongsToRelation(model, relation, property_value, options); | ||
case 'has_one': | ||
return this._saveHasOneRelation(model, relation, property_value, options); | ||
case 'has_and_belongs_to_many': | ||
@@ -552,2 +560,39 @@ return this._saveHasAndBelongsToManyRelation(model, relation, property_value, options); | ||
_saveHasOneRelation(model, relation, related_object, options) { | ||
model = model.toJS ? model.toJS() : model; | ||
// Get or make a Klein Model for the related table | ||
const RelatedModel = this.klein.model(relation.table); | ||
// find any objects that have already been persisted | ||
let new_related_objects_ids = [related_object].map(r => r.id).filter(id => id && typeof id !== 'undefined'); | ||
// Unset any objects that have this model as their relation id | ||
return this.knex(relation.table, options) | ||
.where(relation.key, model.id) | ||
.whereNotIn('id', new_related_objects_ids) | ||
.update({ [relation.key]: null }) | ||
.then(() => { | ||
// Find any related objects that are already in the database | ||
return this.knex(relation.table, options) | ||
.select('id') | ||
.whereIn('id', new_related_objects_ids) | ||
.then(existing_related_ids => { | ||
related_object[relation.key] = model.id; | ||
// save/update the related object (which will then in turn save any relations on itself) | ||
return RelatedModel.save( | ||
related_object, | ||
Object.assign({}, options, { | ||
exists: new_related_objects_ids.includes(related_object.id) | ||
}) | ||
); | ||
}).then(saved_related_object => { | ||
return { | ||
name: relation.name, | ||
value: saved_related_object | ||
}; | ||
}); | ||
}); | ||
} | ||
_saveHasAndBelongsToManyRelation(model, relation, related_objects, options) { | ||
@@ -682,3 +727,3 @@ model = model.toJS ? model.toJS() : model; | ||
}); | ||
} else if (relation.type === 'one') { | ||
} else if (relation.type === 'has_one') { | ||
// Not sure when this would ever be used | ||
@@ -688,3 +733,2 @@ return this.knex(relation.table, options) | ||
.whereIn(relation.key, ids) | ||
.limit(1) | ||
.then(related_rows => { | ||
@@ -726,2 +770,6 @@ related_rows = related_rows.map(r => Object.assign({}, r)); | ||
case 'has_one': | ||
result[relation.name] = relation.rows.find(r => r[relation.properties.key] === result.id); | ||
break; | ||
case 'has_and_belongs_to_many': | ||
@@ -728,0 +776,0 @@ // Make a list of rows that match up with the result |
{ | ||
"name": "klein", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "A small ORM that combines ImmutableJS and knex", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
165031
1276