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

Commit 030777e

Browse files
committed
fix: revert c3f93cc only one relation between same objects"
1 parent 99f07c0 commit 030777e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

lib/model/saveAll.belongsToMany.js

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

34
function baseSaveAll (Model, inst, requelize, key, join, RelModel, subTree, subInst_) {
45
debug(`saving belongsToMany ${Model._name}:${key} -> ${RelModel._name}`)
@@ -7,7 +8,9 @@ function baseSaveAll (Model, inst, requelize, key, join, RelModel, subTree, subI
78
return subInst
89
.then(subInst => subInst.saveAll(subTree))
910
.then((subInst) => {
11+
const joinId = generateJoinId(Model, RelModel, inst, subInst)
1012
const joinRel = new join.JoinModel({
13+
id: joinId,
1114
[Model._name]: inst[Model._options.primaryKey],
1215
[RelModel._name]: subInst[RelModel._options.primaryKey]
1316
})
@@ -30,6 +33,7 @@ function saveThrough (Model, inst, requelize, key, join, RelModel, subTree, subI
3033

3134
return subInst.document.saveAll(subTree)
3235
.then(() => {
36+
joinRel.id = generateJoinId(Model, RelModel, inst, subInst.document)
3337
joinRel._data[Model._name] = inst[Model._options.primaryKey]
3438
joinRel._data[RelModel._name] = subInst.document[RelModel._options.primaryKey]
3539

lib/model/util/generateJoinId.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const debug = require('debug')('requelize:model:util')
2+
3+
/**
4+
* Return join id based on two model instances
5+
* @internal
6+
* @param {Class} ModelA First model
7+
* @param {Class} ModelB Second model
8+
* @param {Model} instA First instance. Must be instance of ModelA (first argument)
9+
* @param {Model} instB Second instance. Must be instance of ModelB (second argument)
10+
* @return {string} Genreated id
11+
*/
12+
module.exports = (ModelA, ModelB, instA, instB) => {
13+
const idA = instA[ModelA._options.primaryKey]
14+
const idB = instB[ModelB._options.primaryKey]
15+
16+
debug(`generating join id between ${ModelA._name} and ${ModelB._name}`)
17+
18+
return [ModelA._name, ModelB._name]
19+
.sort((n1, n2) => n1.toLowerCase().localeCompare(n2.toLowerCase()))
20+
.map((name, index) => (index === 0) ? idA : idB)
21+
.join('_')
22+
}

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.9.1",
3+
"version": "0.9.2",
44
"description": "RethinkDB ORM",
55
"main": "index.js",
66
"repository": "https://github.com/buckless/requelize.git",

0 commit comments

Comments
 (0)