@nocobase/database 0.20.0-alpha.16 → 0.20.0-alpha.17
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.
- package/lib/database.d.ts +1 -1
- package/lib/database.js +2 -2
- package/lib/features/{ReferencesMap.d.ts → references-map.d.ts} +3 -0
- package/lib/features/{ReferencesMap.js → references-map.js} +49 -19
- package/lib/features/referential-integrity-check.js +3 -0
- package/lib/fields/belongs-to-field.d.ts +2 -8
- package/lib/fields/belongs-to-field.js +12 -5
- package/lib/fields/belongs-to-many-field.d.ts +1 -1
- package/lib/fields/belongs-to-many-field.js +3 -5
- package/lib/fields/has-many-field.d.ts +1 -1
- package/lib/fields/has-many-field.js +3 -2
- package/lib/fields/has-one-field.d.ts +1 -1
- package/lib/fields/has-one-field.js +3 -2
- package/package.json +4 -4
package/lib/database.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { CollectionFactory } from './collection-factory';
|
|
|
10
10
|
import { CollectionGroupManager } from './collection-group-manager';
|
|
11
11
|
import { ImportFileExtension } from './collection-importer';
|
|
12
12
|
import DatabaseUtils from './database-utils';
|
|
13
|
-
import ReferencesMap from './features/
|
|
13
|
+
import ReferencesMap from './features/references-map';
|
|
14
14
|
import { ArrayFieldRepository } from './field-repository/array-field-repository';
|
|
15
15
|
import * as FieldTypes from './fields';
|
|
16
16
|
import { Field, FieldContext, RelationField } from './fields';
|
package/lib/database.js
CHANGED
|
@@ -52,7 +52,7 @@ var import_collection_factory = require("./collection-factory");
|
|
|
52
52
|
var import_collection_group_manager = require("./collection-group-manager");
|
|
53
53
|
var import_collection_importer = require("./collection-importer");
|
|
54
54
|
var import_database_utils = __toESM(require("./database-utils"));
|
|
55
|
-
var
|
|
55
|
+
var import_references_map = __toESM(require("./features/references-map"));
|
|
56
56
|
var import_referential_integrity_check = require("./features/referential-integrity-check");
|
|
57
57
|
var FieldTypes = __toESM(require("./fields"));
|
|
58
58
|
var import_helpers = require("./helpers");
|
|
@@ -137,7 +137,7 @@ const _Database = class _Database extends import_events.EventEmitter {
|
|
|
137
137
|
context = {};
|
|
138
138
|
queryInterface;
|
|
139
139
|
utils = new import_database_utils.default(this);
|
|
140
|
-
referenceMap = new
|
|
140
|
+
referenceMap = new import_references_map.default();
|
|
141
141
|
inheritanceMap = new import_inherited_map.default();
|
|
142
142
|
importedFrom = /* @__PURE__ */ new Map();
|
|
143
143
|
modelHook;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
+
export type ReferencePriority = 'default' | 'user';
|
|
1
2
|
export interface Reference {
|
|
2
3
|
sourceCollectionName: string;
|
|
3
4
|
sourceField: string;
|
|
4
5
|
targetField: string;
|
|
5
6
|
targetCollectionName: string;
|
|
6
7
|
onDelete: string;
|
|
8
|
+
priority: ReferencePriority;
|
|
7
9
|
}
|
|
10
|
+
export declare function buildReference(options: Partial<Reference>): Reference;
|
|
8
11
|
declare class ReferencesMap {
|
|
9
12
|
protected map: Map<string, Reference[]>;
|
|
10
13
|
addReference(reference: Reference): void;
|
|
@@ -16,30 +16,56 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var
|
|
20
|
-
__export(
|
|
21
|
-
|
|
19
|
+
var references_map_exports = {};
|
|
20
|
+
__export(references_map_exports, {
|
|
21
|
+
buildReference: () => buildReference,
|
|
22
|
+
default: () => references_map_default
|
|
22
23
|
});
|
|
23
|
-
module.exports = __toCommonJS(
|
|
24
|
+
module.exports = __toCommonJS(references_map_exports);
|
|
25
|
+
const DEFAULT_ON_DELETE = "NO ACTION";
|
|
26
|
+
function buildReference(options) {
|
|
27
|
+
const { sourceCollectionName, sourceField, targetField, targetCollectionName, onDelete, priority } = options;
|
|
28
|
+
return {
|
|
29
|
+
sourceCollectionName,
|
|
30
|
+
sourceField,
|
|
31
|
+
targetField,
|
|
32
|
+
targetCollectionName,
|
|
33
|
+
onDelete: (onDelete || DEFAULT_ON_DELETE).toUpperCase(),
|
|
34
|
+
priority: assignPriority(priority, onDelete)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
__name(buildReference, "buildReference");
|
|
38
|
+
function assignPriority(priority, onDelete) {
|
|
39
|
+
if (priority) {
|
|
40
|
+
return priority;
|
|
41
|
+
}
|
|
42
|
+
return onDelete ? "user" : "default";
|
|
43
|
+
}
|
|
44
|
+
__name(assignPriority, "assignPriority");
|
|
45
|
+
const PRIORITY_MAP = {
|
|
46
|
+
default: 1,
|
|
47
|
+
user: 2
|
|
48
|
+
};
|
|
24
49
|
const _ReferencesMap = class _ReferencesMap {
|
|
25
50
|
map = /* @__PURE__ */ new Map();
|
|
26
51
|
addReference(reference) {
|
|
27
|
-
if (!reference.onDelete) {
|
|
28
|
-
reference.onDelete = "SET NULL";
|
|
29
|
-
}
|
|
30
|
-
reference.onDelete = reference.onDelete.toUpperCase();
|
|
31
52
|
const existReference = this.existReference(reference);
|
|
32
53
|
if (existReference && existReference.onDelete !== reference.onDelete) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
54
|
+
const existPriority = PRIORITY_MAP[existReference.priority];
|
|
55
|
+
const newPriority = PRIORITY_MAP[reference.priority];
|
|
56
|
+
if (newPriority > existPriority) {
|
|
36
57
|
existReference.onDelete = reference.onDelete;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
existReference.priority = reference.priority;
|
|
59
|
+
} else if (newPriority === existPriority && newPriority === PRIORITY_MAP["user"]) {
|
|
60
|
+
if (existReference.onDelete === "SET NULL" && reference.onDelete === "CASCADE") {
|
|
61
|
+
existReference.onDelete = reference.onDelete;
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`On Delete Conflict, exist reference ${JSON.stringify(existReference)}, new reference ${JSON.stringify(
|
|
65
|
+
reference
|
|
66
|
+
)}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
43
69
|
}
|
|
44
70
|
}
|
|
45
71
|
if (!existReference) {
|
|
@@ -57,7 +83,7 @@ const _ReferencesMap = class _ReferencesMap {
|
|
|
57
83
|
if (!references) {
|
|
58
84
|
return null;
|
|
59
85
|
}
|
|
60
|
-
const keys = Object.keys(reference).filter((k) => k !== "onDelete");
|
|
86
|
+
const keys = Object.keys(reference).filter((k) => k !== "onDelete" && k !== "priority");
|
|
61
87
|
return references.find((ref) => keys.every((key) => ref[key] === reference[key]));
|
|
62
88
|
}
|
|
63
89
|
removeReference(reference) {
|
|
@@ -74,4 +100,8 @@ const _ReferencesMap = class _ReferencesMap {
|
|
|
74
100
|
};
|
|
75
101
|
__name(_ReferencesMap, "ReferencesMap");
|
|
76
102
|
let ReferencesMap = _ReferencesMap;
|
|
77
|
-
var
|
|
103
|
+
var references_map_default = ReferencesMap;
|
|
104
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
105
|
+
0 && (module.exports = {
|
|
106
|
+
buildReference
|
|
107
|
+
});
|
|
@@ -31,6 +31,9 @@ async function referentialIntegrityCheck(options) {
|
|
|
31
31
|
}
|
|
32
32
|
for (const reference of references) {
|
|
33
33
|
const { sourceCollectionName, sourceField, targetField, onDelete } = reference;
|
|
34
|
+
if (onDelete === "NO ACTION") {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
34
37
|
const sourceCollection = db.collections.get(sourceCollectionName);
|
|
35
38
|
const sourceRepository = sourceCollection.repository;
|
|
36
39
|
if (sourceCollection.isView()) {
|
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
import { BelongsToOptions as SequelizeBelongsToOptions } from 'sequelize';
|
|
2
|
-
import { Reference } from '../features/
|
|
2
|
+
import { Reference, ReferencePriority } from '../features/references-map';
|
|
3
3
|
import { BaseRelationFieldOptions, RelationField } from './relation-field';
|
|
4
4
|
export declare class BelongsToField extends RelationField {
|
|
5
5
|
static type: string;
|
|
6
6
|
get dataType(): string;
|
|
7
7
|
get target(): any;
|
|
8
|
-
static toReference(db: any, association: any, onDelete: any):
|
|
9
|
-
sourceCollectionName: any;
|
|
10
|
-
sourceField: any;
|
|
11
|
-
targetField: any;
|
|
12
|
-
targetCollectionName: any;
|
|
13
|
-
onDelete: any;
|
|
14
|
-
};
|
|
8
|
+
static toReference(db: any, association: any, onDelete: any, priority?: ReferencePriority): Reference;
|
|
15
9
|
reference(association: any): Reference;
|
|
16
10
|
checkAssociationKeys(): void;
|
|
17
11
|
bind(): boolean;
|
|
@@ -38,6 +38,7 @@ __export(belongs_to_field_exports, {
|
|
|
38
38
|
module.exports = __toCommonJS(belongs_to_field_exports);
|
|
39
39
|
var import_lodash = __toESM(require("lodash"));
|
|
40
40
|
var import_sequelize = require("sequelize");
|
|
41
|
+
var import_references_map = require("../features/references-map");
|
|
41
42
|
var import_utils = require("../utils");
|
|
42
43
|
var import_relation_field = require("./relation-field");
|
|
43
44
|
const _BelongsToField = class _BelongsToField extends import_relation_field.RelationField {
|
|
@@ -48,18 +49,24 @@ const _BelongsToField = class _BelongsToField extends import_relation_field.Rela
|
|
|
48
49
|
const { target, name } = this.options;
|
|
49
50
|
return target || import_sequelize.Utils.pluralize(name);
|
|
50
51
|
}
|
|
51
|
-
static toReference(db, association, onDelete) {
|
|
52
|
+
static toReference(db, association, onDelete, priority = "default") {
|
|
52
53
|
const targetKey = association.targetKey;
|
|
53
|
-
return {
|
|
54
|
+
return (0, import_references_map.buildReference)({
|
|
54
55
|
sourceCollectionName: db.modelCollection.get(association.source).name,
|
|
55
56
|
sourceField: association.foreignKey,
|
|
56
57
|
targetField: targetKey,
|
|
57
58
|
targetCollectionName: db.modelCollection.get(association.target).name,
|
|
58
|
-
onDelete
|
|
59
|
-
|
|
59
|
+
onDelete,
|
|
60
|
+
priority
|
|
61
|
+
});
|
|
60
62
|
}
|
|
61
63
|
reference(association) {
|
|
62
|
-
return _BelongsToField.toReference(
|
|
64
|
+
return _BelongsToField.toReference(
|
|
65
|
+
this.database,
|
|
66
|
+
association,
|
|
67
|
+
this.options.onDelete,
|
|
68
|
+
this.options.onDelete ? "user" : "default"
|
|
69
|
+
);
|
|
63
70
|
}
|
|
64
71
|
checkAssociationKeys() {
|
|
65
72
|
let { foreignKey, targetKey } = this.options;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AssociationScope, BelongsToManyOptions as SequelizeBelongsToManyOptions } from 'sequelize';
|
|
2
|
-
import { Reference } from '../features/
|
|
2
|
+
import { Reference } from '../features/references-map';
|
|
3
3
|
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
|
|
4
4
|
export declare class BelongsToManyField extends RelationField {
|
|
5
5
|
get dataType(): string;
|
|
@@ -41,6 +41,7 @@ const _BelongsToManyField = class _BelongsToManyField extends import_relation_fi
|
|
|
41
41
|
references(association) {
|
|
42
42
|
const db = this.context.database;
|
|
43
43
|
const onDelete = this.options.onDelete || "CASCADE";
|
|
44
|
+
const priority = this.options.onDelete ? "user" : "default";
|
|
44
45
|
const targetAssociation = association.toTarget;
|
|
45
46
|
if (association.targetKey) {
|
|
46
47
|
targetAssociation.targetKey = association.targetKey;
|
|
@@ -50,8 +51,8 @@ const _BelongsToManyField = class _BelongsToManyField extends import_relation_fi
|
|
|
50
51
|
sourceAssociation.targetKey = association.sourceKey;
|
|
51
52
|
}
|
|
52
53
|
return [
|
|
53
|
-
import_belongs_to_field.BelongsToField.toReference(db, targetAssociation, onDelete),
|
|
54
|
-
import_belongs_to_field.BelongsToField.toReference(db, sourceAssociation, onDelete)
|
|
54
|
+
import_belongs_to_field.BelongsToField.toReference(db, targetAssociation, onDelete, priority),
|
|
55
|
+
import_belongs_to_field.BelongsToField.toReference(db, sourceAssociation, onDelete, priority)
|
|
55
56
|
];
|
|
56
57
|
}
|
|
57
58
|
checkAssociationKeys(database) {
|
|
@@ -126,9 +127,6 @@ const _BelongsToManyField = class _BelongsToManyField extends import_relation_fi
|
|
|
126
127
|
Through = database.collection(throughCollectionOptions);
|
|
127
128
|
Object.defineProperty(Through.model, "isThrough", { value: true });
|
|
128
129
|
}
|
|
129
|
-
if (!this.options.onDelete) {
|
|
130
|
-
this.options.onDelete = "CASCADE";
|
|
131
|
-
}
|
|
132
130
|
const belongsToManyOptions = {
|
|
133
131
|
constraints: false,
|
|
134
132
|
...(0, import_lodash.omit)(this.options, ["name", "type", "target"]),
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AssociationScope, DataType, ForeignKeyOptions, HasManyOptions, HasManyOptions as SequelizeHasManyOptions } from 'sequelize';
|
|
2
|
-
import { Reference } from '../features/
|
|
2
|
+
import { Reference } from '../features/references-map';
|
|
3
3
|
import { MultipleRelationFieldOptions, RelationField } from './relation-field';
|
|
4
4
|
export interface HasManyFieldOptions extends HasManyOptions {
|
|
5
5
|
/**
|
|
@@ -23,6 +23,7 @@ __export(has_many_field_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(has_many_field_exports);
|
|
24
24
|
var import_lodash = require("lodash");
|
|
25
25
|
var import_sequelize = require("sequelize");
|
|
26
|
+
var import_references_map = require("../features/references-map");
|
|
26
27
|
var import_utils = require("../utils");
|
|
27
28
|
var import_relation_field = require("./relation-field");
|
|
28
29
|
const _HasManyField = class _HasManyField extends import_relation_field.RelationField {
|
|
@@ -38,13 +39,13 @@ const _HasManyField = class _HasManyField extends import_relation_field.Relation
|
|
|
38
39
|
}
|
|
39
40
|
reference(association) {
|
|
40
41
|
const sourceKey = association.sourceKey;
|
|
41
|
-
return {
|
|
42
|
+
return (0, import_references_map.buildReference)({
|
|
42
43
|
sourceCollectionName: this.database.modelCollection.get(association.target).name,
|
|
43
44
|
sourceField: association.foreignKey,
|
|
44
45
|
targetField: sourceKey,
|
|
45
46
|
targetCollectionName: this.database.modelCollection.get(association.source).name,
|
|
46
47
|
onDelete: this.options.onDelete
|
|
47
|
-
};
|
|
48
|
+
});
|
|
48
49
|
}
|
|
49
50
|
checkAssociationKeys() {
|
|
50
51
|
let { foreignKey, sourceKey } = this.options;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AssociationScope, DataType, ForeignKeyOptions, HasOneOptions, HasOneOptions as SequelizeHasOneOptions } from 'sequelize';
|
|
2
|
-
import { Reference } from '../features/
|
|
2
|
+
import { Reference } from '../features/references-map';
|
|
3
3
|
import { BaseRelationFieldOptions, RelationField } from './relation-field';
|
|
4
4
|
export interface HasOneFieldOptions extends HasOneOptions {
|
|
5
5
|
/**
|
|
@@ -23,6 +23,7 @@ __export(has_one_field_exports, {
|
|
|
23
23
|
module.exports = __toCommonJS(has_one_field_exports);
|
|
24
24
|
var import_lodash = require("lodash");
|
|
25
25
|
var import_sequelize = require("sequelize");
|
|
26
|
+
var import_references_map = require("../features/references-map");
|
|
26
27
|
var import_utils = require("../utils");
|
|
27
28
|
var import_relation_field = require("./relation-field");
|
|
28
29
|
const _HasOneField = class _HasOneField extends import_relation_field.RelationField {
|
|
@@ -45,13 +46,13 @@ const _HasOneField = class _HasOneField extends import_relation_field.RelationFi
|
|
|
45
46
|
}
|
|
46
47
|
reference(association) {
|
|
47
48
|
const sourceKey = association.sourceKey;
|
|
48
|
-
return {
|
|
49
|
+
return (0, import_references_map.buildReference)({
|
|
49
50
|
sourceCollectionName: this.database.modelCollection.get(association.target).name,
|
|
50
51
|
sourceField: association.foreignKey,
|
|
51
52
|
targetField: sourceKey,
|
|
52
53
|
targetCollectionName: this.database.modelCollection.get(association.source).name,
|
|
53
54
|
onDelete: this.options.onDelete
|
|
54
|
-
};
|
|
55
|
+
});
|
|
55
56
|
}
|
|
56
57
|
checkAssociationKeys() {
|
|
57
58
|
let { foreignKey, sourceKey } = this.options;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "0.20.0-alpha.
|
|
3
|
+
"version": "0.20.0-alpha.17",
|
|
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.20.0-alpha.
|
|
10
|
-
"@nocobase/utils": "0.20.0-alpha.
|
|
9
|
+
"@nocobase/logger": "0.20.0-alpha.17",
|
|
10
|
+
"@nocobase/utils": "0.20.0-alpha.17",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"chalk": "^4.1.1",
|
|
13
13
|
"cron-parser": "4.4.0",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
36
36
|
"directory": "packages/database"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "a2460c222bc0b8a3bcb783b5c856499d756efa82"
|
|
39
39
|
}
|