@nocobase/database 0.11.1-alpha.1 → 0.11.1-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -35,6 +35,9 @@ function _referentialIntegrityCheck() {
|
|
|
35
35
|
onDelete = reference.onDelete;
|
|
36
36
|
const sourceCollection = db.collections.get(sourceCollectionName);
|
|
37
37
|
const sourceRepository = sourceCollection.repository;
|
|
38
|
+
if (sourceCollection.isView()) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
38
41
|
const filter = {
|
|
39
42
|
[sourceField]: referencedInstance[targetField]
|
|
40
43
|
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "0.11.1-alpha.
|
|
3
|
+
"version": "0.11.1-alpha.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "0.11.1-alpha.
|
|
10
|
-
"@nocobase/utils": "0.11.1-alpha.
|
|
9
|
+
"@nocobase/logger": "0.11.1-alpha.3",
|
|
10
|
+
"@nocobase/utils": "0.11.1-alpha.3",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"cron-parser": "4.4.0",
|
|
13
13
|
"dayjs": "^1.11.8",
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
30
30
|
"directory": "packages/database"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "5ed3bd7d5b16bd38d268961b34875d5cd45799ef"
|
|
33
33
|
}
|
|
@@ -18,6 +18,133 @@ pgOnly()('', () => {
|
|
|
18
18
|
await db.close();
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
+
it('should skip on delete on view collection', async () => {
|
|
22
|
+
const Order = db.collection({
|
|
23
|
+
name: 'orders',
|
|
24
|
+
fields: [
|
|
25
|
+
{
|
|
26
|
+
type: 'string',
|
|
27
|
+
name: 'name',
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: 'hasMany',
|
|
31
|
+
name: 'orderItems',
|
|
32
|
+
foreignKey: 'order_id',
|
|
33
|
+
target: 'orderItems',
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const OrderItem = db.collection({
|
|
39
|
+
name: 'orderItems',
|
|
40
|
+
timestamps: false,
|
|
41
|
+
fields: [
|
|
42
|
+
{
|
|
43
|
+
type: 'integer',
|
|
44
|
+
name: 'count',
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: 'belongsTo',
|
|
48
|
+
name: 'item',
|
|
49
|
+
target: 'items',
|
|
50
|
+
foreignKey: 'item_id',
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: 'belongsTo',
|
|
54
|
+
name: 'order',
|
|
55
|
+
target: 'orders',
|
|
56
|
+
foreignKey: 'order_id',
|
|
57
|
+
onDelete: 'NO ACTION',
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const Item = db.collection({
|
|
63
|
+
name: 'items',
|
|
64
|
+
fields: [{ name: 'name', type: 'string' }],
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
await db.sync();
|
|
68
|
+
|
|
69
|
+
const viewName = 'order_item_view';
|
|
70
|
+
|
|
71
|
+
const dropViewSQL = `DROP VIEW IF EXISTS ${viewName}`;
|
|
72
|
+
await db.sequelize.query(dropViewSQL);
|
|
73
|
+
|
|
74
|
+
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`;
|
|
75
|
+
|
|
76
|
+
await db.sequelize.query(viewSQL);
|
|
77
|
+
|
|
78
|
+
const OrderItemView = db.collection({
|
|
79
|
+
name: viewName,
|
|
80
|
+
view: true,
|
|
81
|
+
schema: db.inDialect('postgres') ? 'public' : undefined,
|
|
82
|
+
fields: [
|
|
83
|
+
{
|
|
84
|
+
type: 'bigInt',
|
|
85
|
+
name: 'order_id',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
type: 'bigInt',
|
|
89
|
+
name: 'item_id',
|
|
90
|
+
onDelete: 'CASCADE',
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
await db.sync();
|
|
96
|
+
|
|
97
|
+
Order.setField('items', {
|
|
98
|
+
type: 'belongsToMany',
|
|
99
|
+
target: 'orderItems',
|
|
100
|
+
through: viewName,
|
|
101
|
+
foreignKey: 'order_id',
|
|
102
|
+
otherKey: 'item_id',
|
|
103
|
+
sourceKey: 'id',
|
|
104
|
+
targetKey: 'id',
|
|
105
|
+
onDelete: 'CASCADE',
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
await db.sync();
|
|
109
|
+
|
|
110
|
+
const order1 = await db.getRepository('orders').create({
|
|
111
|
+
values: {
|
|
112
|
+
name: 'order1',
|
|
113
|
+
orderItems: [
|
|
114
|
+
{
|
|
115
|
+
count: 1,
|
|
116
|
+
item: {
|
|
117
|
+
name: 'item1',
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
count: 2,
|
|
122
|
+
item: {
|
|
123
|
+
name: 'item2',
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const item1 = await db.getRepository('items').findOne({
|
|
131
|
+
filter: {
|
|
132
|
+
name: 'item1',
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
let error;
|
|
137
|
+
try {
|
|
138
|
+
await db.getRepository('orders').destroy({
|
|
139
|
+
filterByTk: order1.get('id'),
|
|
140
|
+
});
|
|
141
|
+
} catch (err) {
|
|
142
|
+
error = err;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
expect(error).toBeUndefined();
|
|
146
|
+
});
|
|
147
|
+
|
|
21
148
|
it('should update view collection', async () => {
|
|
22
149
|
const UserCollection = db.collection({
|
|
23
150
|
name: 'users',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import Database from '../database';
|
|
2
1
|
import { Model, Transactionable } from 'sequelize';
|
|
2
|
+
import Database from '../database';
|
|
3
3
|
|
|
4
4
|
interface ReferentialIntegrityCheckOptions extends Transactionable {
|
|
5
5
|
db: Database;
|
|
@@ -24,6 +24,10 @@ export async function referentialIntegrityCheck(options: ReferentialIntegrityChe
|
|
|
24
24
|
const sourceCollection = db.collections.get(sourceCollectionName);
|
|
25
25
|
const sourceRepository = sourceCollection.repository;
|
|
26
26
|
|
|
27
|
+
if (sourceCollection.isView()) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
|
|
27
31
|
const filter = {
|
|
28
32
|
[sourceField]: referencedInstance[targetField],
|
|
29
33
|
};
|