@allbluecn/database 0.9.4 → 0.10.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.
package/desktop-init.ts CHANGED
@@ -18,18 +18,11 @@
18
18
  // 桌面端 SQLite 首次启动初始化:建关系表(schema-sqlite.sql)+ 建 vec0 向量虚拟表
19
19
  // 幂等:表已存在则跳过。仅 DATABASE_PROVIDER=sqlite 时被调用。
20
20
  // better-sqlite3/sqlite-vec 动态 import,不进 postgresql 服务器 bundle。
21
- import { readFileSync } from "node:fs";
21
+ import { accessSync, readFileSync } from "node:fs";
22
22
  import { join } from "node:path";
23
+ import { VEC_TABLES } from "./src/lib/vec-tables.ts";
23
24
 
24
- // 与 apps/web/src/lib/vector-store/sqlite-vec-store.ts 的 ALLOWED_TABLES 保持一致
25
- const VEC_TABLES = [
26
- "design_pages",
27
- "components",
28
- "blocks",
29
- "design_embeddings",
30
- "knowledge_embeddings",
31
- ] as const;
32
-
25
+ let initPromise: Promise<void> | null = null;
33
26
  let initialized = false;
34
27
 
35
28
  /**
@@ -37,10 +30,20 @@ let initialized = false;
37
30
  * - 关系表:sqlite_master 表数为 0 时执行 schema-sqlite.sql(Prisma migrate diff 产物)
38
31
  * - 向量表:sqliteVec.load + 建 vec0 虚拟表(cosine 列级语法)
39
32
  * 幂等:已初始化或表已存在则跳过。
33
+ * 并发安全:module-level promise 缓存,并发调用共享同一次初始化;
34
+ * 失败时清空缓存允许重试(成功后 never retry)。
40
35
  */
41
- export async function initDesktopDb(dbPath: string): Promise<void> {
42
- if (initialized) return;
36
+ export function initDesktopDb(dbPath: string): Promise<void> {
37
+ if (!initPromise) {
38
+ initPromise = doInit(dbPath).catch((err) => {
39
+ initPromise = null;
40
+ throw err;
41
+ });
42
+ }
43
+ return initPromise;
44
+ }
43
45
 
46
+ async function doInit(dbPath: string): Promise<void> {
44
47
  const Database = (await import("better-sqlite3")).default;
45
48
  const sqliteVec = await import("sqlite-vec");
46
49
 
@@ -55,8 +58,29 @@ export async function initDesktopDb(dbPath: string): Promise<void> {
55
58
  if (tableCount === 0) {
56
59
  const sqlPath = locateSchemaSql();
57
60
  const sql = readFileSync(sqlPath, "utf-8");
58
- db.exec(sql);
61
+ // 事务包裹:SQLite DDL 可回滚,建表中途崩溃不再留下「部分表」残库
62
+ // (历史缺陷:tableCount>0 即跳过 schema 执行,部分初始化的库永远无法自愈)
63
+ db.exec("BEGIN");
64
+ try {
65
+ db.exec(sql);
66
+ db.exec("COMMIT");
67
+ } catch (err) {
68
+ try {
69
+ db.exec("ROLLBACK");
70
+ } catch {
71
+ // ROLLBACK 失败(如连接已损坏)时保留原始建表错误
72
+ }
73
+ throw err;
74
+ }
59
75
  console.log("[desktop] 关系表初始化完成");
76
+ } else if (!hasUsersTable(db)) {
77
+ // 历史部分初始化残库:有表但缺关键表 users,显式报错替代下游晦涩的
78
+ // "no such table: users"(schema-sqlite.sql 非幂等,无法在残库上补跑)
79
+ throw new Error(
80
+ `[desktop] 检测到部分初始化的数据库(存在表但缺 users 表,共 ${tableCount} 张)。` +
81
+ "请删除该数据库文件后重启应用重新初始化: " +
82
+ dbPath,
83
+ );
60
84
  }
61
85
 
62
86
  sqliteVec.load(db);
@@ -75,6 +99,15 @@ export async function initDesktopDb(dbPath: string): Promise<void> {
75
99
  }
76
100
  }
77
101
 
102
+ function hasUsersTable(db: import("better-sqlite3").Database): boolean {
103
+ const row = db
104
+ .prepare(
105
+ "SELECT count(*) AS c FROM sqlite_master WHERE type='table' AND name='users'"
106
+ )
107
+ .get() as { c: number };
108
+ return row.c > 0;
109
+ }
110
+
78
111
  function seedLocalGuestUser(db: import("better-sqlite3").Database): void {
79
112
  const existing = db
80
113
  .prepare("SELECT id FROM users WHERE id = ?")
@@ -114,7 +147,8 @@ function locateSchemaSql(): string {
114
147
 
115
148
  for (const p of candidates) {
116
149
  try {
117
- readFileSync(p);
150
+ // accessSync 仅探测存在性,文件内容由调用方单次读取(避免双读)
151
+ accessSync(p);
118
152
  return p;
119
153
  } catch {}
120
154
  }
@@ -737,3 +737,13 @@ export type AiTask = Prisma.AiTaskModel
737
737
  *
738
738
  */
739
739
  export type AiTaskRun = Prisma.AiTaskRunModel
740
+ /**
741
+ * Model KnowledgeEmbedding
742
+ *
743
+ */
744
+ export type KnowledgeEmbedding = Prisma.KnowledgeEmbeddingModel
745
+ /**
746
+ * Model DesignEmbedding
747
+ *
748
+ */
749
+ export type DesignEmbedding = Prisma.DesignEmbeddingModel
@@ -761,3 +761,13 @@ export type AiTask = Prisma.AiTaskModel
761
761
  *
762
762
  */
763
763
  export type AiTaskRun = Prisma.AiTaskRunModel
764
+ /**
765
+ * Model KnowledgeEmbedding
766
+ *
767
+ */
768
+ export type KnowledgeEmbedding = Prisma.KnowledgeEmbeddingModel
769
+ /**
770
+ * Model DesignEmbedding
771
+ *
772
+ */
773
+ export type DesignEmbedding = Prisma.DesignEmbeddingModel
@@ -135,6 +135,17 @@ export type EnumUserStatusFilter<$PrismaModel = never> = {
135
135
  not?: Prisma.NestedEnumUserStatusFilter<$PrismaModel> | $Enums.UserStatus
136
136
  }
137
137
 
138
+ export type IntFilter<$PrismaModel = never> = {
139
+ equals?: number | Prisma.IntFieldRefInput<$PrismaModel>
140
+ in?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
141
+ notIn?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
142
+ lt?: number | Prisma.IntFieldRefInput<$PrismaModel>
143
+ lte?: number | Prisma.IntFieldRefInput<$PrismaModel>
144
+ gt?: number | Prisma.IntFieldRefInput<$PrismaModel>
145
+ gte?: number | Prisma.IntFieldRefInput<$PrismaModel>
146
+ not?: Prisma.NestedIntFilter<$PrismaModel> | number
147
+ }
148
+
138
149
  export type JsonNullableFilter<$PrismaModel = never> =
139
150
  | Prisma.PatchUndefined<
140
151
  Prisma.Either<Required<JsonNullableFilterBase<$PrismaModel>>, Exclude<keyof Required<JsonNullableFilterBase<$PrismaModel>>, 'path'>>,
@@ -193,6 +204,22 @@ export type EnumUserStatusWithAggregatesFilter<$PrismaModel = never> = {
193
204
  _max?: Prisma.NestedEnumUserStatusFilter<$PrismaModel>
194
205
  }
195
206
 
207
+ export type IntWithAggregatesFilter<$PrismaModel = never> = {
208
+ equals?: number | Prisma.IntFieldRefInput<$PrismaModel>
209
+ in?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
210
+ notIn?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
211
+ lt?: number | Prisma.IntFieldRefInput<$PrismaModel>
212
+ lte?: number | Prisma.IntFieldRefInput<$PrismaModel>
213
+ gt?: number | Prisma.IntFieldRefInput<$PrismaModel>
214
+ gte?: number | Prisma.IntFieldRefInput<$PrismaModel>
215
+ not?: Prisma.NestedIntWithAggregatesFilter<$PrismaModel> | number
216
+ _count?: Prisma.NestedIntFilter<$PrismaModel>
217
+ _avg?: Prisma.NestedFloatFilter<$PrismaModel>
218
+ _sum?: Prisma.NestedIntFilter<$PrismaModel>
219
+ _min?: Prisma.NestedIntFilter<$PrismaModel>
220
+ _max?: Prisma.NestedIntFilter<$PrismaModel>
221
+ }
222
+
196
223
  export type JsonNullableWithAggregatesFilter<$PrismaModel = never> =
197
224
  | Prisma.PatchUndefined<
198
225
  Prisma.Either<Required<JsonNullableWithAggregatesFilterBase<$PrismaModel>>, Exclude<keyof Required<JsonNullableWithAggregatesFilterBase<$PrismaModel>>, 'path'>>,
@@ -324,33 +351,6 @@ export type JsonWithAggregatesFilterBase<$PrismaModel = never> = {
324
351
  _max?: Prisma.NestedJsonFilter<$PrismaModel>
325
352
  }
326
353
 
327
- export type IntFilter<$PrismaModel = never> = {
328
- equals?: number | Prisma.IntFieldRefInput<$PrismaModel>
329
- in?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
330
- notIn?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
331
- lt?: number | Prisma.IntFieldRefInput<$PrismaModel>
332
- lte?: number | Prisma.IntFieldRefInput<$PrismaModel>
333
- gt?: number | Prisma.IntFieldRefInput<$PrismaModel>
334
- gte?: number | Prisma.IntFieldRefInput<$PrismaModel>
335
- not?: Prisma.NestedIntFilter<$PrismaModel> | number
336
- }
337
-
338
- export type IntWithAggregatesFilter<$PrismaModel = never> = {
339
- equals?: number | Prisma.IntFieldRefInput<$PrismaModel>
340
- in?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
341
- notIn?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
342
- lt?: number | Prisma.IntFieldRefInput<$PrismaModel>
343
- lte?: number | Prisma.IntFieldRefInput<$PrismaModel>
344
- gt?: number | Prisma.IntFieldRefInput<$PrismaModel>
345
- gte?: number | Prisma.IntFieldRefInput<$PrismaModel>
346
- not?: Prisma.NestedIntWithAggregatesFilter<$PrismaModel> | number
347
- _count?: Prisma.NestedIntFilter<$PrismaModel>
348
- _avg?: Prisma.NestedFloatFilter<$PrismaModel>
349
- _sum?: Prisma.NestedIntFilter<$PrismaModel>
350
- _min?: Prisma.NestedIntFilter<$PrismaModel>
351
- _max?: Prisma.NestedIntFilter<$PrismaModel>
352
- }
353
-
354
354
  export type FloatFilter<$PrismaModel = never> = {
355
355
  equals?: number | Prisma.FloatFieldRefInput<$PrismaModel>
356
356
  in?: number[] | Prisma.ListFloatFieldRefInput<$PrismaModel>
@@ -797,6 +797,33 @@ export type NestedEnumUserStatusWithAggregatesFilter<$PrismaModel = never> = {
797
797
  _max?: Prisma.NestedEnumUserStatusFilter<$PrismaModel>
798
798
  }
799
799
 
800
+ export type NestedIntWithAggregatesFilter<$PrismaModel = never> = {
801
+ equals?: number | Prisma.IntFieldRefInput<$PrismaModel>
802
+ in?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
803
+ notIn?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
804
+ lt?: number | Prisma.IntFieldRefInput<$PrismaModel>
805
+ lte?: number | Prisma.IntFieldRefInput<$PrismaModel>
806
+ gt?: number | Prisma.IntFieldRefInput<$PrismaModel>
807
+ gte?: number | Prisma.IntFieldRefInput<$PrismaModel>
808
+ not?: Prisma.NestedIntWithAggregatesFilter<$PrismaModel> | number
809
+ _count?: Prisma.NestedIntFilter<$PrismaModel>
810
+ _avg?: Prisma.NestedFloatFilter<$PrismaModel>
811
+ _sum?: Prisma.NestedIntFilter<$PrismaModel>
812
+ _min?: Prisma.NestedIntFilter<$PrismaModel>
813
+ _max?: Prisma.NestedIntFilter<$PrismaModel>
814
+ }
815
+
816
+ export type NestedFloatFilter<$PrismaModel = never> = {
817
+ equals?: number | Prisma.FloatFieldRefInput<$PrismaModel>
818
+ in?: number[] | Prisma.ListFloatFieldRefInput<$PrismaModel>
819
+ notIn?: number[] | Prisma.ListFloatFieldRefInput<$PrismaModel>
820
+ lt?: number | Prisma.FloatFieldRefInput<$PrismaModel>
821
+ lte?: number | Prisma.FloatFieldRefInput<$PrismaModel>
822
+ gt?: number | Prisma.FloatFieldRefInput<$PrismaModel>
823
+ gte?: number | Prisma.FloatFieldRefInput<$PrismaModel>
824
+ not?: Prisma.NestedFloatFilter<$PrismaModel> | number
825
+ }
826
+
800
827
  export type NestedJsonNullableFilter<$PrismaModel = never> =
801
828
  | Prisma.PatchUndefined<
802
829
  Prisma.Either<Required<NestedJsonNullableFilterBase<$PrismaModel>>, Exclude<keyof Required<NestedJsonNullableFilterBase<$PrismaModel>>, 'path'>>,
@@ -898,33 +925,6 @@ export type NestedJsonFilterBase<$PrismaModel = never> = {
898
925
  not?: runtime.InputJsonValue | Prisma.JsonFieldRefInput<$PrismaModel> | Prisma.JsonNullValueFilter
899
926
  }
900
927
 
901
- export type NestedIntWithAggregatesFilter<$PrismaModel = never> = {
902
- equals?: number | Prisma.IntFieldRefInput<$PrismaModel>
903
- in?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
904
- notIn?: number[] | Prisma.ListIntFieldRefInput<$PrismaModel>
905
- lt?: number | Prisma.IntFieldRefInput<$PrismaModel>
906
- lte?: number | Prisma.IntFieldRefInput<$PrismaModel>
907
- gt?: number | Prisma.IntFieldRefInput<$PrismaModel>
908
- gte?: number | Prisma.IntFieldRefInput<$PrismaModel>
909
- not?: Prisma.NestedIntWithAggregatesFilter<$PrismaModel> | number
910
- _count?: Prisma.NestedIntFilter<$PrismaModel>
911
- _avg?: Prisma.NestedFloatFilter<$PrismaModel>
912
- _sum?: Prisma.NestedIntFilter<$PrismaModel>
913
- _min?: Prisma.NestedIntFilter<$PrismaModel>
914
- _max?: Prisma.NestedIntFilter<$PrismaModel>
915
- }
916
-
917
- export type NestedFloatFilter<$PrismaModel = never> = {
918
- equals?: number | Prisma.FloatFieldRefInput<$PrismaModel>
919
- in?: number[] | Prisma.ListFloatFieldRefInput<$PrismaModel>
920
- notIn?: number[] | Prisma.ListFloatFieldRefInput<$PrismaModel>
921
- lt?: number | Prisma.FloatFieldRefInput<$PrismaModel>
922
- lte?: number | Prisma.FloatFieldRefInput<$PrismaModel>
923
- gt?: number | Prisma.FloatFieldRefInput<$PrismaModel>
924
- gte?: number | Prisma.FloatFieldRefInput<$PrismaModel>
925
- not?: Prisma.NestedFloatFilter<$PrismaModel> | number
926
- }
927
-
928
928
  export type NestedFloatWithAggregatesFilter<$PrismaModel = never> = {
929
929
  equals?: number | Prisma.FloatFieldRefInput<$PrismaModel>
930
930
  in?: number[] | Prisma.ListFloatFieldRefInput<$PrismaModel>