Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 4d38eed

Browse files
authored
feat(saveAll): allow saving an array of ids (#20)
* feat(saveAll): allow saving an array of ids
1 parent be79cfe commit 4d38eed

File tree

6 files changed

+106
-14
lines changed

6 files changed

+106
-14
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,22 @@ user.saveAll({
174174
})
175175
```
176176

177+
You can also save an array of ids with hasMany or belongsToMany:
178+
179+
```js
180+
user.role = roleA
181+
user.rights = [ rightA.id, rightB.id ]
182+
183+
user.saveAll({
184+
role: true,
185+
rights: true
186+
})
187+
```
188+
189+
Note: be careful that user.rights will be repopulated with rightA and rightB, but those objects will not
190+
be the same as the original rightA and rightB.
191+
That means `rightA.User_id` is undefined (because saveAll knows nothing about it), but `user.rights[0].User_id` will be set.
192+
177193
Tree has the same possibilities that you have in `embed`
178194

179195
### pivot data

lib/model/saveAll.belongsToMany.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const debug = require('debug')('requelize:model:saveAll')
22
const generateJoinId = require('./util/generateJoinId')
33

4-
function baseSaveAll (Model, inst, requelize, key, join, RelModel, subTree, subInst) {
4+
function baseSaveAll (Model, inst, requelize, key, join, RelModel, subTree, subInst_) {
55
debug(`saving belongsToMany ${Model._name}:${key} -> ${RelModel._name}`)
6-
subInst = new RelModel(subInst)
6+
const subInst = (typeof subInst_ === 'string') ? RelModel.get(subInst_) : Promise.resolve(new RelModel(subInst_))
77

8-
return subInst.saveAll(subTree)
9-
.then(() => {
8+
return subInst
9+
.then(subInst => subInst.saveAll(subTree))
10+
.then((subInst) => {
1011
const joinId = generateJoinId(Model, RelModel, inst, subInst)
1112
const joinRel = new join.JoinModel({
1213
id: joinId,

lib/model/saveAll.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,21 @@ module.exports = function (Model, inst, requelize, tree) {
4949
// hasMany
5050
if (Array.isArray(inst[join.field])) {
5151
debug(`saving hasMany ${Model._name}:${key} -> ${RelModel._name}`)
52-
return Promise
53-
.all(
54-
inst[join.field].map((subInst) => {
55-
subInst = new RelModel(subInst)
5652

53+
const saverInstsOrIds = inst[join.field].map((subInst) => {
54+
return (typeof subInst === 'string') ? RelModel.get(subInst) : Promise.resolve(new RelModel(subInst))
55+
})
56+
57+
return Promise
58+
.all(saverInstsOrIds)
59+
.then((subInsts) => {
60+
return Promise.all(subInsts.map(subInst => {
5761
debug(`adding ${Model._name}:${join.foreignKey} = ${RelModel._name}:${RelModel._options.primaryKey}`)
5862
subInst[join.foreignKey] = inst[Model._options.primaryKey]
5963

60-
return subInst
61-
.saveAll(subTree)
62-
.then(() => subInst)
63-
})
64-
)
64+
return subInst.saveAll(subTree)
65+
}))
66+
})
6567
.then((subInsts) => {
6668
inst[join.field] = subInsts
6769
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "requelize",
3-
"version": "0.7.2",
3+
"version": "0.8.0",
44
"description": "RethinkDB ORM",
55
"main": "index.js",
66
"repository": "https://github.com/buckless/requelize.git",

test/test.pivot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ test('instance - pivot', (t) => {
3838
t.equal('pivot', res[0].bars[0].getPivot().data, 'has pivot data')
3939
})
4040
.catch((err) => {
41+
console.log(err)
4142
t.fail(err)
4243
})
4344
.then(() => {

test/test.saveAll.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,78 @@ test('instance - saveAll', (t) => {
9494
})
9595
})
9696

97+
test('instance - saveAll with ids only', (t) => {
98+
t.plan(6)
99+
100+
let Foo
101+
let Baz
102+
let Boz
103+
let foo
104+
let baz
105+
let boz
106+
107+
dropDb()
108+
.then(() => {
109+
Foo = requelize.model('foo', {
110+
name: Joi.string()
111+
})
112+
113+
Baz = requelize.model('baz', {
114+
name: Joi.string()
115+
})
116+
117+
Boz = requelize.model('boz', {
118+
name: Joi.string()
119+
})
120+
121+
Foo.hasMany('baz', 'bazes', 'foo_id')
122+
Baz.belongsTo('foo', 'foo', 'foo_id')
123+
124+
Foo.belongsToMany('boz', 'bozes')
125+
Boz.belongsToMany('foo', 'foos')
126+
127+
return requelize.sync()
128+
})
129+
.then(() => {
130+
baz = new Baz({ name: 'baz' })
131+
boz = new Boz({ name: 'boz' })
132+
133+
return Promise.all([ baz.save(), boz.save() ])
134+
})
135+
.then(() => {
136+
foo = new Foo({ name: 'foo' })
137+
138+
foo.bazes = [ baz.id ]
139+
foo.bozes = [ boz.id ]
140+
141+
return foo.saveAll({
142+
bazes: true,
143+
bozes: true
144+
})
145+
})
146+
.then(() => {
147+
t.equal('string', typeof foo.id, 'foo is saved')
148+
t.equal('string', typeof baz.id, 'baz is saved')
149+
t.equal('string', typeof boz.id, 'boz is saved')
150+
151+
// Be careful here : foo.bazes[0] is not the same object as baz because it was recreated from the id
152+
// That means baz.foo_id will be undefined whereas foo.bazes[0].foo_id is set
153+
t.equal(foo.id, foo.bazes[0].foo_id, 'hasMany relationship')
154+
155+
return Foo.embed({ bozes: true })
156+
})
157+
.then((res) => {
158+
t.equal(true, Array.isArray(res[0].bozes), 'belongsToMany relationship')
159+
t.equal(1, res[0].bozes.length, 'belongsToMany relationship')
160+
})
161+
.catch((err) => {
162+
t.fail(err)
163+
})
164+
.then(() => {
165+
t.end()
166+
})
167+
})
168+
97169
test('instance - resave', (t) => {
98170
t.plan(2)
99171

0 commit comments

Comments
 (0)