Comparing version 0.3.1 to 0.4.0
15
index.js
@@ -64,3 +64,3 @@ (function() { | ||
rowBase = function() { | ||
var fieldsForObject, foreignFieldsForObject, insert, update, saveManyToOne, saveManyToOnes, saveOneToMany, saveOneToManys, hash; | ||
var fieldsForObject, foreignFieldsForObject, insert, update, foreignField, saveManyToOne, saveManyToOnes, saveOneToMany, saveOneToManys, hash; | ||
fieldsForObject = function(obj) { | ||
@@ -218,6 +218,15 @@ return function() { | ||
}; | ||
foreignField = function(obj, field) { | ||
var v; | ||
v = obj[field]; | ||
if (v instanceof Function) { | ||
return obj[field](); | ||
} else { | ||
return v; | ||
} | ||
}; | ||
saveManyToOne = function(obj, field) { | ||
var value, gen24_asyncResult, foreignId; | ||
return new Promise(function(gen9_onFulfilled) { | ||
value = obj[field]; | ||
value = foreignField(obj, field); | ||
gen9_onFulfilled(Promise.resolve(function() { | ||
@@ -260,3 +269,3 @@ if (!(value instanceof Array)) { | ||
return new Promise(function(gen9_onFulfilled) { | ||
items = obj[field]; | ||
items = foreignField(obj, field); | ||
gen9_onFulfilled(Promise.resolve(function() { | ||
@@ -263,0 +272,0 @@ if (items instanceof Array) { |
{ | ||
"name": "sworm", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "a lightweight write-only ORM for MSSQL, MySQL, PostgreSQL", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -288,2 +288,25 @@ # SWORM | ||
Alternatively, we can return the people in the address using a function. When the address is saved, the `people` function will be called with the owner address as `this`, then we can set the foreign key for the people. | ||
```JavaScript | ||
var person = db.model({ table: 'people' }); | ||
var address = db.model({ table: 'addresses' }); | ||
var essert = address({ | ||
address: "15 Rue d'Essert", | ||
people: function() { | ||
return [ | ||
person({ name: 'bob', address: this }), | ||
person({ name: 'jane', address: this }) | ||
]; | ||
} | ||
}); | ||
essert.save().then(function () { | ||
// all objects saved. | ||
}); | ||
``` | ||
Notice that whether we use an array or a function, the field itself is never saved to the database, only the entities inside the array. | ||
In SQL: | ||
@@ -290,0 +313,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
73867
771
406