@nocobase/server 1.9.0-alpha.14 → 1.9.0-alpha.15

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.
@@ -37,6 +37,8 @@ import { Environment } from './environment';
37
37
  import { ServiceContainer } from './service-container';
38
38
  import { EventQueue, EventQueueOptions } from './event-queue';
39
39
  import { BackgroundJobManager, BackgroundJobManagerOptions } from './background-job-manager';
40
+ import { RedisConfig, RedisConnectionManager } from './redis-connection-manager';
41
+ import { WorkerIdAllocator } from './worker-id-allocator';
40
42
  export type PluginType = string | typeof Plugin;
41
43
  export type PluginConfiguration = PluginType | [PluginType, any];
42
44
  export interface ResourceManagerOptions {
@@ -57,8 +59,9 @@ export interface AppTelemetryOptions extends TelemetryOptions {
57
59
  enabled?: boolean;
58
60
  }
59
61
  export interface ApplicationOptions {
60
- instanceId?: string;
62
+ instanceId?: number;
61
63
  database?: IDatabaseOptions | Database;
64
+ redisConfig?: RedisConfig;
62
65
  cacheManager?: CacheManagerOptions;
63
66
  /**
64
67
  * this property is deprecated and should not be used.
@@ -131,9 +134,12 @@ export type MaintainingCommandStatus = {
131
134
  status: MaintainingStatus;
132
135
  error?: Error;
133
136
  };
137
+ interface SnowflakeIdGenerator {
138
+ generate(): number | BigInt;
139
+ }
134
140
  export declare class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
135
141
  options: ApplicationOptions;
136
- readonly instanceId: string;
142
+ private _instanceId;
137
143
  /**
138
144
  * @internal
139
145
  */
@@ -168,6 +174,9 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
168
174
  /**
169
175
  * @internal
170
176
  */
177
+ redisConnectionManager: RedisConnectionManager;
178
+ workerIdAllocator: WorkerIdAllocator;
179
+ snowflakeIdGenerator: SnowflakeIdGenerator;
171
180
  pubSubManager: PubSubManager;
172
181
  syncMessageManager: SyncMessageManager;
173
182
  requestLogger: Logger;
@@ -186,6 +195,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
186
195
  private static staticCommands;
187
196
  static addCommand(callback: (app: Application) => void): void;
188
197
  private _sqlLogger;
198
+ get instanceId(): number;
189
199
  get sqlLogger(): Logger;
190
200
  protected _logger: SystemLogger;
191
201
  get logger(): SystemLogger;
@@ -309,6 +319,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
309
319
  actions(handlers: any, options?: ActionsOptions): void;
310
320
  command(name: string, desc?: string, opts?: CommandOptions): AppCommand;
311
321
  findCommand(name: string): Command;
322
+ private disposeServices;
312
323
  /**
313
324
  * @internal
314
325
  */
@@ -51,12 +51,12 @@ var import_logger = require("@nocobase/logger");
51
51
  var import_telemetry = require("@nocobase/telemetry");
52
52
  var import_lock_manager = require("@nocobase/lock-manager");
53
53
  var import_utils = require("@nocobase/utils");
54
+ var import_snowflake_id = require("@nocobase/snowflake-id");
54
55
  var import_crypto = require("crypto");
55
56
  var import_glob = __toESM(require("glob"));
56
57
  var import_koa = __toESM(require("koa"));
57
58
  var import_koa_compose = __toESM(require("koa-compose"));
58
59
  var import_lodash = __toESM(require("lodash"));
59
- var import_nanoid = require("nanoid");
60
60
  var import_path = __toESM(require("path"));
61
61
  var import_semver = __toESM(require("semver"));
62
62
  var import_acl = require("./acl");
@@ -84,11 +84,13 @@ var import_environment = require("./environment");
84
84
  var import_service_container = require("./service-container");
85
85
  var import_event_queue = require("./event-queue");
86
86
  var import_background_job_manager = require("./background-job-manager");
87
+ var import_redis_connection_manager = require("./redis-connection-manager");
88
+ var import_worker_id_allocator = require("./worker-id-allocator");
89
+ var import_snowflake_id_field = require("./snowflake-id-field");
87
90
  const _Application = class _Application extends import_koa.default {
88
91
  constructor(options) {
89
92
  super();
90
93
  this.options = options;
91
- this.instanceId = options.instanceId || (0, import_nanoid.nanoid)();
92
94
  this.context.reqId = (0, import_crypto.randomUUID)();
93
95
  this.rawOptions = this.name == "main" ? import_lodash.default.cloneDeep(options) : {};
94
96
  this.init();
@@ -96,7 +98,7 @@ const _Application = class _Application extends import_koa.default {
96
98
  this._appSupervisor.addApp(this);
97
99
  }
98
100
  }
99
- instanceId;
101
+ _instanceId;
100
102
  /**
101
103
  * @internal
102
104
  */
@@ -124,6 +126,9 @@ const _Application = class _Application extends import_koa.default {
124
126
  /**
125
127
  * @internal
126
128
  */
129
+ redisConnectionManager;
130
+ workerIdAllocator;
131
+ snowflakeIdGenerator;
127
132
  pubSubManager;
128
133
  syncMessageManager;
129
134
  requestLogger;
@@ -142,6 +147,9 @@ const _Application = class _Application extends import_koa.default {
142
147
  this.staticCommands.push(callback);
143
148
  }
144
149
  _sqlLogger;
150
+ get instanceId() {
151
+ return this._instanceId;
152
+ }
145
153
  get sqlLogger() {
146
154
  return this._sqlLogger;
147
155
  }
@@ -392,16 +400,10 @@ const _Application = class _Application extends import_koa.default {
392
400
  findCommand(name) {
393
401
  return this.cli._findCommand(name);
394
402
  }
395
- /**
396
- * @internal
397
- */
398
- async reInit() {
399
- if (!this._loaded) {
400
- return;
403
+ async disposeServices() {
404
+ if (this.redisConnectionManager) {
405
+ await this.redisConnectionManager.close();
401
406
  }
402
- this.log.info("app reinitializing");
403
- await this.emitAsync("beforeStop");
404
- await this.emitAsync("afterStop");
405
407
  if (this.cacheManager) {
406
408
  await this.cacheManager.close();
407
409
  }
@@ -411,6 +413,19 @@ const _Application = class _Application extends import_koa.default {
411
413
  if (this.telemetry.started) {
412
414
  await this.telemetry.shutdown();
413
415
  }
416
+ await this.workerIdAllocator.release();
417
+ }
418
+ /**
419
+ * @internal
420
+ */
421
+ async reInit() {
422
+ if (!this._loaded) {
423
+ return;
424
+ }
425
+ this.log.info("app reinitializing");
426
+ await this.emitAsync("beforeStop");
427
+ await this.emitAsync("afterStop");
428
+ await this.disposeServices();
414
429
  this.closeLogger();
415
430
  const oldDb = this.db;
416
431
  this.init();
@@ -434,12 +449,7 @@ const _Application = class _Application extends import_koa.default {
434
449
  if (options == null ? void 0 : options.reload) {
435
450
  this.setMaintainingMessage("app reload");
436
451
  this.log.info(`app.reload()`, { method: "load" });
437
- if (this.cacheManager) {
438
- await this.cacheManager.close();
439
- }
440
- if (this.telemetry.started) {
441
- await this.telemetry.shutdown();
442
- }
452
+ await this.disposeServices();
443
453
  const oldDb = this.db;
444
454
  this.init();
445
455
  if (!oldDb.closed()) {
@@ -460,6 +470,15 @@ const _Application = class _Application extends import_koa.default {
460
470
  if ((options == null ? void 0 : options.hooks) !== false) {
461
471
  await this.emitAsync("beforeLoad", this, options);
462
472
  }
473
+ if (!this._instanceId) {
474
+ this._instanceId = await this.workerIdAllocator.getWorkerId();
475
+ this.log.info(`allocate worker id: ${this._instanceId}`, { method: "load" });
476
+ }
477
+ if (!this.snowflakeIdGenerator) {
478
+ this.snowflakeIdGenerator = new import_snowflake_id.Snowflake({
479
+ workerId: this._instanceId
480
+ });
481
+ }
463
482
  if (!this.telemetry.started) {
464
483
  this.telemetry.init();
465
484
  if ((_a = this.options.telemetry) == null ? void 0 : _a.enabled) {
@@ -707,12 +726,7 @@ const _Application = class _Application extends import_koa.default {
707
726
  } catch (e) {
708
727
  log.error(e.message, { method: "stop", err: e.stack });
709
728
  }
710
- if (this.cacheManager) {
711
- await this.cacheManager.close();
712
- }
713
- if (this.telemetry.started) {
714
- await this.telemetry.shutdown();
715
- }
729
+ await this.disposeServices();
716
730
  await this.emitAsync("afterStop", this, options);
717
731
  this.emit("__stopped", this, options);
718
732
  this.stopped = true;
@@ -876,6 +890,7 @@ const _Application = class _Application extends import_koa.default {
876
890
  }
877
891
  init() {
878
892
  const options = this.options;
893
+ this._instanceId = options.instanceId;
879
894
  this.initLogger(options.logger);
880
895
  this.reInitEvents();
881
896
  this.middleware = new import_utils.Toposort();
@@ -884,6 +899,11 @@ const _Application = class _Application extends import_koa.default {
884
899
  this.db.removeAllListeners();
885
900
  }
886
901
  this.createMainDataSource(options);
902
+ this.redisConnectionManager = new import_redis_connection_manager.RedisConnectionManager({
903
+ redisConfig: options.redisConfig,
904
+ logger: this._logger.child({ module: "redis-connection-manager" })
905
+ });
906
+ this.workerIdAllocator = new import_worker_id_allocator.WorkerIdAllocator();
887
907
  this._cronJobManager = new import_cron_job_manager.CronJobManager(this);
888
908
  this._env = new import_environment.Environment();
889
909
  this._cli = this.createCLI();
@@ -962,6 +982,7 @@ const _Application = class _Application extends import_koa.default {
962
982
  app: this
963
983
  });
964
984
  this.dataSourceManager.dataSources.set("main", mainDataSourceInstance);
985
+ (0, import_snowflake_id_field.setupSnowflakeIdField)(this);
965
986
  }
966
987
  createDatabase(options) {
967
988
  const logging = /* @__PURE__ */ __name((...args) => {
@@ -7,6 +7,7 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  import Application from './application';
10
+ import { SystemLogger } from '@nocobase/logger';
10
11
  export declare const QUEUE_DEFAULT_INTERVAL = 250;
11
12
  export declare const QUEUE_DEFAULT_CONCURRENCY = 1;
12
13
  export declare const QUEUE_DEFAULT_ACK_TIMEOUT = 15000;
@@ -58,6 +59,7 @@ export declare class MemoryEventQueueAdapter implements IEventQueueAdapter {
58
59
  listen: (channel: string) => void;
59
60
  constructor(options: {
60
61
  appName: string;
62
+ logger: SystemLogger;
61
63
  });
62
64
  isConnected(): boolean;
63
65
  setConnected(connected: boolean): void;
@@ -77,9 +77,10 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
77
77
  if (!this.connected) {
78
78
  return;
79
79
  }
80
+ const { logger } = this.options;
80
81
  const event = this.events.get(channel);
81
82
  if (!event) {
82
- console.warn(`memory queue (${channel}) not found, skipping...`);
83
+ logger.warn(`memory queue (${channel}) not found, skipping...`);
83
84
  return;
84
85
  }
85
86
  if (!event.idle()) {
@@ -88,12 +89,9 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
88
89
  const reading = this.reading.get(channel) || [];
89
90
  const count = (event.concurrency || QUEUE_DEFAULT_CONCURRENCY) - reading.length;
90
91
  if (count <= 0) {
91
- console.debug(
92
- `memory queue (${channel}) is already reading as max concurrency (${reading.length}), waiting last reading to end...`
93
- );
94
92
  return;
95
93
  }
96
- console.debug(`reading more from queue (${channel}), count: ${count}`);
94
+ logger.debug(`reading more from queue (${channel}), count: ${count}`);
97
95
  this.read(channel, count).forEach((promise) => {
98
96
  reading.push(promise);
99
97
  promise.finally(() => {
@@ -114,20 +112,21 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
114
112
  async loadFromStorage() {
115
113
  let queues = {};
116
114
  let exists = false;
115
+ const { logger } = this.options;
117
116
  try {
118
117
  await import_promises.default.stat(this.storagePath);
119
118
  exists = true;
120
119
  } catch (ex) {
121
- console.info(`memory queue storage file not found, skip`);
120
+ logger.info(`memory queue storage file not found, skip`);
122
121
  }
123
122
  if (exists) {
124
123
  try {
125
124
  const queueJson = await import_promises.default.readFile(this.storagePath);
126
125
  queues = JSON.parse(queueJson.toString());
127
- console.debug("memory queue loaded from storage", queues);
126
+ logger.debug("memory queue loaded from storage", queues);
128
127
  await import_promises.default.unlink(this.storagePath);
129
128
  } catch (ex) {
130
- console.error("failed to load queue from storage", ex);
129
+ logger.error("failed to load queue from storage", ex);
131
130
  }
132
131
  }
133
132
  this.queues = new Map(Object.entries(queues));
@@ -139,12 +138,13 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
139
138
  }
140
139
  return acc;
141
140
  }, {});
141
+ const { logger } = this.options;
142
142
  if (Object.keys(queues).length) {
143
143
  await import_promises.default.mkdir(import_path.default.dirname(this.storagePath), { recursive: true });
144
144
  await import_promises.default.writeFile(this.storagePath, JSON.stringify(queues));
145
- console.debug("memory queue saved to storage", queues);
145
+ logger.debug("memory queue saved to storage", queues);
146
146
  } else {
147
- console.debug("memory queue empty, no need to save to storage");
147
+ logger.debug("memory queue empty, no need to save to storage");
148
148
  }
149
149
  }
150
150
  async connect() {
@@ -163,13 +163,14 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
163
163
  if (!this.connected) {
164
164
  return;
165
165
  }
166
+ const { logger } = this.options;
166
167
  this.connected = false;
167
168
  if (this.processing) {
168
- console.info("memory queue waiting for processing job...");
169
+ logger.info("memory queue waiting for processing job...");
169
170
  await this.processing;
170
- console.info("memory queue job cleaned");
171
+ logger.info("memory queue job cleaned");
171
172
  }
172
- console.log("memory queue gracefully shutting down...");
173
+ logger.info("memory queue gracefully shutting down...");
173
174
  await this.saveToStorage();
174
175
  }
175
176
  subscribe(channel, options) {
@@ -204,7 +205,8 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
204
205
  const queue = this.queues.get(channel);
205
206
  const message = { id: (0, import_crypto.randomUUID)(), content, options };
206
207
  queue.push(message);
207
- console.debug(`memory queue (${channel}) published message`, content);
208
+ const { logger } = this.options;
209
+ logger.debug(`memory queue (${channel}) published message`, content);
208
210
  setImmediate(() => {
209
211
  this.emitter.emit(channel, channel);
210
212
  });
@@ -228,8 +230,9 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
228
230
  if (!(queue == null ? void 0 : queue.length)) {
229
231
  return [];
230
232
  }
233
+ const { logger } = this.options;
231
234
  const messages = queue.slice(0, n);
232
- console.debug(`memory queue (${channel}) read ${messages.length} messages`, messages);
235
+ logger.debug(`memory queue (${channel}) read ${messages.length} messages`, messages);
233
236
  queue.splice(0, messages.length);
234
237
  const batch = messages.map(({ id, ...message }) => this.process(channel, { id, message }));
235
238
  return batch;
@@ -237,17 +240,18 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
237
240
  async process(channel, { id, message }) {
238
241
  const event = this.events.get(channel);
239
242
  const { content, options: { timeout = QUEUE_DEFAULT_ACK_TIMEOUT, maxRetries = 0, retried = 0 } = {} } = message;
240
- console.debug(`memory queue (${channel}) processing message (${id})...`, content);
243
+ const { logger } = this.options;
244
+ logger.debug(`memory queue (${channel}) processing message (${id})...`, content);
241
245
  return (async () => event.process(content, {
242
246
  id,
243
247
  retried,
244
248
  signal: AbortSignal.timeout(timeout)
245
249
  }))().then(() => {
246
- console.debug(`memory queue (${channel}) consumed message (${id})`);
250
+ logger.debug(`memory queue (${channel}) consumed message (${id})`);
247
251
  }).catch((ex) => {
248
252
  if (maxRetries > 0 && retried < maxRetries) {
249
253
  const currentRetry = retried + 1;
250
- console.warn(
254
+ logger.warn(
251
255
  `memory queue (${channel}) consum message (${id}) failed, retrying (${currentRetry} / ${maxRetries})...`,
252
256
  ex
253
257
  );
@@ -255,7 +259,7 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
255
259
  this.publish(channel, content, { timeout, maxRetries, retried: currentRetry, timestamp: Date.now() });
256
260
  }, 500);
257
261
  } else {
258
- console.error(ex);
262
+ logger.error(ex);
259
263
  }
260
264
  });
261
265
  }
@@ -267,7 +271,7 @@ const _EventQueue = class _EventQueue {
267
271
  this.app = app;
268
272
  this.options = options;
269
273
  if (app.serving()) {
270
- this.setAdapter(new MemoryEventQueueAdapter({ appName: this.app.name }));
274
+ this.setAdapter(new MemoryEventQueueAdapter({ appName: this.app.name, logger: this.app.logger }));
271
275
  app.on("afterStart", async () => {
272
276
  await this.connect();
273
277
  });
package/lib/index.d.ts CHANGED
@@ -19,6 +19,8 @@ export * from './plugin-manager';
19
19
  export * from './pub-sub-manager';
20
20
  export * from './event-queue';
21
21
  export * from './background-job-manager';
22
+ export * from './worker-id-allocator';
23
+ export * from './redis-connection-manager';
22
24
  export declare const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
23
25
  export { appendToBuiltInPlugins, findAllPlugins, findBuiltInPlugins, findLocalPlugins, packageNameTrim, } from './plugin-manager/findPackageNames';
24
26
  export { runPluginStaticImports } from './run-plugin-static-imports';
package/lib/index.js CHANGED
@@ -61,6 +61,8 @@ __reExport(src_exports, require("./plugin-manager"), module.exports);
61
61
  __reExport(src_exports, require("./pub-sub-manager"), module.exports);
62
62
  __reExport(src_exports, require("./event-queue"), module.exports);
63
63
  __reExport(src_exports, require("./background-job-manager"), module.exports);
64
+ __reExport(src_exports, require("./worker-id-allocator"), module.exports);
65
+ __reExport(src_exports, require("./redis-connection-manager"), module.exports);
64
66
  var import_findPackageNames = require("./plugin-manager/findPackageNames");
65
67
  var import_run_plugin_static_imports = require("./run-plugin-static-imports");
66
68
  const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
@@ -84,5 +86,7 @@ const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
84
86
  ...require("./plugin-manager"),
85
87
  ...require("./pub-sub-manager"),
86
88
  ...require("./event-queue"),
87
- ...require("./background-job-manager")
89
+ ...require("./background-job-manager"),
90
+ ...require("./worker-id-allocator"),
91
+ ...require("./redis-connection-manager")
88
92
  });
@@ -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 '../migration';
10
+ export default class extends Migration {
11
+ on: string;
12
+ appVersion: string;
13
+ up(): Promise<void>;
14
+ }
@@ -0,0 +1,126 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var update_primary_keys_exports = {};
29
+ __export(update_primary_keys_exports, {
30
+ default: () => update_primary_keys_default
31
+ });
32
+ module.exports = __toCommonJS(update_primary_keys_exports);
33
+ var import_sequelize = require("sequelize");
34
+ var import_migration = require("../migration");
35
+ const collections = [
36
+ "departments",
37
+ "desktopRoutes",
38
+ "mobileRoutes",
39
+ "collectionCategories",
40
+ "dataSourcesRolesResources",
41
+ "dataSourcesRolesResourcesActions",
42
+ "dataSourcesRolesResourcesScopes",
43
+ "storages",
44
+ "workflows",
45
+ "flow_nodes",
46
+ "executions",
47
+ "userWokrflowTasks",
48
+ "workflowCategories",
49
+ "approvalExecutions",
50
+ "approvalRecords",
51
+ "approvals",
52
+ "workflowManualTasks",
53
+ "workflowCcTasks",
54
+ "approvalMsgTpls",
55
+ "mailAccounts",
56
+ "mailSettings"
57
+ ];
58
+ const _update_primary_keys_default = class _update_primary_keys_default extends import_migration.Migration {
59
+ on = "afterLoad";
60
+ // 'beforeLoad' or 'afterLoad'
61
+ appVersion = "<1.9.0";
62
+ async up() {
63
+ const repo = this.db.getRepository("fields");
64
+ if (!repo) {
65
+ return;
66
+ }
67
+ const queryInterface = this.db.sequelize.getQueryInterface();
68
+ for (const collectionName of collections) {
69
+ const collection = this.db.getCollection(collectionName);
70
+ if (collection) {
71
+ const tableName = collection.getTableNameWithSchema();
72
+ if (this.db.isPostgresCompatibleDialect()) {
73
+ await this.db.sequelize.transaction(async (transaction) => {
74
+ const schema = collection.collectionSchema();
75
+ const table = collection.model.tableName;
76
+ const seqName = `"${schema}"."${table}_id_seq"`;
77
+ await this.db.sequelize.query(`ALTER TABLE "${schema}"."${table}" ALTER COLUMN id DROP DEFAULT;`, {
78
+ transaction
79
+ });
80
+ await this.db.sequelize.query(`DROP SEQUENCE IF EXISTS ${seqName} CASCADE;`, { transaction });
81
+ });
82
+ } else {
83
+ await queryInterface.changeColumn(tableName, "id", {
84
+ type: import_sequelize.DataTypes.BIGINT,
85
+ primaryKey: true,
86
+ allowNull: false,
87
+ autoIncrement: false
88
+ });
89
+ }
90
+ }
91
+ const field = await repo.findOne({
92
+ filter: {
93
+ collectionName,
94
+ name: "id"
95
+ }
96
+ });
97
+ if (field) {
98
+ const options = { ...field.options };
99
+ delete options["autoIncrement"];
100
+ await repo.update({
101
+ filter: { key: field.key },
102
+ values: {
103
+ type: "snowflakeId",
104
+ options
105
+ }
106
+ });
107
+ }
108
+ }
109
+ const treeCollections = [...this.db.collections.values()].filter((collection) => collection.options.tree);
110
+ for (const collection of treeCollections) {
111
+ const pathCollection = this.db.getCollection(`main_${collection.name}_path`);
112
+ if (pathCollection) {
113
+ const nodePk = pathCollection.getField("nodePk").columnName();
114
+ await queryInterface.changeColumn(pathCollection.getTableNameWithSchema(), nodePk, {
115
+ type: import_sequelize.DataTypes.BIGINT
116
+ });
117
+ const rootPk = pathCollection.getField("rootPk").columnName();
118
+ await queryInterface.changeColumn(pathCollection.getTableNameWithSchema(), rootPk, {
119
+ type: import_sequelize.DataTypes.BIGINT
120
+ });
121
+ }
122
+ }
123
+ }
124
+ };
125
+ __name(_update_primary_keys_default, "default");
126
+ let update_primary_keys_default = _update_primary_keys_default;
@@ -0,0 +1,28 @@
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 Redis from 'ioredis';
10
+ import { Logger } from '@nocobase/logger';
11
+ export { Redis };
12
+ export interface RedisConfig {
13
+ connectionString: string;
14
+ }
15
+ export declare class RedisConnectionManager {
16
+ private logger;
17
+ private config;
18
+ private connections;
19
+ constructor(config: {
20
+ redisConfig: RedisConfig;
21
+ logger: Logger;
22
+ });
23
+ private bindEvents;
24
+ private getClient;
25
+ getConnection(key?: string, config?: RedisConfig): Redis | null;
26
+ getConnectionSync(key?: string, config?: RedisConfig): Promise<Redis>;
27
+ close(): Promise<void>;
28
+ }
@@ -0,0 +1,119 @@
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 __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var redis_connection_manager_exports = {};
39
+ __export(redis_connection_manager_exports, {
40
+ Redis: () => import_ioredis.default,
41
+ RedisConnectionManager: () => RedisConnectionManager
42
+ });
43
+ module.exports = __toCommonJS(redis_connection_manager_exports);
44
+ var import_ioredis = __toESM(require("ioredis"));
45
+ const _RedisConnectionManager = class _RedisConnectionManager {
46
+ logger;
47
+ config;
48
+ connections = /* @__PURE__ */ new Map();
49
+ constructor(config) {
50
+ this.config = config.redisConfig;
51
+ this.logger = config.logger;
52
+ }
53
+ bindEvents(conn, key, config) {
54
+ conn.on("connect", () => {
55
+ this.logger.info(`Redis connected`, {
56
+ method: "getConnection",
57
+ key,
58
+ config
59
+ });
60
+ });
61
+ conn.on("error", (err) => {
62
+ this.logger.error(err.message, {
63
+ err,
64
+ method: "getConnection",
65
+ key,
66
+ config
67
+ });
68
+ });
69
+ conn.on("close", () => {
70
+ this.logger.trace(`Redis closed`, {
71
+ method: "getConnection",
72
+ key,
73
+ config
74
+ });
75
+ });
76
+ }
77
+ getClient(key = "default", config) {
78
+ let conn = this.connections.get(key);
79
+ if (conn) {
80
+ return conn;
81
+ }
82
+ const cfg = config || this.config;
83
+ if (!cfg.connectionString) {
84
+ return null;
85
+ }
86
+ conn = new import_ioredis.default(cfg.connectionString);
87
+ this.connections.set(key, conn);
88
+ this.bindEvents(conn, key, cfg);
89
+ return conn;
90
+ }
91
+ getConnection(key = "default", config) {
92
+ return this.getClient(key, config);
93
+ }
94
+ async getConnectionSync(key = "default", config) {
95
+ return new Promise((resolve, reject) => {
96
+ const conn = this.getClient(key, config);
97
+ if (!conn) {
98
+ return reject(new Error("Redis connect string is missing"));
99
+ }
100
+ conn.once("connect", () => resolve(conn));
101
+ conn.once("error", reject);
102
+ });
103
+ }
104
+ async close() {
105
+ for (const conn of this.connections.values()) {
106
+ if (!(conn == null ? void 0 : conn.status) || conn.status === "close" || conn.status === "end") {
107
+ continue;
108
+ }
109
+ await conn.quit();
110
+ }
111
+ }
112
+ };
113
+ __name(_RedisConnectionManager, "RedisConnectionManager");
114
+ let RedisConnectionManager = _RedisConnectionManager;
115
+ // Annotate the CommonJS export names for ESM import in node:
116
+ 0 && (module.exports = {
117
+ Redis,
118
+ RedisConnectionManager
119
+ });
@@ -0,0 +1,10 @@
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 Application from './application';
10
+ export declare function setupSnowflakeIdField(app: Application): void;
@@ -0,0 +1,48 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var snowflake_id_field_exports = {};
29
+ __export(snowflake_id_field_exports, {
30
+ setupSnowflakeIdField: () => setupSnowflakeIdField
31
+ });
32
+ module.exports = __toCommonJS(snowflake_id_field_exports);
33
+ var import_database = require("@nocobase/database");
34
+ function setupSnowflakeIdField(app) {
35
+ const _SnowflakeIdField = class _SnowflakeIdField extends import_database.SnowflakeIdField {
36
+ };
37
+ __name(_SnowflakeIdField, "SnowflakeIdField");
38
+ let SnowflakeIdField = _SnowflakeIdField;
39
+ SnowflakeIdField.setApp(app);
40
+ app.db.registerFieldTypes({
41
+ snowflakeId: SnowflakeIdField
42
+ });
43
+ }
44
+ __name(setupSnowflakeIdField, "setupSnowflakeIdField");
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ setupSnowflakeIdField
48
+ });
@@ -0,0 +1,18 @@
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
+ export interface WorkerIdAllocatorAdapter {
10
+ getWorkerId(): Promise<number>;
11
+ release(): Promise<void>;
12
+ }
13
+ export declare class WorkerIdAllocator {
14
+ private adapter;
15
+ setAdapter(adapter: WorkerIdAllocatorAdapter): void;
16
+ getWorkerId(): Promise<number>;
17
+ release(): Promise<void>;
18
+ }
@@ -0,0 +1,56 @@
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 __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var worker_id_allocator_exports = {};
29
+ __export(worker_id_allocator_exports, {
30
+ WorkerIdAllocator: () => WorkerIdAllocator
31
+ });
32
+ module.exports = __toCommonJS(worker_id_allocator_exports);
33
+ const _WorkerIdAllocator = class _WorkerIdAllocator {
34
+ adapter;
35
+ setAdapter(adapter) {
36
+ this.adapter = adapter;
37
+ }
38
+ async getWorkerId() {
39
+ if (this.adapter) {
40
+ return this.adapter.getWorkerId();
41
+ }
42
+ return Math.floor(Math.random() * 32);
43
+ }
44
+ async release() {
45
+ if (this.adapter) {
46
+ return this.adapter.release();
47
+ }
48
+ return;
49
+ }
50
+ };
51
+ __name(_WorkerIdAllocator, "WorkerIdAllocator");
52
+ let WorkerIdAllocator = _WorkerIdAllocator;
53
+ // Annotate the CommonJS export names for ESM import in node:
54
+ 0 && (module.exports = {
55
+ WorkerIdAllocator
56
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "1.9.0-alpha.14",
3
+ "version": "1.9.0-alpha.15",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "AGPL-3.0",
@@ -10,19 +10,20 @@
10
10
  "@koa/cors": "^5.0.0",
11
11
  "@koa/multer": "^3.1.0",
12
12
  "@koa/router": "^13.1.0",
13
- "@nocobase/acl": "1.9.0-alpha.14",
14
- "@nocobase/actions": "1.9.0-alpha.14",
15
- "@nocobase/auth": "1.9.0-alpha.14",
16
- "@nocobase/cache": "1.9.0-alpha.14",
17
- "@nocobase/data-source-manager": "1.9.0-alpha.14",
18
- "@nocobase/database": "1.9.0-alpha.14",
19
- "@nocobase/evaluators": "1.9.0-alpha.14",
20
- "@nocobase/lock-manager": "1.9.0-alpha.14",
21
- "@nocobase/logger": "1.9.0-alpha.14",
22
- "@nocobase/resourcer": "1.9.0-alpha.14",
23
- "@nocobase/sdk": "1.9.0-alpha.14",
24
- "@nocobase/telemetry": "1.9.0-alpha.14",
25
- "@nocobase/utils": "1.9.0-alpha.14",
13
+ "@nocobase/acl": "1.9.0-alpha.15",
14
+ "@nocobase/actions": "1.9.0-alpha.15",
15
+ "@nocobase/auth": "1.9.0-alpha.15",
16
+ "@nocobase/cache": "1.9.0-alpha.15",
17
+ "@nocobase/data-source-manager": "1.9.0-alpha.15",
18
+ "@nocobase/database": "1.9.0-alpha.15",
19
+ "@nocobase/evaluators": "1.9.0-alpha.15",
20
+ "@nocobase/lock-manager": "1.9.0-alpha.15",
21
+ "@nocobase/logger": "1.9.0-alpha.15",
22
+ "@nocobase/resourcer": "1.9.0-alpha.15",
23
+ "@nocobase/sdk": "1.9.0-alpha.15",
24
+ "@nocobase/snowflake-id": "1.9.0-alpha.15",
25
+ "@nocobase/telemetry": "1.9.0-alpha.15",
26
+ "@nocobase/utils": "1.9.0-alpha.15",
26
27
  "@types/decompress": "4.2.7",
27
28
  "@types/ini": "^1.3.31",
28
29
  "@types/koa-send": "^4.1.3",
@@ -40,6 +41,7 @@
40
41
  "fs-extra": "^11.1.1",
41
42
  "i18next": "^22.4.9",
42
43
  "ini": "^4.1.1",
44
+ "ioredis": "^5.7.0",
43
45
  "koa": "^2.15.4",
44
46
  "koa-bodyparser": "^4.3.0",
45
47
  "koa-send": "^5.0.1",
@@ -57,5 +59,5 @@
57
59
  "@types/serve-handler": "^6.1.1",
58
60
  "@types/ws": "^8.5.5"
59
61
  },
60
- "gitHead": "0e2ad0f673bff626b4d59aaa9657ce5d5efb54c6"
62
+ "gitHead": "620cda051dd97134add40055d83039e3d9b6891e"
61
63
  }