@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,188 @@
|
|
|
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 field_exports = {};
|
|
19
|
+
__export(field_exports, {
|
|
20
|
+
FieldModel: () => FieldModel
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(field_exports);
|
|
23
|
+
var import_database = require("@tachybase/database");
|
|
24
|
+
class FieldModel extends import_database.MagicAttributeModel {
|
|
25
|
+
get db() {
|
|
26
|
+
return this.constructor.database;
|
|
27
|
+
}
|
|
28
|
+
isAssociationField() {
|
|
29
|
+
return ["belongsTo", "hasOne", "hasMany", "belongsToMany"].includes(this.get("type"));
|
|
30
|
+
}
|
|
31
|
+
async load(loadOptions) {
|
|
32
|
+
const { skipExist = false, transaction } = loadOptions || {};
|
|
33
|
+
const collectionName = this.get("collectionName");
|
|
34
|
+
if (!this.db.hasCollection(collectionName)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const collection = this.db.getCollection(collectionName);
|
|
38
|
+
const name = this.get("name");
|
|
39
|
+
if (skipExist && collection.hasField(name)) {
|
|
40
|
+
return collection.getField(name);
|
|
41
|
+
}
|
|
42
|
+
const options = this.get();
|
|
43
|
+
const field = await (async () => {
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
45
|
+
if (process.env.DEBUG_COLLECTION_FIELD) {
|
|
46
|
+
this.db.logger.debug({ collectionName, options });
|
|
47
|
+
}
|
|
48
|
+
return collection.setField(name, options);
|
|
49
|
+
})();
|
|
50
|
+
await this.db.emitAsync("field:loaded", {
|
|
51
|
+
fieldKey: this.get("key"),
|
|
52
|
+
transaction
|
|
53
|
+
});
|
|
54
|
+
return field;
|
|
55
|
+
}
|
|
56
|
+
async syncSortByField(options) {
|
|
57
|
+
const collectionName = this.get("collectionName");
|
|
58
|
+
const collection = this.db.getCollection(collectionName);
|
|
59
|
+
await this.load(options);
|
|
60
|
+
await collection.sync({
|
|
61
|
+
force: false,
|
|
62
|
+
alter: {
|
|
63
|
+
drop: false
|
|
64
|
+
},
|
|
65
|
+
// @ts-ignore
|
|
66
|
+
transaction: options.transaction
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
async migrate({ isNew, ...options } = {}) {
|
|
70
|
+
let field;
|
|
71
|
+
try {
|
|
72
|
+
field = await this.load({
|
|
73
|
+
transaction: options.transaction
|
|
74
|
+
});
|
|
75
|
+
if (!field) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const collection = this.getFieldCollection();
|
|
79
|
+
if (isNew && collection.model.rawAttributes[this.get("name")] && this.get("unique")) {
|
|
80
|
+
collection.model.rawAttributes[this.get("name")].unique = false;
|
|
81
|
+
}
|
|
82
|
+
await field.sync(options);
|
|
83
|
+
if (isNew && this.get("unique")) {
|
|
84
|
+
await this.syncUniqueIndex({
|
|
85
|
+
transaction: options.transaction
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (isNew && field) {
|
|
90
|
+
field.remove();
|
|
91
|
+
}
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async remove(options) {
|
|
96
|
+
const collection = this.getFieldCollection();
|
|
97
|
+
if (!collection) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
return collection.removeFieldFromDb(this.get("name"), {
|
|
101
|
+
transaction: options.transaction
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async syncUniqueIndex(options) {
|
|
105
|
+
const unique = this.get("unique");
|
|
106
|
+
const collection = this.getFieldCollection();
|
|
107
|
+
const field = collection.getField(this.get("name"));
|
|
108
|
+
const columnName = collection.model.rawAttributes[this.get("name")].field;
|
|
109
|
+
const tableName = collection.model.tableName;
|
|
110
|
+
const queryInterface = this.db.sequelize.getQueryInterface();
|
|
111
|
+
const existsIndexes = await queryInterface.showIndex(collection.getTableNameWithSchema(), {
|
|
112
|
+
transaction: options.transaction
|
|
113
|
+
});
|
|
114
|
+
const existUniqueIndex = existsIndexes.find((item) => {
|
|
115
|
+
return item.unique && item.fields[0].attribute === columnName && item.fields.length === 1;
|
|
116
|
+
});
|
|
117
|
+
let existsUniqueConstraint;
|
|
118
|
+
const constraintName = `${tableName}_${field.name}_uk`;
|
|
119
|
+
if (existUniqueIndex) {
|
|
120
|
+
const existsUniqueConstraints = await queryInterface.showConstraint(
|
|
121
|
+
collection.getTableNameWithSchema(),
|
|
122
|
+
constraintName,
|
|
123
|
+
{}
|
|
124
|
+
);
|
|
125
|
+
existsUniqueConstraint = existsUniqueConstraints[0];
|
|
126
|
+
}
|
|
127
|
+
if (unique && !existsUniqueConstraint) {
|
|
128
|
+
await collection.sync({ ...options, force: false, alter: { drop: false } });
|
|
129
|
+
await queryInterface.addConstraint(collection.getTableNameWithSchema(), {
|
|
130
|
+
type: "unique",
|
|
131
|
+
fields: [columnName],
|
|
132
|
+
name: constraintName,
|
|
133
|
+
transaction: options.transaction
|
|
134
|
+
});
|
|
135
|
+
this.db.logger.info(`add unique index ${constraintName}`);
|
|
136
|
+
}
|
|
137
|
+
if (!unique && existsUniqueConstraint) {
|
|
138
|
+
await queryInterface.removeConstraint(collection.getTableNameWithSchema(), constraintName, {
|
|
139
|
+
transaction: options.transaction
|
|
140
|
+
});
|
|
141
|
+
this.db.logger.info(`remove unique index ${constraintName}`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async syncDefaultValue(options) {
|
|
145
|
+
const collection = this.getFieldCollection();
|
|
146
|
+
if (!collection) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const field = collection.getField(this.get("name"));
|
|
150
|
+
if (field.get("overriding")) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const queryInterface = collection.db.sequelize.getQueryInterface();
|
|
154
|
+
await queryInterface.changeColumn(
|
|
155
|
+
collection.getTableNameWithSchema(),
|
|
156
|
+
collection.model.rawAttributes[this.get("name")].field,
|
|
157
|
+
{
|
|
158
|
+
type: field.dataType,
|
|
159
|
+
defaultValue: options.defaultValue
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
transaction: options.transaction
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
async syncReferenceCheckOption(options) {
|
|
167
|
+
const reverseKey = this.get("reverseKey");
|
|
168
|
+
if (!reverseKey) return;
|
|
169
|
+
const reverseField = await this.db.getCollection("fields").repository.findOne({
|
|
170
|
+
filterByTk: reverseKey,
|
|
171
|
+
transaction: options.transaction
|
|
172
|
+
});
|
|
173
|
+
if (!reverseField) return;
|
|
174
|
+
reverseField.set("onDelete", this.get("onDelete"));
|
|
175
|
+
await reverseField.save({ hooks: false, transaction: options.transaction });
|
|
176
|
+
}
|
|
177
|
+
getFieldCollection() {
|
|
178
|
+
const collectionName = this.get("collectionName");
|
|
179
|
+
if (!this.db.hasCollection(collectionName)) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
return this.db.getCollection(collectionName);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
186
|
+
0 && (module.exports = {
|
|
187
|
+
FieldModel
|
|
188
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var models_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(models_exports);
|
|
17
|
+
__reExport(models_exports, require("./collection"), module.exports);
|
|
18
|
+
__reExport(models_exports, require("./field"), module.exports);
|
|
19
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
20
|
+
0 && (module.exports = {
|
|
21
|
+
...require("./collection"),
|
|
22
|
+
...require("./field")
|
|
23
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Repository } from '@tachybase/database';
|
|
2
|
+
interface LoadOptions {
|
|
3
|
+
filter?: any;
|
|
4
|
+
skipExist?: boolean;
|
|
5
|
+
}
|
|
6
|
+
export declare class CollectionRepository extends Repository {
|
|
7
|
+
private app;
|
|
8
|
+
setApp(app: any): void;
|
|
9
|
+
load(options?: LoadOptions): Promise<void>;
|
|
10
|
+
db2cm(collectionName: string): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
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_repository_exports = {};
|
|
29
|
+
__export(collection_repository_exports, {
|
|
30
|
+
CollectionRepository: () => CollectionRepository
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(collection_repository_exports);
|
|
33
|
+
var import_database = require("@tachybase/database");
|
|
34
|
+
var import_utils = require("@tachybase/utils");
|
|
35
|
+
var import_lodash = __toESM(require("lodash"));
|
|
36
|
+
class CollectionRepository extends import_database.Repository {
|
|
37
|
+
app;
|
|
38
|
+
setApp(app) {
|
|
39
|
+
this.app = app;
|
|
40
|
+
}
|
|
41
|
+
async load(options = {}) {
|
|
42
|
+
var _a, _b, _c;
|
|
43
|
+
const { filter, skipExist } = options;
|
|
44
|
+
const instances = await this.find({ filter, appends: ["fields"] });
|
|
45
|
+
const graphlib = import_utils.CollectionsGraph.graphlib();
|
|
46
|
+
const graph = new graphlib.Graph();
|
|
47
|
+
const nameMap = {};
|
|
48
|
+
const viewCollections = [];
|
|
49
|
+
for (const instance of instances) {
|
|
50
|
+
graph.setNode(instance.get("name"));
|
|
51
|
+
if (instance.get("view") || instance.get("sql")) {
|
|
52
|
+
viewCollections.push(instance.get("name"));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const instance of instances) {
|
|
56
|
+
const collectionName = instance.get("name");
|
|
57
|
+
nameMap[collectionName] = instance;
|
|
58
|
+
if (instance.get("inherits")) {
|
|
59
|
+
for (const parent of instance.get("inherits")) {
|
|
60
|
+
graph.setEdge(parent, collectionName);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (graph.nodeCount() === 0) return;
|
|
65
|
+
if (!graphlib.alg.isAcyclic(graph)) {
|
|
66
|
+
const cycles = graphlib.alg.findCycles(graph);
|
|
67
|
+
throw new Error(`Cyclic dependencies: ${cycles.map((cycle) => cycle.join(" -> ")).join(", ")}`);
|
|
68
|
+
}
|
|
69
|
+
const sortedNames = graphlib.alg.topsort(graph);
|
|
70
|
+
const lazyCollectionFields = /* @__PURE__ */ new Map();
|
|
71
|
+
for (const instanceName of sortedNames) {
|
|
72
|
+
if (!nameMap[instanceName]) continue;
|
|
73
|
+
const skipField = (() => {
|
|
74
|
+
if (viewCollections.includes(instanceName)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
const fields = nameMap[instanceName].get("fields");
|
|
78
|
+
return fields.filter((field) => field["type"] === "belongsTo" || field["type"] === "belongsToMany").map((field) => field.get("name"));
|
|
79
|
+
})();
|
|
80
|
+
if (import_lodash.default.isArray(skipField) && skipField.length) {
|
|
81
|
+
lazyCollectionFields.set(instanceName, skipField);
|
|
82
|
+
}
|
|
83
|
+
process.env.DEBUG_LOAD_COLLECTION && this.database.logger.debug(`load collection`, {
|
|
84
|
+
instanceName,
|
|
85
|
+
submodule: "CollectionRepository",
|
|
86
|
+
method: "load"
|
|
87
|
+
});
|
|
88
|
+
(_a = this.app) == null ? void 0 : _a.setMaintainingMessage(`load ${instanceName} collection`);
|
|
89
|
+
await nameMap[instanceName].load({ skipField });
|
|
90
|
+
}
|
|
91
|
+
for (const viewCollectionName of viewCollections) {
|
|
92
|
+
process.env.DEBUG_LOAD_COLLECTION_FIELDS && this.database.logger.debug(`load collection fields`, {
|
|
93
|
+
submodule: "CollectionRepository",
|
|
94
|
+
method: "load",
|
|
95
|
+
viewCollectionName
|
|
96
|
+
});
|
|
97
|
+
(_b = this.app) == null ? void 0 : _b.setMaintainingMessage(`load ${viewCollectionName} collection fields`);
|
|
98
|
+
await nameMap[viewCollectionName].loadFields({});
|
|
99
|
+
}
|
|
100
|
+
for (const [collectionName, skipField] of lazyCollectionFields) {
|
|
101
|
+
process.env.DEBUG_LOAD_COLLECTION_FIELDS && this.database.logger.debug(`load collection fields`, {
|
|
102
|
+
submodule: "CollectionRepository",
|
|
103
|
+
method: "load",
|
|
104
|
+
collectionName
|
|
105
|
+
});
|
|
106
|
+
(_c = this.app) == null ? void 0 : _c.setMaintainingMessage(`load ${collectionName} collection fields`);
|
|
107
|
+
await nameMap[collectionName].loadFields({ includeFields: skipField });
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async db2cm(collectionName) {
|
|
111
|
+
const collection = this.database.getCollection(collectionName);
|
|
112
|
+
if (await this.findOne({ filter: { name: collectionName } })) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
const options = collection.options;
|
|
116
|
+
const fields = [];
|
|
117
|
+
for (const [name, field] of collection.fields) {
|
|
118
|
+
fields.push({
|
|
119
|
+
name,
|
|
120
|
+
...field.options
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
await this.create({
|
|
124
|
+
values: {
|
|
125
|
+
...options,
|
|
126
|
+
fields,
|
|
127
|
+
from: "db2cm"
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
133
|
+
0 && (module.exports = {
|
|
134
|
+
CollectionRepository
|
|
135
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './collection-repository';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var repositories_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(repositories_exports);
|
|
17
|
+
__reExport(repositories_exports, require("./collection-repository"), module.exports);
|
|
18
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
19
|
+
0 && (module.exports = {
|
|
20
|
+
...require("./collection-repository")
|
|
21
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
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 collections_exports = {};
|
|
19
|
+
__export(collections_exports, {
|
|
20
|
+
default: () => collections_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(collections_exports);
|
|
23
|
+
var collections_default = {
|
|
24
|
+
async ["collections:setFields"](ctx, next) {
|
|
25
|
+
var _a;
|
|
26
|
+
const { filterByTk, values } = ctx.action.params;
|
|
27
|
+
const transaction = await ctx.app.db.sequelize.transaction();
|
|
28
|
+
try {
|
|
29
|
+
const fields = (_a = values.fields) == null ? void 0 : _a.map((f) => {
|
|
30
|
+
delete f.key;
|
|
31
|
+
return f;
|
|
32
|
+
});
|
|
33
|
+
const db = ctx.app.db;
|
|
34
|
+
const collectionModel = await db.getRepository("collections").findOne({
|
|
35
|
+
filter: {
|
|
36
|
+
name: filterByTk
|
|
37
|
+
},
|
|
38
|
+
transaction
|
|
39
|
+
});
|
|
40
|
+
const existFields = await collectionModel.getFields({
|
|
41
|
+
transaction
|
|
42
|
+
});
|
|
43
|
+
const needUpdateFields = fields.filter((f) => {
|
|
44
|
+
return existFields.find((ef) => ef.name === f.name);
|
|
45
|
+
}).map((f) => {
|
|
46
|
+
return {
|
|
47
|
+
...f,
|
|
48
|
+
key: existFields.find((ef) => ef.name === f.name).key
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
const needDestroyFields = existFields.filter((ef) => {
|
|
52
|
+
return !fields.find((f) => f.name === ef.name);
|
|
53
|
+
});
|
|
54
|
+
const needCreatedFields = fields.filter((f) => {
|
|
55
|
+
return !existFields.find((ef) => ef.name === f.name);
|
|
56
|
+
});
|
|
57
|
+
if (needDestroyFields.length) {
|
|
58
|
+
await db.getRepository("fields").destroy({
|
|
59
|
+
filterByTk: needDestroyFields.map((f) => f.key),
|
|
60
|
+
transaction
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
if (needUpdateFields.length) {
|
|
64
|
+
await db.getRepository("fields").updateMany({
|
|
65
|
+
records: needUpdateFields,
|
|
66
|
+
transaction
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (needCreatedFields.length) {
|
|
70
|
+
await db.getRepository("collections.fields", filterByTk).create({
|
|
71
|
+
values: needCreatedFields,
|
|
72
|
+
transaction
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
await collectionModel.loadFields({
|
|
76
|
+
transaction
|
|
77
|
+
});
|
|
78
|
+
const collection = db.getCollection(filterByTk);
|
|
79
|
+
await collection.sync({
|
|
80
|
+
force: false,
|
|
81
|
+
alter: {
|
|
82
|
+
drop: false
|
|
83
|
+
},
|
|
84
|
+
transaction
|
|
85
|
+
});
|
|
86
|
+
await transaction.commit();
|
|
87
|
+
} catch (e) {
|
|
88
|
+
await transaction.rollback();
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
await next();
|
|
92
|
+
}
|
|
93
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Context, Next } from '@tachybase/actions';
|
|
2
|
+
declare const _default: {
|
|
3
|
+
name: string;
|
|
4
|
+
actions: {
|
|
5
|
+
execute: (ctx: Context, next: Next) => Promise<void>;
|
|
6
|
+
setFields: (ctx: Context, next: Next) => Promise<void>;
|
|
7
|
+
update: (ctx: Context, next: Next) => Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,108 @@
|
|
|
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 sql_exports = {};
|
|
19
|
+
__export(sql_exports, {
|
|
20
|
+
default: () => sql_default
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(sql_exports);
|
|
23
|
+
var import_database = require("@tachybase/database");
|
|
24
|
+
const updateCollection = async (ctx, transaction) => {
|
|
25
|
+
const { filterByTk, values } = ctx.action.params;
|
|
26
|
+
const repo = ctx.db.getRepository("collections");
|
|
27
|
+
const collection = await repo.findOne({
|
|
28
|
+
filter: {
|
|
29
|
+
name: filterByTk
|
|
30
|
+
},
|
|
31
|
+
transaction
|
|
32
|
+
});
|
|
33
|
+
const existFields = await collection.getFields({ transaction });
|
|
34
|
+
const deletedFields = existFields.filter((field) => {
|
|
35
|
+
var _a;
|
|
36
|
+
return !((_a = values.fields) == null ? void 0 : _a.find((f) => f.name === field.name));
|
|
37
|
+
});
|
|
38
|
+
for (const field of deletedFields) {
|
|
39
|
+
await field.destroy({ transaction });
|
|
40
|
+
}
|
|
41
|
+
const upRes = await repo.update({
|
|
42
|
+
filterByTk,
|
|
43
|
+
values,
|
|
44
|
+
updateAssociationValues: ["fields"],
|
|
45
|
+
transaction
|
|
46
|
+
});
|
|
47
|
+
return { collection, upRes };
|
|
48
|
+
};
|
|
49
|
+
var sql_default = {
|
|
50
|
+
name: "sqlCollection",
|
|
51
|
+
actions: {
|
|
52
|
+
execute: async (ctx, next) => {
|
|
53
|
+
let {
|
|
54
|
+
values: { sql }
|
|
55
|
+
} = ctx.action.params;
|
|
56
|
+
sql = sql.trim().split(";").shift();
|
|
57
|
+
if (!sql) {
|
|
58
|
+
ctx.throw(400, ctx.t("SQL is empty"));
|
|
59
|
+
}
|
|
60
|
+
if (!/^select/i.test(sql) && !/^with([\s\S]+)select([\s\S]+)/i.test(sql)) {
|
|
61
|
+
ctx.throw(400, ctx.t("Only select query allowed"));
|
|
62
|
+
}
|
|
63
|
+
const tmpCollection = new import_database.SqlCollection({ name: "tmp", sql }, { database: ctx.db });
|
|
64
|
+
const model = tmpCollection.model;
|
|
65
|
+
const data = await model.findAll({ attributes: ["*"], limit: 5, raw: true });
|
|
66
|
+
let fields = {};
|
|
67
|
+
try {
|
|
68
|
+
fields = model.inferFields();
|
|
69
|
+
} catch (err) {
|
|
70
|
+
ctx.logger.warn(`resource: sql-collection, action: execute, error: ${err}`);
|
|
71
|
+
fields = {};
|
|
72
|
+
}
|
|
73
|
+
const sources = Array.from(new Set(Object.values(fields).map((field) => field.collection)));
|
|
74
|
+
ctx.body = { data, fields, sources };
|
|
75
|
+
await next();
|
|
76
|
+
},
|
|
77
|
+
setFields: async (ctx, next) => {
|
|
78
|
+
const transaction = await ctx.app.db.sequelize.transaction();
|
|
79
|
+
try {
|
|
80
|
+
const {
|
|
81
|
+
upRes: [collection]
|
|
82
|
+
} = await updateCollection(ctx, transaction);
|
|
83
|
+
await collection.loadFields({
|
|
84
|
+
transaction
|
|
85
|
+
});
|
|
86
|
+
await transaction.commit();
|
|
87
|
+
} catch (e) {
|
|
88
|
+
await transaction.rollback();
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
await next();
|
|
92
|
+
},
|
|
93
|
+
update: async (ctx, next) => {
|
|
94
|
+
const transaction = await ctx.app.db.sequelize.transaction();
|
|
95
|
+
try {
|
|
96
|
+
const { upRes } = await updateCollection(ctx, transaction);
|
|
97
|
+
const [collection] = upRes;
|
|
98
|
+
await collection.load({ transaction, resetFields: true });
|
|
99
|
+
await transaction.commit();
|
|
100
|
+
ctx.body = upRes;
|
|
101
|
+
} catch (e) {
|
|
102
|
+
await transaction.rollback();
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
await next();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|