@nocobase/plugin-data-source-main 1.5.0-beta.3 → 1.5.0-beta.30

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.
@@ -7,4 +7,4 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- (function(e,n){typeof exports=="object"&&typeof module!="undefined"?n(exports,require("@nocobase/client")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client"],n):(e=typeof globalThis!="undefined"?globalThis:e||self,n(e["@nocobase/plugin-data-source-main"]={},e["@nocobase/client"]))})(this,function(e,n){"use strict";var a=(e,n,o)=>new Promise((u,d)=>{var c=i=>{try{t(o.next(i))}catch(s){d(s)}},l=i=>{try{t(o.throw(i))}catch(s){d(s)}},t=i=>i.done?u(i.value):Promise.resolve(i.value).then(c,l);t((o=o.apply(e,n)).next())});class o extends n.Plugin{load(){return a(this,null,function*(){})}}e.default=o,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
10
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("@nocobase/client")):"function"==typeof define&&define.amd?define("@nocobase/plugin-data-source-main",["@nocobase/client"],t):"object"==typeof exports?exports["@nocobase/plugin-data-source-main"]=t(require("@nocobase/client")):e["@nocobase/plugin-data-source-main"]=t(e["@nocobase/client"])}(self,function(e){return function(){"use strict";var t={772:function(t){t.exports=e}},n={};function r(e){var o=n[e];if(void 0!==o)return o.exports;var u=n[e]={exports:{}};return t[e](u,u.exports,r),u.exports}r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,{a:t}),t},r.d=function(e,t){for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};r.r(o);var u=r(772);function i(e,t,n,r,o,u,i){try{var c=e[u](i),a=c.value}catch(e){n(e);return}c.done?t(a):Promise.resolve(a).then(r,o)}function c(e,t,n){return(c=s()?Reflect.construct:function(e,t,n){var r=[null];r.push.apply(r,t);var o=new(Function.bind.apply(e,r));return n&&l(o,n.prototype),o}).apply(null,arguments)}function a(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function l(e,t){return(l=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function p(e){var t="function"==typeof Map?new Map:void 0;return(p=function(e){var n;if(null===e||(n=e,-1===Function.toString.call(n).indexOf("[native code]")))return e;if("function"!=typeof e)throw TypeError("Super expression must either be null or a function");if(void 0!==t){if(t.has(e))return t.get(e);t.set(e,r)}function r(){return c(e,arguments,f(this).constructor)}return r.prototype=Object.create(e.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),l(r,e)})(e)}function s(){try{var e=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(e){}return(s=function(){return!!e})()}var b=function(e){var t,n,r;function o(){var e,t,n;return!function(e,t){if(!(e instanceof t))throw TypeError("Cannot call a class as a function")}(this,o),e=this,t=o,n=arguments,t=f(t),function(e,t){return t&&("object"===function(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}(t)||"function"==typeof t)?t:function(e){if(void 0===e)throw ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e)}(e,s()?Reflect.construct(t,n||[],f(e).constructor):t.apply(e,n))}return!function(e,t){if("function"!=typeof t&&null!==t)throw TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&l(e,t)}(o,e),t=o,n=[{key:"load",value:function(){var e;return(e=function(){return function(e,t){var n,r,o,u,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return u={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function c(u){return function(c){return function(u){if(n)throw TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(o=2&u[0]?r.return:u[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,u[1])).done)return o;switch(r=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return i.label++,{value:u[1],done:!1};case 5:i.label++,r=u[1],u=[0];continue;case 7:u=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===u[0]||2===u[0])){i=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){i.label=u[1];break}if(6===u[0]&&i.label<o[1]){i.label=o[1],o=u;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(u);break}o[2]&&i.ops.pop(),i.trys.pop();continue}u=t.call(e,i)}catch(e){u=[6,e],r=0}finally{n=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,c])}}}(this,function(e){return[2]})},function(){var t=this,n=arguments;return new Promise(function(r,o){var u=e.apply(t,n);function c(e){i(u,r,o,c,a,"next",e)}function a(e){i(u,r,o,c,a,"throw",e)}c(void 0)})})()}}],a(t.prototype,n),o}(p(u.Plugin));return o.default=b,o}()});
@@ -8,14 +8,13 @@
8
8
  */
9
9
 
10
10
  module.exports = {
11
- "@nocobase/client": "1.5.0-beta.3",
12
- "@nocobase/database": "1.5.0-beta.3",
13
- "@nocobase/plugin-error-handler": "1.5.0-beta.3",
14
- "@nocobase/server": "1.5.0-beta.3",
15
- "async-mutex": "0.3.2",
11
+ "@nocobase/client": "1.5.0-beta.30",
12
+ "@nocobase/database": "1.5.0-beta.30",
13
+ "@nocobase/plugin-error-handler": "1.5.0-beta.30",
14
+ "@nocobase/server": "1.5.0-beta.30",
16
15
  "lodash": "4.17.21",
17
- "@nocobase/test": "1.5.0-beta.3",
18
- "@nocobase/utils": "1.5.0-beta.3",
16
+ "@nocobase/test": "1.5.0-beta.30",
17
+ "@nocobase/utils": "1.5.0-beta.30",
19
18
  "sequelize": "6.35.2",
20
19
  "dayjs": "1.11.10"
21
20
  };
@@ -83,7 +83,7 @@ function afterCreateForForeignKeyField(db) {
83
83
  }
84
84
  return data;
85
85
  }
86
- async function createFieldIfNotExists({ values, transaction }) {
86
+ async function createFieldIfNotExists({ values, transaction, interfaceType = null }) {
87
87
  const { collectionName, name } = values;
88
88
  if (!collectionName || !name) {
89
89
  throw new Error(`field options invalid`);
@@ -104,13 +104,17 @@ function afterCreateForForeignKeyField(db) {
104
104
  instance.set("isForeignKey", true);
105
105
  await instance.save({ transaction });
106
106
  } else {
107
- const creatInstance = await r.create({
107
+ const createOptions = {
108
108
  values: {
109
109
  isForeignKey: true,
110
110
  ...values
111
111
  },
112
112
  transaction
113
- });
113
+ };
114
+ if (interfaceType === "m2o") {
115
+ createOptions["context"] = {};
116
+ }
117
+ const creatInstance = await r.create(createOptions);
114
118
  creatInstance.set("sort", 1);
115
119
  await creatInstance.save({ transaction });
116
120
  }
@@ -149,13 +153,15 @@ function afterCreateForForeignKeyField(db) {
149
153
  collectionName: target,
150
154
  ...values
151
155
  },
156
+ interfaceType,
152
157
  transaction
153
158
  });
154
159
  } else if (["obo", "m2o"].includes(interfaceType)) {
155
160
  const values = generateFkOptions(collectionName, foreignKey);
156
161
  await createFieldIfNotExists({
157
162
  values: { collectionName, ...values },
158
- transaction
163
+ transaction,
164
+ interfaceType
159
165
  });
160
166
  } else if (["linkTo", "m2m"].includes(interfaceType)) {
161
167
  if (type !== "belongsToMany") {
@@ -0,0 +1,13 @@
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
+ up(): Promise<void>;
13
+ }
@@ -0,0 +1,59 @@
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 update_bigint_to_unixtimestamp_exports = {};
28
+ __export(update_bigint_to_unixtimestamp_exports, {
29
+ default: () => update_bigint_to_unixtimestamp_default
30
+ });
31
+ module.exports = __toCommonJS(update_bigint_to_unixtimestamp_exports);
32
+ var import_server = require("@nocobase/server");
33
+ /* istanbul ignore file -- @preserve */
34
+ class update_bigint_to_unixtimestamp_default extends import_server.Migration {
35
+ on = "afterLoad";
36
+ async up() {
37
+ var _a, _b;
38
+ const Field = this.context.db.getRepository("fields");
39
+ const fields = await Field.find({
40
+ filter: {
41
+ interface: "unixTimestamp",
42
+ type: "bigInt"
43
+ }
44
+ });
45
+ for (const field of fields) {
46
+ const uiSchema = field.get("uiSchema");
47
+ uiSchema["x-component-props"] = {
48
+ picker: "date",
49
+ dateFormat: "YYYY-MM-DD",
50
+ timeFormat: "HH:mm:ss",
51
+ showTime: ((_a = uiSchema["x-component-props"]) == null ? void 0 : _a.showTime) || true,
52
+ accuracy: ((_b = uiSchema["x-component-props"]) == null ? void 0 : _b.accuracy) || "second"
53
+ };
54
+ field.set("type", "unixTimestamp");
55
+ field.set("uiSchema", uiSchema);
56
+ await field.save();
57
+ }
58
+ }
59
+ }
@@ -22,5 +22,6 @@ export declare class FieldModel extends MagicAttributeModel {
22
22
  }): Promise<void>;
23
23
  syncReferenceCheckOption(options: Transactionable): Promise<void>;
24
24
  protected getFieldCollection(): Collection | null;
25
+ toJSON(): any;
25
26
  }
26
27
  export {};
@@ -7,9 +7,11 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
+ var __create = Object.create;
10
11
  var __defProp = Object.defineProperty;
11
12
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
13
  var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
13
15
  var __hasOwnProp = Object.prototype.hasOwnProperty;
14
16
  var __export = (target, all) => {
15
17
  for (var name in all)
@@ -23,6 +25,14 @@ var __copyProps = (to, from, except, desc) => {
23
25
  }
24
26
  return to;
25
27
  };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
26
36
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
37
  var field_exports = {};
28
38
  __export(field_exports, {
@@ -30,6 +40,7 @@ __export(field_exports, {
30
40
  });
31
41
  module.exports = __toCommonJS(field_exports);
32
42
  var import_database = require("@nocobase/database");
43
+ var import_lodash = __toESM(require("lodash"));
33
44
  class FieldModel extends import_database.MagicAttributeModel {
34
45
  get db() {
35
46
  return this.constructor.database;
@@ -48,10 +59,13 @@ class FieldModel extends import_database.MagicAttributeModel {
48
59
  if (skipExist && collection.hasField(name)) {
49
60
  return collection.getField(name);
50
61
  }
51
- const options = this.get();
62
+ const options = this.toJSON();
52
63
  const field = await (async () => {
53
64
  await new Promise((resolve) => setTimeout(resolve, 0));
54
- return collection.setField(name, options);
65
+ return collection.setField(name, {
66
+ ...options,
67
+ __sort: this.get("sort")
68
+ });
55
69
  })();
56
70
  await this.db.emitAsync("field:loaded", {
57
71
  fieldKey: this.get("key"),
@@ -169,6 +183,13 @@ class FieldModel extends import_database.MagicAttributeModel {
169
183
  }
170
184
  return this.db.getCollection(collectionName);
171
185
  }
186
+ toJSON() {
187
+ const json = super.toJSON();
188
+ if (json.interface === "unixTimestamp" && json.accuracy) {
189
+ import_lodash.default.set(json, "uiSchema.x-component-props.accuracy", json.accuracy);
190
+ }
191
+ return json;
192
+ }
172
193
  }
173
194
  // Annotate the CommonJS export names for ESM import in node:
174
195
  0 && (module.exports = {
@@ -61,7 +61,7 @@ var collections_default = {
61
61
  ...field.options
62
62
  };
63
63
  }),
64
- "sort"
64
+ "__sort"
65
65
  );
66
66
  results.push(obj);
67
67
  });
@@ -11,7 +11,7 @@ import { Plugin } from '@nocobase/server';
11
11
  export declare class PluginDataSourceMainServer extends Plugin {
12
12
  private loadFilter;
13
13
  setLoadFilter(filter: Filter): void;
14
- onSync(message: any): Promise<void>;
14
+ handleSyncMessage(message: any): Promise<void>;
15
15
  beforeLoad(): Promise<void>;
16
16
  load(): Promise<void>;
17
17
  install(): Promise<void>;
@@ -42,7 +42,6 @@ __export(server_exports, {
42
42
  module.exports = __toCommonJS(server_exports);
43
43
  var import_database = require("@nocobase/database");
44
44
  var import_server = require("@nocobase/server");
45
- var import_async_mutex = require("async-mutex");
46
45
  var import_lodash = __toESM(require("lodash"));
47
46
  var import_path = __toESM(require("path"));
48
47
  var import__ = require(".");
@@ -61,9 +60,9 @@ class PluginDataSourceMainServer extends import_server.Plugin {
61
60
  setLoadFilter(filter) {
62
61
  this.loadFilter = filter;
63
62
  }
64
- async onSync(message) {
63
+ async handleSyncMessage(message) {
65
64
  const { type, collectionName } = message;
66
- if (type === "newCollection") {
65
+ if (type === "syncCollection") {
67
66
  const collectionModel = await this.app.db.getCollection("collections").repository.findOne({
68
67
  filter: {
69
68
  name: collectionName
@@ -71,6 +70,22 @@ class PluginDataSourceMainServer extends import_server.Plugin {
71
70
  });
72
71
  await collectionModel.load();
73
72
  }
73
+ if (type === "removeField") {
74
+ const { collectionName: collectionName2, fieldName } = message;
75
+ const collection = this.app.db.getCollection(collectionName2);
76
+ if (!collection) {
77
+ return;
78
+ }
79
+ return collection.removeFieldFromDb(fieldName);
80
+ }
81
+ if (type === "removeCollection") {
82
+ const { collectionName: collectionName2 } = message;
83
+ const collection = this.app.db.getCollection(collectionName2);
84
+ if (!collection) {
85
+ return;
86
+ }
87
+ collection.remove();
88
+ }
74
89
  }
75
90
  async beforeLoad() {
76
91
  this.app.db.registerRepositories({
@@ -88,6 +103,11 @@ class PluginDataSourceMainServer extends import_server.Plugin {
88
103
  }
89
104
  });
90
105
  this.app.db.on("collections.beforeCreate", (0, import_beforeCreateForViewCollection.beforeCreateForViewCollection)(this.db));
106
+ this.app.db.on("collections.beforeCreate", async (model, options) => {
107
+ if (this.app.db.getCollection(model.get("name")) && model.get("from") !== "db2cm" && !model.get("isThrough")) {
108
+ throw new Error(`Collection named ${model.get("name")} already exists`);
109
+ }
110
+ });
91
111
  this.app.db.on(
92
112
  "collections.afterSaveWithAssociations",
93
113
  async (model, { context, transaction }) => {
@@ -95,10 +115,15 @@ class PluginDataSourceMainServer extends import_server.Plugin {
95
115
  await model.migrate({
96
116
  transaction
97
117
  });
98
- this.app.syncManager.publish(this.name, {
99
- type: "newCollection",
100
- collectionName: model.get("name")
101
- });
118
+ this.sendSyncMessage(
119
+ {
120
+ type: "syncCollection",
121
+ collectionName: model.get("name")
122
+ },
123
+ {
124
+ transaction
125
+ }
126
+ );
102
127
  }
103
128
  }
104
129
  );
@@ -112,6 +137,15 @@ class PluginDataSourceMainServer extends import_server.Plugin {
112
137
  removeOptions["cascade"] = true;
113
138
  }
114
139
  await model.remove(removeOptions);
140
+ this.sendSyncMessage(
141
+ {
142
+ type: "removeCollection",
143
+ collectionName: model.get("name")
144
+ },
145
+ {
146
+ transaction: options.transaction
147
+ }
148
+ );
115
149
  });
116
150
  this.app.db.on("fields.beforeCreate", (0, import_beforeCreateCheckFieldInMySQL.beforeCreateCheckFieldInMySQL)(this.app.db));
117
151
  this.app.db.on("fields.beforeCreate", (0, import_hooks.beforeCreateForReverseField)(this.app.db));
@@ -222,14 +256,33 @@ class PluginDataSourceMainServer extends import_server.Plugin {
222
256
  }
223
257
  };
224
258
  await collection.sync(syncOptions);
259
+ this.sendSyncMessage(
260
+ {
261
+ type: "syncCollection",
262
+ collectionName: model.get("collectionName")
263
+ },
264
+ {
265
+ transaction
266
+ }
267
+ );
225
268
  }
226
269
  });
227
270
  this.app.db.on("fields.beforeDestroy", (0, import_beforeDestoryField.beforeDestoryField)(this.app.db));
228
271
  this.app.db.on("fields.beforeDestroy", (0, import_hooks.beforeDestroyForeignKey)(this.app.db));
229
- const mutex = new import_async_mutex.Mutex();
230
272
  this.app.db.on("fields.beforeDestroy", async (model, options) => {
231
- await mutex.runExclusive(async () => {
273
+ const lockKey = `${this.name}:fields.beforeDestroy:${model.get("collectionName")}`;
274
+ await this.app.lockManager.runExclusive(lockKey, async () => {
232
275
  await model.remove(options);
276
+ this.sendSyncMessage(
277
+ {
278
+ type: "removeField",
279
+ collectionName: model.get("collectionName"),
280
+ fieldName: model.get("name")
281
+ },
282
+ {
283
+ transaction: options.transaction
284
+ }
285
+ );
233
286
  });
234
287
  });
235
288
  this.app.db.on("fields.afterDestroy", async (model, options) => {
package/package.json CHANGED
@@ -4,25 +4,25 @@
4
4
  "displayName.zh-CN": "数据源:主数据库",
5
5
  "description": "NocoBase main database, supports relational databases such as PostgreSQL, MySQL, MariaDB and so on.",
6
6
  "description.zh-CN": "NocoBase 主数据库,支持 PostgreSQL、MySQL、MariaDB 等关系型数据库。",
7
- "version": "1.5.0-beta.3",
7
+ "version": "1.5.0-beta.30",
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",
11
11
  "license": "AGPL-3.0",
12
12
  "devDependencies": {
13
13
  "@hapi/topo": "^6.0.0",
14
- "async-mutex": "^0.3.2",
15
14
  "toposort": "^2.0.2"
16
15
  },
17
16
  "peerDependencies": {
18
17
  "@nocobase/client": "1.x",
19
18
  "@nocobase/database": "1.x",
20
19
  "@nocobase/plugin-error-handler": "1.x",
20
+ "@nocobase/plugin-field-sort": "1.x",
21
21
  "@nocobase/server": "1.x",
22
22
  "@nocobase/test": "1.x",
23
23
  "@nocobase/utils": "1.x"
24
24
  },
25
- "gitHead": "81afcf4affdb652faf4636e6d9351828ce8906be",
25
+ "gitHead": "40c48b2ecf3e2315a76dcb729dbda825b6a1331c",
26
26
  "keywords": [
27
27
  "Data sources"
28
28
  ]