test: view collection as through model (#2336)
This commit is contained in:
parent
e8c9164b5d
commit
1b33755342
@ -18,8 +18,14 @@ pgOnly()('', () => {
|
||||
await db.close();
|
||||
});
|
||||
|
||||
it('should skip on delete on view collection', async () => {
|
||||
const Order = db.collection({
|
||||
describe('view as through table', () => {
|
||||
let Order;
|
||||
let OrderItem;
|
||||
let Item;
|
||||
let OrderItemView;
|
||||
|
||||
beforeEach(async () => {
|
||||
Order = db.collection({
|
||||
name: 'orders',
|
||||
fields: [
|
||||
{
|
||||
@ -35,7 +41,7 @@ pgOnly()('', () => {
|
||||
],
|
||||
});
|
||||
|
||||
const OrderItem = db.collection({
|
||||
OrderItem = db.collection({
|
||||
name: 'orderItems',
|
||||
timestamps: false,
|
||||
fields: [
|
||||
@ -59,7 +65,7 @@ pgOnly()('', () => {
|
||||
],
|
||||
});
|
||||
|
||||
const Item = db.collection({
|
||||
Item = db.collection({
|
||||
name: 'items',
|
||||
fields: [{ name: 'name', type: 'string' }],
|
||||
});
|
||||
@ -71,11 +77,11 @@ pgOnly()('', () => {
|
||||
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`;
|
||||
const viewSQL = `CREATE VIEW ${viewName} as SELECT order_item.order_id as order_id, order_item.item_id as item_id, items.name as item_name FROM ${OrderItem.quotedTableName()} as order_item INNER JOIN ${Item.quotedTableName()} as items ON order_item.item_id = items.id`;
|
||||
|
||||
await db.sequelize.query(viewSQL);
|
||||
|
||||
const OrderItemView = db.collection({
|
||||
OrderItemView = db.collection({
|
||||
name: viewName,
|
||||
view: true,
|
||||
schema: db.inDialect('postgres') ? 'public' : undefined,
|
||||
@ -96,7 +102,7 @@ pgOnly()('', () => {
|
||||
|
||||
Order.setField('items', {
|
||||
type: 'belongsToMany',
|
||||
target: 'orderItems',
|
||||
target: 'items',
|
||||
through: viewName,
|
||||
foreignKey: 'order_id',
|
||||
otherKey: 'item_id',
|
||||
@ -107,7 +113,7 @@ pgOnly()('', () => {
|
||||
|
||||
await db.sync();
|
||||
|
||||
const order1 = await db.getRepository('orders').create({
|
||||
await db.getRepository('orders').create({
|
||||
values: {
|
||||
name: 'order1',
|
||||
orderItems: [
|
||||
@ -126,6 +132,10 @@ pgOnly()('', () => {
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should skip on delete on view collection', async () => {
|
||||
const order1 = await db.getRepository('orders').findOne({});
|
||||
|
||||
const item1 = await db.getRepository('items').findOne({
|
||||
filter: {
|
||||
@ -145,6 +155,20 @@ pgOnly()('', () => {
|
||||
expect(error).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should filter by view collection as through table', async () => {
|
||||
const orders = await db.getRepository('orders').find({
|
||||
appends: ['items'],
|
||||
filter: {
|
||||
items: {
|
||||
name: 'not exists',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(orders).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
it('should update view collection', async () => {
|
||||
const UserCollection = db.collection({
|
||||
name: 'users',
|
||||
|
Loading…
Reference in New Issue
Block a user