@mindbase/express-common 1.0.1 → 1.0.2

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.
package/dist/index.d.mts CHANGED
@@ -173,14 +173,8 @@ interface DocDatabaseConfig {
173
173
  }
174
174
 
175
175
  interface MindBaseAppOptions {
176
- /**
177
- * 启动模式:
178
- * - normal: 正常启动服务
179
- * - sync: 同步数据库 Schema(仅收集)
180
- * - migrate: 执行数据库迁移
181
- * @default "normal"
182
- */
183
- mode?: "normal" | "sync" | "migrate";
176
+ /** 服务监听主机名 @default 127.0.0.1 */
177
+ host?: string;
184
178
  /** 服务监听端口 @default 3000 */
185
179
  port?: number;
186
180
  /** 是否启用请求日志 @default true */
@@ -366,10 +360,4 @@ declare function initDatabase(options: {
366
360
  state?: AppState;
367
361
  }): any;
368
362
 
369
- /**
370
- * 处理数据库迁移(Schema 同步 + Drizzle Push)
371
- * 这是 migrate 模式的核心逻辑
372
- */
373
- declare function handleSchemaMigrate(): Promise<void>;
374
-
375
- export { type ApiResponse, type AppState, type CronConfig, type DocDatabaseConfig, DocManager, Errors, type FieldConstraints, type FieldValidation, type IpInfo, type LogLevel, type MindBaseApp, type MindBaseAppOptions, type ModuleItem, type RequestSchema, type ResponseSchema, RouteDoc, type RouteDocConfig, type RouteInfo, type ScanResult, type UaInfo, addCron, createApp, createAppError, createModule, docManager, handleSchemaMigrate, initDatabase, initDocManager, logger, precache, prepare, stopAllCron, stopCron, validate };
363
+ export { type ApiResponse, type AppState, type CronConfig, type DocDatabaseConfig, DocManager, Errors, type FieldConstraints, type FieldValidation, type IpInfo, type LogLevel, type MindBaseApp, type MindBaseAppOptions, type ModuleItem, type RequestSchema, type ResponseSchema, RouteDoc, type RouteDocConfig, type RouteInfo, type ScanResult, type UaInfo, addCron, createApp, createAppError, createModule, docManager, initDatabase, initDocManager, logger, precache, prepare, stopAllCron, stopCron, validate };
package/dist/index.mjs CHANGED
@@ -10,7 +10,7 @@ import express from "express";
10
10
  function createState(options = {}) {
11
11
  return {
12
12
  options: {
13
- mode: options.mode || process.env.MIND_BASE_MODE || "normal",
13
+ host: options.host || "127.0.0.1",
14
14
  port: options.port || 3e3,
15
15
  logging: options.logging !== false,
16
16
  staticPath: options.staticPath,
@@ -1902,10 +1902,10 @@ function getRoutes(router2, prefix = "") {
1902
1902
  if (router2 && router2.stack) {
1903
1903
  router2.stack.forEach((layer) => {
1904
1904
  if (layer.route) {
1905
- const path11 = (prefix + layer.route.path).replace(/\/+/g, "/");
1905
+ const path9 = (prefix + layer.route.path).replace(/\/+/g, "/");
1906
1906
  const methods = Object.keys(layer.route.methods).map((m) => m.toUpperCase());
1907
1907
  methods.forEach((method) => {
1908
- routes.push({ path: path11, method });
1908
+ routes.push({ path: path9, method });
1909
1909
  });
1910
1910
  } else if (layer.name === "router" && layer.handle && layer.handle.stack) {
1911
1911
  const newPrefix = (prefix + (layer.regexp.source.replace("^\\/", "").replace("\\/?(?=\\/|$)", "") || "")).replace(/\/+/g, "/");
@@ -1990,7 +1990,7 @@ function setupErrorHandlers(app, logging) {
1990
1990
  res.status(404).json({
1991
1991
  code: 404,
1992
1992
  data: null,
1993
- msg: "\u63A5\u53E3\u4E0D\u5B58\u5728",
1993
+ msg: "\u8D44\u6E90\u4E0D\u5B58\u5728",
1994
1994
  error: "Not Found",
1995
1995
  fields: {}
1996
1996
  });
@@ -2008,8 +2008,8 @@ function setupErrorHandlers(app, logging) {
2008
2008
  if (err instanceof ZodError) {
2009
2009
  const fields = {};
2010
2010
  err.errors.forEach((e) => {
2011
- const path11 = e.path.join(".");
2012
- fields[path11] = e.message;
2011
+ const path9 = e.path.join(".");
2012
+ fields[path9] = e.message;
2013
2013
  });
2014
2014
  return res.status(400).json({
2015
2015
  code: 400,
@@ -2040,163 +2040,6 @@ function setupErrorHandlers(app, logging) {
2040
2040
  });
2041
2041
  }
2042
2042
 
2043
- // utils/SchemaSync.ts
2044
- import fs5 from "fs";
2045
- import path6 from "path";
2046
- async function handleSchemaSync() {
2047
- Logger_default.info("\u{1F680} \u68C0\u6D4B\u5230\u540C\u6B65\u6A21\u5F0F\uFF1A\u6B63\u5728\u6536\u96C6 Schema \u8DEF\u5F84...");
2048
- try {
2049
- try {
2050
- setBaseDir(process.cwd());
2051
- const scannedResults = await scan(true);
2052
- Logger_default.startup(`\u626B\u63CF\u5B8C\u6210\uFF0C\u53D1\u73B0 ${scannedResults.length} \u4E2A\u6587\u4EF6`);
2053
- const schemas = scannedResults.filter((item) => item.type === "schema").map((item) => item.filePath);
2054
- Logger_default.startup(`\u63D0\u53D6\u5230 ${schemas.length} \u4E2A Schema \u6587\u4EF6\u8DEF\u5F84`);
2055
- const outputPath = path6.join(process.cwd(), ".drizzle-schemas.json");
2056
- try {
2057
- fs5.writeFileSync(outputPath, JSON.stringify(schemas, null, 2));
2058
- Logger_default.startup(`\u5199\u5165\u6E05\u5355\u6587\u4EF6\uFF1A${outputPath}`);
2059
- } catch (e) {
2060
- throw new Error(`\u5199\u5165\u6E05\u5355\u6587\u4EF6\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}`);
2061
- }
2062
- Logger_default.startup(`\u2705 \u5DF2\u53D1\u73B0 ${schemas.length} \u4E2A Schema\uFF0C\u6E05\u5355\u5DF2\u66F4\u65B0\u81F3 .drizzle-schemas.json`);
2063
- } catch (e) {
2064
- throw new Error(`Schema \u626B\u63CF\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}`);
2065
- }
2066
- process.exit(0);
2067
- } catch (error) {
2068
- Logger_default.error("Schema \u540C\u6B65\u5931\u8D25\uFF1A", error);
2069
- Logger_default.error(`\u9519\u8BEF\uFF1A${error instanceof Error ? error.message : String(error)}`);
2070
- process.exit(1);
2071
- }
2072
- }
2073
-
2074
- // utils/SchemaMigrate.ts
2075
- import fs6 from "fs";
2076
- import path7 from "path";
2077
-
2078
- // utils/DatabaseMigration.ts
2079
- async function executeDatabaseMigration(db, schemas) {
2080
- try {
2081
- if (!db) {
2082
- Logger_default.error("\u6570\u636E\u5E93\u5B9E\u4F8B\u4E0D\u80FD\u4E3A\u7A7A");
2083
- return false;
2084
- }
2085
- if (!schemas) {
2086
- Logger_default.error("\u6570\u636E\u5E93 schema \u5B9A\u4E49\u4E0D\u80FD\u4E3A\u7A7A");
2087
- return false;
2088
- }
2089
- Logger_default.startup("\u6570\u636E\u5E93", "\u5F00\u59CB\u6267\u884C\u6570\u636E\u5E93\u8868\u7ED3\u6784\u540C\u6B65...");
2090
- try {
2091
- const { spawn } = await import("child_process");
2092
- Logger_default.startup("\u6570\u636E\u5E93", "\u6B63\u5728\u8C03\u7528 drizzle-kit push \u540C\u6B65\u8868\u7ED3\u6784...");
2093
- const result = await new Promise((resolve3) => {
2094
- let output = "";
2095
- let errorOutput = "";
2096
- const push = spawn("npx", ["drizzle-kit", "push"], {
2097
- shell: true
2098
- });
2099
- push.stdout?.on("data", (data) => {
2100
- const text = data.toString();
2101
- output += text;
2102
- });
2103
- push.stderr?.on("data", (data) => {
2104
- const text = data.toString();
2105
- errorOutput += text;
2106
- });
2107
- push.on("close", (code) => {
2108
- if (code === 0) {
2109
- Logger_default.startup("\u6570\u636E\u5E93", "\u2705 drizzle-kit push \u6267\u884C\u6210\u529F");
2110
- resolve3(true);
2111
- } else {
2112
- const errorMsg = errorOutput || output;
2113
- const benignErrors = [
2114
- "already exists",
2115
- "\u7D22\u5F15.*\u5DF2\u5B58\u5728",
2116
- "index.*already exists",
2117
- "table.*already exists"
2118
- ];
2119
- const isBenign = benignErrors.some(
2120
- (pattern) => new RegExp(pattern, "i").test(errorMsg)
2121
- );
2122
- if (isBenign) {
2123
- Logger_default.startup("\u6570\u636E\u5E93", "\u2705 \u6570\u636E\u5E93\u7ED3\u6784\u5DF2\u540C\u6B65\uFF08\u7D22\u5F15/\u8868\u5DF2\u5B58\u5728\uFF09");
2124
- resolve3(true);
2125
- } else {
2126
- Logger_default.error(`\u274C drizzle-kit push \u6267\u884C\u5931\u8D25\uFF0C\u9000\u51FA\u7801: ${code}`);
2127
- if (errorMsg) {
2128
- Logger_default.error(errorMsg);
2129
- }
2130
- resolve3(false);
2131
- }
2132
- }
2133
- });
2134
- push.on("error", (error) => {
2135
- Logger_default.error("\u6267\u884C drizzle-kit push \u65F6\u51FA\u9519:", error);
2136
- resolve3(false);
2137
- });
2138
- });
2139
- return result;
2140
- } catch (pushError) {
2141
- Logger_default.error("\u8C03\u7528 drizzle-kit push \u5931\u8D25:", pushError);
2142
- Logger_default.info("\u{1F4A1} \u63D0\u793A\uFF1A\u8BF7\u624B\u52A8\u8FD0\u884C 'npx drizzle-kit push' \u6765\u540C\u6B65\u8868\u7ED3\u6784");
2143
- return false;
2144
- }
2145
- } catch (error) {
2146
- Logger_default.error("\u6570\u636E\u5E93\u8FC1\u79FB\u6267\u884C\u5931\u8D25:", error);
2147
- return false;
2148
- }
2149
- }
2150
-
2151
- // utils/SchemaMigrate.ts
2152
- async function handleSchemaMigrate() {
2153
- Logger_default.startup("\u8FC1\u79FB", "\u{1F680} \u5F00\u59CB\u6570\u636E\u5E93\u8FC1\u79FB\u6D41\u7A0B...");
2154
- try {
2155
- try {
2156
- setBaseDir(process.cwd());
2157
- const scannedResults = await scan(true);
2158
- Logger_default.startup("\u8FC1\u79FB", `\u626B\u63CF\u5B8C\u6210\uFF0C\u53D1\u73B0 ${scannedResults.length} \u4E2A\u6587\u4EF6`);
2159
- const schemaFiles = scannedResults.filter((item) => item.type === "schema").map((item) => item.filePath);
2160
- Logger_default.startup("\u8FC1\u79FB", `\u63D0\u53D6\u5230 ${schemaFiles.length} \u4E2A Schema \u6587\u4EF6\u8DEF\u5F84`);
2161
- const schemaListPath = path7.join(process.cwd(), ".drizzle-schemas.json");
2162
- try {
2163
- fs6.writeFileSync(schemaListPath, JSON.stringify(schemaFiles, null, 2));
2164
- Logger_default.startup("\u8FC1\u79FB", `\u5DF2\u751F\u6210\u6E05\u5355\u6587\u4EF6\uFF1A${schemaListPath}`);
2165
- } catch (e) {
2166
- throw new Error(`\u5199\u5165\u6E05\u5355\u6587\u4EF6\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}`);
2167
- }
2168
- const schemas = {};
2169
- for (const schemaPath of schemaFiles) {
2170
- try {
2171
- const module2 = await import(schemaPath);
2172
- Object.assign(schemas, module2);
2173
- } catch (e) {
2174
- Logger_default.warn(`\u52A0\u8F7D Schema \u6587\u4EF6\u5931\u8D25: ${schemaPath}`, e);
2175
- }
2176
- }
2177
- Logger_default.startup("\u8FC1\u79FB", `\u5DF2\u52A0\u8F7D ${Object.keys(schemas).length} \u4E2A Schema \u5B9A\u4E49`);
2178
- const db = initDatabase({ schemas });
2179
- Logger_default.startup("\u8FC1\u79FB", "\u6570\u636E\u5E93\u8FDE\u63A5\u5DF2\u5EFA\u7ACB");
2180
- Logger_default.startup("\u8FC1\u79FB", "\u6B63\u5728\u6267\u884C\u6570\u636E\u5E93\u7ED3\u6784\u540C\u6B65...");
2181
- const success = await executeDatabaseMigration(db, schemas);
2182
- if (success) {
2183
- Logger_default.startup("\u8FC1\u79FB", "\u2705 \u6570\u636E\u5E93\u8FC1\u79FB\u5B8C\u6210");
2184
- process.exit(0);
2185
- } else {
2186
- Logger_default.error("\u274C \u6570\u636E\u5E93\u8FC1\u79FB\u5931\u8D25");
2187
- Logger_default.info("\u{1F4A1} \u63D0\u793A\uFF1A\u8BF7\u68C0\u67E5\u6570\u636E\u5E93\u8FDE\u63A5\u6216\u624B\u52A8\u8FD0\u884C 'npx drizzle-kit push'");
2188
- process.exit(1);
2189
- }
2190
- } catch (e) {
2191
- throw new Error(`Schema \u626B\u63CF\u6216\u6570\u636E\u5E93\u8FC1\u79FB\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}`);
2192
- }
2193
- } catch (error) {
2194
- Logger_default.error("\u6570\u636E\u5E93\u8FC1\u79FB\u5931\u8D25\uFF1A", error);
2195
- Logger_default.error(`\u9519\u8BEF\uFF1A${error instanceof Error ? error.message : String(error)}`);
2196
- process.exit(1);
2197
- }
2198
- }
2199
-
2200
2043
  // utils/HttpServer.ts
2201
2044
  async function startServer(app, port) {
2202
2045
  if (!app) {
@@ -2349,14 +2192,6 @@ function createApp(options = {}) {
2349
2192
  }
2350
2193
  },
2351
2194
  startup: async () => {
2352
- if (stateInstance.options.mode === "migrate") {
2353
- await handleSchemaMigrate();
2354
- return;
2355
- }
2356
- if (stateInstance.options.mode === "sync") {
2357
- await handleSchemaSync();
2358
- return;
2359
- }
2360
2195
  initExpress(stateInstance);
2361
2196
  await prepareScanEnvironment();
2362
2197
  const scannedResults = await scanComponents();
@@ -2407,8 +2242,7 @@ function createApp(options = {}) {
2407
2242
  async function startHttpServer() {
2408
2243
  const listenPort = stateInstance.options.port || 3e3;
2409
2244
  await startServer(stateInstance.express, listenPort);
2410
- const serverUrl = `http://localhost:${listenPort}`;
2411
- Logger_default.startup("\u670D\u52A1", `\u76D1\u542C\u7AEF\u53E3: ${listenPort}`);
2245
+ const serverUrl = `http://${stateInstance.options.host}:${listenPort}`;
2412
2246
  Logger_default.startup("\u670D\u52A1", `\u8BBF\u95EE\u5730\u5740: ${serverUrl}`);
2413
2247
  }
2414
2248
  function registerDocRoutes() {
@@ -2428,7 +2262,7 @@ function createApp(options = {}) {
2428
2262
  }
2429
2263
 
2430
2264
  // commands/prepare.ts
2431
- import path8 from "path";
2265
+ import path6 from "path";
2432
2266
  async function prepare(cwd = process.cwd()) {
2433
2267
  console.log("\u{1F680} MindBase \u73AF\u5883\u51C6\u5907\u5DE5\u5177");
2434
2268
  console.log(`\u5DE5\u4F5C\u76EE\u5F55: ${cwd}
@@ -2462,7 +2296,7 @@ async function prepare(cwd = process.cwd()) {
2462
2296
  });
2463
2297
  console.log(` \u6570\u636E\u5E93\u5DF2\u521D\u59CB\u5316
2464
2298
  `);
2465
- const cacheFile = path8.join(cwd, "node_modules/.cache/mindbase/startup-cache.json");
2299
+ const cacheFile = path6.join(cwd, "node_modules/.cache/mindbase/startup-cache.json");
2466
2300
  console.log("\u2705 \u73AF\u5883\u51C6\u5907\u5B8C\u6210\uFF01");
2467
2301
  console.log(`
2468
2302
  \u{1F4E6} \u7F13\u5B58\u6587\u4EF6: ${cacheFile}`);
@@ -2517,47 +2351,47 @@ function generateId() {
2517
2351
  }
2518
2352
 
2519
2353
  // core/module/FindPackageRoot.ts
2520
- import path9 from "path";
2521
- import fs7 from "fs";
2354
+ import path7 from "path";
2355
+ import fs5 from "fs";
2522
2356
  function findPackageRoot(startPath) {
2523
2357
  if (!startPath || typeof startPath !== "string") {
2524
2358
  throw new Error("\u65E0\u6548\u7684\u8D77\u59CB\u8DEF\u5F84\uFF1A\u8DEF\u5F84\u5FC5\u987B\u662F\u975E\u7A7A\u5B57\u7B26\u4E32");
2525
2359
  }
2526
2360
  let currentPath;
2527
2361
  try {
2528
- currentPath = path9.isAbsolute(startPath) ? startPath : path9.resolve(startPath);
2362
+ currentPath = path7.isAbsolute(startPath) ? startPath : path7.resolve(startPath);
2529
2363
  } catch (e) {
2530
2364
  throw new Error(`\u65E0\u6CD5\u89E3\u6790\u8DEF\u5F84\uFF1A${startPath}`);
2531
2365
  }
2532
2366
  try {
2533
- if (!fs7.existsSync(currentPath)) {
2367
+ if (!fs5.existsSync(currentPath)) {
2534
2368
  throw new Error(`\u8DEF\u5F84\u4E0D\u5B58\u5728\uFF1A${currentPath}`);
2535
2369
  }
2536
- const stat = fs7.statSync(currentPath);
2370
+ const stat = fs5.statSync(currentPath);
2537
2371
  if (stat.isFile()) {
2538
- currentPath = path9.dirname(currentPath);
2372
+ currentPath = path7.dirname(currentPath);
2539
2373
  }
2540
2374
  } catch (e) {
2541
2375
  throw new Error(`\u8DEF\u5F84\u9A8C\u8BC1\u5931\u8D25\uFF1A${e instanceof Error ? e.message : String(e)}`);
2542
2376
  }
2543
- while (currentPath !== path9.parse(currentPath).root) {
2377
+ while (currentPath !== path7.parse(currentPath).root) {
2544
2378
  try {
2545
- const pkgJsonPath = path9.join(currentPath, "package.json");
2546
- if (fs7.existsSync(pkgJsonPath)) {
2547
- const pkgStat = fs7.statSync(pkgJsonPath);
2379
+ const pkgJsonPath = path7.join(currentPath, "package.json");
2380
+ if (fs5.existsSync(pkgJsonPath)) {
2381
+ const pkgStat = fs5.statSync(pkgJsonPath);
2548
2382
  if (pkgStat.isFile()) {
2549
2383
  return currentPath;
2550
2384
  }
2551
2385
  }
2552
2386
  } catch (e) {
2553
2387
  }
2554
- currentPath = path9.dirname(currentPath);
2388
+ currentPath = path7.dirname(currentPath);
2555
2389
  }
2556
- return path9.dirname(startPath);
2390
+ return path7.dirname(startPath);
2557
2391
  }
2558
2392
 
2559
2393
  // core/module/GetModulePath.ts
2560
- import * as path10 from "path";
2394
+ import * as path8 from "path";
2561
2395
  function getModulePath() {
2562
2396
  try {
2563
2397
  const error = new Error();
@@ -2572,7 +2406,7 @@ function getModulePath() {
2572
2406
  if (match && match[1]) {
2573
2407
  const filePath = match[1];
2574
2408
  console.log("\u63D0\u53D6\u5230\u7684\u8DEF\u5F84:", filePath);
2575
- return path10.resolve(filePath);
2409
+ return path8.resolve(filePath);
2576
2410
  }
2577
2411
  }
2578
2412
  }
@@ -2622,7 +2456,6 @@ export {
2622
2456
  createModule,
2623
2457
  dayjs,
2624
2458
  docManager,
2625
- handleSchemaMigrate,
2626
2459
  initDatabase,
2627
2460
  initDocManager,
2628
2461
  logger,