@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.
@@ -0,0 +1,38 @@
1
+ -- 重建 pgvector 裸向量表(审计 2026-10-25 A02 证实的 P1 缺陷)
2
+ --
3
+ -- 20260911120000_add_pgvector_tables 建表后,20260914041010_ai_p0_interrupt_requested_at
4
+ -- 由 `prisma migrate dev` 生成时把这两张不在 schema.prisma 里的表 diff 成 DROP TABLE,
5
+ -- 此后链上再无重建:全新 pg 库(含本地开发库)没有 knowledge_embeddings /
6
+ -- design_embeddings,知识库向量索引/检索静默失效(写入 42P01 被上层 catch 吞掉后
7
+ -- 降级模糊匹配)。存量库多靠手工建表存活。
8
+ --
9
+ -- 本迁移起两表以 Unsupported("vector(1536)") 纳入 schema.prisma(KnowledgeEmbedding /
10
+ -- DesignEmbedding),migrate 自此感知其存在,不会再被 diff 成 DROP。
11
+ -- 全部 IF NOT EXISTS:对已手工建表的库安全跳过。
12
+
13
+ CREATE EXTENSION IF NOT EXISTS vector;
14
+
15
+ CREATE TABLE IF NOT EXISTS "knowledge_embeddings" (
16
+ "id" TEXT NOT NULL,
17
+ "content" TEXT NOT NULL,
18
+ "kbId" TEXT,
19
+ "documentId" TEXT,
20
+ "chunkIndex" INTEGER,
21
+ "embedding" vector(1536) NOT NULL,
22
+
23
+ CONSTRAINT "knowledge_embeddings_pkey" PRIMARY KEY ("id")
24
+ );
25
+
26
+ CREATE TABLE IF NOT EXISTS "design_embeddings" (
27
+ "id" TEXT NOT NULL,
28
+ "content" TEXT NOT NULL,
29
+ "designPageId" TEXT,
30
+ "chunkIndex" INTEGER,
31
+ "embedding" vector(1536) NOT NULL,
32
+
33
+ CONSTRAINT "design_embeddings_pkey" PRIMARY KEY ("id")
34
+ );
35
+
36
+ CREATE INDEX IF NOT EXISTS "knowledge_embeddings_kbId_idx" ON "knowledge_embeddings"("kbId");
37
+ CREATE INDEX IF NOT EXISTS "knowledge_embeddings_documentId_idx" ON "knowledge_embeddings"("documentId");
38
+ CREATE INDEX IF NOT EXISTS "design_embeddings_designPageId_idx" ON "design_embeddings"("designPageId");
@@ -0,0 +1,8 @@
1
+ -- 审计 A02/A08/A10 收口:
2
+ -- 1) shares.token 已有 @unique(shares_token_key),shares_token_idx 普通索引冗余,移除
3
+ -- 2) User.tokenVersion:重置密码递增凭据版本号,auth callback 比对不匹配即拒绝旧 JWT 会话
4
+ -- DropIndex
5
+ DROP INDEX "shares_token_idx";
6
+
7
+ -- AlterTable
8
+ ALTER TABLE "users" ADD COLUMN "tokenVersion" INTEGER NOT NULL DEFAULT 0;
@@ -0,0 +1,9 @@
1
+ -- 审计 S7:LegalRegulation.code 持久化列,根治 canonicalId「改标题后漂移」。
2
+ -- 历史上 code 仅参与 canonicalId 计算(code||title,code 优先)不落库,
3
+ -- update 仅改 title 时 canonicalId 从 code 维度回落 title 维度,分享/收藏
4
+ -- 关联(entityId=canonicalId)静默失联。落列后 update 以 existing.code 回退。
5
+ ALTER TABLE "legal_regulations" ADD COLUMN "code" TEXT;
6
+
7
+ -- 存量回填:canonicalId 当年按「code 优先」计算,canonicalId <> title 的行
8
+ -- 其 canonicalId 即创建/最近一次显式更新时的 code。
9
+ UPDATE "legal_regulations" SET "code" = "canonicalId" WHERE "canonicalId" <> "title";
package/package.json CHANGED
@@ -1,19 +1,19 @@
1
1
  {
2
2
  "name": "@allbluecn/database",
3
- "version": "0.9.4",
3
+ "version": "0.10.0",
4
4
  "license": "AGPL-3.0-or-later",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
7
7
  "types": "./index.ts",
8
8
  "dependencies": {
9
- "@allbluecn/kernel": "0.10.0",
9
+ "@allbluecn/kernel": "0.10.2",
10
10
  "@prisma/adapter-better-sqlite3": "^7.9.0",
11
11
  "@prisma/adapter-pg": "7.8.0",
12
12
  "@prisma/client": "7.8.0",
13
13
  "@types/pg": "^8.20.0",
14
14
  "bcryptjs": "^3.0.2",
15
15
  "better-sqlite3": "^13.0.1",
16
- "dotenv": "^16.4.5",
16
+ "dotenv": "^17.4.2",
17
17
  "pg": "^8.22.0",
18
18
  "sqlite-vec": "^0.1.9"
19
19
  },
@@ -21,14 +21,15 @@
21
21
  "@types/better-sqlite3": "^7.6.13",
22
22
  "prisma": "^7.8.0",
23
23
  "tsx": "^4.22.0",
24
- "vitest": "^4.1.10"
24
+ "vitest": "^5.0.5"
25
25
  },
26
26
  "exports": {
27
27
  ".": "./index.ts",
28
28
  "./get-prisma": "./get-prisma.ts",
29
29
  "./cf-env": "./cf-env.ts",
30
30
  "./cf-env-stub": "./cf-env-stub.ts",
31
- "./lib/share-utils": "./src/lib/share-utils.ts"
31
+ "./lib/share-utils": "./src/lib/share-utils.ts",
32
+ "./lib/vec-tables": "./src/lib/vec-tables.ts"
32
33
  },
33
34
  "repository": {
34
35
  "type": "git",
package/schema.prisma CHANGED
@@ -31,6 +31,8 @@ model User {
31
31
  image String?
32
32
  role Role @default(USER)
33
33
  status UserStatus @default(ACTIVE)
34
+ /// 密码凭据版本号:重置/管理重置密码时 +1,auth callback 比对 JWT 内记账值不匹配即拒绝旧会话
35
+ tokenVersion Int @default(0)
34
36
  createdAt DateTime @default(now())
35
37
  updatedAt DateTime @updatedAt
36
38
  lastLoginAt DateTime?
@@ -1186,7 +1188,6 @@ model Share {
1186
1188
  design Design @relation(fields: [designId], references: [id], onDelete: Cascade)
1187
1189
  shareVersions ShareVersion[]
1188
1190
 
1189
- @@index([token])
1190
1191
  @@index([designId, type, status])
1191
1192
  @@index([status])
1192
1193
  @@index([expiresAt])
@@ -1404,6 +1405,7 @@ model LegalRegulation {
1404
1405
  id String @id @default(cuid())
1405
1406
  canonicalId String
1406
1407
  title String
1408
+ code String?
1407
1409
  jurisdiction String
1408
1410
  region String?
1409
1411
  country String?
@@ -3109,3 +3111,32 @@ model AiTaskRun {
3109
3111
  @@index([taskId, startedAt(sort: Desc)])
3110
3112
  @@map("ai_task_runs")
3111
3113
  }
3114
+
3115
+ // ── pgvector 裸向量表(apps/web/src/lib/vector-store/pg-vector-store.ts 以 raw SQL 读写,
3116
+ // Prisma Client 不查询)。纳入 schema 只为让 migrate 感知其存在:20260911120000 建表后,
3117
+ // 20260914041010 因它们不在 schema 里被 `migrate dev` diff 成 DROP TABLE 丢表,此后全新
3118
+ // pg 库再无向量表(知识库索引/搜索静默失效,审计 2026-10-25 A02 证实)。
3119
+ // sqlite 侧由 desktop-init 建 vec0 虚拟表,故本两模型列入 check-schema-sync 的 pg-only 白名单。
3120
+ model KnowledgeEmbedding {
3121
+ id String @id
3122
+ content String
3123
+ kbId String?
3124
+ documentId String?
3125
+ chunkIndex Int?
3126
+ embedding Unsupported("vector(1536)")
3127
+
3128
+ @@index([kbId])
3129
+ @@index([documentId])
3130
+ @@map("knowledge_embeddings")
3131
+ }
3132
+
3133
+ model DesignEmbedding {
3134
+ id String @id
3135
+ content String
3136
+ designPageId String?
3137
+ chunkIndex Int?
3138
+ embedding Unsupported("vector(1536)")
3139
+
3140
+ @@index([designPageId])
3141
+ @@map("design_embeddings")
3142
+ }
@@ -38,6 +38,8 @@ model User {
38
38
  image String?
39
39
  role String @default("USER")
40
40
  status String @default("ACTIVE")
41
+ /// 密码凭据版本号:重置/管理重置密码时 +1,auth callback 比对 JWT 内记账值不匹配即拒绝旧会话
42
+ tokenVersion Int @default(0)
41
43
  createdAt DateTime @default(now())
42
44
  updatedAt DateTime @updatedAt
43
45
  lastLoginAt DateTime?
@@ -1120,7 +1122,6 @@ model Share {
1120
1122
  design Design @relation(fields: [designId], references: [id], onDelete: Cascade)
1121
1123
  shareVersions ShareVersion[]
1122
1124
 
1123
- @@index([token])
1124
1125
  @@index([designId, type, status])
1125
1126
  @@index([status])
1126
1127
  @@index([expiresAt])
@@ -1337,6 +1338,7 @@ model LegalRegulation {
1337
1338
  id String @id @default(cuid())
1338
1339
  canonicalId String
1339
1340
  title String
1341
+ code String?
1340
1342
  jurisdiction String
1341
1343
  region String?
1342
1344
  country String?
@@ -15,18 +15,23 @@
15
15
  // You should have received a copy of the GNU Affero General Public License
16
16
  // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
17
 
18
+ import { createHash, timingSafeEqual } from "node:crypto"
18
19
  import bcrypt from "bcryptjs"
19
20
 
20
21
  export async function verifySharePassword(input: string, stored: string): Promise<boolean> {
21
- if (input === stored) return true
22
22
  if (stored.startsWith('$2')) {
23
23
  return bcrypt.compare(input, stored)
24
24
  }
25
- return false
25
+ // 明文存量兼容分支:init 迁移的 shares.password 为裸 TEXT 列。过渡期保留,
26
+ // 但用 sha256 + timingSafeEqual 做常量时间比较(sha256 先归一长度,避免长度泄露)。
27
+ // 存量 bcrypt 化后应删除本分支(见审计 fixes/A02-database-rootcfg.md 待决策)。
28
+ const inputHash = createHash("sha256").update(input).digest()
29
+ const storedHash = createHash("sha256").update(stored).digest()
30
+ return timingSafeEqual(inputHash, storedHash)
26
31
  }
27
32
 
28
33
  export async function verifyDesignOwner(designId: string, userId: string): Promise<boolean> {
29
34
  const { prisma } = await import("@allbluecn/database")
30
- const prd = await prisma.design.findUnique({ where: { id: designId }, select: { userId: true } })
31
- return prd?.userId === userId
35
+ const design = await prisma.design.findUnique({ where: { id: designId }, select: { userId: true } })
36
+ return design?.userId === userId
32
37
  }
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-or-later
2
+ // AllBlue - 理想之海
3
+ // Copyright (C) 2026 AllBlue Contributors
4
+ //
5
+ // This program is free software: you can redistribute it and/or modify
6
+ // it under the terms of the GNU Affero General Public License as published by
7
+ // the Free Software Foundation, either version 3 of the License, or
8
+ // (at your option) any later version.
9
+ //
10
+ // This program is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with this program. If not, see <https://www.gnu.org/licenses/>.
17
+
18
+ // 向量表清单:单一事实源(零依赖常量模块,客户端/服务端均可安全 import)。
19
+ // 消费方:
20
+ // - packages/database/desktop-init.ts(SQLite 桌面端建 vec0 虚拟表)
21
+ // - apps/web/src/lib/vector-store/{sqlite-vec-store,pg-vector-store}.ts(表名白名单,
22
+ // 待切换到本模块,见审计 fixes/A02-database-rootcfg.md)
23
+ // 注意:knowledge_embeddings / design_embeddings 为 raw 向量表(schema.prisma 无模型,
24
+ // 由幂等迁移 / desktop-init 运行时建表),新增向量表时本清单与迁移须同步。
25
+ export const VEC_TABLES = [
26
+ "design_pages",
27
+ "components",
28
+ "blocks",
29
+ "design_embeddings",
30
+ "knowledge_embeddings",
31
+ ] as const;
32
+
33
+ export type VecTable = (typeof VEC_TABLES)[number];