@nocobase/plugin-data-source-main 1.0.0-alpha.9 → 1.0.1-alpha.2

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.
@@ -8,15 +8,14 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- "@nocobase/client": "1.0.0-alpha.9",
12
- "@nocobase/database": "1.0.0-alpha.9",
13
- "@nocobase/plugin-error-handler": "1.0.0-alpha.9",
14
- "@nocobase/server": "1.0.0-alpha.9",
11
+ "@nocobase/client": "1.0.1-alpha.2",
12
+ "@nocobase/database": "1.0.1-alpha.2",
13
+ "@nocobase/plugin-error-handler": "1.0.1-alpha.2",
14
+ "@nocobase/server": "1.0.1-alpha.2",
15
15
  "async-mutex": "0.3.2",
16
16
  "lodash": "4.17.21",
17
- "@nocobase/test": "1.0.0-alpha.9",
18
- "@nocobase/utils": "1.0.0-alpha.9",
17
+ "@nocobase/test": "1.0.1-alpha.2",
18
+ "@nocobase/utils": "1.0.1-alpha.2",
19
19
  "sequelize": "6.35.2",
20
- "@nocobase/actions": "1.0.0-alpha.9",
21
20
  "dayjs": "1.11.10"
22
21
  };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Migration } from '@nocobase/server';
10
+ export default class extends Migration {
11
+ on: string;
12
+ appVersion: string;
13
+ up(): Promise<void>;
14
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var fix_target_option_in_tree_collection_exports = {};
28
+ __export(fix_target_option_in_tree_collection_exports, {
29
+ default: () => fix_target_option_in_tree_collection_default
30
+ });
31
+ module.exports = __toCommonJS(fix_target_option_in_tree_collection_exports);
32
+ var import_server = require("@nocobase/server");
33
+ class fix_target_option_in_tree_collection_default extends import_server.Migration {
34
+ on = "afterLoad";
35
+ // 'beforeLoad' or 'afterLoad'
36
+ appVersion = "<1.0.0-alpha.15";
37
+ async up() {
38
+ const treeCollections = await this.app.db.getRepository("collections").find({
39
+ appends: ["fields"],
40
+ filter: {
41
+ "options.tree": "adjacencyList"
42
+ }
43
+ });
44
+ for (const treeCollection of treeCollections) {
45
+ const fields = treeCollection.get("fields");
46
+ for (const field of fields) {
47
+ if (!["belongsTo", "hasMany", "belongsToMany", "hasOne"].includes(field.get("type")) && field.get("target")) {
48
+ field.set("target", void 0);
49
+ await field.save();
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
@@ -48,6 +48,7 @@ class CollectionRepository extends import_database.Repository {
48
48
  this.app = app;
49
49
  }
50
50
  async load(options = {}) {
51
+ this.database.logger.debug("loading collections...");
51
52
  const { filter, skipExist } = options;
52
53
  const instances = await this.find({ filter, appends: ["fields"] });
53
54
  const graphlib = import_utils.CollectionsGraph.graphlib();
@@ -90,8 +91,7 @@ class CollectionRepository extends import_database.Repository {
90
91
  if (import_lodash.default.isArray(skipField) && skipField.length) {
91
92
  lazyCollectionFields.set(instanceName, skipField);
92
93
  }
93
- this.database.logger.debug(`load collection`, {
94
- instanceName,
94
+ this.database.logger.trace(`load ${instanceName} collection`, {
95
95
  submodule: "CollectionRepository",
96
96
  method: "load"
97
97
  });
@@ -100,7 +100,7 @@ class CollectionRepository extends import_database.Repository {
100
100
  }
101
101
  const fieldWithSourceAttributes = /* @__PURE__ */ new Map();
102
102
  for (const viewCollectionName of viewCollections) {
103
- this.database.logger.debug(`load collection fields`, {
103
+ this.database.logger.trace(`load collection fields`, {
104
104
  submodule: "CollectionRepository",
105
105
  method: "load",
106
106
  viewCollectionName
@@ -122,7 +122,7 @@ class CollectionRepository extends import_database.Repository {
122
122
  await nameMap[viewCollectionName].loadFields({ skipField });
123
123
  }
124
124
  for (const [collectionName, skipField] of lazyCollectionFields) {
125
- this.database.logger.debug(`load collection fields`, {
125
+ this.database.logger.trace(`load collection fields`, {
126
126
  submodule: "CollectionRepository",
127
127
  method: "load",
128
128
  collectionName
@@ -131,7 +131,7 @@ class CollectionRepository extends import_database.Repository {
131
131
  await nameMap[collectionName].loadFields({ includeFields: skipField });
132
132
  }
133
133
  for (const [collectionName, skipField] of fieldWithSourceAttributes) {
134
- this.database.logger.debug(`load collection fields`, {
134
+ this.database.logger.trace(`load collection fields`, {
135
135
  submodule: "CollectionRepository",
136
136
  method: "load",
137
137
  collectionName
@@ -139,6 +139,7 @@ class CollectionRepository extends import_database.Repository {
139
139
  this.app.setMaintainingMessage(`load ${collectionName} collection fields`);
140
140
  await nameMap[collectionName].loadFields({ includeFields: skipField });
141
141
  }
142
+ this.database.logger.debug("collections loaded");
142
143
  }
143
144
  async db2cm(collectionName) {
144
145
  const collection = this.database.getCollection(collectionName);
@@ -52,7 +52,6 @@ var import_beforeCreateForValidateField = require("./hooks/beforeCreateForValida
52
52
  var import_beforeCreateForViewCollection = require("./hooks/beforeCreateForViewCollection");
53
53
  var import_models = require("./models");
54
54
  var import_collections = __toESM(require("./resourcers/collections"));
55
- var import_sql = __toESM(require("./resourcers/sql"));
56
55
  var import_views = __toESM(require("./resourcers/views"));
57
56
  class PluginDataSourceMainServer extends import_server.Plugin {
58
57
  schema;
@@ -259,10 +258,6 @@ class PluginDataSourceMainServer extends import_server.Plugin {
259
258
  name: `pm.data-source-manager.collection-view `,
260
259
  actions: ["dbViews:*"]
261
260
  });
262
- this.app.acl.registerSnippet({
263
- name: `pm.data-source-manager.collection-sql `,
264
- actions: ["sqlCollection:*"]
265
- });
266
261
  }
267
262
  async load() {
268
263
  await this.importCollections(import_path.default.resolve(__dirname, "./collections"));
@@ -285,7 +280,6 @@ class PluginDataSourceMainServer extends import_server.Plugin {
285
280
  await next();
286
281
  });
287
282
  this.app.resource(import_views.default);
288
- this.app.resource(import_sql.default);
289
283
  this.app.actions(import_collections.default);
290
284
  const handleFieldSource = (fields) => {
291
285
  var _a;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "displayName.zh-CN": "数据源:主数据库",
5
5
  "description": "NocoBase main database, supports relational databases such as MySQL, PostgreSQL, SQLite and so on.",
6
6
  "description.zh-CN": "NocoBase 主数据库,支持 MySQL、PostgreSQL、SQLite 等关系型数据库。",
7
- "version": "1.0.0-alpha.9",
7
+ "version": "1.0.1-alpha.2",
8
8
  "main": "./dist/server/index.js",
9
9
  "homepage": "https://docs.nocobase.com/handbook/data-source-main",
10
10
  "homepage.zh-CN": "https://docs-cn.nocobase.com/handbook/data-source-main",
@@ -22,7 +22,7 @@
22
22
  "@nocobase/test": "1.x",
23
23
  "@nocobase/utils": "1.x"
24
24
  },
25
- "gitHead": "562d6ae965317098df05db756d160e03363dcec5",
25
+ "gitHead": "fc5a8e3c812516f787cb22d3d198f058f45b1963",
26
26
  "keywords": [
27
27
  "Data sources"
28
28
  ]
@@ -1,18 +0,0 @@
1
- /**
2
- * This file is part of the NocoBase (R) project.
3
- * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
- * Authors: NocoBase Team.
5
- *
6
- * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
- * For more information, please refer to: https://www.nocobase.com/agreement.
8
- */
9
- import { Context, Next } from '@nocobase/actions';
10
- declare const _default: {
11
- name: string;
12
- actions: {
13
- execute: (ctx: Context, next: Next) => Promise<void>;
14
- setFields: (ctx: Context, next: Next) => Promise<void>;
15
- update: (ctx: Context, next: Next) => Promise<void>;
16
- };
17
- };
18
- export default _default;
@@ -1,115 +0,0 @@
1
- /**
2
- * This file is part of the NocoBase (R) project.
3
- * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
- * Authors: NocoBase Team.
5
- *
6
- * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
- * For more information, please refer to: https://www.nocobase.com/agreement.
8
- */
9
-
10
- var __defProp = Object.defineProperty;
11
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
- var __getOwnPropNames = Object.getOwnPropertyNames;
13
- var __hasOwnProp = Object.prototype.hasOwnProperty;
14
- var __export = (target, all) => {
15
- for (var name in all)
16
- __defProp(target, name, { get: all[name], enumerable: true });
17
- };
18
- var __copyProps = (to, from, except, desc) => {
19
- if (from && typeof from === "object" || typeof from === "function") {
20
- for (let key of __getOwnPropNames(from))
21
- if (!__hasOwnProp.call(to, key) && key !== except)
22
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
- }
24
- return to;
25
- };
26
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
- var sql_exports = {};
28
- __export(sql_exports, {
29
- default: () => sql_default
30
- });
31
- module.exports = __toCommonJS(sql_exports);
32
- var import_database = require("@nocobase/database");
33
- const updateCollection = async (ctx, transaction) => {
34
- const { filterByTk, values } = ctx.action.params;
35
- const repo = ctx.db.getRepository("collections");
36
- const collection = await repo.findOne({
37
- filter: {
38
- name: filterByTk
39
- },
40
- transaction
41
- });
42
- const existFields = await collection.getFields({ transaction });
43
- const deletedFields = existFields.filter((field) => {
44
- var _a;
45
- return !((_a = values.fields) == null ? void 0 : _a.find((f) => f.name === field.name));
46
- });
47
- for (const field of deletedFields) {
48
- await field.destroy({ transaction });
49
- }
50
- const upRes = await repo.update({
51
- filterByTk,
52
- values,
53
- updateAssociationValues: ["fields"],
54
- transaction
55
- });
56
- return { collection, upRes };
57
- };
58
- var sql_default = {
59
- name: "sqlCollection",
60
- actions: {
61
- execute: async (ctx, next) => {
62
- let { sql } = ctx.action.params.values || {};
63
- if (!sql) {
64
- ctx.throw(400, ctx.t("Please enter a SQL statement"));
65
- }
66
- sql = sql.trim().split(";").shift();
67
- if (!/^select/i.test(sql) && !/^with([\s\S]+)select([\s\S]+)/i.test(sql)) {
68
- ctx.throw(400, ctx.t("Only supports SELECT statements or WITH clauses"));
69
- }
70
- const tmpCollection = new import_database.SqlCollection({ name: "tmp", sql }, { database: ctx.db });
71
- const model = tmpCollection.model;
72
- const data = await model.findAll({ attributes: ["*"], limit: 5, raw: true });
73
- let fields = {};
74
- try {
75
- fields = model.inferFields();
76
- } catch (err) {
77
- ctx.logger.warn(`resource: sql-collection, action: execute, error: ${err}`);
78
- fields = {};
79
- }
80
- const sources = Array.from(new Set(Object.values(fields).map((field) => field.collection)));
81
- ctx.body = { data, fields, sources };
82
- await next();
83
- },
84
- setFields: async (ctx, next) => {
85
- const transaction = await ctx.app.db.sequelize.transaction();
86
- try {
87
- const {
88
- upRes: [collection]
89
- } = await updateCollection(ctx, transaction);
90
- await collection.loadFields({
91
- transaction
92
- });
93
- await transaction.commit();
94
- } catch (e) {
95
- await transaction.rollback();
96
- throw e;
97
- }
98
- await next();
99
- },
100
- update: async (ctx, next) => {
101
- const transaction = await ctx.app.db.sequelize.transaction();
102
- try {
103
- const { upRes } = await updateCollection(ctx, transaction);
104
- const [collection] = upRes;
105
- await collection.load({ transaction, resetFields: true });
106
- await transaction.commit();
107
- ctx.body = upRes;
108
- } catch (e) {
109
- await transaction.rollback();
110
- throw e;
111
- }
112
- await next();
113
- }
114
- }
115
- };