Comparing version 3.5.0 to 3.6.0
11
index.js
@@ -126,2 +126,5 @@ var crypto = require("crypto"); | ||
var value = obj[field](obj); | ||
if (value && !(value instanceof Array)) { | ||
throw new Error('functions must return arrays of entities') | ||
} | ||
obj[field] = value; | ||
@@ -135,5 +138,5 @@ return value; | ||
function saveManyToOne(obj, field, options) { | ||
var value = foreignField(obj, field); | ||
var value = obj[field] | ||
if (value && !(value instanceof Array)) { | ||
if (value && !(value instanceof Array || typeof value === 'function')) { | ||
return value.save(options).then(function () { | ||
@@ -149,4 +152,2 @@ var foreignId = | ||
}); | ||
} else { | ||
return Promise.resolve(); | ||
} | ||
@@ -168,4 +169,2 @@ } | ||
})); | ||
} else { | ||
return Promise.resolve(); | ||
} | ||
@@ -172,0 +171,0 @@ } |
@@ -10,2 +10,3 @@ var optionalRequire = require('./optionalRequire'); | ||
var randomstring = require('randomstring'); | ||
var cooperative = require('cooperative') | ||
@@ -36,8 +37,4 @@ module.exports = function () { | ||
}) | ||
} else if (options.formatRows == false) { | ||
} else { | ||
return resultsPromise; | ||
} else { | ||
return resultsPromise.then(function (r) { | ||
return formatRows(r); | ||
}); | ||
} | ||
@@ -78,6 +75,9 @@ }, | ||
return fetchRows([]).then(function (rows) { | ||
return { | ||
metaData: results.metaData, | ||
rows: _.flatten(rows, true) | ||
return fetchRows([]).then(function (_rows) { | ||
var rows = _.flatten(_rows, true) | ||
if (options.formatRows === false) { | ||
return rows | ||
} else { | ||
return formatRows(results.metaData, rows) | ||
} | ||
@@ -167,9 +167,4 @@ }, function (error) { | ||
function formatRows(resultSet) { | ||
var rows = resultSet.rows; | ||
if (!rows) { | ||
return rows; | ||
} | ||
var fields = resultSet.metaData.map(function (field) { | ||
function formatRows(metadata, rows) { | ||
var fields = metadata.map(function (field) { | ||
if (/[a-z]/.test(field.name)) { | ||
@@ -186,13 +181,13 @@ return field.name; | ||
for (var r = 0; r < length; r++) { | ||
var row = {}; | ||
results[r] = row; | ||
return cooperative.forEach(rows, function(row, index) { | ||
var formattedRow = {}; | ||
results[index] = formattedRow; | ||
for (var f = 0; f < fields.length; f++) { | ||
row[fields[f]] = rows[r][f]; | ||
formattedRow[fields[f]] = row[f]; | ||
} | ||
} | ||
return results; | ||
}).then(function () { | ||
return results | ||
}) | ||
} else { | ||
return rows; | ||
return Promise.resolve(rows); | ||
} | ||
@@ -199,0 +194,0 @@ } |
{ | ||
"name": "sworm", | ||
"version": "3.5.0", | ||
"version": "3.6.0", | ||
"description": "a lightweight write-only ORM for MSSQL, MySQL, PostgreSQL, Oracle, Sqlite 3", | ||
@@ -10,2 +10,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"cooperative": "1.1.0", | ||
"debug": "2.2.0", | ||
@@ -12,0 +13,0 @@ "randomstring": "1.1.5", |
103
readme.md
@@ -12,2 +12,6 @@ # SWORM [![npm version](https://img.shields.io/npm/v/sworm.svg)](https://www.npmjs.com/package/sworm) [![npm](https://img.shields.io/npm/dm/sworm.svg)](https://www.npmjs.com/package/sworm) [![Build Status](https://travis-ci.org/featurist/sworm.svg?branch=master)](https://travis-ci.org/featurist/sworm) | ||
## Changelog | ||
See [changelog.md](changelog.md) | ||
## NPM | ||
@@ -469,2 +473,18 @@ | ||
Alternatively, you can specify the objects the other way around, the address on the outside (see [one-to-many](#one-to-many) for how this works): | ||
```js | ||
var essert = address({ | ||
address: "15 Rue d'Essert", | ||
person: (address) => [ | ||
person({ | ||
name: 'bob', | ||
address: address | ||
}) | ||
] | ||
}) | ||
essert.save() | ||
``` | ||
In SQL: | ||
@@ -510,3 +530,3 @@ | ||
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. Following the `save()` the results of the function will be saved as an array on the object. | ||
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 the first argument, then we can set the foreign key for the people. Following the `save()` the results of the function will be saved as an array on the object. | ||
@@ -519,8 +539,6 @@ ```js | ||
address: "15 Rue d'Essert", | ||
people: function(addr) { | ||
return [ | ||
person({ name: 'bob', address: addr }), | ||
person({ name: 'jane', address: addr }) | ||
]; | ||
} | ||
people: (addr) => [ | ||
person({ name: 'bob', address: addr }), | ||
person({ name: 'jane', address: addr }) | ||
] | ||
}); | ||
@@ -556,2 +574,3 @@ | ||
```js | ||
var db = sworm.db('test/test.db') | ||
var person = db.model({ table: 'people' }); | ||
@@ -561,12 +580,2 @@ var personAddress = db.model({ table: 'people_addresses', id: ['address_id', 'person_id'] }); | ||
function personLivesInAddress(person, address) { | ||
pa = personAddress({person: person, address: address}); | ||
person.addresses = person.addresses || []; | ||
person.addresses.push(pa); | ||
address.people = address.people || []; | ||
address.people.push(pa); | ||
} | ||
var bob = person({name: 'bob'}); | ||
@@ -576,12 +585,15 @@ var jane = person({name: 'jane'}); | ||
var fremantle = address({ | ||
address: "Fremantle" | ||
address: 'Fremantle', | ||
personAddresses: (address) => [ | ||
personAddress({ person: bob, address: address }), | ||
personAddress({ person: jane, address: address }) | ||
] | ||
}); | ||
var essert = address({ | ||
address: "15 Rue d'Essert" | ||
address: "15 Rue d'Essert", | ||
personAddresses: (address) => [ | ||
personAddress({ person: jane, address: address }) | ||
] | ||
}); | ||
personLivesInAddress(bob, fremantle); | ||
personLivesInAddress(jane, fremantle); | ||
personLivesInAddress(jane, essert); | ||
Promise.all([essert.save(), fremantle.save()]); | ||
@@ -614,2 +626,47 @@ ``` | ||
## Relationships Summary | ||
In summary, a relationship can be a field containing one of the following: | ||
* a sworm entity | ||
```js | ||
outer({ | ||
field: inner({ ... }) | ||
}) | ||
``` | ||
1. the entity is saved | ||
2. the ID is placed in the outer entity's `field_id` field. (See `foreignKeyFor`) | ||
3. the outer entity is saved | ||
* an array of sworm entities | ||
```js | ||
outer({ | ||
field: [ | ||
inner({ ... }), | ||
inner({ ... }) | ||
] | ||
}) | ||
``` | ||
1. the outer entity is saved | ||
2. each of the entities in the array are saved | ||
* a function that returns an array of sworm entities | ||
```js | ||
outer({ | ||
field: (outer) => [ | ||
inner({ outer: outer, ... }), | ||
inner({ outer: outer, ... }) | ||
] | ||
}) | ||
``` | ||
1. the outer entity is saved | ||
2. the function is called, passing the outer entity as the first argument | ||
3. the function returns an array of entities | ||
4. each of those entities are saved | ||
5. the array is assigned to the outer entity's field | ||
# Unescaping | ||
@@ -616,0 +673,0 @@ |
@@ -811,26 +811,2 @@ var fs = require("fs-promise"); | ||
it("can save a many to one relationship with function", function() { | ||
var bobsAddress; | ||
var bob = person({ | ||
name: "bob", | ||
address: function () { | ||
return bobsAddress = address({ | ||
address: "15, Rue d'Essert" | ||
}) | ||
} | ||
}); | ||
return bob.save().then(function() { | ||
expect(statements).to.eql([ "insert", "insert" ]); | ||
expect(bob.address).to.equal(bobsAddress); | ||
return db.query("select * from addresses").then(function(addresses) { | ||
expect(database.clean(addresses)).to.eql([{ | ||
id: bob.address_id, | ||
address: "15, Rue d'Essert" | ||
}]); | ||
}); | ||
}); | ||
}); | ||
it("can save a many to one relationship with function that returns undefined", function() { | ||
@@ -930,3 +906,17 @@ var bobsAddress; | ||
it("can save a one to many relationship with function", function() { | ||
it('throw if a non-array is returned from a function', function() { | ||
var rueDEssert = address({ | ||
address: "15, Rue d'Essert", | ||
people: function(address) { | ||
return person({ | ||
name: "bob", | ||
address: address | ||
}) | ||
} | ||
}); | ||
return expect(rueDEssert.save()).to.eventually.be.rejectedWith('must return arrays') | ||
}); | ||
it("can save a one to many relationship with function that returns an array", function() { | ||
var bob; | ||
@@ -1325,2 +1315,6 @@ var jane; | ||
}) | ||
it('can access the underlying connection', function () { | ||
expect(db.driver.connection).to.not.be.undefined | ||
}) | ||
}); | ||
@@ -1327,0 +1321,0 @@ |
@@ -256,7 +256,9 @@ if (!process.env.TRAVIS) { | ||
return db.query('select * from people', {}, {formatRows: false, outFormat: oracledb.OBJECT}).then(function (rows) { | ||
expect(rows.metaData).to.eql([ | ||
{name: 'ID'}, | ||
{name: 'NAME'}, | ||
{name: 'ADDRESS_ID'}, | ||
{name: 'PHOTO'} | ||
expect(rows).to.eql([ | ||
{ | ||
ID: bob.id, | ||
NAME: 'bob', | ||
PHOTO: null, | ||
ADDRESS_ID: null | ||
}, | ||
]); | ||
@@ -263,0 +265,0 @@ }); |
135207
31
890
4
3164
+ Addedcooperative@1.1.0
+ Addedcooperative@1.1.0(transitive)