baja-lite 1.8.8 → 1.8.10
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/boot-remote.d.ts +1 -1
- package/boot-remote.js +6 -1
- package/boot.d.ts +1 -1
- package/boot.js +6 -1
- package/cache.d.ts +202 -0
- package/cache.js +593 -0
- package/const/index.d.ts +2 -0
- package/const/index.js +2 -0
- package/const/symbols.d.ts +71 -0
- package/const/symbols.js +76 -0
- package/const/types.d.ts +520 -0
- package/const/types.js +127 -0
- package/db/dao/format-dialects.d.ts +6 -0
- package/db/dao/format-dialects.js +8 -0
- package/db/dao/mysql.d.ts +44 -0
- package/db/dao/mysql.js +276 -0
- package/db/dao/postgresql.d.ts +44 -0
- package/db/dao/postgresql.js +294 -0
- package/db/dao/sqlite-remote.d.ts +46 -0
- package/db/dao/sqlite-remote.js +211 -0
- package/db/dao/sqlite.d.ts +41 -0
- package/db/dao/sqlite.js +219 -0
- package/db/index.d.ts +8 -0
- package/db/index.js +8 -0
- package/db/service.d.ts +778 -0
- package/db/service.js +1651 -0
- package/db/sql-template.d.ts +46 -0
- package/db/sql-template.js +661 -0
- package/db/stream-query.d.ts +607 -0
- package/db/stream-query.js +1626 -0
- package/index.d.ts +4 -1
- package/index.js +4 -1
- package/logger.d.ts +73 -0
- package/logger.js +116 -0
- package/package.json +1 -1
- package/sqlite.d.ts +1 -1
- package/sqlite.js +8 -7
- package/{test-mysql.js → test/test-mysql.js} +3 -2
- package/{test-postgresql.js → test/test-postgresql.js} +3 -2
- package/{test-sqlite.js → test/test-sqlite.js} +3 -2
- package/sql.d.ts +0 -2222
- package/sql.js +0 -5503
- /package/{test-mysql.d.ts → test/test-mysql.d.ts} +0 -0
- /package/{test-postgresql.d.ts → test/test-postgresql.d.ts} +0 -0
- /package/{test-sqlite.d.ts → test/test-sqlite.d.ts} +0 -0
- /package/{test-xml.d.ts → test/test-xml.d.ts} +0 -0
- /package/{test-xml.js → test/test-xml.js} +0 -0
package/boot-remote.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { GlobalSqlOptionForWeb } from './
|
|
1
|
+
import { GlobalSqlOptionForWeb } from './const/index.js';
|
|
2
2
|
export declare const BootRomote: (options: GlobalSqlOptionForWeb) => Promise<void>;
|
package/boot-remote.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { DBType, _Hump, getEnums } from 'baja-lite-field';
|
|
2
|
-
import {
|
|
2
|
+
import { SqlCache, SqliteRemote } from './db/index.js';
|
|
3
|
+
import { PrinterLogger } from './logger.js';
|
|
4
|
+
import { ColumnMode, _Context, _DataConvert, _GlobalSqlOption, _LoggerService, _dao, _defOption, _enum, _primaryDB, _sqlCache } from './const/index.js';
|
|
3
5
|
export const BootRomote = async function (options) {
|
|
4
6
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption);
|
|
5
7
|
if (options.skipEmptyString !== undefined) {
|
|
@@ -11,6 +13,9 @@ export const BootRomote = async function (options) {
|
|
|
11
13
|
if (options.maxDeal !== undefined) {
|
|
12
14
|
globalThis[_GlobalSqlOption].maxDeal = options.maxDeal;
|
|
13
15
|
}
|
|
16
|
+
if (options.memCacheMaxSize !== undefined) {
|
|
17
|
+
globalThis[_GlobalSqlOption].memCacheMaxSize = options.memCacheMaxSize;
|
|
18
|
+
}
|
|
14
19
|
if (options.SqliteRemote !== undefined) {
|
|
15
20
|
globalThis[_GlobalSqlOption].SqliteRemote = options.SqliteRemote;
|
|
16
21
|
}
|
package/boot.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { GlobalSqlOption } from './
|
|
1
|
+
import { GlobalSqlOption } from './const/index.js';
|
|
2
2
|
export declare const Boot: (options: GlobalSqlOption) => Promise<void>;
|
package/boot.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { _Hump, DBType, getEnums } from 'baja-lite-field';
|
|
2
2
|
import events from 'events';
|
|
3
|
-
import { _Context, _dao, _DataConvert, _defOption, _enum, _EventBus, _fs, _GlobalSqlOption, _LoggerService, _MysqlKeepAliveTime, _path, _primaryDB, _sqlCache, ColumnMode
|
|
3
|
+
import { _Context, _dao, _DataConvert, _defOption, _enum, _EventBus, _fs, _GlobalSqlOption, _LoggerService, _MysqlKeepAliveTime, _path, _primaryDB, _sqlCache, ColumnMode } from './const/index.js';
|
|
4
|
+
import { Mysql, Postgresql, SqlCache, Sqlite, SqliteRemote } from './db/index.js';
|
|
5
|
+
import { PrinterLogger } from './logger.js';
|
|
4
6
|
export const Boot = async function (options) {
|
|
5
7
|
globalThis[_GlobalSqlOption] = Object.assign({}, _defOption);
|
|
6
8
|
if (options.skipEmptyString !== undefined) {
|
|
@@ -12,6 +14,9 @@ export const Boot = async function (options) {
|
|
|
12
14
|
if (options.maxDeal !== undefined) {
|
|
13
15
|
globalThis[_GlobalSqlOption].maxDeal = options.maxDeal;
|
|
14
16
|
}
|
|
17
|
+
if (options.memCacheMaxSize !== undefined) {
|
|
18
|
+
globalThis[_GlobalSqlOption].memCacheMaxSize = options.memCacheMaxSize;
|
|
19
|
+
}
|
|
15
20
|
if (options.logger) {
|
|
16
21
|
globalThis[_LoggerService] = options.logger;
|
|
17
22
|
}
|
package/cache.d.ts
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal 统一的 stale-allowed 注册表 key。
|
|
3
|
+
* 用带 TTL 的 string(而非全局 set),每个 stale-allowed 缓存独立管理自己的标记。
|
|
4
|
+
* key = [cache-meta]realKey,value = '1',TTL = 缓存 TTL。
|
|
5
|
+
* 好处:自动过期,不再需要全局 set,内存占用与缓存数量而非请求数量成正比。
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* 缓存被清理时的行为策略。
|
|
9
|
+
* 由 @MethodCache 的 `staleMode` 配置决定,clearMethodCache 依此自动选择清理方式。
|
|
10
|
+
*/
|
|
11
|
+
export declare enum CacheStaleMode {
|
|
12
|
+
/**
|
|
13
|
+
* 默认。清理时直接删除缓存值,下次请求触发 single-flight 重建。
|
|
14
|
+
* 优点:数据强一致,不会读到旧值。
|
|
15
|
+
* 缺点:重建期间所有请求被阻塞,直到 fn() 执行完毕。
|
|
16
|
+
*/
|
|
17
|
+
Purge = 0,
|
|
18
|
+
/**
|
|
19
|
+
* 清理时保留旧值并标记为过期(stale),同时设一个 stale 窗口(默认 30s)。
|
|
20
|
+
* 窗口内:请求仍读到旧值(零阻塞),同时后台 single-flight 异步刷新。
|
|
21
|
+
* 窗口过后:stale 标记自动消失,退化回 Purge 行为。
|
|
22
|
+
*
|
|
23
|
+
* 适用场景:读多写少、能容忍秒级旧数据、fn() 执行代价高。
|
|
24
|
+
* 注意:开启后业务层可能收到乱序数据,需自行处理(如按时间戳丢弃过期包)。
|
|
25
|
+
*/
|
|
26
|
+
StaleWhileRevalidate = 1
|
|
27
|
+
}
|
|
28
|
+
export declare function getRedisDB<T = any>(db?: string): T;
|
|
29
|
+
export declare function GetRedisLock(key: string, lockMaxActive?: number): Promise<boolean>;
|
|
30
|
+
/** 对FN加锁、缓存执行 */
|
|
31
|
+
export declare function excuteWithLock<T>(config: {
|
|
32
|
+
/** 返回缓存key,参数=方法的参数+当前用户对象,可以用来清空缓存。 */
|
|
33
|
+
key: ((...args: any[]) => string) | string;
|
|
34
|
+
/** 被锁定线程是否sleep直到解锁为止? 默认true */
|
|
35
|
+
lockWait?: boolean;
|
|
36
|
+
/** 当设置了lockWait=true时,等待多少【毫秒】进行一次锁查询? 默认:100MS */
|
|
37
|
+
lockRetryInterval?: number;
|
|
38
|
+
/** 当设置了lockWait=true时,等待多少【毫秒】即视为超时,放弃本次访问?默认:永不放弃 */
|
|
39
|
+
lockMaxWaitTime?: number;
|
|
40
|
+
/** 错误信息 */
|
|
41
|
+
errorMessage?: string;
|
|
42
|
+
/** 允许的并发数,默认:1 */
|
|
43
|
+
lockMaxActive?: number;
|
|
44
|
+
/** 单个锁多少【毫秒】后自动释放?默认:60*1000MS */
|
|
45
|
+
lockMaxTime?: number;
|
|
46
|
+
}, fn__: () => Promise<T>): Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* 方法加锁装饰器。
|
|
49
|
+
* 与 `MethodCache` 组合时,**不要**手工叠装饰器——`MethodCache` 内部已经实现了
|
|
50
|
+
* "先查缓存 → miss 才加锁 → 锁内二次检查 → 仍 miss 才执行" 的 single-flight 语义,
|
|
51
|
+
* 直接给方法加 `@MethodCache` 即可。`@MethodLock` 单独使用时仅做并发互斥,不感知缓存。
|
|
52
|
+
*/
|
|
53
|
+
export declare function MethodLock<T = any>(config: {
|
|
54
|
+
/** 返回缓存key,参数=方法的参数[注意:必须和主方法的参数数量、完全一致,同时会追加一个当前用户对象]+当前用户对象,可以用来清空缓存。 */
|
|
55
|
+
key: ((this: T, ...args: any[]) => string) | string;
|
|
56
|
+
/** 被锁定线程是否sleep直到解锁为止? 默认true */
|
|
57
|
+
lockWait?: boolean;
|
|
58
|
+
/** 当设置了lockWait=true时,等待多少【毫秒】进行一次锁查询? 默认100ms */
|
|
59
|
+
lockRetryInterval?: number;
|
|
60
|
+
/** 当设置了lockWait=true时,等待多少【毫秒】即视为超时,放弃本次访问?默认永不放弃 */
|
|
61
|
+
lockMaxWaitTime?: number;
|
|
62
|
+
/** 错误信息 */
|
|
63
|
+
errorMessage?: string;
|
|
64
|
+
/** 允许的并发数,默认=1 */
|
|
65
|
+
lockMaxActive?: number;
|
|
66
|
+
/** 单个锁多少【毫秒】后自动释放?即时任务没有执行完毕或者没有主动释放锁? */
|
|
67
|
+
lockMaxTime?: number;
|
|
68
|
+
}): (target: T, _propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
69
|
+
/**
|
|
70
|
+
* 清空方法缓存。
|
|
71
|
+
* - redis 侧只在配置了 redis 时才操作;没配 redis 时安静跳过,不抛错。
|
|
72
|
+
* - 关联清除(clearKey)生效。
|
|
73
|
+
* - 被清理的 key 如果启用了 StaleWhileRevalidate(CacheValue.sa=1),会保留旧值并标记 stale。
|
|
74
|
+
*
|
|
75
|
+
* @param key 要清理的缓存 key 或 parent key
|
|
76
|
+
* @param staleTimeoutSec stale 窗口秒数(仅对 StaleWhileRevalidate 的 key 生效)。默认 30。
|
|
77
|
+
*/
|
|
78
|
+
export declare function clearMethodCache(key: string, staleTimeoutSec?: number): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* 执行一个方法fn,
|
|
81
|
+
* 如果有缓存,则返回缓存,否则【加锁、二次检查】后执行方法并缓存,防止缓存击穿。
|
|
82
|
+
* 可选 `cacheNullValue=true` 开启负缓存,防穿透;负缓存默认沿用 autoClearTime,可单独用 nullCacheTime 设短。
|
|
83
|
+
* 可选 `staleMode: CacheStaleMode.StaleWhileRevalidate` 开启异步重新验证(清理时保留旧值,零阻塞)。
|
|
84
|
+
*/
|
|
85
|
+
export declare function excuteWithCache<T>(config: {
|
|
86
|
+
/** 返回缓存key,参数=方法的参数+当前用户对象,可以用来清空缓存。 */
|
|
87
|
+
key: ((...args: any[]) => string) | string;
|
|
88
|
+
/** 返回缓存清除key,参数=方法的参数+当前用户对象,可以用来批量清空缓存 */
|
|
89
|
+
clearKey?: string[];
|
|
90
|
+
/** 自动清空缓存的时间,单位分钟 */
|
|
91
|
+
autoClearTime?: number;
|
|
92
|
+
/** 是否缓存 null / undefined(负缓存防穿透),默认 false。 */
|
|
93
|
+
cacheNullValue?: boolean;
|
|
94
|
+
/** 负缓存 TTL(分钟),默认沿用 autoClearTime。 */
|
|
95
|
+
nullCacheTime?: number;
|
|
96
|
+
/** 存储后端:'redis'(跨进程共享)。 */
|
|
97
|
+
/** 随着当前用户sesion的清空而一起清空 */
|
|
98
|
+
clearWithSession?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* 缓存被清理时的行为策略。默认 Purge。
|
|
101
|
+
* StaleWhileRevalidate 时设 sa=1 使 clearMethodCache 走"标记 stale + 保留旧值"路径。
|
|
102
|
+
*/
|
|
103
|
+
staleMode?: CacheStaleMode;
|
|
104
|
+
/** stale 窗口秒数(仅 StaleWhileRevalidate 生效)。默认 30。 */
|
|
105
|
+
staleTimeout?: number;
|
|
106
|
+
/**
|
|
107
|
+
* 缓存更新回调。异步方法,在 setMethodCache 成功写缓存后调用。
|
|
108
|
+
* 调用时三个参数:this → 方法的执行上下文(config.ctx);value → 缓存值;args → 方法输入参数。
|
|
109
|
+
*/
|
|
110
|
+
onCacheUpdated?: (this: any, value: T, ...args: any[]) => void | Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* 方法的执行上下文。在 onCacheUpdated 回调中作为 this 传入。
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* excuteWithCache({
|
|
116
|
+
* key: 'calibration',
|
|
117
|
+
* ctx: service,
|
|
118
|
+
* args: ['device001'],
|
|
119
|
+
* onCacheUpdated(this: MyService, args, value) {
|
|
120
|
+
* this.broadcast(value);
|
|
121
|
+
* },
|
|
122
|
+
* }, () => service.getCalibration('device001'));
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
ctx?: any;
|
|
126
|
+
/**
|
|
127
|
+
* 方法的输入参数。在 onCacheUpdated 回调中作为 args 传入。
|
|
128
|
+
* 用于告知回调是哪些参数触发了本次缓存更新。
|
|
129
|
+
*/
|
|
130
|
+
args?: any[];
|
|
131
|
+
}, fn: () => Promise<T>): Promise<T>;
|
|
132
|
+
/**
|
|
133
|
+
* 缓存注解:先查缓存 → miss 才加锁 → 锁内再查一次 → 仍 miss 才执行原方法。
|
|
134
|
+
* 已内置 single-flight,不需要再叠 `@MethodLock`。
|
|
135
|
+
* 可选 `cacheNullValue=true` 开启负缓存,防穿透。
|
|
136
|
+
* 可选 `storage: 'memory'` 用进程内 LRU 替代 redis(单进程场景,毫秒级延迟)。
|
|
137
|
+
* 可选 `staleMode: CacheStaleMode.StaleWhileRevalidate` 开启异步重新验证(清理时保留旧值,零阻塞)。
|
|
138
|
+
* 可选 `onCacheUpdated` 设置缓存更新回调(带 this 上下文)。
|
|
139
|
+
*/
|
|
140
|
+
export declare function MethodCache<T = any>(config: {
|
|
141
|
+
/** 返回缓存key,参数=方法的参数[注意:必须和主方法的参数数量、完全一致,同时会追加一个当前用户对象]+当前用户对象,可以用来清空缓存。 */
|
|
142
|
+
key: ((this: T, ...args: any[]) => string) | string;
|
|
143
|
+
/** 返回缓存清除key,参数=方法的参数[注意:必须和主方法的参数数量、完全一致,同时会追加一个当前用户对象]+当前用户对象,可以用来批量清空缓存 */
|
|
144
|
+
clearKey?: ((this: T, ...args: any[]) => string[]) | string[];
|
|
145
|
+
/** 自动清空缓存的时间,单位分钟 */
|
|
146
|
+
autoClearTime?: number;
|
|
147
|
+
/** 是否缓存 null / undefined(负缓存防穿透),默认 false。 */
|
|
148
|
+
cacheNullValue?: boolean;
|
|
149
|
+
/** 负缓存 TTL(分钟),默认沿用 autoClearTime。 */
|
|
150
|
+
nullCacheTime?: number;
|
|
151
|
+
/** 存储后端:'redis'(跨进程共享)。 */
|
|
152
|
+
/** 随着当前用户sesion的清空而一起清空 */
|
|
153
|
+
clearWithSession?: boolean;
|
|
154
|
+
/**
|
|
155
|
+
* 缓存被清理时的行为策略。默认 Purge。
|
|
156
|
+
* StaleWhileRevalidate 时设 sa=1 使 clearMethodCache 走"标记 stale + 保留旧值"路径。
|
|
157
|
+
*/
|
|
158
|
+
staleMode?: CacheStaleMode;
|
|
159
|
+
/** stale 窗口秒数(仅 StaleWhileRevalidate 生效)。默认 30。 */
|
|
160
|
+
staleTimeout?: number;
|
|
161
|
+
/**
|
|
162
|
+
* 缓存更新回调。异步方法,在 setMethodCache 成功写缓存后调用。
|
|
163
|
+
* 调用时三个参数:this → 执行上下文;args → 方法输入参数;value → 缓存值。
|
|
164
|
+
*/
|
|
165
|
+
onCacheUpdated?: (this: any, value: T, ...args: any[]) => void | Promise<void>;
|
|
166
|
+
}): (_target: T, _propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
167
|
+
/**
|
|
168
|
+
* 外部直接设置缓存值。用于绕过 @MethodCache 装饰器直接更新缓存的场景
|
|
169
|
+
* (如外部事件驱动的数据变更、手动刷新等)。
|
|
170
|
+
*
|
|
171
|
+
* 内部使用统一格式 { v, d, s } 序列化,确保与 @MethodCache 的读取逻辑兼容。
|
|
172
|
+
*
|
|
173
|
+
* @param cacheKey 缓存 key(不含 [cache] 前缀,与 @MethodCache 的 key 一致)
|
|
174
|
+
* @param value 要缓存的值
|
|
175
|
+
* @param options 选项
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* // 外部事件驱动更新缓存
|
|
180
|
+
* await setCache('match-timer-123', timer, { autoClearTime: 120 });
|
|
181
|
+
*
|
|
182
|
+
* // 同时触发 onCacheUpdated 回调(如果有)
|
|
183
|
+
* await setCache('match-timer-123', timer, {
|
|
184
|
+
* autoClearTime: 120,
|
|
185
|
+
* onCacheUpdated(this: MyService, value) {
|
|
186
|
+
* this.broadcast(value);
|
|
187
|
+
* }
|
|
188
|
+
* });
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
export declare function setCache<T = any>(cacheKey: string, value: T, options?: {
|
|
192
|
+
/** 自动清空缓存的时间,单位分钟。不设则永不过期。 */
|
|
193
|
+
autoClearTime?: number;
|
|
194
|
+
/** 是否缓存 null / undefined。默认 false。 */
|
|
195
|
+
cacheNullValue?: boolean;
|
|
196
|
+
/** 缓存更新回调 */
|
|
197
|
+
onCacheUpdated?: (this: any, value: T, ...args: any[]) => void | Promise<void>;
|
|
198
|
+
/** 回调的 this 上下文 */
|
|
199
|
+
ctx?: any;
|
|
200
|
+
/** 回调的 args 参数 */
|
|
201
|
+
args?: any[];
|
|
202
|
+
}): Promise<void>;
|