@med1802/repository-manager 2.2.1 → 3.0.0

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.
Files changed (82) hide show
  1. package/README.md +430 -381
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +1 -0
  5. package/dist/manager.d.ts +14 -0
  6. package/dist/manager.d.ts.map +1 -0
  7. package/dist/manager.js +17 -0
  8. package/dist/workspace/index.d.ts +13 -0
  9. package/dist/workspace/index.d.ts.map +1 -0
  10. package/dist/workspace/index.js +30 -0
  11. package/dist/workspace/infrastructure/index.d.ts +4 -0
  12. package/dist/workspace/infrastructure/index.d.ts.map +1 -0
  13. package/dist/workspace/infrastructure/index.js +3 -0
  14. package/dist/{logger.d.ts → workspace/infrastructure/logger.d.ts} +1 -1
  15. package/dist/workspace/infrastructure/logger.d.ts.map +1 -0
  16. package/dist/workspace/infrastructure/scope/index.d.ts +3 -0
  17. package/dist/workspace/infrastructure/scope/index.d.ts.map +1 -0
  18. package/dist/workspace/infrastructure/scope/index.js +2 -0
  19. package/dist/workspace/infrastructure/scope/scope.d.ts +7 -0
  20. package/dist/workspace/infrastructure/scope/scope.d.ts.map +1 -0
  21. package/dist/workspace/infrastructure/scope/scope.js +32 -0
  22. package/dist/workspace/infrastructure/scope/types.d.ts +8 -0
  23. package/dist/workspace/infrastructure/scope/types.d.ts.map +1 -0
  24. package/dist/workspace/infrastructure/store.d.ts.map +1 -0
  25. package/dist/workspace/modules/index.d.ts +2 -0
  26. package/dist/workspace/modules/index.d.ts.map +1 -0
  27. package/dist/workspace/modules/index.js +1 -0
  28. package/dist/workspace/modules/repository/createRepositoryModule.d.ts +10 -0
  29. package/dist/workspace/modules/repository/createRepositoryModule.d.ts.map +1 -0
  30. package/dist/{workspace.js → workspace/modules/repository/createRepositoryModule.js} +22 -23
  31. package/dist/workspace/modules/repository/index.d.ts +3 -0
  32. package/dist/workspace/modules/repository/index.d.ts.map +1 -0
  33. package/dist/workspace/modules/repository/index.js +2 -0
  34. package/dist/workspace/modules/repository/middleware.d.ts.map +1 -0
  35. package/dist/workspace/modules/repository/repositoryAccessor.d.ts +9 -0
  36. package/dist/workspace/modules/repository/repositoryAccessor.d.ts.map +1 -0
  37. package/dist/workspace/modules/repository/repositoryAccessor.js +42 -0
  38. package/dist/workspace/modules/repository/types.d.ts +22 -0
  39. package/dist/workspace/modules/repository/types.d.ts.map +1 -0
  40. package/dist/workspace/modules/repository/types.js +1 -0
  41. package/dist/workspace/providers/index.d.ts +5 -0
  42. package/dist/workspace/providers/index.d.ts.map +1 -0
  43. package/dist/workspace/providers/index.js +3 -0
  44. package/dist/workspace/types.d.ts +5 -0
  45. package/dist/workspace/types.d.ts.map +1 -0
  46. package/dist/workspace/types.js +1 -0
  47. package/package.json +1 -1
  48. package/src/index.ts +1 -0
  49. package/src/manager.ts +23 -0
  50. package/src/workspace/index.ts +38 -0
  51. package/src/workspace/infrastructure/index.ts +3 -0
  52. package/src/{logger.ts → workspace/infrastructure/logger.ts} +1 -1
  53. package/src/workspace/infrastructure/scope/index.ts +2 -0
  54. package/src/workspace/infrastructure/scope/scope.ts +35 -0
  55. package/src/workspace/infrastructure/scope/types.ts +8 -0
  56. package/src/workspace/modules/index.ts +1 -0
  57. package/src/{workspace.ts → workspace/modules/repository/createRepositoryModule.ts} +22 -35
  58. package/src/workspace/modules/repository/index.ts +2 -0
  59. package/src/{middleware.ts → workspace/modules/repository/middleware.ts} +1 -5
  60. package/src/{repositoryAccessor.ts → workspace/modules/repository/repositoryAccessor.ts} +14 -12
  61. package/src/workspace/modules/repository/types.ts +29 -0
  62. package/src/workspace/providers/index.ts +5 -0
  63. package/src/workspace/types.ts +4 -0
  64. package/tsconfig.tsbuildinfo +1 -1
  65. package/dist/logger.d.ts.map +0 -1
  66. package/dist/middleware.d.ts.map +0 -1
  67. package/dist/repositoryAccessor.d.ts +0 -9
  68. package/dist/repositoryAccessor.d.ts.map +0 -1
  69. package/dist/repositoryAccessor.js +0 -40
  70. package/dist/store.d.ts.map +0 -1
  71. package/dist/types.d.ts +0 -28
  72. package/dist/types.d.ts.map +0 -1
  73. package/dist/workspace.d.ts +0 -10
  74. package/dist/workspace.d.ts.map +0 -1
  75. package/src/types.ts +0 -37
  76. /package/dist/{logger.js → workspace/infrastructure/logger.js} +0 -0
  77. /package/dist/{types.js → workspace/infrastructure/scope/types.js} +0 -0
  78. /package/dist/{store.d.ts → workspace/infrastructure/store.d.ts} +0 -0
  79. /package/dist/{store.js → workspace/infrastructure/store.js} +0 -0
  80. /package/dist/{middleware.d.ts → workspace/modules/repository/middleware.d.ts} +0 -0
  81. /package/dist/{middleware.js → workspace/modules/repository/middleware.js} +0 -0
  82. /package/src/{store.ts → workspace/infrastructure/store.ts} +0 -0
@@ -0,0 +1,2 @@
1
+ export * from "./manager";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./manager";
@@ -0,0 +1,14 @@
1
+ import type { IConfiguration } from "./workspace/types";
2
+ import type { IScope } from "./workspace/infrastructure/scope";
3
+ declare const repositoryManager: () => {
4
+ workspace<I>(infrastructure: I, config: IConfiguration): {
5
+ defineRepository: <R = any>(repositoryPlugin: import("./workspace/modules").IRepositoryPlugin<I, R>) => void;
6
+ queryRepository: <R = any>(id: string) => {
7
+ repository: R;
8
+ disconnect(): void;
9
+ };
10
+ createScope: <V = any>(defaultValue: V) => IScope<V>;
11
+ };
12
+ };
13
+ export { repositoryManager };
14
+ //# sourceMappingURL=manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAGxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAE/D,QAAA,MAAM,iBAAiB;cAGT,CAAC,kBAAkB,CAAC,UAAU,cAAc;;;;;;qBAMD,CAAC,CAAC,GAAG,GAAG,EACvD,YAAY,EAAE,CAAC,KACZ,MAAM,CAAC,CAAC,CAAC;;CAIrB,CAAC;AAEF,OAAO,EAAE,iBAAiB,EAAE,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { createWorkspace } from "./workspace";
2
+ import { createStore } from "./workspace/infrastructure";
3
+ const repositoryManager = () => {
4
+ const store = createStore();
5
+ return {
6
+ workspace(infrastructure, config) {
7
+ const workspace = createWorkspace(infrastructure, config);
8
+ store.setState(config.id, workspace);
9
+ return {
10
+ defineRepository: workspace.defineRepository,
11
+ queryRepository: workspace.queryRepository,
12
+ createScope: workspace.createScope,
13
+ };
14
+ },
15
+ };
16
+ };
17
+ export { repositoryManager };
@@ -0,0 +1,13 @@
1
+ import type { IConfiguration } from "./types";
2
+ import { createScope, useScope } from "./infrastructure";
3
+ declare function createWorkspace<I>(infrastructure: I, config: IConfiguration): {
4
+ defineRepository: <R = any>(repositoryPlugin: import("./modules").IRepositoryPlugin<I, R>) => void;
5
+ queryRepository: <R = any>(id: string) => {
6
+ repository: R;
7
+ disconnect(): void;
8
+ };
9
+ createScope: typeof createScope;
10
+ useScope: typeof useScope;
11
+ };
12
+ export { createWorkspace };
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/workspace/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,OAAO,EACL,WAAW,EAGX,QAAQ,EACT,MAAM,kBAAkB,CAAC;AAG1B,iBAAS,eAAe,CAAC,CAAC,EAAE,cAAc,EAAE,CAAC,EAAE,MAAM,EAAE,cAAc;;;;;;;;EAyBpE;AAED,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,30 @@
1
+ import { createRepositoryModule } from "./modules";
2
+ import { createScope, createLogger, createStore, useScope, } from "./infrastructure";
3
+ import { repositoryScope } from "./providers";
4
+ function createWorkspace(infrastructure, config) {
5
+ var _a;
6
+ const defaultConfig = {
7
+ id: config.id,
8
+ logging: (_a = config.logging) !== null && _a !== void 0 ? _a : false,
9
+ };
10
+ const logger = createLogger(defaultConfig);
11
+ const store = createStore();
12
+ let repositoryModule;
13
+ repositoryScope.provider({
14
+ value: {
15
+ store,
16
+ logger,
17
+ infrastructure,
18
+ },
19
+ children: () => {
20
+ repositoryModule = createRepositoryModule();
21
+ },
22
+ });
23
+ return {
24
+ defineRepository: repositoryModule.defineRepository,
25
+ queryRepository: repositoryModule.queryRepository,
26
+ createScope,
27
+ useScope,
28
+ };
29
+ }
30
+ export { createWorkspace };
@@ -0,0 +1,4 @@
1
+ export { createStore } from "./store";
2
+ export { createLogger } from "./logger";
3
+ export { createScope, useScope } from "./scope";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/workspace/infrastructure/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { createStore } from "./store";
2
+ export { createLogger } from "./logger";
3
+ export { createScope, useScope } from "./scope";
@@ -1,4 +1,4 @@
1
- import type { IConfiguration } from "./types";
1
+ import type { IConfiguration } from "../types";
2
2
  declare const createLogger: (config: IConfiguration) => {
3
3
  log: (callback: () => void, { type, scope, metadata, }: {
4
4
  type: string;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/workspace/infrastructure/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,QAAA,MAAM,YAAY,GAAI,QAAQ,cAAc;oBAI5B,MAAM,IAAI,8BAKjB;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,GAAG,CAAA;KAAE;CAgC5D,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { createScope, useScope } from "./scope";
2
+ export * from "./types";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/workspace/infrastructure/scope/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAChD,cAAc,SAAS,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { createScope, useScope } from "./scope";
2
+ export * from "./types";
@@ -0,0 +1,7 @@
1
+ import type { IScope } from "./types";
2
+ declare function createScope<V = any>(defaultValue: V): IScope<V> & {
3
+ currentValue: V;
4
+ };
5
+ declare function useScope<V = any>(ctx: IScope<V>): V;
6
+ export { createScope, useScope };
7
+ //# sourceMappingURL=scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../../../../src/workspace/infrastructure/scope/scope.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAyB,MAAM,SAAS,CAAC;AAE7D,iBAAS,WAAW,CAAC,CAAC,GAAG,GAAG,EAC1B,YAAY,EAAE,CAAC,GACd,MAAM,CAAC,CAAC,CAAC,GAAG;IAAE,YAAY,EAAE,CAAC,CAAA;CAAE,CAwBjC;AACD,iBAAS,QAAQ,CAAC,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,MAAM,CAAC,CAAC,CAAC,KAGxC;AACD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { createStore } from "../store";
2
+ function createScope(defaultValue) {
3
+ const store = createStore();
4
+ store.setState("stack", []);
5
+ const stack = store.getState("stack");
6
+ function provider(options) {
7
+ const { value, children } = options;
8
+ const stack = store.getState("stack");
9
+ if (!stack) {
10
+ throw new Error("Scope stack not found");
11
+ }
12
+ try {
13
+ stack.push(value ? value : defaultValue);
14
+ children();
15
+ }
16
+ finally {
17
+ stack.pop();
18
+ }
19
+ }
20
+ return {
21
+ provider,
22
+ get currentValue() {
23
+ let last = (stack === null || stack === void 0 ? void 0 : stack.length) ? stack[stack.length - 1] : null;
24
+ return last ? last : defaultValue;
25
+ },
26
+ };
27
+ }
28
+ function useScope(ctx) {
29
+ const scope = ctx;
30
+ return scope.currentValue;
31
+ }
32
+ export { createScope, useScope };
@@ -0,0 +1,8 @@
1
+ export interface IScope<V = any> {
2
+ provider(options: IScopeProviderOptions<V>): void;
3
+ }
4
+ export interface IScopeProviderOptions<V = any> {
5
+ value: V;
6
+ children: () => void;
7
+ }
8
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/workspace/infrastructure/scope/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM,CAAC,CAAC,GAAG,GAAG;IAC7B,QAAQ,CAAC,OAAO,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;CACnD;AAED,MAAM,WAAW,qBAAqB,CAAC,CAAC,GAAG,GAAG;IAC5C,KAAK,EAAE,CAAC,CAAC;IACT,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../src/workspace/infrastructure/store.ts"],"names":[],"mappings":"AAAA,iBAAS,WAAW,CAAC,CAAC;iBAGL,MAAM,QAAQ,CAAC;iBAGf,MAAM;iBAGN,MAAM;oBAGH,MAAM;;EAOzB;AACD,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./repository";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/workspace/modules/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./repository";
@@ -0,0 +1,10 @@
1
+ import type { IRepositoryPlugin, repositoryType } from "./types";
2
+ declare function createRepositoryModule<I>(): {
3
+ queryRepository: <R = any>(id: string) => {
4
+ repository: ReturnType<repositoryType<I, R>>;
5
+ disconnect(): void;
6
+ };
7
+ defineRepository: <R = any>(repositoryPlugin: IRepositoryPlugin<I, R>) => void;
8
+ };
9
+ export { createRepositoryModule };
10
+ //# sourceMappingURL=createRepositoryModule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createRepositoryModule.d.ts","sourceRoot":"","sources":["../../../../src/workspace/modules/repository/createRepositoryModule.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAIjE,iBAAS,sBAAsB,CAAC,CAAC;sBAsCN,CAAC,YAAY,MAAM;oBAgBd,UAAU,CAAC,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;;;uBA1CpC,CAAC,0BACP,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC;EAmD5C;AAED,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
@@ -1,24 +1,23 @@
1
- import { createLogger } from "./logger";
2
1
  import { createRepositoryAccessor } from "./repositoryAccessor";
3
- import { createStore } from "./store";
4
- function createWorkspace(infrastructure, config) {
5
- var _a;
6
- const defaultConfig = {
7
- id: config.id,
8
- logging: (_a = config.logging) !== null && _a !== void 0 ? _a : false,
9
- };
10
- const logger = createLogger(defaultConfig);
11
- const store = createStore();
12
- const hasRepository = (id) => store.hasState(id);
13
- const allRepositories = () => Array.from(store.getEntries()).map(([id, repository]) => ({
14
- repository: id,
15
- connections: repository.connections,
16
- }));
17
- const defineRepository = (id, repository, config) => {
2
+ import { useScope } from "../../infrastructure";
3
+ import { repositoryScope } from "../../providers";
4
+ function createRepositoryModule() {
5
+ const { store, logger, infrastructure } = useScope(repositoryScope);
6
+ function hasRepository(id) {
7
+ return store.hasState(id);
8
+ }
9
+ function allRepositories() {
10
+ return Array.from(store.getEntries()).map(([id, repository]) => ({
11
+ repository: id,
12
+ connections: repository.connections,
13
+ }));
14
+ }
15
+ function defineRepository(repositoryPlugin) {
16
+ const { id } = repositoryPlugin;
18
17
  if (hasRepository(id))
19
18
  return;
20
19
  logger.log(() => {
21
- store.setState(id, createRepositoryAccessor(repository, infrastructure, config));
20
+ store.setState(id, createRepositoryAccessor(infrastructure, repositoryPlugin));
22
21
  }, {
23
22
  type: "repository.define",
24
23
  scope: id,
@@ -30,8 +29,8 @@ function createWorkspace(infrastructure, config) {
30
29
  };
31
30
  },
32
31
  });
33
- };
34
- const queryRepository = (id) => {
32
+ }
33
+ function queryRepository(id) {
35
34
  const entity = store.getState(id);
36
35
  if (!entity) {
37
36
  throw new Error(`Repository "${id}" not found`);
@@ -47,15 +46,15 @@ function createWorkspace(infrastructure, config) {
47
46
  });
48
47
  const { repository } = entity;
49
48
  return {
50
- repository,
49
+ repository: repository,
51
50
  disconnect() {
52
51
  entity.disconnect();
53
52
  },
54
53
  };
55
- };
54
+ }
56
55
  return {
57
- defineRepository,
58
56
  queryRepository,
57
+ defineRepository,
59
58
  };
60
59
  }
61
- export { createWorkspace };
60
+ export { createRepositoryModule };
@@ -0,0 +1,3 @@
1
+ export * from "./createRepositoryModule";
2
+ export * from "./types";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/workspace/modules/repository/index.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,SAAS,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./createRepositoryModule";
2
+ export * from "./types";
@@ -0,0 +1 @@
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../../src/workspace/modules/repository/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAE1C,wBAAgB,eAAe,CAAC,UAAU,EAAE,GAAG,EAAE,WAAW,EAAE,UAAU,EAAE,OAkCzE"}
@@ -0,0 +1,9 @@
1
+ import type { IRepositoryPlugin } from "./types";
2
+ declare function createRepositoryAccessor<I>(infrastructure: I, repositoryPlugin: IRepositoryPlugin<I, any>): {
3
+ readonly repository: unknown;
4
+ readonly connections: number;
5
+ connect(): void;
6
+ disconnect(): void;
7
+ };
8
+ export { createRepositoryAccessor };
9
+ //# sourceMappingURL=repositoryAccessor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repositoryAccessor.d.ts","sourceRoot":"","sources":["../../../../src/workspace/modules/repository/repositoryAccessor.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,iBAAS,wBAAwB,CAAC,CAAC,EACjC,cAAc,EAAE,CAAC,EACjB,gBAAgB,EAAE,iBAAiB,CAAC,CAAC,EAAE,GAAG,CAAC;;;;;EAuC5C;AAED,OAAO,EAAE,wBAAwB,EAAE,CAAC"}
@@ -0,0 +1,42 @@
1
+ import { useScope } from "../../infrastructure";
2
+ import { applyMiddleware } from "./middleware";
3
+ function createRepositoryAccessor(infrastructure, repositoryPlugin) {
4
+ const { install, middlewares, onConnect, onDisconnect } = repositoryPlugin;
5
+ let repository = undefined;
6
+ let connections = 0;
7
+ const obj = {
8
+ get repository() {
9
+ return repository;
10
+ },
11
+ get connections() {
12
+ return connections;
13
+ },
14
+ connect() {
15
+ if (connections === 0) {
16
+ const rawRepository = install({
17
+ instance: { infrastructure, useScope },
18
+ });
19
+ repository = middlewares
20
+ ? applyMiddleware(rawRepository, middlewares)
21
+ : rawRepository;
22
+ if (onConnect) {
23
+ onConnect();
24
+ }
25
+ }
26
+ connections += 1;
27
+ },
28
+ disconnect() {
29
+ if (connections === 0)
30
+ return;
31
+ connections -= 1;
32
+ if (connections === 0) {
33
+ repository = undefined;
34
+ if (onDisconnect) {
35
+ onDisconnect();
36
+ }
37
+ }
38
+ },
39
+ };
40
+ return obj;
41
+ }
42
+ export { createRepositoryAccessor };
@@ -0,0 +1,22 @@
1
+ import type { useScope } from "../../infrastructure/scope";
2
+ export type repositoryType<I = any, R = any> = (obj: {
3
+ instance: {
4
+ infrastructure: I;
5
+ useScope: typeof useScope;
6
+ };
7
+ }) => R;
8
+ export interface IRepositoryPlugin<I = any, R = any> {
9
+ id: string;
10
+ install: repositoryType<I, R>;
11
+ middlewares?: Middleware[];
12
+ onConnect?: () => void;
13
+ onDisconnect?: () => void;
14
+ }
15
+ export interface IRepositoryInstance<R = any> {
16
+ connect(): void;
17
+ disconnect(): void;
18
+ repository: ReturnType<repositoryType<any, R>> | undefined;
19
+ connections: number;
20
+ }
21
+ export type Middleware = (method: string, args: any[], next: (...nextArgs: any[]) => any) => any;
22
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/workspace/modules/repository/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAC;AAE3D,MAAM,MAAM,cAAc,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE;IACnD,QAAQ,EAAE;QACR,cAAc,EAAE,CAAC,CAAC;QAClB,QAAQ,EAAE,OAAO,QAAQ,CAAC;KAC3B,CAAC;CACH,KAAK,CAAC,CAAC;AAER,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG;IACjD,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9B,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,IAAI,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,IAAI,CAAC;CAC3B;AAED,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,GAAG;IAC1C,OAAO,IAAI,IAAI,CAAC;IAChB,UAAU,IAAI,IAAI,CAAC;IACnB,UAAU,EAAE,UAAU,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;IAC3D,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,MAAM,UAAU,GAAG,CACvB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,GAAG,EAAE,EACX,IAAI,EAAE,CAAC,GAAG,QAAQ,EAAE,GAAG,EAAE,KAAK,GAAG,KAC9B,GAAG,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ declare const repositoryScope: import("../infrastructure/scope").IScope<any> & {
2
+ currentValue: any;
3
+ };
4
+ export { repositoryScope };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/workspace/providers/index.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,eAAe;;CAA8B,CAAC;AAEpD,OAAO,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ import { createScope } from "../infrastructure";
2
+ const repositoryScope = createScope(undefined);
3
+ export { repositoryScope };
@@ -0,0 +1,5 @@
1
+ export interface IConfiguration {
2
+ id: string;
3
+ logging?: boolean;
4
+ }
5
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/workspace/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB"}
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@med1802/repository-manager",
3
- "version": "2.2.1",
3
+ "version": "3.0.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./manager";
package/src/manager.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { IConfiguration } from "./workspace/types";
2
+ import { createWorkspace } from "./workspace";
3
+ import { createStore } from "./workspace/infrastructure";
4
+ import type { IScope } from "./workspace/infrastructure/scope";
5
+
6
+ const repositoryManager = () => {
7
+ const store = createStore<ReturnType<typeof createWorkspace>>();
8
+ return {
9
+ workspace<I>(infrastructure: I, config: IConfiguration) {
10
+ const workspace = createWorkspace<I>(infrastructure, config);
11
+ store.setState(config.id, workspace);
12
+ return {
13
+ defineRepository: workspace.defineRepository,
14
+ queryRepository: workspace.queryRepository,
15
+ createScope: workspace.createScope as unknown as <V = any>(
16
+ defaultValue: V
17
+ ) => IScope<V>,
18
+ };
19
+ },
20
+ };
21
+ };
22
+
23
+ export { repositoryManager };
@@ -0,0 +1,38 @@
1
+ import type { IConfiguration } from "./types";
2
+ import { createRepositoryModule, type IRepositoryInstance } from "./modules";
3
+ import {
4
+ createScope,
5
+ createLogger,
6
+ createStore,
7
+ useScope,
8
+ } from "./infrastructure";
9
+ import { repositoryScope } from "./providers";
10
+
11
+ function createWorkspace<I>(infrastructure: I, config: IConfiguration) {
12
+ const defaultConfig: IConfiguration = {
13
+ id: config.id,
14
+ logging: config.logging ?? false,
15
+ };
16
+ const logger = createLogger(defaultConfig);
17
+ const store = createStore<IRepositoryInstance<any>>();
18
+ let repositoryModule!: ReturnType<typeof createRepositoryModule<I>>;
19
+ repositoryScope.provider({
20
+ value: {
21
+ store,
22
+ logger,
23
+ infrastructure,
24
+ },
25
+ children: () => {
26
+ repositoryModule = createRepositoryModule<I>();
27
+ },
28
+ });
29
+
30
+ return {
31
+ defineRepository: repositoryModule.defineRepository,
32
+ queryRepository: repositoryModule.queryRepository,
33
+ createScope,
34
+ useScope,
35
+ };
36
+ }
37
+
38
+ export { createWorkspace };
@@ -0,0 +1,3 @@
1
+ export { createStore } from "./store";
2
+ export { createLogger } from "./logger";
3
+ export { createScope, useScope } from "./scope";
@@ -1,4 +1,4 @@
1
- import type { IConfiguration } from "./types";
1
+ import type { IConfiguration } from "../types";
2
2
 
3
3
  const createLogger = (config: IConfiguration) => {
4
4
  const { logging } = config;
@@ -0,0 +1,2 @@
1
+ export { createScope, useScope } from "./scope";
2
+ export * from "./types";
@@ -0,0 +1,35 @@
1
+ import { createStore } from "../store";
2
+ import type { IScope, IScopeProviderOptions } from "./types";
3
+
4
+ function createScope<V = any>(
5
+ defaultValue: V
6
+ ): IScope<V> & { currentValue: V } {
7
+ const store = createStore<V[]>();
8
+ store.setState("stack", []);
9
+ const stack = store.getState("stack");
10
+ function provider(options: IScopeProviderOptions) {
11
+ const { value, children } = options;
12
+ const stack = store.getState("stack");
13
+ if (!stack) {
14
+ throw new Error("Scope stack not found");
15
+ }
16
+ try {
17
+ stack.push(value ? value : defaultValue);
18
+ children();
19
+ } finally {
20
+ stack.pop();
21
+ }
22
+ }
23
+ return {
24
+ provider,
25
+ get currentValue() {
26
+ let last = stack?.length ? stack[stack.length - 1] : null;
27
+ return last ? last : defaultValue;
28
+ },
29
+ };
30
+ }
31
+ function useScope<V = any>(ctx: IScope<V>) {
32
+ const scope = ctx as IScope<V> & { currentValue: V };
33
+ return scope.currentValue;
34
+ }
35
+ export { createScope, useScope };
@@ -0,0 +1,8 @@
1
+ export interface IScope<V = any> {
2
+ provider(options: IScopeProviderOptions<V>): void;
3
+ }
4
+
5
+ export interface IScopeProviderOptions<V = any> {
6
+ value: V;
7
+ children: () => void;
8
+ }
@@ -0,0 +1 @@
1
+ export * from "./repository";