Commit 02a03d15 authored by Chareice's avatar Chareice
Browse files

feat: reverseAssociationPair with HasMany && HasOne

parent 0d4add54
Showing with 101 additions and 12 deletions
+101 -12
import { mockDatabase } from '../index';
import Database from '../../database';
describe('create with hasMany', () => {
let db: Database;
let Post;
let User;
afterEach(async () => {
await db.close();
});
beforeEach(async () => {
db = mockDatabase();
await db.clean({ drop: true });
User = db.collection({
name: 'users',
fields: [
{
type: 'string',
name: 'name',
},
{
type: 'hasMany',
name: 'posts',
},
],
});
Post = db.collection({
name: 'posts',
fields: [
{
type: 'string',
name: 'title',
},
{
type: 'belongsTo',
name: 'user',
},
],
});
await db.sync();
});
it('should save associations with reverseField value', async () => {
const u1 = await db.getRepository('users').create({
values: {
name: 'u1',
posts: [{ title: 't1', user: null }],
},
});
const p1 = await db.getRepository('posts').findOne();
// @ts-ignore
expect(await p1.getUser()).not.toBeNull();
});
});
describe('create with belongsToMany', () => {
let db: Database;
let Post;
......
......@@ -165,16 +165,32 @@ export async function updateAssociations(instance: Model, values: any, options:
}
}
function isReverseAssociationPair(a: Association, b: Association) {
if (a.associationType == 'BelongsToMany' && b.associationType == 'BelongsToMany') {
// @ts-ignore
if (
(a as any).through.tableName === (b as any).through.tableName &&
function isReverseAssociationPair(a: any, b: any) {
const typeSet = new Set();
typeSet.add(a.associationType);
typeSet.add(b.associationType);
if (typeSet.size == 1 && typeSet.has('BelongsToMany')) {
return (
a.through.tableName === b.through.tableName &&
a.target.name === b.source.name &&
b.target.name === a.source.name
) {
return true;
}
b.target.name === a.source.name &&
a.foreignKey === b.otherKey &&
a.sourceKey === b.targetKey &&
a.otherKey === b.foreignKey &&
a.targetKey === b.sourceKey
);
}
if ((typeSet.has('HasOne') && typeSet.has('BelongsTo')) || (typeSet.has('HasMany') && typeSet.has('BelongsTo'))) {
const sourceAssoc = a.associationType == 'BelongsTo' ? b : a;
const targetAssoc = sourceAssoc == a ? b : a;
return (
sourceAssoc.source.name === targetAssoc.target.name &&
sourceAssoc.foreignKey === targetAssoc.foreignKey &&
sourceAssoc.sourceKey === targetAssoc.targetKey
);
}
return false;
......@@ -300,7 +316,12 @@ export async function updateSingleAssociation(
await instance.update(value, { ...options, transaction });
}
await updateAssociations(instance, value, { ...options, transaction, updateAssociationValues: keys });
await updateAssociations(instance, value, {
...options,
transaction,
associationContext: association,
updateAssociationValues: keys,
});
model.setDataValue(key, instance);
if (!options.transaction) {
await transaction.commit();
......@@ -310,7 +331,12 @@ export async function updateSingleAssociation(
}
const instance = await model[createAccessor](value, { context, transaction });
await updateAssociations(instance, value, { ...options, transaction, updateAssociationValues: keys });
await updateAssociations(instance, value, {
...options,
transaction,
associationContext: association,
updateAssociationValues: keys,
});
model.setDataValue(key, instance);
// @ts-ignore
if (association.targetKey) {
......@@ -412,7 +438,12 @@ export async function updateMultipleAssociation(
if (isUndefinedOrNull(item[pk])) {
// create new record
const instance = await model[createAccessor](item, accessorOptions);
await updateAssociations(instance, item, { ...options, transaction, updateAssociationValues: keys });
await updateAssociations(instance, item, {
...options,
transaction,
associationContext: association,
updateAssociationValues: keys,
});
list3.push(instance);
} else {
// set & update record
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment