chore(database): skip update association if through model is a view (#2173)
* chore(database): skip update association if through model is a view * chore: test
This commit is contained in:
parent
9d2a4834a5
commit
4557f99949
@ -3,6 +3,185 @@ import { Database } from '../database';
|
|||||||
import { updateAssociations } from '../update-associations';
|
import { updateAssociations } from '../update-associations';
|
||||||
import { mockDatabase } from './';
|
import { mockDatabase } from './';
|
||||||
|
|
||||||
|
describe('update belongs to many with view as through table', () => {
|
||||||
|
let db: Database;
|
||||||
|
beforeEach(async () => {
|
||||||
|
db = mockDatabase({
|
||||||
|
tablePrefix: '',
|
||||||
|
});
|
||||||
|
await db.clean({
|
||||||
|
drop: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await db.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not update through table', async () => {
|
||||||
|
const Order = db.collection({
|
||||||
|
name: 'orders',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
name: 'name',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'hasMany',
|
||||||
|
name: 'orderItems',
|
||||||
|
foreignKey: 'order_id',
|
||||||
|
target: 'orderItems',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const OrderItem = db.collection({
|
||||||
|
name: 'orderItems',
|
||||||
|
timestamps: false,
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
type: 'integer',
|
||||||
|
name: 'count',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'belongsTo',
|
||||||
|
name: 'item',
|
||||||
|
target: 'items',
|
||||||
|
foreignKey: 'item_id',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'belongsTo',
|
||||||
|
name: 'order',
|
||||||
|
target: 'orders',
|
||||||
|
foreignKey: 'order_id',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const Item = db.collection({
|
||||||
|
name: 'items',
|
||||||
|
fields: [{ name: 'name', type: 'string' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sync();
|
||||||
|
|
||||||
|
const viewName = 'order_item_view';
|
||||||
|
|
||||||
|
const dropViewSQL = `DROP VIEW IF EXISTS ${viewName}`;
|
||||||
|
await db.sequelize.query(dropViewSQL);
|
||||||
|
|
||||||
|
const viewSQL = `CREATE VIEW ${viewName} as SELECT orders.*, items.name as item_name FROM ${OrderItem.quotedTableName()} as orders INNER JOIN ${Item.quotedTableName()} as items ON orders.item_id = items.id`;
|
||||||
|
|
||||||
|
await db.sequelize.query(viewSQL);
|
||||||
|
|
||||||
|
const OrderItemView = db.collection({
|
||||||
|
name: viewName,
|
||||||
|
view: true,
|
||||||
|
schema: db.inDialect('postgres') ? 'public' : undefined,
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
type: 'bigInt',
|
||||||
|
name: 'order_id',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'bigInt',
|
||||||
|
name: 'item_id',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sync();
|
||||||
|
|
||||||
|
Order.setField('items', {
|
||||||
|
type: 'belongsToMany',
|
||||||
|
target: 'orderItems',
|
||||||
|
through: viewName,
|
||||||
|
foreignKey: 'order_id',
|
||||||
|
otherKey: 'item_id',
|
||||||
|
sourceKey: 'id',
|
||||||
|
targetKey: 'id',
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sync();
|
||||||
|
|
||||||
|
const order1 = await db.getRepository('orders').create({
|
||||||
|
values: {
|
||||||
|
name: 'order1',
|
||||||
|
orderItems: [
|
||||||
|
{
|
||||||
|
count: 1,
|
||||||
|
item: {
|
||||||
|
name: 'item1',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
count: 2,
|
||||||
|
item: {
|
||||||
|
name: 'item2',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const item1 = await db.getRepository('items').findOne({
|
||||||
|
filter: {
|
||||||
|
name: 'item1',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const item2 = await db.getRepository('items').findOne({
|
||||||
|
filter: {
|
||||||
|
name: 'item2',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.getRepository('orders').update({
|
||||||
|
filterByTk: order1.get('id'),
|
||||||
|
values: {
|
||||||
|
name: 'order1',
|
||||||
|
orderItems: [
|
||||||
|
{
|
||||||
|
count: 1,
|
||||||
|
item: {
|
||||||
|
id: item1.get('id'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
count: 2,
|
||||||
|
item: {
|
||||||
|
id: item2.get('id'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
count: 3,
|
||||||
|
item: {
|
||||||
|
name: 'item3',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
items: [
|
||||||
|
{
|
||||||
|
id: item1.get('id'),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: item2.get('id'),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const order1AfterUpdate = await db.getRepository('orders').findOne({
|
||||||
|
filter: {
|
||||||
|
name: 'order1',
|
||||||
|
},
|
||||||
|
appends: ['items'],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(order1AfterUpdate.get('items').length).toBe(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('update associations', () => {
|
describe('update associations', () => {
|
||||||
describe('belongsTo', () => {
|
describe('belongsTo', () => {
|
||||||
let db: Database;
|
let db: Database;
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import lodash from 'lodash';
|
||||||
import {
|
import {
|
||||||
Association,
|
Association,
|
||||||
BelongsTo,
|
BelongsTo,
|
||||||
@ -10,7 +11,6 @@ import {
|
|||||||
} from 'sequelize';
|
} from 'sequelize';
|
||||||
import { Model } from './model';
|
import { Model } from './model';
|
||||||
import { UpdateGuard } from './update-guard';
|
import { UpdateGuard } from './update-guard';
|
||||||
import lodash from 'lodash';
|
|
||||||
|
|
||||||
function isUndefinedOrNull(value: any) {
|
function isUndefinedOrNull(value: any) {
|
||||||
return typeof value === 'undefined' || value === null;
|
return typeof value === 'undefined' || value === null;
|
||||||
@ -377,6 +377,11 @@ export async function updateMultipleAssociation(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @ts-ignore skip update association if through model is a view
|
||||||
|
if (association.through && association.through.model.options.view) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
|
if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user