@nocobase/data-source-manager 0.21.0-alpha.1 → 0.21.0-alpha.11

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.
@@ -1,13 +1,19 @@
1
1
  import { ToposortOptions } from '@nocobase/utils';
2
2
  import { DataSource } from './data-source';
3
3
  import { DataSourceFactory } from './data-source-factory';
4
+ type DataSourceHook = (dataSource: DataSource) => void;
4
5
  export declare class DataSourceManager {
5
6
  options: {};
6
7
  dataSources: Map<string, DataSource>;
7
8
  factory: DataSourceFactory;
9
+ onceHooks: Array<DataSourceHook>;
8
10
  protected middlewares: any[];
9
11
  constructor(options?: {});
12
+ get(dataSourceKey: string): DataSource;
10
13
  add(dataSource: DataSource, options?: any): Promise<void>;
11
14
  use(fn: any, options?: ToposortOptions): void;
12
15
  middleware(): (ctx: any, next: any) => Promise<any>;
16
+ afterAddDataSource(hook: DataSourceHook): void;
17
+ private addHookAndRun;
13
18
  }
19
+ export {};
@@ -30,30 +30,41 @@ const _DataSourceManager = class _DataSourceManager {
30
30
  }
31
31
  dataSources;
32
32
  factory = new import_data_source_factory.DataSourceFactory();
33
+ onceHooks = [];
33
34
  middlewares = [];
35
+ get(dataSourceKey) {
36
+ return this.dataSources.get(dataSourceKey);
37
+ }
34
38
  async add(dataSource, options = {}) {
35
39
  await dataSource.load(options);
36
40
  this.dataSources.set(dataSource.name, dataSource);
41
+ for (const hook of this.onceHooks) {
42
+ hook(dataSource);
43
+ }
37
44
  }
38
45
  use(fn, options) {
39
46
  this.middlewares.push([fn, options]);
40
47
  }
41
48
  middleware() {
42
49
  return async (ctx, next) => {
43
- const name = ctx.get("x-data-source");
44
- if (name) {
45
- if (this.dataSources.has(name)) {
46
- const ds = this.dataSources.get(name);
47
- ctx.dataSource = ds;
48
- return ds.middleware(this.middlewares)(ctx, next);
49
- } else {
50
- ctx.throw(`data source ${name} does not exist`);
51
- }
50
+ const name = ctx.get("x-data-source") || "main";
51
+ if (!this.dataSources.has(name)) {
52
+ ctx.throw(`data source ${name} does not exist`);
52
53
  }
53
- await next();
54
- console.log("next....");
54
+ const ds = this.dataSources.get(name);
55
+ ctx.dataSource = ds;
56
+ return ds.middleware(this.middlewares)(ctx, next);
55
57
  };
56
58
  }
59
+ afterAddDataSource(hook) {
60
+ this.addHookAndRun(hook);
61
+ }
62
+ addHookAndRun(hook) {
63
+ this.onceHooks.push(hook);
64
+ for (const dataSource of this.dataSources.values()) {
65
+ hook(dataSource);
66
+ }
67
+ }
57
68
  };
58
69
  __name(_DataSourceManager, "DataSourceManager");
59
70
  let DataSourceManager = _DataSourceManager;
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import { ACL } from '@nocobase/acl';
3
+ import { ResourceManager } from '@nocobase/resourcer';
3
4
  import EventEmitter from 'events';
4
- import { ResourceManager } from './resource-manager';
5
5
  import { ICollectionManager } from './types';
6
6
  export type DataSourceOptions = any;
7
7
  export declare abstract class DataSource extends EventEmitter {
@@ -36,7 +36,6 @@ var import_resourcer = require("@nocobase/resourcer");
36
36
  var import_events = __toESM(require("events"));
37
37
  var import_koa_compose = __toESM(require("koa-compose"));
38
38
  var import_load_default_actions = require("./load-default-actions");
39
- var import_resource_manager = require("./resource-manager");
40
39
  const _DataSource = class _DataSource extends import_events.default {
41
40
  constructor(options) {
42
41
  super();
@@ -83,7 +82,9 @@ const _DataSource = class _DataSource extends import_events.default {
83
82
  if (this.resourceManager.isDefined(resourceName)) {
84
83
  return next();
85
84
  }
86
- if (!this.collectionManager.hasCollection(resourceName)) {
85
+ const splitResult = resourceName.split(".");
86
+ const collectionName = splitResult[0];
87
+ if (!this.collectionManager.hasCollection(collectionName)) {
87
88
  return next();
88
89
  }
89
90
  this.resourceManager.define({
@@ -93,6 +94,7 @@ const _DataSource = class _DataSource extends import_events.default {
93
94
  };
94
95
  }
95
96
  middleware(middlewares = []) {
97
+ const dataSource = this;
96
98
  if (!this["_used"]) {
97
99
  for (const [fn, options] of middlewares) {
98
100
  this.resourceManager.use(fn, options);
@@ -104,14 +106,15 @@ const _DataSource = class _DataSource extends import_events.default {
104
106
  const { resourceName, resourceOf } = ctx.action;
105
107
  return this.collectionManager.getRepository(resourceName, resourceOf);
106
108
  };
107
- return (0, import_koa_compose.default)([this.collectionToResourceMiddleware(), this.resourceManager.restApiMiddleware()])(ctx, next);
109
+ ctx.dataSource = dataSource;
110
+ return (0, import_koa_compose.default)([this.collectionToResourceMiddleware(), this.resourceManager.middleware()])(ctx, next);
108
111
  };
109
112
  }
110
113
  createACL() {
111
114
  return new import_acl.ACL();
112
115
  }
113
116
  createResourceManager(options) {
114
- return new import_resource_manager.ResourceManager(options);
117
+ return new import_resourcer.ResourceManager(options);
115
118
  }
116
119
  async load(options = {}) {
117
120
  }
@@ -30,10 +30,14 @@ function pageArgsToLimitArgs(page, pageSize) {
30
30
  }
31
31
  __name(pageArgsToLimitArgs, "pageArgsToLimitArgs");
32
32
  function getRepositoryFromParams(ctx) {
33
- const { resourceName, resourceOf } = ctx.action;
33
+ const { resourceName, sourceId, actionName } = ctx.action;
34
34
  const dataSource = ctx.dataSource;
35
- if (resourceOf) {
36
- return dataSource.collectionManager.getRepository(resourceName, resourceOf);
35
+ if (sourceId === "_" && ["get", "list"].includes(actionName)) {
36
+ const collection = dataSource.collectionManager.getCollection(resourceName);
37
+ return dataSource.collectionManager.getRepository(collection.name);
38
+ }
39
+ if (sourceId) {
40
+ return dataSource.collectionManager.getRepository(resourceName, sourceId);
37
41
  }
38
42
  return dataSource.collectionManager.getRepository(resourceName);
39
43
  }
package/lib/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export * from './collection-manager';
2
2
  export * from './data-source';
3
3
  export * from './data-source-manager';
4
- export * from './resource-manager';
5
4
  export * from './sequelize-collection-manager';
6
5
  export * from './sequelize-data-source';
7
6
  export * from './load-default-actions';
package/lib/index.js CHANGED
@@ -17,7 +17,6 @@ module.exports = __toCommonJS(src_exports);
17
17
  __reExport(src_exports, require("./collection-manager"), module.exports);
18
18
  __reExport(src_exports, require("./data-source"), module.exports);
19
19
  __reExport(src_exports, require("./data-source-manager"), module.exports);
20
- __reExport(src_exports, require("./resource-manager"), module.exports);
21
20
  __reExport(src_exports, require("./sequelize-collection-manager"), module.exports);
22
21
  __reExport(src_exports, require("./sequelize-data-source"), module.exports);
23
22
  __reExport(src_exports, require("./load-default-actions"), module.exports);
@@ -29,7 +28,6 @@ __reExport(src_exports, require("./utils"), module.exports);
29
28
  ...require("./collection-manager"),
30
29
  ...require("./data-source"),
31
30
  ...require("./data-source-manager"),
32
- ...require("./resource-manager"),
33
31
  ...require("./sequelize-collection-manager"),
34
32
  ...require("./sequelize-data-source"),
35
33
  ...require("./load-default-actions"),
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@nocobase/data-source-manager",
3
- "version": "0.21.0-alpha.1",
3
+ "version": "0.21.0-alpha.11",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/actions": "0.21.0-alpha.1",
10
- "@nocobase/cache": "0.21.0-alpha.1",
11
- "@nocobase/database": "0.21.0-alpha.1",
12
- "@nocobase/resourcer": "0.21.0-alpha.1",
13
- "@nocobase/utils": "0.21.0-alpha.1",
9
+ "@nocobase/actions": "0.21.0-alpha.11",
10
+ "@nocobase/cache": "0.21.0-alpha.11",
11
+ "@nocobase/database": "0.21.0-alpha.11",
12
+ "@nocobase/resourcer": "0.21.0-alpha.11",
13
+ "@nocobase/utils": "0.21.0-alpha.11",
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": "afd2f3d1341b85ea9daa7b2667dd4ace1fafb7ff"
22
+ "gitHead": "69fe8a6d75864a3ba98c5a6d3ebfe43a585d4cd3"
23
23
  }
@@ -1,3 +0,0 @@
1
- import { Resourcer } from '@nocobase/resourcer';
2
- export declare class ResourceManager extends Resourcer {
3
- }
@@ -1,32 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var resource_manager_exports = {};
20
- __export(resource_manager_exports, {
21
- ResourceManager: () => ResourceManager
22
- });
23
- module.exports = __toCommonJS(resource_manager_exports);
24
- var import_resourcer = require("@nocobase/resourcer");
25
- const _ResourceManager = class _ResourceManager extends import_resourcer.Resourcer {
26
- };
27
- __name(_ResourceManager, "ResourceManager");
28
- let ResourceManager = _ResourceManager;
29
- // Annotate the CommonJS export names for ESM import in node:
30
- 0 && (module.exports = {
31
- ResourceManager
32
- });