phecda-server 6.0.2 → 7.0.0-alpha.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 (83) hide show
  1. package/bin/cli.mjs +160 -157
  2. package/dist/{chunk-HDQAT2IM.mjs → chunk-5622RBNB.mjs} +118 -90
  3. package/dist/chunk-BGXSMOLX.js +265 -0
  4. package/dist/{chunk-F3W3NN7F.mjs → chunk-HMVLXNV3.mjs} +5 -14
  5. package/dist/{chunk-GHFSIZUO.js → chunk-J5CFUN4V.js} +18 -3
  6. package/dist/{chunk-XCND2QW3.js → chunk-MBCHNDAY.js} +27 -36
  7. package/dist/{chunk-WXFZZZRY.mjs → chunk-P75VKZJY.mjs} +101 -137
  8. package/dist/chunk-SW5IKE5H.js +503 -0
  9. package/dist/{chunk-UXD62LGG.mjs → chunk-WHJ5FALK.mjs} +17 -2
  10. package/dist/core-BIcUwV18.d.mts +47 -0
  11. package/dist/core-CYwEPfN4.d.ts +47 -0
  12. package/dist/helper.d.mts +4 -6
  13. package/dist/helper.d.ts +4 -6
  14. package/dist/helper.js +3 -3
  15. package/dist/helper.mjs +2 -2
  16. package/dist/{server → http}/elysia/index.d.mts +5 -5
  17. package/dist/{server → http}/elysia/index.d.ts +5 -5
  18. package/dist/{server → http}/elysia/index.js +80 -62
  19. package/dist/{server → http}/elysia/index.mjs +50 -32
  20. package/dist/{server → http}/express/index.d.mts +5 -5
  21. package/dist/{server → http}/express/index.d.ts +5 -5
  22. package/dist/{server → http}/express/index.js +71 -55
  23. package/dist/{server → http}/express/index.mjs +42 -26
  24. package/dist/{server → http}/fastify/index.d.mts +5 -5
  25. package/dist/{server → http}/fastify/index.d.ts +5 -5
  26. package/dist/{server → http}/fastify/index.js +72 -72
  27. package/dist/{server → http}/fastify/index.mjs +47 -47
  28. package/dist/{server → http}/h3/index.d.mts +5 -5
  29. package/dist/{server → http}/h3/index.d.ts +5 -5
  30. package/dist/http/h3/index.js +160 -0
  31. package/dist/{server → http}/h3/index.mjs +68 -62
  32. package/dist/{server → http}/hono/index.d.mts +5 -5
  33. package/dist/{server → http}/hono/index.d.ts +5 -5
  34. package/dist/{server → http}/hono/index.js +69 -54
  35. package/dist/{server → http}/hono/index.mjs +41 -26
  36. package/dist/{server → http}/hyper-express/index.d.mts +5 -5
  37. package/dist/{server → http}/hyper-express/index.d.ts +5 -5
  38. package/dist/{server → http}/hyper-express/index.js +69 -56
  39. package/dist/{server → http}/hyper-express/index.mjs +41 -28
  40. package/dist/{server → http}/koa/index.d.mts +5 -5
  41. package/dist/{server → http}/koa/index.d.ts +5 -5
  42. package/dist/{server → http}/koa/index.js +71 -55
  43. package/dist/{server → http}/koa/index.mjs +42 -26
  44. package/dist/index.d.mts +53 -41
  45. package/dist/index.d.ts +53 -41
  46. package/dist/index.js +62 -78
  47. package/dist/index.mjs +46 -62
  48. package/dist/{meta-KtfiBIde.d.ts → meta-BXKLFTgG.d.mts} +2 -3
  49. package/dist/{meta-KtfiBIde.d.mts → meta-BXKLFTgG.d.ts} +2 -3
  50. package/dist/rpc/bullmq/index.d.mts +6 -6
  51. package/dist/rpc/bullmq/index.d.ts +6 -6
  52. package/dist/rpc/bullmq/index.js +24 -24
  53. package/dist/rpc/bullmq/index.mjs +14 -14
  54. package/dist/rpc/kafka/index.d.mts +3 -3
  55. package/dist/rpc/kafka/index.d.ts +3 -3
  56. package/dist/rpc/kafka/index.js +25 -21
  57. package/dist/rpc/kafka/index.mjs +15 -11
  58. package/dist/rpc/nats/index.d.mts +3 -3
  59. package/dist/rpc/nats/index.d.ts +3 -3
  60. package/dist/rpc/nats/index.js +22 -21
  61. package/dist/rpc/nats/index.mjs +13 -12
  62. package/dist/rpc/rabbitmq/index.d.mts +3 -3
  63. package/dist/rpc/rabbitmq/index.d.ts +3 -3
  64. package/dist/rpc/rabbitmq/index.js +23 -22
  65. package/dist/rpc/rabbitmq/index.mjs +12 -11
  66. package/dist/rpc/redis/index.d.mts +3 -3
  67. package/dist/rpc/redis/index.d.ts +3 -3
  68. package/dist/rpc/redis/index.js +24 -20
  69. package/dist/rpc/redis/index.mjs +15 -11
  70. package/dist/test.d.mts +3 -3
  71. package/dist/test.d.ts +3 -3
  72. package/dist/test.js +6 -6
  73. package/dist/test.mjs +2 -2
  74. package/dist/{types-JVT0mgLD.d.ts → types-BtbL49Zs.d.mts} +2 -1
  75. package/dist/{types-DIw7B6Up.d.ts → types-VFzEM7LL.d.ts} +5 -5
  76. package/dist/{types-BvD3B5Ny.d.mts → types-h40T3cRG.d.mts} +5 -5
  77. package/dist/{types-t9D1fU6G.d.mts → types-m3IEDKjP.d.ts} +2 -1
  78. package/package.json +24 -24
  79. package/dist/chunk-7SYPUIBY.js +0 -539
  80. package/dist/chunk-LYEZ6RGX.js +0 -237
  81. package/dist/core-2SNw8m2B.d.mts +0 -30
  82. package/dist/core-BmJJLUqj.d.ts +0 -30
  83. package/dist/server/h3/index.js +0 -154
package/bin/cli.mjs CHANGED
@@ -1,158 +1,161 @@
1
1
  #! /usr/bin/env node
2
- import { fork } from 'child_process'
3
- import { createRequire } from 'module'
4
- import { fileURLToPath } from 'url'
5
-
6
- import { dirname, join, resolve } from 'path'
7
- import pc from 'picocolors'
8
- import cac from 'cac'
9
- import fse from 'fs-extra'
10
- import { log } from '../dist/index.mjs'
11
-
12
- const cli = cac('phecda').option('-c,--config <config>', 'config file', {
13
- default: 'ps.json',
14
- })
15
-
16
- const __dirname = dirname(fileURLToPath(import.meta.url))
17
-
18
- const require = createRequire(import.meta.url)
19
- let child
20
-
21
- let closePromise
22
- const nodeVersion = parseFloat(process.version.slice(1))
23
-
24
- if (nodeVersion < 18.19) {
25
- log(
26
- `Nodejs version less than 18.19(current is ${nodeVersion}) can't support hmr`,
27
- 'yellow',
28
- )
29
- }
30
-
31
- function startChild(file, args) {
32
- child = fork(file, {
33
- env: { NODE_ENV: 'development', ...process.env },
34
- stdio: 'inherit',
35
- execArgv: [
36
- nodeVersion < 18.19
37
- ? '--loader=phecda-server/register/loader.mjs'
38
- : '--import=phecda-server/register',
39
- ...args,
40
- ],
41
- })
42
-
43
- closePromise = new Promise((resolve) => {
44
- child.once('exit', (code) => {
45
- if (code === 4) {
46
- log('only generate code')
47
- process.exit(0)
48
- }
49
- if (code >= 2) {
50
- // for relaunch
51
- log('relaunch...')
52
- startChild(file, args)
53
- }
54
- child = undefined
55
-
56
- resolve()
57
- })
58
- })
59
- }
60
-
61
- function exit() {
62
- log('process exit')
63
-
64
- if (child) {
65
- child.kill()
66
- process.exit(0)
67
- }
68
- else {
69
- process.exit(0)
70
- }
71
- }
72
- process.on('SIGINT', () => {
73
- process.exit()
74
- })
75
-
76
- cli
77
- .command('init [root]', 'init config file')
78
- .allowUnknownOptions()
79
- .option('-t,--tsconfig <tsconfig>', 'init tsconfig file', {
80
- default: 'tsconfig.json',
81
- })
82
- .action(async (root, options) => {
83
- if (!root)
84
- root = ''
85
-
86
- const tsconfigPath = join(root, options.tsconfig)
87
- const psconfigPath = join(root, options.config)
88
-
89
- if (!fse.existsSync(tsconfigPath)) {
90
- log(`create ${tsconfigPath}`)
91
-
92
- await fse.copyFile(
93
- resolve(__dirname, '../assets/tsconfig.json'),
94
- tsconfigPath,
95
- )
96
- }
97
-
98
- if (!fse.existsSync(psconfigPath)) {
99
- log(`create ${psconfigPath}`)
100
-
101
- await fse.copyFile(resolve(__dirname, '../assets/ps.json'), psconfigPath)
102
- }
103
-
104
- log('init finish')
105
- })
106
-
107
- cli
108
- .command('<file> [root]', 'run file')
109
- .alias('run')
110
- .allowUnknownOptions()
111
- .alias('run')
112
- .action((file, root, options) => {
113
- if (root)
114
- process.env.PS_WORKDIR = root
115
- process.env.PS_CONFIG_FILE = options.config
116
-
117
- log('process start!')
118
-
119
- startChild(file, options['--'])
120
- console.log(`${pc.green('->')} press ${pc.green('e')} to exit`)
121
- console.log(`${pc.green('->')} press ${pc.green('r')} to relaunch`)
122
-
123
- process.stdin.on('data', async (data) => {
124
- const input = data.toString().trim().toLocaleLowerCase()
125
- if (input === 'r') {
126
- if (child) {
127
- await child.kill()
128
- if (closePromise)
129
- await closePromise
130
- log('relaunch...')
131
- startChild(file, options['--'])
132
- }
133
- else {
134
- log('relaunch...')
135
-
136
- startChild(file, options['--'])
137
- }
138
- }
139
- if (input === 'e')
140
- exit()
141
- })
142
- })
143
-
144
- cli
145
- .command('generate <file> [root]', 'generate code(mainly for ci)')
146
- .allowUnknownOptions()
147
- .action((file, root, options) => {
148
- if (root)
149
- process.env.PS_WORKDIR = root
150
- process.env.PS_GENERATE = 'true'
151
- process.env.PS_CONFIG_FILE = options.config
152
- startChild(file, options['--'])
153
- })
154
-
155
- cli.help()
156
- cli.version(require('../package.json').version)
157
-
158
- cli.parse()
2
+ import { fork } from 'child_process'
3
+ import { createRequire } from 'module'
4
+ import { fileURLToPath } from 'url'
5
+
6
+ import { dirname, join, resolve } from 'path'
7
+ import pc from 'picocolors'
8
+ import cac from 'cac'
9
+ import fse from 'fs-extra'
10
+ import { log } from '../dist/index.mjs'
11
+
12
+ const cli = cac('phecda').option('-c,--config <config>', 'config file', {
13
+ default: 'ps.json',
14
+ })
15
+
16
+ const __dirname = dirname(fileURLToPath(import.meta.url))
17
+
18
+ const require = createRequire(import.meta.url)
19
+ let child
20
+
21
+ let closePromise
22
+ const nodeVersion = parseFloat(process.version.slice(1))
23
+
24
+ if (nodeVersion < 18.19) {
25
+ log(
26
+ `Nodejs version less than 18.19(current is ${nodeVersion}) can't support hmr`,
27
+ 'yellow',
28
+ )
29
+ }
30
+
31
+ function startChild(file, args) {
32
+ child = fork(file, {
33
+ env: { NODE_ENV: 'development', ...process.env },
34
+ stdio: 'inherit',
35
+ execArgv: [
36
+ nodeVersion < 18.19
37
+ ? '--loader=phecda-server/register/loader.mjs'
38
+ : '--import=phecda-server/register',
39
+ ...args,
40
+ ],
41
+ })
42
+
43
+ closePromise = new Promise((resolve) => {
44
+ child.once('exit', (code) => {
45
+ if (code === 4) {
46
+ log('only generate code')
47
+ process.exit(0)
48
+ }
49
+ if (code >= 2) {
50
+ // for relaunch
51
+ log('relaunch...')
52
+ startChild(file, args)
53
+ }
54
+ child = undefined
55
+
56
+ resolve()
57
+ })
58
+ })
59
+ }
60
+
61
+ function exit() {
62
+ log('process exit')
63
+
64
+ if (child) {
65
+ child.kill()
66
+ process.exit(0)
67
+ }
68
+ else {
69
+ process.exit(0)
70
+ }
71
+ }
72
+ process.on('SIGINT', () => {
73
+ process.exit()
74
+ })
75
+
76
+ cli
77
+ .command('init [root]', 'init config file')
78
+ .allowUnknownOptions()
79
+ .option('-t,--tsconfig <tsconfig>', 'init tsconfig file', {
80
+ default: 'tsconfig.json',
81
+ })
82
+ .action(async (root, options) => {
83
+ if (!root)
84
+ root = ''
85
+
86
+ const tsconfigPath = join(root, options.tsconfig)
87
+ const psconfigPath = join(root, options.config)
88
+
89
+ if (!fse.existsSync(tsconfigPath)) {
90
+ log(`create ${tsconfigPath}`)
91
+
92
+ await fse.copyFile(
93
+ resolve(__dirname, '../assets/tsconfig.json'),
94
+ tsconfigPath,
95
+ )
96
+ }
97
+
98
+ if (!fse.existsSync(psconfigPath)) {
99
+ log(`create ${psconfigPath}`)
100
+
101
+ await fse.copyFile(resolve(__dirname, '../assets/ps.json'), psconfigPath)
102
+ }
103
+
104
+ log('init finish')
105
+ })
106
+
107
+ cli
108
+ .command('<file> [root]', 'run file')
109
+ .alias('run')
110
+ .allowUnknownOptions()
111
+ .alias('run')
112
+ .action((file, root, options) => {
113
+ if (root)
114
+ process.env.PS_WORKDIR = root
115
+ process.env.PS_CONFIG_FILE = options.config
116
+
117
+ log('process start!')
118
+
119
+ startChild(file, options['--'])
120
+ console.log(`${pc.green('->')} press ${pc.green('e')} to exit`)
121
+ console.log(`${pc.green('->')} press ${pc.green('r')} to relaunch`)
122
+
123
+ process.stdin.on('data', async (data) => {
124
+ const input = data.toString().trim().toLocaleLowerCase()
125
+ if (input === 'r') {
126
+ if (child) {
127
+ await child.kill()
128
+ if (closePromise)
129
+ await closePromise
130
+ log('relaunch...')
131
+ startChild(file, options['--'])
132
+ }
133
+ else {
134
+ log('relaunch...')
135
+
136
+ startChild(file, options['--'])
137
+ }
138
+ }
139
+ if (input === 'e')
140
+ exit()
141
+
142
+ if (input === 'c')
143
+ console.clear()
144
+ })
145
+ })
146
+
147
+ cli
148
+ .command('generate <file> [root]', 'generate code(mainly for ci)')
149
+ .allowUnknownOptions()
150
+ .action((file, root, options) => {
151
+ if (root)
152
+ process.env.PS_WORKDIR = root
153
+ process.env.PS_GENERATE = 'true'
154
+ process.env.PS_CONFIG_FILE = options.config
155
+ startChild(file, options['--'])
156
+ })
157
+
158
+ cli.help()
159
+ cli.version(require('../package.json').version)
160
+
161
+ cli.parse()
@@ -3,7 +3,7 @@ import {
3
3
  IS_ONLY_GENERATE,
4
4
  __name,
5
5
  log
6
- } from "./chunk-UXD62LGG.mjs";
6
+ } from "./chunk-WHJ5FALK.mjs";
7
7
 
8
8
  // src/meta.ts
9
9
  var Meta = class {
@@ -25,13 +25,7 @@ import { getInject, getMergedMeta, getMetaKey, getMetaParams, getTag, invokeInit
25
25
  import Debug from "debug";
26
26
  var debug = Debug("phecda-server(createPhecda)");
27
27
  var emitter = new EventEmitter();
28
- async function createPhecda(models, opts = {}) {
29
- const moduleMap = /* @__PURE__ */ new Map();
30
- const meta = [];
31
- const modelMap = /* @__PURE__ */ new WeakMap();
32
- const modelSet = /* @__PURE__ */ new WeakSet();
33
- const dependenceGraph = /* @__PURE__ */ new Map();
34
- const { parseModule = /* @__PURE__ */ __name((module) => module, "parseModule"), parseMeta = /* @__PURE__ */ __name((meta2) => meta2, "parseMeta"), generators } = opts;
28
+ function defaultServerInject() {
35
29
  if (!getInject("watcher")) {
36
30
  setInject("watcher", ({ eventName, instance, property, options }) => {
37
31
  const fn = typeof instance[property] === "function" ? instance[property].bind(instance) : (v) => instance[property] = v;
@@ -42,51 +36,98 @@ async function createPhecda(models, opts = {}) {
42
36
  };
43
37
  });
44
38
  }
45
- async function del(tag) {
46
- if (!moduleMap.has(tag)) return;
47
- const module = moduleMap.get(tag);
48
- debug(`unmount module "${String(tag)}"`);
49
- await invokeUnmount(module);
50
- debug(`del module "${String(tag)}"`);
51
- moduleMap.delete(tag);
52
- modelMap.delete(module);
53
- for (let i = meta.length - 1; i >= 0; i--) {
54
- if (meta[i].data.tag === tag) meta.splice(i, 1);
55
- }
56
- return module;
39
+ }
40
+ __name(defaultServerInject, "defaultServerInject");
41
+ var phecdaNamespace = /* @__PURE__ */ new Map();
42
+ var ServerPhecda = class {
43
+ static {
44
+ __name(this, "ServerPhecda");
57
45
  }
58
- __name(del, "del");
59
- async function destroy() {
60
- debug("destroy all");
61
- for (const [tag] of moduleMap) await del(tag);
46
+ moduleMap = /* @__PURE__ */ new Map();
47
+ meta = [];
48
+ modelMap = /* @__PURE__ */ new WeakMap();
49
+ modelSet = /* @__PURE__ */ new WeakSet();
50
+ dependenceGraph = /* @__PURE__ */ new Map();
51
+ parseModule;
52
+ parseMeta;
53
+ generators;
54
+ constructor(options) {
55
+ defaultServerInject();
56
+ const { namespace = "default", parseModule = /* @__PURE__ */ __name((module) => module, "parseModule"), parseMeta = /* @__PURE__ */ __name((meta) => meta, "parseMeta"), generators = [] } = options;
57
+ phecdaNamespace.set(namespace, this);
58
+ this.parseMeta = parseMeta;
59
+ this.parseModule = parseModule;
60
+ this.generators = generators;
62
61
  }
63
- __name(destroy, "destroy");
64
- async function add(Model) {
62
+ async start(models) {
63
+ for (const model of models) await this.buildDepModule(model);
64
+ const generateCode = /* @__PURE__ */ __name(async () => {
65
+ if (IS_HMR) {
66
+ return Promise.all(this.generators.map((generator) => {
67
+ debug(`generate "${generator.name}" code to ${generator.path}`);
68
+ return generator.output(this.meta);
69
+ }));
70
+ }
71
+ }, "generateCode");
72
+ generateCode().then(() => {
73
+ if (IS_ONLY_GENERATE) process.exit(4);
74
+ });
75
+ if (IS_HMR) {
76
+ if (!globalThis.__PS_HMR__) globalThis.__PS_HMR__ = [];
77
+ globalThis.__PS_HMR__?.push(async (files) => {
78
+ debug("reload files ");
79
+ for (const file of files) {
80
+ const models2 = await import(file);
81
+ for (const i in models2) {
82
+ if (isPhecda(models2[i])) await this.add(models2[i]);
83
+ }
84
+ }
85
+ generateCode();
86
+ });
87
+ }
88
+ }
89
+ async add(Model) {
65
90
  const tag = getTag(Model);
66
- const oldInstance = await del(tag);
67
- const { module: newModule } = await buildDepModule(Model);
68
- if (oldInstance && dependenceGraph.has(tag)) {
91
+ const oldInstance = await this.del(tag);
92
+ const { module: newModule } = await this.buildDepModule(Model);
93
+ if (oldInstance && this.dependenceGraph.has(tag)) {
69
94
  debug(`replace module "${String(tag)}"`);
70
95
  [
71
- ...dependenceGraph.get(tag)
96
+ ...this.dependenceGraph.get(tag)
72
97
  ].forEach((tag2) => {
73
- const module = moduleMap.get(tag2);
98
+ const module = this.moduleMap.get(tag2);
74
99
  for (const key in module) {
75
100
  if (module[key] === oldInstance) module[key] = newModule;
76
101
  }
77
102
  });
78
103
  }
79
104
  }
80
- __name(add, "add");
81
- async function buildDepModule(Model) {
105
+ async del(tag) {
106
+ if (!this.moduleMap.has(tag)) return;
107
+ const module = this.moduleMap.get(tag);
108
+ debug(`unmount module "${String(tag)}"`);
109
+ await invokeUnmount(module);
110
+ debug(`del module "${String(tag)}"`);
111
+ this.moduleMap.delete(tag);
112
+ this.modelMap.delete(module);
113
+ for (let i = this.meta.length - 1; i >= 0; i--) {
114
+ if (this.meta[i].data.tag === tag) this.meta.splice(i, 1);
115
+ }
116
+ return module;
117
+ }
118
+ async destroy() {
119
+ debug("destroy all");
120
+ for (const [tag] of this.moduleMap) await this.del(tag);
121
+ }
122
+ async buildDepModule(Model) {
82
123
  const paramtypes = getParamTypes(Model);
83
124
  let module;
84
125
  const tag = getTag(Model);
85
- if (moduleMap.has(tag)) {
86
- module = moduleMap.get(tag);
126
+ if (this.moduleMap.has(tag)) {
127
+ module = this.moduleMap.get(tag);
87
128
  if (!module) throw new Error(`exist Circular-Dependency or Multiple modules with the same name/tag [tag] ${String(tag)}--[module] ${Model}`);
88
- if (modelMap.get(module) !== Model && !modelSet.has(Model)) {
89
- modelSet.add(Model);
129
+ if (this.modelMap.get(module) !== Model && !this.modelSet.has(Model)) {
130
+ this.modelSet.add(Model);
90
131
  if (module instanceof Model) log(`Module taged ${String(tag)} has been overridden`);
91
132
  else log(`Synonym module: Module taged "${String(tag)}" has been loaded before, so phecda-server won't load Module "${Model.name}"`, "warn");
92
133
  }
@@ -95,69 +136,59 @@ async function createPhecda(models, opts = {}) {
95
136
  tag
96
137
  };
97
138
  }
98
- moduleMap.set(tag, void 0);
139
+ this.moduleMap.set(tag, void 0);
99
140
  debug(`instantiate module "${String(tag)}"`);
100
141
  if (paramtypes) {
101
142
  const paramtypesInstances = [];
102
143
  for (const i in paramtypes) {
103
- const { module: sub, tag: subTag } = await buildDepModule(paramtypes[i]);
144
+ const { module: sub, tag: subTag } = await this.buildDepModule(paramtypes[i]);
104
145
  paramtypesInstances[i] = sub;
105
- if (!dependenceGraph.has(subTag)) dependenceGraph.set(subTag, /* @__PURE__ */ new Set());
106
- dependenceGraph.get(subTag).add(tag);
146
+ if (!this.dependenceGraph.has(subTag)) this.dependenceGraph.set(subTag, /* @__PURE__ */ new Set());
147
+ this.dependenceGraph.get(subTag).add(tag);
107
148
  }
108
- module = parseModule(new Model(...paramtypesInstances));
149
+ module = this.parseModule(new Model(...paramtypesInstances));
109
150
  } else {
110
- module = parseModule(new Model());
151
+ module = this.parseModule(new Model());
111
152
  }
112
- meta.push(...getMetaFromInstance(module, tag, Model.name).map(parseMeta).filter((item) => !!item));
153
+ this.meta.push(...getMetaFromInstance(module, tag, Model.name).map(this.parseMeta).filter((item) => !!item));
113
154
  debug(`init module "${String(tag)}"`);
114
155
  if (!IS_ONLY_GENERATE) await invokeInit(module);
115
156
  debug(`add module "${String(tag)}"`);
116
- moduleMap.set(tag, module);
117
- modelMap.set(module, Model);
157
+ this.moduleMap.set(tag, module);
158
+ this.modelMap.set(module, Model);
118
159
  return {
119
160
  module,
120
161
  tag
121
162
  };
122
163
  }
123
- __name(buildDepModule, "buildDepModule");
124
- for (const model of models) await buildDepModule(model);
125
- async function generateCode() {
126
- if (generators && IS_HMR) {
127
- return Promise.all(generators.map((generator) => {
128
- debug(`generate "${generator.name}" code to ${generator.path}`);
129
- return generator.output(meta);
130
- }));
131
- }
164
+ has(modelOrTag) {
165
+ return this.moduleMap.has(typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag);
132
166
  }
133
- __name(generateCode, "generateCode");
134
- generateCode().then(() => {
135
- if (IS_ONLY_GENERATE) process.exit(4);
136
- });
137
- if (IS_HMR) {
138
- if (!globalThis.__PS_HMR__) globalThis.__PS_HMR__ = [];
139
- globalThis.__PS_HMR__?.push(async (files) => {
140
- debug("reload files ");
141
- for (const file of files) {
142
- const models2 = await import(file);
143
- for (const i in models2) {
144
- if (isPhecda(models2[i])) await add(models2[i]);
145
- }
146
- }
147
- generateCode();
148
- });
167
+ get(modelOrTag) {
168
+ const tag = typeof modelOrTag === "function" ? getTag(modelOrTag) : modelOrTag;
169
+ if (!this.has(tag)) throw new Error(`module "${tag.toString()}" doesn't exist`);
170
+ return this.moduleMap.get(tag);
149
171
  }
150
- return {
151
- moduleMap,
152
- modelMap,
153
- meta,
154
- add,
155
- del,
156
- destroy
157
- };
172
+ };
173
+ async function Factory(models, opts = {}) {
174
+ const phecda = new ServerPhecda(opts);
175
+ await phecda.start(models);
176
+ return phecda;
158
177
  }
159
- __name(createPhecda, "createPhecda");
160
- var Factory = createPhecda;
178
+ __name(Factory, "Factory");
179
+ function useS(nsOrModel, namespace) {
180
+ if (!nsOrModel) {
181
+ namespace = "default";
182
+ } else {
183
+ if (typeof nsOrModel === "string") namespace = nsOrModel;
184
+ else if (!namespace) namespace = "default";
185
+ }
186
+ if (!phecdaNamespace.has(namespace)) throw new Error(`namespace "${namespace}" doesn't exist`);
187
+ const serverPhecda = phecdaNamespace.get(namespace);
188
+ if (nsOrModel && typeof nsOrModel !== "string") return serverPhecda.get(nsOrModel);
189
+ else return serverPhecda;
190
+ }
191
+ __name(useS, "useS");
161
192
  function getMetaFromInstance(instance, tag, name) {
162
193
  const propertyKeys = getMetaKey(instance).filter((item) => typeof item === "string");
163
194
  const baseMeta = getMergedMeta(instance, void 0);
@@ -191,9 +222,8 @@ function getMetaFromInstance(instance, tag, name) {
191
222
  ...meta.define
192
223
  };
193
224
  for (const item of [
194
- "plugins",
195
- "guards",
196
- "interceptors"
225
+ "addons",
226
+ "guards"
197
227
  ]) {
198
228
  const set = new Set(baseMeta[item]);
199
229
  if (meta[item]) {
@@ -223,15 +253,13 @@ function getParamTypes(Model, key) {
223
253
  return Reflect.getMetadata("design:paramtypes", Model, key);
224
254
  }
225
255
  __name(getParamTypes, "getParamTypes");
226
- function isObject(o) {
227
- return Object.prototype.toString.call(o) === "[object Object]";
228
- }
229
- __name(isObject, "isObject");
230
256
 
231
257
  export {
232
258
  Meta,
233
259
  emitter,
234
- createPhecda,
260
+ defaultServerInject,
261
+ phecdaNamespace,
262
+ ServerPhecda,
235
263
  Factory,
236
- isObject
264
+ useS
237
265
  };