@axiom-lattice/pg-stores 1.0.13 → 1.0.15
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/.turbo/turbo-build.log +10 -10
- package/CHANGELOG.md +18 -0
- package/README_DATABASE_CONFIG.md +217 -0
- package/dist/index.d.mts +481 -3
- package/dist/index.d.ts +481 -3
- package/dist/index.js +1757 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1743 -1
- package/dist/index.mjs.map +1 -1
- package/examples/database-config-store.example.ts +178 -0
- package/examples/workspace-project-store.example.ts +151 -0
- package/package.json +3 -3
- package/src/__tests__/workspace-project-store.test.ts +235 -0
- package/src/index.ts +54 -0
- package/src/migrations/database_config_migrations.ts +47 -0
- package/src/migrations/metrics_config_migrations.ts +47 -0
- package/src/migrations/project_migrations.ts +50 -0
- package/src/migrations/tenant_migrations.ts +44 -0
- package/src/migrations/user_migrations.ts +42 -0
- package/src/migrations/user_status_migration.ts +40 -0
- package/src/migrations/user_tenant_link_migrations.ts +76 -0
- package/src/migrations/workspace_migrations.ts +44 -0
- package/src/stores/PostgreSQLDatabaseConfigStore.ts +434 -0
- package/src/stores/PostgreSQLMetricsServerConfigStore.ts +448 -0
- package/src/stores/PostgreSQLProjectStore.ts +282 -0
- package/src/stores/PostgreSQLTenantStore.ts +292 -0
- package/src/stores/PostgreSQLUserStore.ts +206 -0
- package/src/stores/PostgreSQLUserTenantLinkStore.ts +214 -0
- package/src/stores/PostgreSQLWorkspaceStore.ts +286 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @axiom-lattice/pg-stores@1.0.
|
|
2
|
+
> @axiom-lattice/pg-stores@1.0.15 build /home/runner/work/agentic/agentic/packages/pg-stores
|
|
3
3
|
> tsup src/index.ts --format cjs,esm --dts --sourcemap
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
[34mCLI[39m Target: es2020
|
|
9
9
|
[34mCJS[39m Build start
|
|
10
10
|
[34mESM[39m Build start
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
[
|
|
16
|
-
[
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m95.79 KB[39m
|
|
12
|
+
[32mCJS[39m [1mdist/index.js.map [22m[32m180.15 KB[39m
|
|
13
|
+
[32mCJS[39m ⚡️ Build success in 321ms
|
|
14
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m93.28 KB[39m
|
|
15
|
+
[32mESM[39m [1mdist/index.mjs.map [22m[32m176.85 KB[39m
|
|
16
|
+
[32mESM[39m ⚡️ Build success in 322ms
|
|
17
17
|
[34mDTS[39m Build start
|
|
18
|
-
[32mDTS[39m ⚡️ Build success in
|
|
19
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
20
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[
|
|
18
|
+
[32mDTS[39m ⚡️ Build success in 11780ms
|
|
19
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m26.47 KB[39m
|
|
20
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m26.47 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @axiom-lattice/pg-stores
|
|
2
2
|
|
|
3
|
+
## 1.0.15
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 203d94b: update metrics middleware and tenant / user
|
|
8
|
+
- Updated dependencies [203d94b]
|
|
9
|
+
- @axiom-lattice/protocols@2.1.15
|
|
10
|
+
- @axiom-lattice/core@2.1.25
|
|
11
|
+
|
|
12
|
+
## 1.0.14
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- faf1bad: update team and more
|
|
17
|
+
- Updated dependencies [faf1bad]
|
|
18
|
+
- @axiom-lattice/core@2.1.24
|
|
19
|
+
- @axiom-lattice/protocols@2.1.14
|
|
20
|
+
|
|
3
21
|
## 1.0.13
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# DatabaseConfigStore 实现说明
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
`DatabaseConfigStore` 是用于存储和管理数据库连接配置的持久化存储方案,支持多租户隔离和密码自动加密。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- ✅ **多租户隔离**: 基于 `tenant_id` 字段实现租户级别的数据隔离
|
|
10
|
+
- ✅ **密码加密存储**: 使用 AES-256-GCM 自动加密数据库密码
|
|
11
|
+
- ✅ **自动解密**: 读取配置时自动解密密码,调用方透明
|
|
12
|
+
- ✅ **两种存储实现**: InMemory(开发)和 PostgreSQL(生产)
|
|
13
|
+
- ✅ **集成 SqlDatabaseManager**: 可直接从 Store 加载配置到连接管理器
|
|
14
|
+
|
|
15
|
+
## 快速开始
|
|
16
|
+
|
|
17
|
+
### 1. 使用 InMemory 存储(默认)
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { storeLatticeManager } from '@axiom-lattice/core';
|
|
21
|
+
import type { DatabaseConfig } from '@axiom-lattice/protocols';
|
|
22
|
+
|
|
23
|
+
// 获取默认的 InMemory Store
|
|
24
|
+
const store = await storeLatticeManager.getStoreLattice('default', 'database').store;
|
|
25
|
+
|
|
26
|
+
// 创建配置
|
|
27
|
+
await store.createConfig('tenant-1', 'config-1', {
|
|
28
|
+
key: 'main-db',
|
|
29
|
+
config: {
|
|
30
|
+
type: 'postgres',
|
|
31
|
+
host: 'localhost',
|
|
32
|
+
port: 5432,
|
|
33
|
+
database: 'mydb',
|
|
34
|
+
user: 'admin',
|
|
35
|
+
password: 'secret',
|
|
36
|
+
},
|
|
37
|
+
name: 'Main Database',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// 获取配置
|
|
41
|
+
const config = await store.getConfigByKey('tenant-1', 'main-db');
|
|
42
|
+
console.log(config?.config.password); // 密码已自动解密
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 2. 使用 PostgreSQL 存储
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { PostgreSQLDatabaseConfigStore } from '@axiom-lattice/pg-stores';
|
|
49
|
+
|
|
50
|
+
const pgStore = new PostgreSQLDatabaseConfigStore({
|
|
51
|
+
poolConfig: process.env.DATABASE_URL,
|
|
52
|
+
autoMigrate: true,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// 使用方式与 InMemory 相同
|
|
56
|
+
await pgStore.createConfig('tenant-1', 'config-1', {
|
|
57
|
+
key: 'main-db',
|
|
58
|
+
config: { /* ... */ },
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 3. 集成到 SqlDatabaseManager
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { sqlDatabaseManager } from '@axiom-lattice/core';
|
|
66
|
+
|
|
67
|
+
// 从 Store 加载所有配置并注册
|
|
68
|
+
await sqlDatabaseManager.loadConfigsFromStore(store, 'tenant-1');
|
|
69
|
+
|
|
70
|
+
// 现在可以使用注册的数据库
|
|
71
|
+
const db = sqlDatabaseManager.getDatabase('main-db');
|
|
72
|
+
const tables = await db.listTables();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 环境变量
|
|
76
|
+
|
|
77
|
+
### LATTICE_ENCRYPTION_KEY(推荐设置)
|
|
78
|
+
|
|
79
|
+
用于加密敏感数据的全局密钥。
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# .env 文件
|
|
83
|
+
LATTICE_ENCRYPTION_KEY="your-secret-key-at-least-32-characters-long"
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**注意**:
|
|
87
|
+
- 未设置时使用默认密钥(会输出警告)
|
|
88
|
+
- 生产环境**必须**设置此变量
|
|
89
|
+
- 密钥丢失将导致无法解密已存储的密码
|
|
90
|
+
|
|
91
|
+
## 表结构
|
|
92
|
+
|
|
93
|
+
PostgreSQL 实现会创建以下表:
|
|
94
|
+
|
|
95
|
+
```sql
|
|
96
|
+
CREATE TABLE lattice_database_configs (
|
|
97
|
+
id VARCHAR(255) NOT NULL,
|
|
98
|
+
tenant_id VARCHAR(255) NOT NULL,
|
|
99
|
+
key VARCHAR(255) NOT NULL,
|
|
100
|
+
name VARCHAR(255),
|
|
101
|
+
description TEXT,
|
|
102
|
+
config JSONB NOT NULL, -- 密码字段加密存储
|
|
103
|
+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
104
|
+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
105
|
+
|
|
106
|
+
PRIMARY KEY (tenant_id, id),
|
|
107
|
+
CONSTRAINT uk_lattice_database_configs_tenant_key UNIQUE (tenant_id, key)
|
|
108
|
+
);
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## 核心接口
|
|
112
|
+
|
|
113
|
+
### DatabaseConfigEntry
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
interface DatabaseConfigEntry {
|
|
117
|
+
id: string;
|
|
118
|
+
tenantId: string;
|
|
119
|
+
key: string;
|
|
120
|
+
config: DatabaseConfig; // 密码已解密
|
|
121
|
+
name?: string;
|
|
122
|
+
description?: string;
|
|
123
|
+
createdAt: Date;
|
|
124
|
+
updatedAt: Date;
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### DatabaseConfigStore
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
interface DatabaseConfigStore {
|
|
132
|
+
getAllConfigs(tenantId: string): Promise<DatabaseConfigEntry[]>;
|
|
133
|
+
getConfigById(tenantId: string, id: string): Promise<DatabaseConfigEntry | null>;
|
|
134
|
+
getConfigByKey(tenantId: string, key: string): Promise<DatabaseConfigEntry | null>;
|
|
135
|
+
createConfig(tenantId: string, id: string, data: CreateDatabaseConfigRequest): Promise<DatabaseConfigEntry>;
|
|
136
|
+
updateConfig(tenantId: string, id: string, updates: Partial<UpdateDatabaseConfigRequest>): Promise<DatabaseConfigEntry | null>;
|
|
137
|
+
deleteConfig(tenantId: string, id: string): Promise<boolean>;
|
|
138
|
+
hasConfig(tenantId: string, id: string): Promise<boolean>;
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## 加密方案
|
|
143
|
+
|
|
144
|
+
- **算法**: AES-256-GCM
|
|
145
|
+
- **密钥派生**: PBKDF2 (100,000 次迭代)
|
|
146
|
+
- **存储格式**: `salt:iv:ciphertext:authTag` (Base64 编码)
|
|
147
|
+
- **解密时机**: Store 层 `getConfig*()` 方法返回时自动解密
|
|
148
|
+
|
|
149
|
+
## 最佳实践
|
|
150
|
+
|
|
151
|
+
### 1. 生产环境配置
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// 使用 PostgreSQL Store
|
|
155
|
+
const store = new PostgreSQLDatabaseConfigStore({
|
|
156
|
+
poolConfig: process.env.DATABASE_URL,
|
|
157
|
+
autoMigrate: true,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// 确保设置了加密密钥
|
|
161
|
+
if (!process.env.LATTICE_ENCRYPTION_KEY) {
|
|
162
|
+
throw new Error('LATTICE_ENCRYPTION_KEY must be set in production');
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 2. 应用启动时加载配置
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
async function initializeApp() {
|
|
170
|
+
const store = new PostgreSQLDatabaseConfigStore({ /* ... */ });
|
|
171
|
+
|
|
172
|
+
// 加载所有租户的数据库配置
|
|
173
|
+
const tenants = ['tenant-1', 'tenant-2'];
|
|
174
|
+
for (const tenantId of tenants) {
|
|
175
|
+
await sqlDatabaseManager.loadConfigsFromStore(store, tenantId);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### 3. 密钥管理
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
# 生成安全密钥 (推荐)
|
|
184
|
+
openssl rand -base64 32
|
|
185
|
+
|
|
186
|
+
# 或使用 Node.js
|
|
187
|
+
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## 示例代码
|
|
191
|
+
|
|
192
|
+
查看完整示例:`packages/pg-stores/examples/database-config-store.example.ts`
|
|
193
|
+
|
|
194
|
+
## 文件结构
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
packages/
|
|
198
|
+
├── protocols/src/
|
|
199
|
+
│ └── DatabaseConfigStoreProtocol.ts # 协议接口定义
|
|
200
|
+
├── core/src/
|
|
201
|
+
│ ├── util/encryption.ts # 通用加密工具
|
|
202
|
+
│ └── store_lattice/
|
|
203
|
+
│ ├── StoreLatticeManager.ts # Store 管理器(已添加 database 类型)
|
|
204
|
+
│ └── InMemoryDatabaseConfigStore.ts # InMemory 实现
|
|
205
|
+
└── pg-stores/src/
|
|
206
|
+
├── stores/
|
|
207
|
+
│ └── PostgreSQLDatabaseConfigStore.ts # PostgreSQL 实现
|
|
208
|
+
└── migrations/
|
|
209
|
+
└── database_config_migrations.ts # 数据库迁移
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## 注意事项
|
|
213
|
+
|
|
214
|
+
1. **密码解密**: Store 层返回的配置中密码已解密,请妥善保管
|
|
215
|
+
2. **多租户**: 所有操作都需要传入 `tenantId` 参数
|
|
216
|
+
3. **唯一性**: `(tenant_id, key)` 组合必须唯一
|
|
217
|
+
4. **加密密钥**: 生产环境务必设置 `LATTICE_ENCRYPTION_KEY`
|