@tachybase/module-collection 0.23.8
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/.turbo/turbo-build.log +14 -0
- package/LICENSE +201 -0
- package/README.md +9 -0
- package/README.zh-CN.md +9 -0
- package/client.d.ts +2 -0
- package/client.js +1 -0
- package/dist/client/index.d.ts +5 -0
- package/dist/client/index.js +1 -0
- package/dist/externalVersion.js +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +39 -0
- package/dist/server/actions/collectionImportExportMeta.d.ts +2 -0
- package/dist/server/actions/collectionImportExportMeta.js +226 -0
- package/dist/server/collections/collectionCategories.d.ts +3 -0
- package/dist/server/collections/collectionCategories.js +52 -0
- package/dist/server/collections/collections.d.ts +3 -0
- package/dist/server/collections/collections.js +92 -0
- package/dist/server/collections/fields.d.ts +3 -0
- package/dist/server/collections/fields.js +94 -0
- package/dist/server/hooks/afterCreateForForeignKeyField.d.ts +2 -0
- package/dist/server/hooks/afterCreateForForeignKeyField.js +195 -0
- package/dist/server/hooks/afterCreateForReverseField.d.ts +4 -0
- package/dist/server/hooks/afterCreateForReverseField.js +37 -0
- package/dist/server/hooks/afterDestroyForForeignKeyField.d.ts +2 -0
- package/dist/server/hooks/afterDestroyForForeignKeyField.js +89 -0
- package/dist/server/hooks/beforeCreateForChildrenCollection.d.ts +5 -0
- package/dist/server/hooks/beforeCreateForChildrenCollection.js +58 -0
- package/dist/server/hooks/beforeCreateForReverseField.d.ts +4 -0
- package/dist/server/hooks/beforeCreateForReverseField.js +64 -0
- package/dist/server/hooks/beforeCreateForValidateField.d.ts +4 -0
- package/dist/server/hooks/beforeCreateForValidateField.js +48 -0
- package/dist/server/hooks/beforeCreateForViewCollection.d.ts +5 -0
- package/dist/server/hooks/beforeCreateForViewCollection.js +30 -0
- package/dist/server/hooks/beforeDestroyForeignKey.d.ts +2 -0
- package/dist/server/hooks/beforeDestroyForeignKey.js +66 -0
- package/dist/server/hooks/beforeInitOptions.d.ts +29 -0
- package/dist/server/hooks/beforeInitOptions.js +233 -0
- package/dist/server/hooks/index.d.ts +6 -0
- package/dist/server/hooks/index.js +31 -0
- package/dist/server/index.d.ts +4 -0
- package/dist/server/index.js +45 -0
- package/dist/server/migrations/20220613103214-alert-sub-table.d.ts +5 -0
- package/dist/server/migrations/20220613103214-alert-sub-table.js +40 -0
- package/dist/server/migrations/20220704225714-drop-foreign-keys.d.ts +5 -0
- package/dist/server/migrations/20220704225714-drop-foreign-keys.js +67 -0
- package/dist/server/migrations/20221101111110-update-foreign-fields.d.ts +5 -0
- package/dist/server/migrations/20221101111110-update-foreign-fields.js +64 -0
- package/dist/server/migrations/20221104151410-update-collections-hidden.d.ts +5 -0
- package/dist/server/migrations/20221104151410-update-collections-hidden.js +47 -0
- package/dist/server/migrations/20221121111110-update-fk-type.d.ts +5 -0
- package/dist/server/migrations/20221121111110-update-fk-type.js +144 -0
- package/dist/server/migrations/20221121111113-update-id-to-bigint.d.ts +5 -0
- package/dist/server/migrations/20221121111113-update-id-to-bigint.js +178 -0
- package/dist/server/migrations/20221221103220-timestamps.d.ts +5 -0
- package/dist/server/migrations/20221221103220-timestamps.js +45 -0
- package/dist/server/migrations/20230225111112-drop-ui-schema-relation.d.ts +5 -0
- package/dist/server/migrations/20230225111112-drop-ui-schema-relation.js +87 -0
- package/dist/server/migrations/20230509111114-alert-ui-schema.d.ts +5 -0
- package/dist/server/migrations/20230509111114-alert-ui-schema.js +77 -0
- package/dist/server/migrations/20230623145414-number-step.d.ts +5 -0
- package/dist/server/migrations/20230623145414-number-step.js +71 -0
- package/dist/server/migrations/20230704222935-tableoid.d.ts +5 -0
- package/dist/server/migrations/20230704222935-tableoid.js +73 -0
- package/dist/server/migrations/20230918024546-set-collection-schema.d.ts +5 -0
- package/dist/server/migrations/20230918024546-set-collection-schema.js +45 -0
- package/dist/server/migrations/20240220153103-install-main-datasource.d.ts +6 -0
- package/dist/server/migrations/20240220153103-install-main-datasource.js +41 -0
- package/dist/server/models/collection.d.ts +23 -0
- package/dist/server/models/collection.js +221 -0
- package/dist/server/models/field.d.ts +22 -0
- package/dist/server/models/field.js +188 -0
- package/dist/server/models/index.d.ts +2 -0
- package/dist/server/models/index.js +23 -0
- package/dist/server/repositories/collection-repository.d.ts +12 -0
- package/dist/server/repositories/collection-repository.js +135 -0
- package/dist/server/repositories/index.d.ts +1 -0
- package/dist/server/repositories/index.js +21 -0
- package/dist/server/resourcers/collections.d.ts +4 -0
- package/dist/server/resourcers/collections.js +93 -0
- package/dist/server/resourcers/sql.d.ts +10 -0
- package/dist/server/resourcers/sql.js +108 -0
- package/dist/server/resourcers/views.d.ts +9 -0
- package/dist/server/resourcers/views.js +98 -0
- package/dist/server/server.d.ts +11 -0
- package/dist/server/server.js +332 -0
- package/dist/swagger/index.d.ts +405 -0
- package/dist/swagger/index.js +431 -0
- package/package.json +34 -0
- package/server.d.ts +2 -0
- package/server.js +1 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var number_step_exports = {};
|
|
29
|
+
__export(number_step_exports, {
|
|
30
|
+
default: () => number_step_default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(number_step_exports);
|
|
33
|
+
var import_server = require("@tachybase/server");
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
class number_step_default extends import_server.Migration {
|
|
36
|
+
appVersion = "<0.10.0-alpha.3";
|
|
37
|
+
async up() {
|
|
38
|
+
const transaction = await this.db.sequelize.transaction();
|
|
39
|
+
const migrateFieldsSchema = async (collection) => {
|
|
40
|
+
var _a;
|
|
41
|
+
this.app.logger.info(`Start to migrate ${collection.name} collection's ui schema`);
|
|
42
|
+
const fieldRecords = await collection.repository.find({
|
|
43
|
+
transaction,
|
|
44
|
+
filter: {
|
|
45
|
+
type: ["bigInt", "float", "double"]
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
this.app.logger.info(`Total ${fieldRecords.length} fields need to be migrated`);
|
|
49
|
+
for (const fieldRecord of fieldRecords) {
|
|
50
|
+
const uiSchema = fieldRecord.get("uiSchema");
|
|
51
|
+
if (((_a = uiSchema == null ? void 0 : uiSchema["x-component-props"]) == null ? void 0 : _a.step) !== "0") {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
import_lodash.default.set(uiSchema, "x-component-props.step", "1");
|
|
55
|
+
fieldRecord.set("uiSchema", uiSchema);
|
|
56
|
+
await fieldRecord.save({
|
|
57
|
+
transaction
|
|
58
|
+
});
|
|
59
|
+
console.log(`changed: ${fieldRecord.get("collectionName")}.${fieldRecord.get("name")}`);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
try {
|
|
63
|
+
await migrateFieldsSchema(this.db.getCollection("fields"));
|
|
64
|
+
await transaction.commit();
|
|
65
|
+
} catch (error) {
|
|
66
|
+
await transaction.rollback();
|
|
67
|
+
this.app.logger.error(error);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var tableoid_exports = {};
|
|
29
|
+
__export(tableoid_exports, {
|
|
30
|
+
default: () => tableoid_default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(tableoid_exports);
|
|
33
|
+
var import_server = require("@tachybase/server");
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
class tableoid_default extends import_server.Migration {
|
|
36
|
+
appVersion = "<0.10.1-alpha.1";
|
|
37
|
+
async up() {
|
|
38
|
+
var _a;
|
|
39
|
+
if (!this.db.inDialect("postgres")) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const repository = this.db.getRepository("collections");
|
|
43
|
+
let names = [];
|
|
44
|
+
const items = await repository.find();
|
|
45
|
+
for (const item of items) {
|
|
46
|
+
if (Array.isArray((_a = item.options) == null ? void 0 : _a.inherits) && item.options.inherits.length) {
|
|
47
|
+
names.push(item.name);
|
|
48
|
+
names.push(...item.options.inherits);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
names = import_lodash.default.uniq(names);
|
|
52
|
+
console.log("collection names:", names);
|
|
53
|
+
for (const name of names) {
|
|
54
|
+
const fieldRepository = this.db.getRepository("fields");
|
|
55
|
+
await fieldRepository.firstOrCreate({
|
|
56
|
+
values: {
|
|
57
|
+
collectionName: name,
|
|
58
|
+
name: "__collection",
|
|
59
|
+
type: "virtual",
|
|
60
|
+
interface: "tableoid",
|
|
61
|
+
uiSchema: {
|
|
62
|
+
type: "string",
|
|
63
|
+
title: '{{t("Table OID")}}',
|
|
64
|
+
"x-component": "CollectionSelect",
|
|
65
|
+
"x-component-props": { isTableOid: true },
|
|
66
|
+
"x-read-pretty": true
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
filterKeys: ["name", "collectionName"]
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var set_collection_schema_exports = {};
|
|
19
|
+
__export(set_collection_schema_exports, {
|
|
20
|
+
default: () => set_collection_schema_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(set_collection_schema_exports);
|
|
23
|
+
var import_server = require("@tachybase/server");
|
|
24
|
+
class set_collection_schema_default extends import_server.Migration {
|
|
25
|
+
appVersion = "<0.14.0-alpha.4";
|
|
26
|
+
async up() {
|
|
27
|
+
if (!this.db.inDialect("postgres")) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (this.context.app.name !== "main") {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const userCollections = await this.db.getRepository("collections").find({
|
|
34
|
+
filter: {
|
|
35
|
+
"options.schema": null,
|
|
36
|
+
"options.from.$ne": "db2cm"
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
for (const collection of userCollections) {
|
|
40
|
+
await collection.set("schema", process.env.COLLECTION_MANAGER_SCHEMA || this.db.options.schema || "public");
|
|
41
|
+
await collection.save();
|
|
42
|
+
}
|
|
43
|
+
await this.context.app.emitAsync("loadCollections");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var install_main_datasource_exports = {};
|
|
19
|
+
__export(install_main_datasource_exports, {
|
|
20
|
+
default: () => install_main_datasource_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(install_main_datasource_exports);
|
|
23
|
+
var import_server = require("@tachybase/server");
|
|
24
|
+
class install_main_datasource_default extends import_server.Migration {
|
|
25
|
+
on = "afterLoad";
|
|
26
|
+
// 'beforeLoad' or 'afterLoad'
|
|
27
|
+
appVersion = "<0.20.0-alpha.1";
|
|
28
|
+
async up() {
|
|
29
|
+
const dataSourcesCollection = this.app.db.getCollection("dataSources");
|
|
30
|
+
await dataSourcesCollection.repository.firstOrCreate({
|
|
31
|
+
filterKeys: ["key"],
|
|
32
|
+
values: {
|
|
33
|
+
key: "main",
|
|
34
|
+
type: "main",
|
|
35
|
+
displayName: "Main",
|
|
36
|
+
fixed: true,
|
|
37
|
+
options: {}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Database, { Collection, MagicAttributeModel, SyncOptions, Transactionable } from '@tachybase/database';
|
|
2
|
+
import { QueryInterfaceDropTableOptions } from 'sequelize';
|
|
3
|
+
interface LoadOptions extends Transactionable {
|
|
4
|
+
skipField?: boolean | Array<string>;
|
|
5
|
+
skipExist?: boolean;
|
|
6
|
+
resetFields?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class CollectionModel extends MagicAttributeModel {
|
|
9
|
+
get db(): Database;
|
|
10
|
+
toJSON(): any;
|
|
11
|
+
load(loadOptions?: LoadOptions): Promise<Collection<any, any>>;
|
|
12
|
+
loadFields(options?: Transactionable & {
|
|
13
|
+
skipField?: Array<string>;
|
|
14
|
+
includeFields?: Array<string>;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
remove(options?: Transactionable & QueryInterfaceDropTableOptions): Promise<void>;
|
|
17
|
+
migrate(options?: SyncOptions & Transactionable): Promise<void>;
|
|
18
|
+
isInheritedModel(): any;
|
|
19
|
+
findParents(options: Transactionable): Promise<any[]>;
|
|
20
|
+
parentFields(options: Transactionable): Promise<any>;
|
|
21
|
+
syncParentFields(options: Transactionable): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var collection_exports = {};
|
|
29
|
+
__export(collection_exports, {
|
|
30
|
+
CollectionModel: () => CollectionModel
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(collection_exports);
|
|
33
|
+
var import_database = require("@tachybase/database");
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
class CollectionModel extends import_database.MagicAttributeModel {
|
|
36
|
+
get db() {
|
|
37
|
+
return this.constructor.database;
|
|
38
|
+
}
|
|
39
|
+
toJSON() {
|
|
40
|
+
const json = super.toJSON();
|
|
41
|
+
if (!json.filterTargetKey) {
|
|
42
|
+
const collection = this.db.getCollection(json.name);
|
|
43
|
+
json.filterTargetKey = collection == null ? void 0 : collection.filterTargetKey;
|
|
44
|
+
}
|
|
45
|
+
return json;
|
|
46
|
+
}
|
|
47
|
+
async load(loadOptions = {}) {
|
|
48
|
+
const { skipExist, skipField, resetFields, transaction } = loadOptions;
|
|
49
|
+
const name = this.get("name");
|
|
50
|
+
let collection;
|
|
51
|
+
const collectionOptions = {
|
|
52
|
+
origin: "@tachybase/module-collection",
|
|
53
|
+
...this.get(),
|
|
54
|
+
fields: [],
|
|
55
|
+
loadedFromCollectionManager: true
|
|
56
|
+
};
|
|
57
|
+
if (!this.db.inDialect("postgres") && collectionOptions.schema) {
|
|
58
|
+
delete collectionOptions.schema;
|
|
59
|
+
}
|
|
60
|
+
if (this.db.hasCollection(name)) {
|
|
61
|
+
collection = this.db.getCollection(name);
|
|
62
|
+
if (skipExist) {
|
|
63
|
+
return collection;
|
|
64
|
+
}
|
|
65
|
+
if (resetFields) {
|
|
66
|
+
collection.resetFields();
|
|
67
|
+
}
|
|
68
|
+
collection.updateOptions(collectionOptions);
|
|
69
|
+
} else {
|
|
70
|
+
if (!collectionOptions.dumpRules) {
|
|
71
|
+
import_lodash.default.set(collectionOptions, "dumpRules.group", "custom");
|
|
72
|
+
}
|
|
73
|
+
collection = this.db.collection(collectionOptions);
|
|
74
|
+
}
|
|
75
|
+
if (!skipField) {
|
|
76
|
+
await this.loadFields({ transaction });
|
|
77
|
+
}
|
|
78
|
+
if (import_lodash.default.isArray(skipField)) {
|
|
79
|
+
await this.loadFields({ transaction, skipField });
|
|
80
|
+
}
|
|
81
|
+
await this.db.emitAsync("collection:loaded", {
|
|
82
|
+
collection,
|
|
83
|
+
transaction
|
|
84
|
+
});
|
|
85
|
+
return collection;
|
|
86
|
+
}
|
|
87
|
+
async loadFields(options = {}) {
|
|
88
|
+
let fields = this.get("fields") || [];
|
|
89
|
+
if (!fields.length) {
|
|
90
|
+
fields = await this.getFields(options);
|
|
91
|
+
}
|
|
92
|
+
if (options.skipField) {
|
|
93
|
+
fields = fields.filter((field) => !options.skipField.includes(field.name));
|
|
94
|
+
}
|
|
95
|
+
if (options.includeFields) {
|
|
96
|
+
fields = fields.filter((field) => options.includeFields.includes(field.name));
|
|
97
|
+
}
|
|
98
|
+
if (this.options.view && fields.find((f) => f.name === "id")) {
|
|
99
|
+
fields = fields.map((field) => {
|
|
100
|
+
if (field.name === "id") {
|
|
101
|
+
field.set("primaryKey", true);
|
|
102
|
+
} else {
|
|
103
|
+
field.set("primaryKey", false);
|
|
104
|
+
}
|
|
105
|
+
return field;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const instances = fields;
|
|
109
|
+
for (const instance of instances) {
|
|
110
|
+
await instance.load(options);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
async remove(options) {
|
|
114
|
+
const { transaction } = options || {};
|
|
115
|
+
const name = this.get("name");
|
|
116
|
+
const collection = this.db.getCollection(name);
|
|
117
|
+
if (!collection) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const fields = await this.db.getRepository("fields").find({
|
|
121
|
+
filter: {
|
|
122
|
+
"type.$in": ["belongsToMany", "belongsTo", "hasMany", "hasOne"]
|
|
123
|
+
},
|
|
124
|
+
transaction
|
|
125
|
+
});
|
|
126
|
+
for (const field of fields) {
|
|
127
|
+
if (field.get("target") && field.get("target") === name) {
|
|
128
|
+
await field.destroy({ transaction });
|
|
129
|
+
} else if (field.get("through") && field.get("through") === name) {
|
|
130
|
+
await field.destroy({ transaction });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
await collection.removeFromDb(options);
|
|
134
|
+
}
|
|
135
|
+
async migrate(options) {
|
|
136
|
+
const pendingFieldsTargetToThis = this.db.pendingFields.get(this.get("name")) || [];
|
|
137
|
+
const getPendingField = () => pendingFieldsTargetToThis.map((field) => {
|
|
138
|
+
return {
|
|
139
|
+
name: field.get("name"),
|
|
140
|
+
collectionName: field.get("collectionName")
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
const beforePendingFields = getPendingField();
|
|
144
|
+
const collection = await this.load({
|
|
145
|
+
transaction: options == null ? void 0 : options.transaction
|
|
146
|
+
});
|
|
147
|
+
const afterPendingFields = getPendingField();
|
|
148
|
+
const resolvedPendingFields = import_lodash.default.differenceWith(beforePendingFields, afterPendingFields, import_lodash.default.isEqual);
|
|
149
|
+
const resolvedPendingFieldsCollections = import_lodash.default.uniq(resolvedPendingFields.map((field) => field.collectionName));
|
|
150
|
+
if (Object.keys(collection.model.tableAttributes).length === 0 && !this.db.inDialect("postgres")) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
try {
|
|
154
|
+
const syncOptions = {
|
|
155
|
+
force: false,
|
|
156
|
+
alter: {
|
|
157
|
+
drop: false
|
|
158
|
+
},
|
|
159
|
+
...options
|
|
160
|
+
};
|
|
161
|
+
await collection.sync(syncOptions);
|
|
162
|
+
for (const collectionName of resolvedPendingFieldsCollections) {
|
|
163
|
+
await this.db.getCollection(collectionName).sync(syncOptions);
|
|
164
|
+
}
|
|
165
|
+
} catch (error) {
|
|
166
|
+
console.error(error);
|
|
167
|
+
const name = this.get("name");
|
|
168
|
+
this.db.removeCollection(name);
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
isInheritedModel() {
|
|
173
|
+
return this.get("inherits");
|
|
174
|
+
}
|
|
175
|
+
async findParents(options) {
|
|
176
|
+
const { transaction } = options;
|
|
177
|
+
const findModelParents = async (model, carry = []) => {
|
|
178
|
+
if (!model.get("inherits")) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const parents = import_lodash.default.castArray(model.get("inherits"));
|
|
182
|
+
for (const parent of parents) {
|
|
183
|
+
const parentModel = await this.db.getCollection("collections").repository.findOne({
|
|
184
|
+
filterByTk: parent,
|
|
185
|
+
transaction
|
|
186
|
+
});
|
|
187
|
+
carry.push(parentModel.get("name"));
|
|
188
|
+
await findModelParents(parentModel, carry);
|
|
189
|
+
}
|
|
190
|
+
return carry;
|
|
191
|
+
};
|
|
192
|
+
return findModelParents(this);
|
|
193
|
+
}
|
|
194
|
+
async parentFields(options) {
|
|
195
|
+
const { transaction } = options;
|
|
196
|
+
return this.db.getCollection("fields").repository.find({
|
|
197
|
+
filter: {
|
|
198
|
+
collectionName: { $in: await this.findParents({ transaction }) }
|
|
199
|
+
},
|
|
200
|
+
transaction
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
// sync fields from parents
|
|
204
|
+
async syncParentFields(options) {
|
|
205
|
+
const { transaction } = options;
|
|
206
|
+
const ancestorFields = await this.parentFields({ transaction });
|
|
207
|
+
const selfFields = await this.getFields({ transaction });
|
|
208
|
+
const inheritedFields = ancestorFields.filter((field) => {
|
|
209
|
+
return !field.isAssociationField() && !selfFields.find((selfField) => selfField.get("name") === field.get("name"));
|
|
210
|
+
});
|
|
211
|
+
for (const inheritedField of inheritedFields) {
|
|
212
|
+
await this.createField(import_lodash.default.omit(inheritedField.toJSON(), ["key", "collectionName", "sort"]), {
|
|
213
|
+
transaction
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
219
|
+
0 && (module.exports = {
|
|
220
|
+
CollectionModel
|
|
221
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Database, { Collection, MagicAttributeModel, SyncOptions, Transactionable } from '@tachybase/database';
|
|
2
|
+
interface LoadOptions extends Transactionable {
|
|
3
|
+
skipExist?: boolean;
|
|
4
|
+
}
|
|
5
|
+
interface MigrateOptions extends SyncOptions, Transactionable {
|
|
6
|
+
isNew?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare class FieldModel extends MagicAttributeModel {
|
|
9
|
+
get db(): Database;
|
|
10
|
+
isAssociationField(): boolean;
|
|
11
|
+
load(loadOptions?: LoadOptions): Promise<import("@tachybase/database").Field>;
|
|
12
|
+
syncSortByField(options: Transactionable): Promise<void>;
|
|
13
|
+
migrate({ isNew, ...options }?: MigrateOptions): Promise<void>;
|
|
14
|
+
remove(options?: any): Promise<void>;
|
|
15
|
+
syncUniqueIndex(options: Transactionable): Promise<void>;
|
|
16
|
+
syncDefaultValue(options: Transactionable & {
|
|
17
|
+
defaultValue: any;
|
|
18
|
+
}): Promise<void>;
|
|
19
|
+
syncReferenceCheckOption(options: Transactionable): Promise<void>;
|
|
20
|
+
protected getFieldCollection(): Collection | null;
|
|
21
|
+
}
|
|
22
|
+
export {};
|