@h-ai/reldb 0.1.0-alpha9 → 1.0.0-beta.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/README.en.md +661 -0
- package/README.ja.md +661 -0
- package/README.md +258 -58
- package/dist/chunk-SDS2ZT3V.js +132 -0
- package/dist/chunk-SDS2ZT3V.js.map +1 -0
- package/dist/index.d.ts +648 -549
- package/dist/index.js +1285 -357
- package/dist/index.js.map +1 -1
- package/dist/providers/sqlite/reldb-sqlite-worker.mjs +143 -0
- package/dist/schemas.d.ts +490 -0
- package/dist/schemas.js +3 -0
- package/dist/schemas.js.map +1 -0
- package/package.json +25 -16
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @h-ai/reldb — SQLite worker 线程脚本
|
|
3
|
+
*
|
|
4
|
+
* 纯 JavaScript,供 worker_threads 直接加载(源码与打包运行位置均可)。
|
|
5
|
+
* 持有唯一 better-sqlite3 连接,按 FIFO 串行处理主线程消息,把同步查询的阻塞移出主线程事件循环。
|
|
6
|
+
*
|
|
7
|
+
* 消息协议:
|
|
8
|
+
* 主线程 → worker: { id, op: 'all'|'get'|'run'|'exec'|'batch'|'close', sql?, params?, statements? }
|
|
9
|
+
* worker → 主线程: { id, ok, result } | { id, ok:false, error:{ message } } | { type:'ready'|'init-error' }
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createRequire } from 'node:module'
|
|
13
|
+
import { setTimeout as delay } from 'node:timers/promises'
|
|
14
|
+
import { parentPort, workerData } from 'node:worker_threads'
|
|
15
|
+
|
|
16
|
+
const require = createRequire(import.meta.url)
|
|
17
|
+
|
|
18
|
+
if (!parentPort) {
|
|
19
|
+
throw new Error('reldb sqlite worker must run inside a worker thread')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 归一化错误为可结构化克隆的对象。 */
|
|
23
|
+
function serializeError(error) {
|
|
24
|
+
return { message: error instanceof Error ? error.message : String(error) }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 打开当前线程拥有的数据库连接;WAL 在初始化阶段单独配置。 */
|
|
28
|
+
function openDatabase() {
|
|
29
|
+
const Database = require('better-sqlite3')
|
|
30
|
+
const database = new Database(workerData.database, { readonly: workerData.readonly ?? false })
|
|
31
|
+
return database
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let db = null
|
|
35
|
+
|
|
36
|
+
async function initialize() {
|
|
37
|
+
try {
|
|
38
|
+
db = openDatabase()
|
|
39
|
+
// 先保存连接,再执行可能失败的 pragma;初始化失败仍能由 close/线程终止释放。
|
|
40
|
+
if (workerData.walMode !== false) {
|
|
41
|
+
// 与同步引擎一致:WAL 锁升级可直接返回 BUSY,原生等待与退避共用 5 秒预算。
|
|
42
|
+
const deadline = performance.now() + 5000
|
|
43
|
+
let contention
|
|
44
|
+
try {
|
|
45
|
+
for (;;) {
|
|
46
|
+
if (contention && performance.now() >= deadline)
|
|
47
|
+
throw contention
|
|
48
|
+
db.pragma(`busy_timeout = ${Math.max(1, Math.ceil(deadline - performance.now()))}`)
|
|
49
|
+
try {
|
|
50
|
+
db.pragma('journal_mode = WAL')
|
|
51
|
+
break
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (!error || typeof error !== 'object' || !('code' in error)
|
|
55
|
+
|| typeof error.code !== 'string' || !/^SQLITE_BUSY(?:_|$)/.test(error.code)
|
|
56
|
+
|| performance.now() >= deadline) {
|
|
57
|
+
throw error
|
|
58
|
+
}
|
|
59
|
+
contention = error
|
|
60
|
+
await delay(Math.min(25, Math.max(1, deadline - performance.now())))
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
db.pragma('busy_timeout = 5000')
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
parentPort.postMessage({ type: 'ready' })
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
parentPort.postMessage({ type: 'init-error', error: serializeError(error) })
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
parentPort.on('message', (msg) => {
|
|
75
|
+
const { id, op, sql, params, statements } = msg
|
|
76
|
+
|
|
77
|
+
if (op === 'close') {
|
|
78
|
+
try {
|
|
79
|
+
db?.close()
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// 忽略关闭异常,仍需回执并退出。
|
|
83
|
+
}
|
|
84
|
+
db = null
|
|
85
|
+
parentPort.postMessage({ id, ok: true, result: undefined })
|
|
86
|
+
parentPort.close()
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
if (!db)
|
|
92
|
+
throw new Error('sqlite worker database is not open')
|
|
93
|
+
|
|
94
|
+
let result
|
|
95
|
+
switch (op) {
|
|
96
|
+
case 'all': {
|
|
97
|
+
const stmt = db.prepare(sql)
|
|
98
|
+
result = params ? stmt.all(...params) : stmt.all()
|
|
99
|
+
break
|
|
100
|
+
}
|
|
101
|
+
case 'get': {
|
|
102
|
+
const stmt = db.prepare(sql)
|
|
103
|
+
result = (params ? stmt.get(...params) : stmt.get()) ?? null
|
|
104
|
+
break
|
|
105
|
+
}
|
|
106
|
+
case 'run': {
|
|
107
|
+
const stmt = db.prepare(sql)
|
|
108
|
+
const runResult = params ? stmt.run(...params) : stmt.run()
|
|
109
|
+
result = { changes: runResult.changes, lastInsertRowid: runResult.lastInsertRowid }
|
|
110
|
+
break
|
|
111
|
+
}
|
|
112
|
+
case 'exec': {
|
|
113
|
+
db.exec(sql)
|
|
114
|
+
result = undefined
|
|
115
|
+
break
|
|
116
|
+
}
|
|
117
|
+
case 'batch': {
|
|
118
|
+
const database = db
|
|
119
|
+
const tx = database.transaction((items) => {
|
|
120
|
+
for (const item of items) {
|
|
121
|
+
const stmt = database.prepare(item.sql)
|
|
122
|
+
if (item.params)
|
|
123
|
+
stmt.run(...item.params)
|
|
124
|
+
else stmt.run()
|
|
125
|
+
}
|
|
126
|
+
})
|
|
127
|
+
tx(statements)
|
|
128
|
+
result = undefined
|
|
129
|
+
break
|
|
130
|
+
}
|
|
131
|
+
default:
|
|
132
|
+
throw new Error(`unknown sqlite worker op: ${op}`)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
parentPort.postMessage({ id, ok: true, result })
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
parentPort.postMessage({ id, ok: false, error: serializeError(error) })
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
void initialize()
|
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @h-ai/reldb — 数据库配置 Schema
|
|
5
|
+
*
|
|
6
|
+
* 本文件定义数据库模块的配置结构,使用 Zod 进行运行时校验。
|
|
7
|
+
* @module reldb-config
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 数据库类型枚举
|
|
12
|
+
*
|
|
13
|
+
* 支持的数据库类型:
|
|
14
|
+
* - `sqlite` - SQLite 嵌入式数据库(使用 better-sqlite3)
|
|
15
|
+
* - `postgresql` - PostgreSQL 数据库(使用 pg)
|
|
16
|
+
* - `mysql` - MySQL 数据库(使用 mysql2)
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { ReldbTypeSchema } from '@h-ai/reldb/schemas'
|
|
20
|
+
*
|
|
21
|
+
* const value = ReldbTypeSchema.parse('postgresql')
|
|
22
|
+
* void value
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare const ReldbTypeSchema: z.ZodEnum<{
|
|
26
|
+
sqlite: "sqlite";
|
|
27
|
+
postgresql: "postgresql";
|
|
28
|
+
mysql: "mysql";
|
|
29
|
+
}>;
|
|
30
|
+
/**
|
|
31
|
+
* 数据库类型
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import type { DbType } from '@h-ai/reldb/schemas'
|
|
35
|
+
*
|
|
36
|
+
* const value: DbType = 'postgresql'
|
|
37
|
+
* void value
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
type DbType = z.infer<typeof ReldbTypeSchema>;
|
|
41
|
+
/**
|
|
42
|
+
* ReldbOperationLogConfigSchema 的数据约束与操作契约。
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { ReldbOperationLogConfigSchema } from '@h-ai/reldb/schemas'
|
|
46
|
+
*
|
|
47
|
+
* const value = ReldbOperationLogConfigSchema.parse({ read: false, write: true, maxLength: 100, level: 'debug' })
|
|
48
|
+
* void value
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
declare const ReldbOperationLogConfigSchema: z.ZodObject<{
|
|
52
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
53
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
54
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
55
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
56
|
+
info: "info";
|
|
57
|
+
debug: "debug";
|
|
58
|
+
trace: "trace";
|
|
59
|
+
}>>;
|
|
60
|
+
}, z.core.$strip>;
|
|
61
|
+
/**
|
|
62
|
+
* ReldbOperationLogConfig 的数据约束与操作契约。
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import { ReldbOperationLogConfigSchema, type ReldbOperationLogConfig } from '@h-ai/reldb/schemas'
|
|
66
|
+
*
|
|
67
|
+
* const value: ReldbOperationLogConfig = ReldbOperationLogConfigSchema.parse({ read: false, write: true, maxLength: 100, level: 'debug' })
|
|
68
|
+
* void value
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
type ReldbOperationLogConfig = z.infer<typeof ReldbOperationLogConfigSchema>;
|
|
72
|
+
/**
|
|
73
|
+
* PostgreSQL 连接池配置 Schema
|
|
74
|
+
*
|
|
75
|
+
* min 是已建立连接的保留下限,不主动预建连接。
|
|
76
|
+
* MySQL 使用独立的 MysqlPoolConfigSchema;SQLite 不使用连接池。
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* import { PoolConfigSchema } from '@h-ai/reldb/schemas'
|
|
80
|
+
*
|
|
81
|
+
* const value = PoolConfigSchema.parse({ max: 10, acquireTimeout: 5000 })
|
|
82
|
+
* void value
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
declare const PoolConfigSchema: z.ZodObject<{
|
|
86
|
+
min: z.ZodDefault<z.ZodNumber>;
|
|
87
|
+
max: z.ZodDefault<z.ZodNumber>;
|
|
88
|
+
idleTimeout: z.ZodDefault<z.ZodNumber>;
|
|
89
|
+
acquireTimeout: z.ZodDefault<z.ZodNumber>;
|
|
90
|
+
}, z.core.$strip>;
|
|
91
|
+
/**
|
|
92
|
+
* PostgreSQL 连接池配置类型。
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { PoolConfigSchema, type PoolConfig } from '@h-ai/reldb/schemas'
|
|
96
|
+
*
|
|
97
|
+
* const value: PoolConfig = PoolConfigSchema.parse({ max: 10, acquireTimeout: 5000 })
|
|
98
|
+
* void value
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
type PoolConfig = z.infer<typeof PoolConfigSchema>;
|
|
102
|
+
/**
|
|
103
|
+
* MySQL 连接池配置:有界 FIFO 等待,拒绝没有可靠驱动语义的 min/idleTimeout。
|
|
104
|
+
*
|
|
105
|
+
* acquireTimeout 覆盖排队和建立连接,不是 SQL 执行时限。驱动建连也使用此值作为 connectTimeout。
|
|
106
|
+
* 空闲连接由 mysql2/服务端管理;本模块不承诺最小保留数或可配置的空闲回收期限。
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* import { MysqlPoolConfigSchema } from '@h-ai/reldb/schemas'
|
|
110
|
+
*
|
|
111
|
+
* const value = MysqlPoolConfigSchema.parse({ max: 10, acquireTimeout: 5000, queueLimit: 100 })
|
|
112
|
+
* void value
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
declare const MysqlPoolConfigSchema: z.ZodObject<{
|
|
116
|
+
max: z.ZodDefault<z.ZodNumber>;
|
|
117
|
+
acquireTimeout: z.ZodDefault<z.ZodNumber>;
|
|
118
|
+
queueLimit: z.ZodDefault<z.ZodNumber>;
|
|
119
|
+
}, z.core.$strict>;
|
|
120
|
+
/**
|
|
121
|
+
* MySQL 解析后的连接池配置。
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { MysqlPoolConfigSchema, type MysqlPoolConfig } from '@h-ai/reldb/schemas'
|
|
125
|
+
*
|
|
126
|
+
* const value: MysqlPoolConfig = MysqlPoolConfigSchema.parse({ max: 10, acquireTimeout: 5000, queueLimit: 100 })
|
|
127
|
+
* void value
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
type MysqlPoolConfig = z.infer<typeof MysqlPoolConfigSchema>;
|
|
131
|
+
/**
|
|
132
|
+
* PostgreSQL SSL 配置 Schema
|
|
133
|
+
*
|
|
134
|
+
* 支持多种配置方式:
|
|
135
|
+
* - `true/false` - 简单开关
|
|
136
|
+
* - `'require'/'prefer'/'allow'/'disable'` - SSL 模式
|
|
137
|
+
* - 自定义对象 - 详细 SSL 配置
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import { SslConfigSchema } from '@h-ai/reldb/schemas'
|
|
141
|
+
*
|
|
142
|
+
* const value = SslConfigSchema.parse({ rejectUnauthorized: true })
|
|
143
|
+
* void value
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
declare const SslConfigSchema: z.ZodUnion<readonly [z.ZodBoolean, z.ZodEnum<{
|
|
147
|
+
require: "require";
|
|
148
|
+
prefer: "prefer";
|
|
149
|
+
allow: "allow";
|
|
150
|
+
disable: "disable";
|
|
151
|
+
}>, z.ZodRecord<z.ZodString, z.ZodUnknown>]>;
|
|
152
|
+
/**
|
|
153
|
+
* PostgreSQL SSL 配置类型
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* import { SslConfigSchema, type SslConfig } from '@h-ai/reldb/schemas'
|
|
157
|
+
*
|
|
158
|
+
* const value: SslConfig = SslConfigSchema.parse({ rejectUnauthorized: true })
|
|
159
|
+
* void value
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
type SslConfig = z.infer<typeof SslConfigSchema>;
|
|
163
|
+
/**
|
|
164
|
+
* MySQL TLS 配置;空对象默认校验证书链和 DNS 主机身份,只接收明确支持的 PEM 字段。
|
|
165
|
+
*
|
|
166
|
+
* cert/key 必须成对。verifyIdentity 在当前驱动仅支持 DNS 主机,不能宣称验证 IP 身份。
|
|
167
|
+
* 显式关闭验证会记录风险日志;本模块不提供任意 TLS 参数或回调透传。
|
|
168
|
+
* @example
|
|
169
|
+
* ```ts
|
|
170
|
+
* import { MysqlSslConfigSchema } from '@h-ai/reldb/schemas'
|
|
171
|
+
*
|
|
172
|
+
* const value = MysqlSslConfigSchema.parse({ rejectUnauthorized: true, verifyIdentity: true })
|
|
173
|
+
* void value
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare const MysqlSslConfigSchema: z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
|
|
177
|
+
rejectUnauthorized: z.ZodDefault<z.ZodBoolean>;
|
|
178
|
+
verifyIdentity: z.ZodDefault<z.ZodBoolean>;
|
|
179
|
+
ca: z.ZodOptional<z.ZodString>;
|
|
180
|
+
cert: z.ZodOptional<z.ZodString>;
|
|
181
|
+
key: z.ZodOptional<z.ZodString>;
|
|
182
|
+
}, z.core.$strict>]>;
|
|
183
|
+
/**
|
|
184
|
+
* SQLite 特定选项 Schema
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* import { SqliteOptionsSchema } from '@h-ai/reldb/schemas'
|
|
188
|
+
*
|
|
189
|
+
* const value = SqliteOptionsSchema.parse({ walMode: true, executor: 'worker' })
|
|
190
|
+
* void value
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
declare const SqliteOptionsSchema: z.ZodObject<{
|
|
194
|
+
walMode: z.ZodDefault<z.ZodBoolean>;
|
|
195
|
+
readonly: z.ZodDefault<z.ZodBoolean>;
|
|
196
|
+
executor: z.ZodDefault<z.ZodEnum<{
|
|
197
|
+
sync: "sync";
|
|
198
|
+
worker: "worker";
|
|
199
|
+
}>>;
|
|
200
|
+
workerConnectTimeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
201
|
+
}, z.core.$strip>;
|
|
202
|
+
/**
|
|
203
|
+
* MySQL 特定选项 Schema
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* import { MysqlOptionsSchema } from '@h-ai/reldb/schemas'
|
|
207
|
+
*
|
|
208
|
+
* const value = MysqlOptionsSchema.parse({ charset: 'utf8mb4' })
|
|
209
|
+
* void value
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
declare const MysqlOptionsSchema: z.ZodObject<{
|
|
213
|
+
charset: z.ZodDefault<z.ZodString>;
|
|
214
|
+
}, z.core.$strict>;
|
|
215
|
+
/**
|
|
216
|
+
* SQLite 配置 Schema
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { SqliteConfigSchema } from '@h-ai/reldb/schemas'
|
|
220
|
+
*
|
|
221
|
+
* const value = SqliteConfigSchema.parse({ type: 'sqlite', database: './data/app.db' })
|
|
222
|
+
* void value
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
declare const SqliteConfigSchema: z.ZodObject<{
|
|
226
|
+
operationLog: z.ZodOptional<z.ZodObject<{
|
|
227
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
228
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
229
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
230
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
231
|
+
info: "info";
|
|
232
|
+
debug: "debug";
|
|
233
|
+
trace: "trace";
|
|
234
|
+
}>>;
|
|
235
|
+
}, z.core.$strip>>;
|
|
236
|
+
type: z.ZodLiteral<"sqlite">;
|
|
237
|
+
database: z.ZodString;
|
|
238
|
+
sqlite: z.ZodOptional<z.ZodObject<{
|
|
239
|
+
walMode: z.ZodDefault<z.ZodBoolean>;
|
|
240
|
+
readonly: z.ZodDefault<z.ZodBoolean>;
|
|
241
|
+
executor: z.ZodDefault<z.ZodEnum<{
|
|
242
|
+
sync: "sync";
|
|
243
|
+
worker: "worker";
|
|
244
|
+
}>>;
|
|
245
|
+
workerConnectTimeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
246
|
+
}, z.core.$strip>>;
|
|
247
|
+
}, z.core.$strip>;
|
|
248
|
+
/**
|
|
249
|
+
* PostgreSQL 配置 Schema
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* import { PostgresqlConfigSchema } from '@h-ai/reldb/schemas'
|
|
253
|
+
*
|
|
254
|
+
* const value = PostgresqlConfigSchema.parse({ type: 'postgresql', database: 'app', host: 'db', pool: { max: 10 } })
|
|
255
|
+
* void value
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
declare const PostgresqlConfigSchema: z.ZodObject<{
|
|
259
|
+
url: z.ZodOptional<z.ZodString>;
|
|
260
|
+
host: z.ZodDefault<z.ZodString>;
|
|
261
|
+
port: z.ZodOptional<z.ZodNumber>;
|
|
262
|
+
database: z.ZodString;
|
|
263
|
+
user: z.ZodOptional<z.ZodString>;
|
|
264
|
+
password: z.ZodOptional<z.ZodString>;
|
|
265
|
+
ssl: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodEnum<{
|
|
266
|
+
require: "require";
|
|
267
|
+
prefer: "prefer";
|
|
268
|
+
allow: "allow";
|
|
269
|
+
disable: "disable";
|
|
270
|
+
}>, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
|
|
271
|
+
pool: z.ZodOptional<z.ZodObject<{
|
|
272
|
+
min: z.ZodDefault<z.ZodNumber>;
|
|
273
|
+
max: z.ZodDefault<z.ZodNumber>;
|
|
274
|
+
idleTimeout: z.ZodDefault<z.ZodNumber>;
|
|
275
|
+
acquireTimeout: z.ZodDefault<z.ZodNumber>;
|
|
276
|
+
}, z.core.$strip>>;
|
|
277
|
+
operationLog: z.ZodOptional<z.ZodObject<{
|
|
278
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
279
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
280
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
281
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
282
|
+
info: "info";
|
|
283
|
+
debug: "debug";
|
|
284
|
+
trace: "trace";
|
|
285
|
+
}>>;
|
|
286
|
+
}, z.core.$strip>>;
|
|
287
|
+
type: z.ZodLiteral<"postgresql">;
|
|
288
|
+
}, z.core.$strip>;
|
|
289
|
+
/**
|
|
290
|
+
* MySQL 配置 Schema
|
|
291
|
+
* @example
|
|
292
|
+
* ```ts
|
|
293
|
+
* import { MysqlConfigSchema } from '@h-ai/reldb/schemas'
|
|
294
|
+
*
|
|
295
|
+
* const value = MysqlConfigSchema.parse({ type: 'mysql', database: 'app', host: 'db', pool: { max: 10, queueLimit: 100 } })
|
|
296
|
+
* void value
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
declare const MysqlConfigSchema: z.ZodObject<{
|
|
300
|
+
host: z.ZodDefault<z.ZodString>;
|
|
301
|
+
port: z.ZodOptional<z.ZodNumber>;
|
|
302
|
+
user: z.ZodOptional<z.ZodString>;
|
|
303
|
+
password: z.ZodOptional<z.ZodString>;
|
|
304
|
+
operationLog: z.ZodOptional<z.ZodObject<{
|
|
305
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
306
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
307
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
308
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
309
|
+
info: "info";
|
|
310
|
+
debug: "debug";
|
|
311
|
+
trace: "trace";
|
|
312
|
+
}>>;
|
|
313
|
+
}, z.core.$strip>>;
|
|
314
|
+
type: z.ZodLiteral<"mysql">;
|
|
315
|
+
url: z.ZodOptional<z.ZodUnion<readonly [z.ZodPipe<z.ZodLiteral<"">, z.ZodTransform<undefined, "">>, z.ZodURL]>>;
|
|
316
|
+
database: z.ZodOptional<z.ZodString>;
|
|
317
|
+
ssl: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
|
|
318
|
+
rejectUnauthorized: z.ZodDefault<z.ZodBoolean>;
|
|
319
|
+
verifyIdentity: z.ZodDefault<z.ZodBoolean>;
|
|
320
|
+
ca: z.ZodOptional<z.ZodString>;
|
|
321
|
+
cert: z.ZodOptional<z.ZodString>;
|
|
322
|
+
key: z.ZodOptional<z.ZodString>;
|
|
323
|
+
}, z.core.$strict>]>>;
|
|
324
|
+
pool: z.ZodOptional<z.ZodObject<{
|
|
325
|
+
max: z.ZodDefault<z.ZodNumber>;
|
|
326
|
+
acquireTimeout: z.ZodDefault<z.ZodNumber>;
|
|
327
|
+
queueLimit: z.ZodDefault<z.ZodNumber>;
|
|
328
|
+
}, z.core.$strict>>;
|
|
329
|
+
mysql: z.ZodOptional<z.ZodObject<{
|
|
330
|
+
charset: z.ZodDefault<z.ZodString>;
|
|
331
|
+
}, z.core.$strict>>;
|
|
332
|
+
}, z.core.$strip>;
|
|
333
|
+
/**
|
|
334
|
+
* 统一数据库配置 Schema(判别联合体)
|
|
335
|
+
*
|
|
336
|
+
* 根据 `type` 字段区分不同数据库类型的配置。
|
|
337
|
+
* @example
|
|
338
|
+
* ```ts
|
|
339
|
+
* import { ReldbConfigSchema } from '@h-ai/reldb/schemas'
|
|
340
|
+
*
|
|
341
|
+
* const value = ReldbConfigSchema.parse({ type: 'postgresql', database: 'app', host: 'db' })
|
|
342
|
+
* void value
|
|
343
|
+
* ```
|
|
344
|
+
*/
|
|
345
|
+
declare const ReldbConfigSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
346
|
+
operationLog: z.ZodOptional<z.ZodObject<{
|
|
347
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
348
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
349
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
350
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
351
|
+
info: "info";
|
|
352
|
+
debug: "debug";
|
|
353
|
+
trace: "trace";
|
|
354
|
+
}>>;
|
|
355
|
+
}, z.core.$strip>>;
|
|
356
|
+
type: z.ZodLiteral<"sqlite">;
|
|
357
|
+
database: z.ZodString;
|
|
358
|
+
sqlite: z.ZodOptional<z.ZodObject<{
|
|
359
|
+
walMode: z.ZodDefault<z.ZodBoolean>;
|
|
360
|
+
readonly: z.ZodDefault<z.ZodBoolean>;
|
|
361
|
+
executor: z.ZodDefault<z.ZodEnum<{
|
|
362
|
+
sync: "sync";
|
|
363
|
+
worker: "worker";
|
|
364
|
+
}>>;
|
|
365
|
+
workerConnectTimeoutMs: z.ZodDefault<z.ZodNumber>;
|
|
366
|
+
}, z.core.$strip>>;
|
|
367
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
368
|
+
url: z.ZodOptional<z.ZodString>;
|
|
369
|
+
host: z.ZodDefault<z.ZodString>;
|
|
370
|
+
port: z.ZodOptional<z.ZodNumber>;
|
|
371
|
+
database: z.ZodString;
|
|
372
|
+
user: z.ZodOptional<z.ZodString>;
|
|
373
|
+
password: z.ZodOptional<z.ZodString>;
|
|
374
|
+
ssl: z.ZodOptional<z.ZodUnion<readonly [z.ZodBoolean, z.ZodEnum<{
|
|
375
|
+
require: "require";
|
|
376
|
+
prefer: "prefer";
|
|
377
|
+
allow: "allow";
|
|
378
|
+
disable: "disable";
|
|
379
|
+
}>, z.ZodRecord<z.ZodString, z.ZodUnknown>]>>;
|
|
380
|
+
pool: z.ZodOptional<z.ZodObject<{
|
|
381
|
+
min: z.ZodDefault<z.ZodNumber>;
|
|
382
|
+
max: z.ZodDefault<z.ZodNumber>;
|
|
383
|
+
idleTimeout: z.ZodDefault<z.ZodNumber>;
|
|
384
|
+
acquireTimeout: z.ZodDefault<z.ZodNumber>;
|
|
385
|
+
}, z.core.$strip>>;
|
|
386
|
+
operationLog: z.ZodOptional<z.ZodObject<{
|
|
387
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
388
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
389
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
390
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
391
|
+
info: "info";
|
|
392
|
+
debug: "debug";
|
|
393
|
+
trace: "trace";
|
|
394
|
+
}>>;
|
|
395
|
+
}, z.core.$strip>>;
|
|
396
|
+
type: z.ZodLiteral<"postgresql">;
|
|
397
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
398
|
+
host: z.ZodDefault<z.ZodString>;
|
|
399
|
+
port: z.ZodOptional<z.ZodNumber>;
|
|
400
|
+
user: z.ZodOptional<z.ZodString>;
|
|
401
|
+
password: z.ZodOptional<z.ZodString>;
|
|
402
|
+
operationLog: z.ZodOptional<z.ZodObject<{
|
|
403
|
+
read: z.ZodDefault<z.ZodBoolean>;
|
|
404
|
+
write: z.ZodDefault<z.ZodBoolean>;
|
|
405
|
+
maxLength: z.ZodDefault<z.ZodNumber>;
|
|
406
|
+
level: z.ZodDefault<z.ZodEnum<{
|
|
407
|
+
info: "info";
|
|
408
|
+
debug: "debug";
|
|
409
|
+
trace: "trace";
|
|
410
|
+
}>>;
|
|
411
|
+
}, z.core.$strip>>;
|
|
412
|
+
type: z.ZodLiteral<"mysql">;
|
|
413
|
+
url: z.ZodOptional<z.ZodUnion<readonly [z.ZodPipe<z.ZodLiteral<"">, z.ZodTransform<undefined, "">>, z.ZodURL]>>;
|
|
414
|
+
database: z.ZodOptional<z.ZodString>;
|
|
415
|
+
ssl: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<false>, z.ZodObject<{
|
|
416
|
+
rejectUnauthorized: z.ZodDefault<z.ZodBoolean>;
|
|
417
|
+
verifyIdentity: z.ZodDefault<z.ZodBoolean>;
|
|
418
|
+
ca: z.ZodOptional<z.ZodString>;
|
|
419
|
+
cert: z.ZodOptional<z.ZodString>;
|
|
420
|
+
key: z.ZodOptional<z.ZodString>;
|
|
421
|
+
}, z.core.$strict>]>>;
|
|
422
|
+
pool: z.ZodOptional<z.ZodObject<{
|
|
423
|
+
max: z.ZodDefault<z.ZodNumber>;
|
|
424
|
+
acquireTimeout: z.ZodDefault<z.ZodNumber>;
|
|
425
|
+
queueLimit: z.ZodDefault<z.ZodNumber>;
|
|
426
|
+
}, z.core.$strict>>;
|
|
427
|
+
mysql: z.ZodOptional<z.ZodObject<{
|
|
428
|
+
charset: z.ZodDefault<z.ZodString>;
|
|
429
|
+
}, z.core.$strict>>;
|
|
430
|
+
}, z.core.$strip>], "type">;
|
|
431
|
+
/**
|
|
432
|
+
* 数据库配置类型(判别联合体)
|
|
433
|
+
* @example
|
|
434
|
+
* ```ts
|
|
435
|
+
* import { ReldbConfigSchema, type ReldbConfig } from '@h-ai/reldb/schemas'
|
|
436
|
+
*
|
|
437
|
+
* const value: ReldbConfig = ReldbConfigSchema.parse({ type: 'postgresql', database: 'app', host: 'db' })
|
|
438
|
+
* void value
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
type ReldbConfig = z.infer<typeof ReldbConfigSchema>;
|
|
442
|
+
/**
|
|
443
|
+
* SQLite 配置类型
|
|
444
|
+
* @example
|
|
445
|
+
* ```ts
|
|
446
|
+
* import { SqliteConfigSchema, type SqliteConfig } from '@h-ai/reldb/schemas'
|
|
447
|
+
*
|
|
448
|
+
* const value: SqliteConfig = SqliteConfigSchema.parse({ type: 'sqlite', database: './data/app.db' })
|
|
449
|
+
* void value
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
type SqliteConfig = z.infer<typeof SqliteConfigSchema>;
|
|
453
|
+
/**
|
|
454
|
+
* PostgreSQL 配置类型
|
|
455
|
+
* @example
|
|
456
|
+
* ```ts
|
|
457
|
+
* import { PostgresqlConfigSchema, type PostgresqlConfig } from '@h-ai/reldb/schemas'
|
|
458
|
+
*
|
|
459
|
+
* const value: PostgresqlConfig = PostgresqlConfigSchema.parse({ type: 'postgresql', database: 'app', host: 'db', pool: { max: 10 } })
|
|
460
|
+
* void value
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
463
|
+
type PostgresqlConfig = z.infer<typeof PostgresqlConfigSchema>;
|
|
464
|
+
/**
|
|
465
|
+
* MySQL 配置类型
|
|
466
|
+
* @example
|
|
467
|
+
* ```ts
|
|
468
|
+
* import { MysqlConfigSchema, type MysqlConfig } from '@h-ai/reldb/schemas'
|
|
469
|
+
*
|
|
470
|
+
* const value: MysqlConfig = MysqlConfigSchema.parse({ type: 'mysql', database: 'app', host: 'db', pool: { max: 10, queueLimit: 100 } })
|
|
471
|
+
* void value
|
|
472
|
+
* ```
|
|
473
|
+
*/
|
|
474
|
+
type MysqlConfig = z.infer<typeof MysqlConfigSchema>;
|
|
475
|
+
/**
|
|
476
|
+
* 数据库配置输入类型(用于 init 等入口)
|
|
477
|
+
*
|
|
478
|
+
* 说明:Zod 的 default 会让输入端字段可省略,但输出端字段为必填。
|
|
479
|
+
* 因此对外 API(如 reldb.init)更适合接收 ReldbConfigInput。
|
|
480
|
+
* @example
|
|
481
|
+
* ```ts
|
|
482
|
+
* import type { ReldbConfigInput } from '@h-ai/reldb/schemas'
|
|
483
|
+
*
|
|
484
|
+
* const value: ReldbConfigInput = { type: 'postgresql', host: 'db', database: 'app' }
|
|
485
|
+
* void value
|
|
486
|
+
* ```
|
|
487
|
+
*/
|
|
488
|
+
type ReldbConfigInput = z.input<typeof ReldbConfigSchema>;
|
|
489
|
+
|
|
490
|
+
export { type DbType, type MysqlConfig, MysqlConfigSchema, MysqlOptionsSchema, type MysqlPoolConfig, MysqlPoolConfigSchema, MysqlSslConfigSchema, type PoolConfig, PoolConfigSchema, type PostgresqlConfig, PostgresqlConfigSchema, type ReldbConfig, type ReldbConfigInput, ReldbConfigSchema, type ReldbOperationLogConfig, ReldbOperationLogConfigSchema, ReldbTypeSchema, type SqliteConfig, SqliteConfigSchema, SqliteOptionsSchema, type SslConfig, SslConfigSchema };
|
package/dist/schemas.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { MysqlConfigSchema, MysqlOptionsSchema, MysqlPoolConfigSchema, MysqlSslConfigSchema, PoolConfigSchema, PostgresqlConfigSchema, ReldbConfigSchema, ReldbOperationLogConfigSchema, ReldbTypeSchema, SqliteConfigSchema, SqliteOptionsSchema, SslConfigSchema } from './chunk-SDS2ZT3V.js';
|
|
2
|
+
//# sourceMappingURL=schemas.js.map
|
|
3
|
+
//# sourceMappingURL=schemas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"schemas.js"}
|