@nocobase/data-source-manager 1.2.21-alpha → 1.2.23-alpha

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.
@@ -9,9 +9,14 @@
9
9
  import { ToposortOptions } from '@nocobase/utils';
10
10
  import { DataSource } from './data-source';
11
11
  import { DataSourceFactory } from './data-source-factory';
12
+ import { Logger, LoggerOptions } from '@nocobase/logger';
12
13
  type DataSourceHook = (dataSource: DataSource) => void;
14
+ type DataSourceManagerOptions = {
15
+ logger?: LoggerOptions | Logger;
16
+ app?: any;
17
+ };
13
18
  export declare class DataSourceManager {
14
- options: {};
19
+ options: DataSourceManagerOptions;
15
20
  dataSources: Map<string, DataSource>;
16
21
  /**
17
22
  * @internal
@@ -20,7 +25,7 @@ export declare class DataSourceManager {
20
25
  protected middlewares: any[];
21
26
  private onceHooks;
22
27
  private beforeAddHooks;
23
- constructor(options?: {});
28
+ constructor(options?: DataSourceManagerOptions);
24
29
  get(dataSourceKey: string): DataSource;
25
30
  add(dataSource: DataSource, options?: any): Promise<void>;
26
31
  use(fn: any, options?: ToposortOptions): void;
@@ -31,11 +31,19 @@ __export(data_source_manager_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(data_source_manager_exports);
33
33
  var import_data_source_factory = require("./data-source-factory");
34
+ var import_logger = require("@nocobase/logger");
34
35
  const _DataSourceManager = class _DataSourceManager {
35
36
  constructor(options = {}) {
36
37
  this.options = options;
37
38
  this.dataSources = /* @__PURE__ */ new Map();
38
39
  this.middlewares = [];
40
+ if (options.app) {
41
+ options.app.on("beforeStop", async () => {
42
+ for (const dataSource of this.dataSources.values()) {
43
+ await dataSource.close();
44
+ }
45
+ });
46
+ }
39
47
  }
40
48
  dataSources;
41
49
  /**
@@ -49,9 +57,24 @@ const _DataSourceManager = class _DataSourceManager {
49
57
  return this.dataSources.get(dataSourceKey);
50
58
  }
51
59
  async add(dataSource, options = {}) {
60
+ let logger;
61
+ if (this.options.logger) {
62
+ if (typeof this.options.logger["log"] === "function") {
63
+ logger = this.options.logger;
64
+ } else {
65
+ logger = (0, import_logger.createLogger)(this.options.logger);
66
+ }
67
+ } else {
68
+ logger = (0, import_logger.createConsoleLogger)();
69
+ }
70
+ dataSource.setLogger(logger);
52
71
  for (const hook of this.beforeAddHooks) {
53
72
  hook(dataSource);
54
73
  }
74
+ const oldDataSource = this.dataSources.get(dataSource.name);
75
+ if (oldDataSource) {
76
+ await oldDataSource.close();
77
+ }
55
78
  await dataSource.load(options);
56
79
  this.dataSources.set(dataSource.name, dataSource);
57
80
  for (const hook of this.onceHooks) {
@@ -11,13 +11,16 @@ import { ACL } from '@nocobase/acl';
11
11
  import { ResourceManager } from '@nocobase/resourcer';
12
12
  import EventEmitter from 'events';
13
13
  import { ICollectionManager } from './types';
14
+ import { Logger } from '@nocobase/logger';
14
15
  export type DataSourceOptions = any;
15
16
  export declare abstract class DataSource extends EventEmitter {
16
17
  protected options: DataSourceOptions;
17
18
  collectionManager: ICollectionManager;
18
19
  resourceManager: ResourceManager;
19
20
  acl: ACL;
21
+ logger: Logger;
20
22
  constructor(options: DataSourceOptions);
23
+ setLogger(logger: Logger): void;
21
24
  get name(): any;
22
25
  static testConnection(options?: any): Promise<boolean>;
23
26
  init(options?: DataSourceOptions): void;
@@ -25,6 +28,7 @@ export declare abstract class DataSource extends EventEmitter {
25
28
  createACL(): ACL;
26
29
  createResourceManager(options: any): ResourceManager;
27
30
  load(options?: any): Promise<void>;
31
+ close(): Promise<void>;
28
32
  abstract createCollectionManager(options?: any): ICollectionManager;
29
33
  protected collectionToResourceMiddleware(): (ctx: any, next: any) => Promise<any>;
30
34
  }
@@ -54,6 +54,10 @@ const _DataSource = class _DataSource extends import_events.default {
54
54
  collectionManager;
55
55
  resourceManager;
56
56
  acl;
57
+ logger;
58
+ setLogger(logger) {
59
+ this.logger = logger;
60
+ }
57
61
  get name() {
58
62
  return this.options.name;
59
63
  }
@@ -97,6 +101,8 @@ const _DataSource = class _DataSource extends import_events.default {
97
101
  }
98
102
  async load(options = {}) {
99
103
  }
104
+ async close() {
105
+ }
100
106
  collectionToResourceMiddleware() {
101
107
  return async (ctx, next) => {
102
108
  const params = (0, import_resourcer.parseRequest)(
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@nocobase/data-source-manager",
3
- "version": "1.2.21-alpha",
3
+ "version": "1.2.23-alpha",
4
4
  "description": "",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/actions": "1.2.21-alpha",
10
- "@nocobase/cache": "1.2.21-alpha",
11
- "@nocobase/database": "1.2.21-alpha",
12
- "@nocobase/resourcer": "1.2.21-alpha",
13
- "@nocobase/utils": "1.2.21-alpha",
9
+ "@nocobase/actions": "1.2.23-alpha",
10
+ "@nocobase/cache": "1.2.23-alpha",
11
+ "@nocobase/database": "1.2.23-alpha",
12
+ "@nocobase/resourcer": "1.2.23-alpha",
13
+ "@nocobase/utils": "1.2.23-alpha",
14
14
  "@types/jsonwebtoken": "^8.5.8",
15
15
  "jsonwebtoken": "^8.5.1"
16
16
  },
@@ -19,5 +19,5 @@
19
19
  "url": "git+https://github.com/nocobase/nocobase.git",
20
20
  "directory": "packages/auth"
21
21
  },
22
- "gitHead": "16299a326eecf8630268654284f35c0aabe6b42f"
22
+ "gitHead": "e2ee9d3c39d123cfdb0b50bfb6d381eaa4a96f77"
23
23
  }