env-plugin 0.6.1 → 0.7.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 (41) hide show
  1. package/dist/Container.js +2 -1
  2. package/dist/PostProxyServer.js +4 -6
  3. package/dist/client/assets/element-plus-BN_JcO6B.js +19 -0
  4. package/dist/client/assets/element-plus-SrDBYmqw.css +1 -0
  5. package/dist/client/assets/index-CcOgzo-G.js +1 -0
  6. package/dist/client/assets/index-DcxBCPsx.css +1 -0
  7. package/dist/client/index.html +4 -4
  8. package/dist/controllers/DevServerController.js +1 -0
  9. package/dist/controllers/EnvController.js +1 -0
  10. package/dist/controllers/ImportExportController.js +1 -0
  11. package/dist/controllers/PasswordController.js +1 -0
  12. package/dist/controllers/RequestLogController.js +1 -0
  13. package/dist/controllers/RouteRuleController.js +1 -0
  14. package/dist/controllers/SystemSettingController.js +2 -2
  15. package/dist/index.js +8 -11
  16. package/dist/models/EnvModel.js +31 -3
  17. package/dist/repositories/DevServerRepo.js +14 -67
  18. package/dist/repositories/EnvRepo.js +26 -94
  19. package/dist/repositories/PasswordRepo.js +40 -104
  20. package/dist/repositories/RouteRuleRepo.js +22 -77
  21. package/dist/repositories/SystemSettingRepo.js +12 -33
  22. package/dist/repositories/database.js +11 -96
  23. package/dist/repositories/migrate.js +198 -0
  24. package/dist/repositories/store.js +118 -0
  25. package/dist/routes/index.js +1 -1
  26. package/dist/service/DevServerService.js +4 -2
  27. package/dist/service/EnvService.js +9 -7
  28. package/dist/service/ImportExportService.js +9 -5
  29. package/dist/service/PasswordService.js +3 -2
  30. package/dist/service/PreProxyServer.js +68 -39
  31. package/dist/service/ProxyAutoStarterService.js +3 -0
  32. package/dist/service/RequestLogService.js +13 -5
  33. package/dist/service/RouteRuleService.js +4 -2
  34. package/dist/service/SystemSettingService.js +17 -17
  35. package/dist/utils/ResolveConfig.js +4 -2
  36. package/dist/utils/errors.js +2 -0
  37. package/package.json +26 -32
  38. package/dist/client/assets/element-plus-aWS7wRSd.js.gz +0 -0
  39. package/dist/client/assets/index-CBMR4n-V.js.gz +0 -0
  40. package/dist/client/assets/index-a3AJbcK4.css.gz +0 -0
  41. package/dist/client/assets/vue-uvJWWccw.js.gz +0 -0
@@ -8,6 +8,7 @@ import { envLogger } from "../utils/logger.js";
8
8
  * - 统一错误处理
9
9
  */
10
10
  class EnvController {
11
+ envService;
11
12
  /**
12
13
  * 构造函数 - 注入环境服务实例
13
14
  * @param private readonly envService - 环境服务实例,处理核心业务逻辑
@@ -4,6 +4,7 @@ import { envLogger } from "../utils/logger.js";
4
4
  * 负责处理环境配置导入和导出的 HTTP 请求
5
5
  */
6
6
  class ImportExportController {
7
+ service;
7
8
  constructor(service) {
8
9
  this.service = service;
9
10
  }
@@ -8,6 +8,7 @@ import { envLogger } from "../utils/logger.js";
8
8
  * - 统一错误处理
9
9
  */
10
10
  class PasswordController {
11
+ passwordService;
11
12
  /**
12
13
  * 构造函数 - 注入密码服务实例
13
14
  * @param private readonly passwordService - 密码服务实例,处理核心业务逻辑
@@ -3,6 +3,7 @@
3
3
  * 提供请求日志的历史查询接口
4
4
  */
5
5
  class RequestLogController {
6
+ logService;
6
7
  constructor(logService) {
7
8
  this.logService = logService;
8
9
  }
@@ -8,6 +8,7 @@ import { envLogger } from "../utils/logger.js";
8
8
  * - 统一错误处理
9
9
  */
10
10
  class RouteRuleController {
11
+ routeRuleService;
11
12
  /**
12
13
  * 构造函数 - 注入路由规则服务实例
13
14
  * @param private readonly routeRuleService - 路由规则服务实例,处理核心业务逻辑
@@ -3,6 +3,7 @@
3
3
  * 提供系统设置的查询与更新接口
4
4
  */
5
5
  class SystemSettingController {
6
+ service;
6
7
  constructor(service) {
7
8
  this.service = service;
8
9
  }
@@ -28,8 +29,7 @@ class SystemSettingController {
28
29
  handleUpdate(req, res, next) {
29
30
  try {
30
31
  const { injectEnabled, logEnabled } = req.dto;
31
- this.service.setInjectEnabled(injectEnabled);
32
- this.service.setLogEnabled(logEnabled);
32
+ this.service.setSettings(injectEnabled, logEnabled);
33
33
  res.success({
34
34
  injectEnabled: this.service.getInjectEnabled(),
35
35
  logEnabled: this.service.getLogEnabled(),
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import portfinder from "portfinder";
1
+ import net from "node:net";
2
2
  import http from "http";
3
3
  import PostProxyServer from "./PostProxyServer.js";
4
4
  import { getConfig, loadConfig } from "./utils/ResolveConfig.js";
@@ -63,16 +63,13 @@ class EnvManage {
63
63
  * @returns
64
64
  */
65
65
  isPortOccupied(port) {
66
- return portfinder
67
- .getPortPromise({
68
- port: port,
69
- stopPort: port,
70
- })
71
- .then(() => {
72
- return false;
73
- })
74
- .catch(() => {
75
- return true;
66
+ return new Promise((resolve) => {
67
+ const server = net.createServer();
68
+ server.once("error", () => resolve(true));
69
+ server.once("listening", () => {
70
+ server.close(() => resolve(false));
71
+ });
72
+ server.listen(port);
76
73
  });
77
74
  }
78
75
  /**
@@ -1,8 +1,36 @@
1
- import { v4 as uuidv4 } from "uuid";
1
+ import { randomUUID } from "node:crypto";
2
2
  class EnvItemModel {
3
+ /**
4
+ * 主键
5
+ */
6
+ id;
7
+ /**
8
+ * 环境名称
9
+ */
10
+ name;
11
+ /**
12
+ * 环境描述
13
+ */
14
+ description;
15
+ /**
16
+ * 环境绑定端口
17
+ **/
18
+ port;
19
+ /**
20
+ * 环境绑定的开发服务器ID
21
+ */
22
+ devServerId;
23
+ /**
24
+ * 环境IP地址
25
+ */
26
+ apiBaseUrl;
27
+ /**
28
+ * 环境首页
29
+ */
30
+ homePage;
31
+ status = "stopped";
3
32
  constructor(envItem) {
4
- this.status = "stopped";
5
- this.id = uuidv4();
33
+ this.id = randomUUID();
6
34
  this.name = envItem.name ?? "";
7
35
  this.description = envItem.description ?? "";
8
36
  this.port = envItem.port ?? "";
@@ -1,112 +1,59 @@
1
1
  import { getDatabase } from "./database.js";
2
+ import { TABLES, insertRow, mergeUpdate, parseData, parseDataAll } from "./store.js";
2
3
  import { DevServerDeleteSchema, DevServerUpdateSchema, DevServerQuerySchema, DevServerModelSchema, } from "../types/index.js";
3
4
  import { AppError } from "../utils/errors.js";
4
- /**
5
- * 开发服务器仓库类
6
- * 负责开发服务器相关数据的持久化操作,封装数据库交互逻辑
7
- */
5
+ const T = TABLES.devServer;
8
6
  class DevServerRepo {
9
- /**
10
- * 获取开发服务器数据集合
11
- * @returns 开发服务器数据集合对象
12
- */
13
- getCollection() {
14
- return getDatabase().getCollection("devServer");
7
+ db() {
8
+ return getDatabase();
15
9
  }
16
- /**
17
- * 获取所有开发服务器信息
18
- * @returns 所有开发服务器信息的数组
19
- */
20
10
  getAll() {
21
- return this.getCollection().find();
11
+ const rows = this.db().prepare(`SELECT data FROM devServer`).all();
12
+ return parseDataAll(rows);
22
13
  }
23
- /**
24
- * 添加新的开发服务器
25
- * @param devServer - 要添加的开发服务器信息(不含ID)
26
- * @throws {AppError} 当验证失败时抛出错误
27
- */
28
14
  addDevServer(devServer) {
29
- // 验证输入数据
30
15
  const validationResult = DevServerModelSchema.safeParse(devServer);
31
16
  if (!validationResult.success) {
32
17
  throw new AppError(`添加开发服务器失败:参数验证错误 - ${JSON.stringify(validationResult.error.issues)}`);
33
18
  }
34
- this.getCollection().insert(validationResult.data);
19
+ insertRow(this.db(), T, validationResult.data);
35
20
  }
36
- /**
37
- * 根据ID删除开发服务器
38
- * @param devServer - 包含要删除的开发服务器ID的对象
39
- * @throws {AppError} 当验证失败或服务器不存在时抛出错误
40
- */
41
21
  deleteDevServer(devServer) {
42
- // 验证输入数据
43
22
  const validationResult = DevServerDeleteSchema.safeParse(devServer);
44
23
  if (!validationResult.success) {
45
24
  throw new AppError(`删除开发服务器失败:参数验证错误 - ${JSON.stringify(validationResult.error.issues)}`);
46
25
  }
47
- // 检查服务器是否存在
48
26
  const existing = this.findOneById(validationResult.data);
49
27
  if (!existing) {
50
28
  throw new AppError(`删除开发服务器失败:服务器【${validationResult.data.id}】不存在`);
51
29
  }
52
- this.getCollection().findAndRemove(validationResult.data);
30
+ this.db().prepare(`DELETE FROM devServer WHERE id = ?`).run(validationResult.data.id);
53
31
  }
54
- /**
55
- * 根据ID查询开发服务器
56
- * @param devServer - 包含要查询的开发服务器ID的对象
57
- * @returns 匹配的开发服务器信息,若未找到则返回null
58
- * @throws {AppError} 当验证失败时抛出错误
59
- */
60
32
  findOneById(devServer) {
61
- // 验证输入数据
62
33
  const validationResult = DevServerQuerySchema.safeParse(devServer);
63
34
  if (!validationResult.success) {
64
35
  throw new AppError(`查询开发服务器失败:参数验证错误 - ${JSON.stringify(validationResult.error.issues)}`);
65
36
  }
66
- return this.getCollection().findOne(validationResult.data);
37
+ const row = this.db().prepare(`SELECT data FROM devServer WHERE id = ? LIMIT 1`).get(validationResult.data.id);
38
+ return parseData(row);
67
39
  }
68
- /**
69
- * 根据URL查询开发服务器
70
- * @param url - 开发服务器URL
71
- * @returns 匹配的开发服务器信息,若未找到则返回null
72
- */
73
40
  findOneByUrl(url) {
74
- return this.getCollection().findOne({ devServerUrl: url });
41
+ const row = this.db().prepare(`SELECT data FROM devServer WHERE devServerUrl = ? LIMIT 1`).get(url);
42
+ return parseData(row);
75
43
  }
76
- /**
77
- * 更新开发服务器信息
78
- * @param devServer - 包含要更新的开发服务器ID及字段的对象
79
- * @throws {AppError} 当验证失败或服务器不存在时抛出错误
80
- */
81
44
  update(devServer) {
82
- // 验证输入数据
83
45
  const validationResult = DevServerUpdateSchema.safeParse(devServer);
84
46
  if (!validationResult.success) {
85
47
  throw new AppError(`更新开发服务器失败:参数验证错误 - ${JSON.stringify(validationResult.error.issues)}`);
86
48
  }
87
- // 检查服务器是否存在
88
49
  const existing = this.findOneById({ id: validationResult.data.id });
89
50
  if (!existing) {
90
51
  throw new AppError(`更新开发服务器失败:服务器【${validationResult.data.id}】不存在`);
91
52
  }
92
- this.getCollection().findAndUpdate({ id: validationResult.data.id }, (server) => {
93
- // 此前已通过 findOneById 确认存在性,回调必定有值
94
- Object.assign(server, validationResult.data);
95
- return server;
96
- });
53
+ mergeUpdate(this.db(), T, validationResult.data.id, validationResult.data);
97
54
  }
98
- /**
99
- * 更新开发服务器排序顺序
100
- * @param id - 开发服务器ID
101
- * @param sortOrder - 排序顺序
102
- */
103
55
  updateSortOrder(id, sortOrder) {
104
- this.getCollection().findAndUpdate({ id }, (server) => {
105
- if (server) {
106
- server.sortOrder = sortOrder;
107
- }
108
- return server;
109
- });
56
+ mergeUpdate(this.db(), T, id, { sortOrder });
110
57
  }
111
58
  }
112
59
  export { DevServerRepo };
@@ -1,128 +1,60 @@
1
1
  import { AppError } from "../utils/errors.js";
2
2
  import { getDatabase } from "./database.js";
3
- import { EnvDeleteSchema, } from "../types/index.js";
4
- /**
5
- * 环境仓库类
6
- * 负责环境相关数据的持久化操作,封装了数据库交互逻辑
7
- */
3
+ import { TABLES, insertRow, mergeUpdate, buildWhere, parseData, parseDataAll, toSqlValue, } from "./store.js";
4
+ import { EnvDeleteSchema } from "../types/index.js";
5
+ const T = TABLES.envms;
8
6
  class EnvRepo {
9
- /**
10
- * 获取环境数据集合
11
- * @returns 环境数据集合对象
12
- */
13
- getCollection() {
14
- return getDatabase().getCollection("envms");
7
+ db() {
8
+ return getDatabase();
15
9
  }
16
- /**
17
- * 构造函数
18
- */
19
- constructor() { }
20
- /**
21
- * 获取所有环境信息
22
- * @returns 所有环境信息的数组(按sortOrder排序)
23
- */
24
10
  getAll() {
25
- return this.getCollection().find().sort((a, b) => {
26
- const orderA = a.sortOrder ?? 0;
27
- const orderB = b.sortOrder ?? 0;
28
- return orderA - orderB;
29
- });
11
+ const rows = this.db()
12
+ .prepare(`SELECT data FROM envms ORDER BY COALESCE(sortOrder, 0) ASC`)
13
+ .all();
14
+ return parseDataAll(rows);
30
15
  }
31
- /**
32
- * 添加新环境
33
- * @param env - 要添加的环境完整信息对象
34
- */
35
16
  addEnv(env) {
36
- this.getCollection().insert(env);
17
+ insertRow(this.db(), T, env);
37
18
  }
38
- /**
39
- * 删除指定环境
40
- * @param env - 包含要删除环境ID的对象
41
- * @throws {Error} 当验证失败或删除操作出现问题时抛出错误
42
- */
43
19
  deleteEnv(env) {
44
20
  const target = EnvDeleteSchema.safeParse(env);
45
21
  if (!target.success) {
46
22
  throw new AppError(`删除环境验证失败: ${JSON.stringify(target.error)}`);
47
23
  }
48
- this.getCollection().findAndRemove(target.data);
24
+ this.db().prepare(`DELETE FROM envms WHERE id = ?`).run(target.data.id);
49
25
  }
50
- /**
51
- * 根据ID查询环境信息
52
- * @param env - 包含要查询环境ID的对象
53
- * @returns 匹配的环境信息对象,若未找到则返回null
54
- * @throws {Error} 当验证失败时抛出错误
55
- */
56
26
  findOne(env) {
57
- return this.getCollection().findOne(env);
27
+ const { where, params } = buildWhere(env, T.columns);
28
+ const row = this.db().prepare(`SELECT data FROM envms ${where} LIMIT 1`).get(...params);
29
+ return parseData(row);
58
30
  }
59
- /**
60
- * 根据ID查询环境信息
61
- * @param id - 包含要查询环境ID
62
- * @returns 匹配的环境信息对象,若未找到则返回null
63
- * @throws {Error} 当验证失败时抛出错误
64
- */
65
31
  findOneById(id) {
66
- return this.findOne({
67
- id,
68
- });
32
+ return this.findOne({ id });
69
33
  }
70
- /**
71
- * 根据devServerID查询数据
72
- * @param devServerId
73
- * @returns
74
- */
75
34
  findEnvsByDevServerId(devServerId) {
76
- return this.getCollection().find({
77
- devServerId,
78
- });
35
+ const rows = this.db().prepare(`SELECT data FROM envms WHERE devServerId = ?`).all(devServerId);
36
+ return parseDataAll(rows);
79
37
  }
80
- /**
81
- * 根据状态查询环境
82
- * @param status - 环境状态(如'running'、'stopped'等)
83
- * @returns 符合条件的环境数组
84
- */
85
38
  findAllByStatus(status) {
86
- return this.getCollection().find({ status });
39
+ const rows = this.db().prepare(`SELECT data FROM envms WHERE status = ?`).all(status);
40
+ return parseDataAll(rows);
87
41
  }
88
- /**
89
- * 根据 apiBaseUrl 环境信息查找单个环境 新增前用于判断是否重复
90
- * @param env 环境信息
91
- * @returns 匹配的环境信息或 undefined
92
- */
93
42
  findOneByApiBaseUrl(env) {
94
- return this.getCollection().findOne({
95
- apiBaseUrl: env.apiBaseUrl,
96
- });
43
+ const row = this.db().prepare(`SELECT data FROM envms WHERE apiBaseUrl = ? LIMIT 1`).get(env.apiBaseUrl);
44
+ return parseData(row);
97
45
  }
98
- /**
99
- * 根据状态和端口查询
100
- * @param env
101
- * @returns
102
- */
103
46
  findOneByPortAndStatus(env) {
104
- return this.getCollection().findOne({
105
- port: env.port,
106
- status: env.status,
107
- });
47
+ const row = this.db()
48
+ .prepare(`SELECT data FROM envms WHERE port = ? AND status = ? LIMIT 1`)
49
+ .get(env.port, toSqlValue(env.status));
50
+ return parseData(row);
108
51
  }
109
- /**
110
- * 更新环境信息
111
- * @param envmItem - 包含要更新的环境ID及字段的对象
112
- * @throws {AppError} 当未找到对应环境时抛出异常
113
- * @throws {Error} 当更新操作出现问题时抛出错误
114
- */
115
52
  update(envmItem) {
116
- // 先检查存在性:findAndUpdate 回调仅在匹配文档时执行,
117
- // 回调内 null 检查是死代码,必须在调用前校验
118
53
  const existing = this.findOneById(envmItem.id);
119
54
  if (!existing) {
120
55
  throw new AppError(`未找到对应的环境【${envmItem.id}】`);
121
56
  }
122
- this.getCollection().findAndUpdate({ id: envmItem.id }, (env) => {
123
- Object.assign(env, envmItem);
124
- return env;
125
- });
57
+ mergeUpdate(this.db(), T, envmItem.id, envmItem);
126
58
  }
127
59
  }
128
60
  export { EnvRepo };
@@ -1,144 +1,80 @@
1
1
  import { getDatabase } from "./database.js";
2
- import { PasswordDeleteSchema, } from "../types/index.js";
3
- /**
4
- * 密码仓库类
5
- * 负责密码相关数据的持久化操作,封装了数据库交互逻辑
6
- */
2
+ import { TABLES, insertRow, mergeUpdate, parseData, parseDataAll, serialize } from "./store.js";
3
+ import { PasswordDeleteSchema } from "../types/index.js";
4
+ const T = TABLES.passwords;
7
5
  class PasswordRepo {
8
- /**
9
- * 获取密码数据集合
10
- * @returns 密码数据集合对象
11
- */
12
- getCollection() {
13
- return getDatabase().getCollection("passwords");
6
+ db() {
7
+ return getDatabase();
14
8
  }
15
- /**
16
- * 构造函数
17
- */
18
- constructor() { }
19
- /**
20
- * 初始化集合索引
21
- */
22
9
  initCollection() {
23
- const collection = this.getCollection();
24
- collection.ensureIndex("id");
25
- collection.ensureIndex("envId");
26
- collection.ensureIndex("isDefault");
10
+ // DDL 已建索引,保留为 no-op
27
11
  }
28
- /**
29
- * 根据环境ID获取所有密码
30
- * @param envId - 环境ID
31
- * @returns 该环境的所有密码数组
32
- */
33
12
  getByEnvId(envId) {
34
- return this.getCollection().find({ envId });
13
+ const rows = this.db().prepare(`SELECT data FROM passwords WHERE envId = ?`).all(envId);
14
+ return parseDataAll(rows);
35
15
  }
36
- /**
37
- * 统计指定环境的密码数量
38
- * @param envId - 环境ID
39
- * @returns 密码数量
40
- */
41
16
  countByEnvId(envId) {
42
- return this.getCollection().count({ envId });
17
+ const row = this.db().prepare(`SELECT COUNT(*) AS c FROM passwords WHERE envId = ?`).get(envId);
18
+ return row.c;
43
19
  }
44
- /**
45
- * 根据ID查询单个密码
46
- * @param id - 密码ID
47
- * @returns 匹配的密码,若未找到则返回null
48
- */
49
20
  findOneById(id) {
50
- return this.getCollection().findOne({ id });
21
+ const row = this.db().prepare(`SELECT data FROM passwords WHERE id = ? LIMIT 1`).get(id);
22
+ return parseData(row);
51
23
  }
52
- /**
53
- * 添加新密码
54
- * @param password - 要添加的密码完整信息对象
55
- */
56
24
  create(password) {
57
- this.getCollection().insert(password);
25
+ insertRow(this.db(), T, password);
58
26
  }
59
- /**
60
- * 删除指定密码
61
- * @param password - 包含要删除密码ID的对象
62
- */
63
27
  delete(password) {
64
28
  const target = PasswordDeleteSchema.safeParse(password);
65
29
  if (!target.success) {
66
30
  throw new Error(`删除密码验证失败: ${JSON.stringify(target.error)}`);
67
31
  }
68
- this.getCollection().findAndRemove(target.data);
32
+ this.db().prepare(`DELETE FROM passwords WHERE id = ?`).run(target.data.id);
69
33
  }
70
- /**
71
- * 删除指定环境的所有密码
72
- * @param envId - 环境ID
73
- */
74
34
  deleteByEnvId(envId) {
75
- this.getCollection().findAndRemove({ envId });
35
+ this.db().prepare(`DELETE FROM passwords WHERE envId = ?`).run(envId);
76
36
  }
77
- /**
78
- * 更新密码信息
79
- * @param passwordItem - 包含要更新的密码ID及字段的对象
80
- */
81
37
  update(passwordItem) {
82
- // 先检查存在性:findAndUpdate 回调仅在匹配文档时执行
83
38
  const existing = this.findOneById(passwordItem.id);
84
39
  if (!existing) {
85
40
  throw new Error(`未找到对应的密码【${passwordItem.id}】`);
86
41
  }
87
- this.getCollection().findAndUpdate({ id: passwordItem.id }, (pwd) => {
88
- Object.assign(pwd, passwordItem);
89
- return pwd;
90
- });
42
+ mergeUpdate(this.db(), T, passwordItem.id, passwordItem);
91
43
  }
92
- /**
93
- * 检查指定环境是否已存在相同名称的密码
94
- * @param envId - 环境ID
95
- * @param name - 密码名称
96
- * @returns 是否存在重复
97
- */
98
44
  existsByEnvIdAndName(envId, name) {
99
- const existing = this.getCollection().findOne({
100
- envId,
101
- name,
102
- });
103
- return existing !== null;
45
+ const row = this.db()
46
+ .prepare(`SELECT id FROM passwords WHERE envId = ? AND name = ? LIMIT 1`)
47
+ .get(envId, name);
48
+ return row !== undefined;
104
49
  }
105
- /**
106
- * 获取指定环境的默认密码
107
- * @param envId - 环境ID
108
- * @returns 默认密码,若未找到则返回null
109
- */
110
50
  findDefaultByEnvId(envId) {
111
- return this.getCollection().findOne({ envId, isDefault: true });
51
+ const row = this.db()
52
+ .prepare(`SELECT data FROM passwords WHERE envId = ? AND isDefault = 1 LIMIT 1`)
53
+ .get(envId);
54
+ return parseData(row);
112
55
  }
113
- /**
114
- * 设置指定环境的默认密码(先将其他密码的isDefault设为false)
115
- * @param envId - 环境ID
116
- * @param excludeId - 排除的密码ID(更新时使用)
117
- */
118
56
  clearDefaultByEnvId(envId, excludeId) {
119
- const query = { envId, isDefault: true };
120
- if (excludeId) {
121
- query.id = { $ne: excludeId };
57
+ const rows = this.db()
58
+ .prepare(`SELECT data FROM passwords WHERE envId = ? AND isDefault = 1`)
59
+ .all(envId);
60
+ for (const row of rows) {
61
+ const pwd = JSON.parse(row.data);
62
+ if (excludeId && pwd.id === excludeId)
63
+ continue;
64
+ pwd.isDefault = false;
65
+ const s = serialize(T, pwd);
66
+ this.db().prepare(`UPDATE passwords SET isDefault = ?, data = ? WHERE id = ?`).run(s.isDefault, s.data, pwd.id);
122
67
  }
123
- this.getCollection().findAndUpdate(query, (pwd) => {
124
- if (pwd) {
125
- pwd.isDefault = false;
126
- }
127
- return pwd;
128
- });
129
68
  }
130
- /**
131
- * 检查指定环境是否已存在默认密码
132
- * @param envId - 环境ID
133
- * @param excludeId - 排除的密码ID(更新时使用)
134
- * @returns 是否存在默认密码
135
- */
136
69
  hasDefaultPassword(envId, excludeId) {
137
- const query = { envId, isDefault: true };
70
+ let sql = `SELECT COUNT(*) AS c FROM passwords WHERE envId = ? AND isDefault = 1`;
71
+ const params = [envId];
138
72
  if (excludeId) {
139
- query.id = { $ne: excludeId };
73
+ sql += ` AND id != ?`;
74
+ params.push(excludeId);
140
75
  }
141
- return this.getCollection().count(query) > 0;
76
+ const row = this.db().prepare(sql).get(...params);
77
+ return row.c > 0;
142
78
  }
143
79
  }
144
80
  export { PasswordRepo };