web-dc-api 0.0.72 → 0.0.74

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,39 @@
1
+ export async function loadBabel() {
2
+ if (!(globalThis as any).process) {
3
+ (globalThis as any).process = { env: {} };
4
+ }
5
+
6
+ const [parserMod, typesMod, traverseMod] = await Promise.all([
7
+ import("@babel/parser"),
8
+ import("@babel/types"),
9
+ import("@babel/traverse"),
10
+ ]);
11
+
12
+ // 规范化 parser.parse(兼容多种打包形态)
13
+ const parse =
14
+ (parserMod as any).parse ??
15
+ (parserMod as any).default?.parse ??
16
+ (parserMod as any).default; // 有些打包器把默认导出直接设为 parse
17
+
18
+ if (typeof parse !== "function") {
19
+ console.error("parserMod keys:", Object.keys(parserMod));
20
+ throw new Error("Failed to resolve @babel/parser.parse as a function");
21
+ }
22
+
23
+ // types:有的环境在 default 下
24
+ const t = (typesMod as any).default ?? typesMod;
25
+
26
+ // 规范化 traverse
27
+ const traverse: (node: any, visitors: any) => void =
28
+ (traverseMod as any).default?.default ??
29
+ (traverseMod as any).default ??
30
+ (traverseMod as any).traverse ??
31
+ (traverseMod as any);
32
+
33
+ if (typeof traverse !== "function") {
34
+ console.error("traverseMod keys:", Object.keys(traverseMod));
35
+ throw new Error("Failed to resolve @babel/traverse as a function");
36
+ }
37
+
38
+ return { parse, t, traverse };
39
+ }
@@ -0,0 +1,78 @@
1
+ // 领域模型基类:不包含任何存储逻辑
2
+ export abstract class BaseEntity {
3
+ // 默认元字段
4
+ public dc_timestamp?: number; // 毫秒时间戳
5
+ public dc_opuser?: string; // 操作人
6
+
7
+ constructor() {
8
+ // 时间戳,opuser 留空,主要用于从dc取回数据时填充
9
+
10
+ }
11
+
12
+ // 可重写以自定义主键策略
13
+ protected getPrimaryKey(): string {
14
+ const anySelf = this as any;
15
+ const id = anySelf.id ?? anySelf.key ?? anySelf.pk;
16
+
17
+ // 仅判 null/undefined,为 0 的数字是合法主键
18
+ if (id === null || id === undefined) {
19
+ throw new Error(`${this.constructor.name}: primary key (id/key/pk) is required`);
20
+ }
21
+
22
+ if (typeof id === 'string') {
23
+ const s = id.trim();
24
+ if (s.length === 0) {
25
+ throw new Error(`${this.constructor.name}: primary key (string) cannot be empty`);
26
+ }
27
+ return s;
28
+ }
29
+
30
+ if (typeof id === 'number') {
31
+ if (Number.isNaN(id)) {
32
+ throw new Error(`${this.constructor.name}: primary key (number) cannot be NaN`);
33
+ }
34
+ return String(id);
35
+ }
36
+
37
+ if (typeof id === 'bigint') {
38
+ return id.toString();
39
+ }
40
+
41
+ // 其它类型统一转字符串(如需自定义请在子类中重写)
42
+ return String(id);
43
+ }
44
+
45
+ // 标记更新时间与操作人
46
+ touch(opuser?: string, timeMs: number = Date.now()): this {
47
+ this.dc_timestamp = timeMs;
48
+ if (opuser !== undefined) this.dc_opuser = opuser;
49
+ return this;
50
+ }
51
+
52
+ // 设置操作人
53
+ setOpUser(opuser: string): this {
54
+ this.dc_opuser = opuser;
55
+ return this;
56
+ }
57
+
58
+ // 可选:领域校验(子类覆盖)
59
+ validate(): void {
60
+ // no-op by default
61
+ }
62
+
63
+ // 工厂:从 PlainObject 创建实体
64
+ static from<T extends BaseEntity>(this: new () => T, data: Partial<T>): T {
65
+ const inst = new this();
66
+ Object.assign(inst, data);
67
+ return inst;
68
+ }
69
+
70
+ // 导出为可序列化对象(可覆写以裁剪敏感字段)
71
+ toJSON(): Record<string, unknown> {
72
+ const out: Record<string, unknown> = {};
73
+ for (const k of Object.keys(this as any)) {
74
+ (out as any)[k] = (this as any)[k];
75
+ }
76
+ return out;
77
+ }
78
+ }
@@ -0,0 +1,284 @@
1
+ import { Direction } from "../common/define";
2
+ import type { IKeyValueOperations } from "../interfaces/keyvalue-interface";
3
+ import type { KeyValueDB } from "../implements/keyvalue/manager";
4
+ import { metadata } from "./decorator_factory";
5
+ import { BaseEntity } from "./base_entity";
6
+
7
+ type Ctor<T> = new (...args: any[]) => T;
8
+
9
+ const EXTRA_SEP = "$$$dckv_extra$$$";
10
+
11
+ function extractRawValue(s: string): [string, string] {
12
+ const i = s.indexOf(EXTRA_SEP);
13
+ return i >= 0 ? [s.slice(0, i), s.slice(i + EXTRA_SEP.length)] : [s, ""];
14
+ }
15
+
16
+ function safeParseJSON<T>(s: string | null): T | null {
17
+ if (!s) return null;
18
+ try { return JSON.parse(s) as T; } catch { return null; }
19
+ }
20
+
21
+
22
+
23
+
24
+ // 兼容 getWithIndex 返回的两种格式(string[] 或 Record<string,string>[])
25
+ function parseIndexResultValues(raw: string): string[] {
26
+ const parsed = safeParseJSON<any>(raw);
27
+ if (!Array.isArray(parsed)) return [];
28
+ const out: string[] = [];
29
+ for (const item of parsed) {
30
+ if (typeof item === "string") {
31
+ out.push(item);
32
+ } else if (item && typeof item === "object") {
33
+ for (const v of Object.values(item)) {
34
+ if (v != null) out.push(String(v));
35
+ }
36
+ }
37
+ }
38
+ return out;
39
+ }
40
+
41
+ function maybeStripKeyPrefix(s: string): string {
42
+ const t = s.trimStart();
43
+ const i = t.indexOf(":");
44
+ if (i > 0 && i < 64 && t[0] !== "{" && t[0] !== "[" && t[0] !== "\"") {
45
+ return t.slice(i + 1).trim();
46
+ }
47
+ return s;
48
+ }
49
+
50
+
51
+
52
+ // 将列声明类型与实际值映射到 KV 可识别的索引类型和值
53
+ function asIndexTypeAndValue(value: unknown, declaredType?: string): { type: string; value: string } {
54
+ const t = declaredType ?? (value instanceof Date ? "date" : typeof value);
55
+ switch (t) {
56
+ case "date":
57
+ return { type: "number", value: String(value instanceof Date ? value.getTime() : Number(value)) };
58
+ case "boolean":
59
+ return { type: "number", value: String(value ? 1 : 0) };
60
+ case "number":
61
+ return { type: "number", value: String(value ?? 0) };
62
+ case "json":
63
+ return { type: "json", value: JSON.stringify(value) };
64
+ default:
65
+ return { type: "string", value: String(value ?? "") };
66
+ }
67
+ }
68
+
69
+ function buildIndexPayload(entity: object, ctor: Function): string {
70
+ const cols = metadata.getColumns(ctor);
71
+ const colTypeByName = new Map<string, string>();
72
+ cols.forEach(c => {
73
+ if (c.options?.type) colTypeByName.set(c.resolvedName, c.options.type);
74
+ });
75
+
76
+ const idxMetas = metadata.getIndexes(ctor);
77
+ const items: Array<{ key: string; type: string; value: string }> = [];
78
+
79
+ for (const idx of idxMetas) {
80
+ if (!idx.fields.length) continue;
81
+
82
+ if (idx.fields.length === 1) {
83
+ const [first] = idx.fields;
84
+ if (!first) continue;
85
+ const f = first.field;
86
+ const raw = (entity as any)[f];
87
+ const { type, value } = asIndexTypeAndValue(raw, colTypeByName.get(f));
88
+ // 单列索引用字段名作为索引键
89
+ items.push({ key: f, type, value });
90
+ } else {
91
+ // 复合索引:索引名作为 key,值为字段值数组的 JSON,类型 json
92
+ const key = idx.name;
93
+ const tuple = idx.fields.map(ff => (entity as any)[ff.field]);
94
+ items.push({ key, type: "json", value: JSON.stringify(tuple) });
95
+ }
96
+ }
97
+
98
+ return JSON.stringify(items);
99
+ }
100
+
101
+ export function composeCompositeIndexValue(values: unknown[]): string {
102
+ // 用于复合索引查询:indexValue 需为 JSON 数组字符串
103
+ return JSON.stringify(values);
104
+ }
105
+
106
+ export type FindIndexOptions = {
107
+ type?: "string" | "number" | "boolean" | "date" | "json" | "binary";
108
+ limit?: number;
109
+ seekKey?: string;
110
+ direction?: Direction;
111
+ offset?: number;
112
+ vaccount?: string;
113
+ };
114
+
115
+ export type FindValuesOptions = {
116
+ limit?: number;
117
+ seekKey?: string;
118
+ direction?: Direction;
119
+ offset?: number;
120
+ vaccount?: string;
121
+ };
122
+
123
+ function sanitizeGetWithIndexOptions(options: FindIndexOptions): { type?: string; limit?: number; seekKey?: string; direction?: Direction; offset?: number } {
124
+ return {
125
+ ...(options.type !== undefined ? { type: options.type } : {}),
126
+ ...(options.limit !== undefined ? { limit: options.limit } : {}),
127
+ ...(options.seekKey !== undefined ? { seekKey: options.seekKey } : {}),
128
+ ...(options.direction !== undefined ? { direction: options.direction } : {}),
129
+ ...(options.offset !== undefined ? { offset: options.offset } : {}),
130
+ };
131
+ }
132
+
133
+ function sanitizeGetValuesOptions(options: FindValuesOptions): { limit?: number; seekKey?: string; direction?: Direction; offset?: number } {
134
+ return {
135
+ ...(options.limit !== undefined ? { limit: options.limit } : {}),
136
+ ...(options.seekKey !== undefined ? { seekKey: options.seekKey } : {}),
137
+ ...(options.direction !== undefined ? { direction: options.direction } : {}),
138
+ ...(options.offset !== undefined ? { offset: options.offset } : {}),
139
+ };
140
+ }
141
+
142
+
143
+
144
+ // 通用仓储:承载所有持久化与查询逻辑
145
+ export class EntityRepository<T extends BaseEntity> {
146
+ constructor(
147
+ private readonly entityCtor: Ctor<T>,
148
+ private readonly ops: IKeyValueOperations,
149
+ private readonly db: KeyValueDB
150
+ ) {}
151
+
152
+ // 保存实体(写入并附带索引)
153
+ async save(entity: T, vaccount?: string): Promise<number> {
154
+ entity.validate();
155
+ const key= (entity as any).getPrimaryKey();
156
+ //移除dc_timestamp和dc_opuser字段,由dc节点自动维护
157
+ delete (entity as any).dc_timestamp;
158
+ delete (entity as any).dc_opuser;
159
+ const value = JSON.stringify(entity.toJSON());
160
+ const indexs = buildIndexPayload(entity, this.entityCtor);
161
+
162
+ const [ok,resTimestamp, err] = await this.ops.set(this.db, key, value, indexs, vaccount);
163
+ if (err) throw err;
164
+ if (!ok) throw new Error(`${this.entityCtor.name}.save failed`);
165
+ entity.dc_timestamp = resTimestamp ? resTimestamp : 0; // 设置时间戳
166
+ return entity.dc_timestamp;
167
+ }
168
+
169
+
170
+ async deleteById(id: string, vaccount?: string): Promise<void> {
171
+ const key = id;
172
+ const [ok,_ ,err] = await this.ops.set(this.db, key, "","", vaccount); // 空值用于标记删除
173
+ if (err) throw err;
174
+ if (!ok) throw new Error(`${this.entityCtor.name}.deleteById failed`);
175
+ }
176
+
177
+ // 局部更新(按 id 合并字段后保存)
178
+ async update(id: string, patch: Partial<T>, vaccount?: string): Promise<T | null> {
179
+ const key = id;
180
+ const [curr, err] = await this.ops.getValueSetByCurrentUser(this.db, key, vaccount);
181
+ if (err || !curr) return null;
182
+ const [json, extra] = extractRawValue(curr);
183
+ const obj = safeParseJSON<any>(json);
184
+ if (!obj) return null;
185
+ if (extra) {
186
+ const extraObj = safeParseJSON<any>(extra);
187
+ if (extraObj) Object.assign(obj, extraObj);
188
+ }
189
+ const inst = new this.entityCtor();
190
+ Object.assign(inst, obj);
191
+ Object.assign(inst as any, patch);
192
+ const resTimestamp = await this.save(inst, vaccount);
193
+ inst.dc_timestamp = resTimestamp;
194
+ return inst;
195
+ }
196
+
197
+
198
+
199
+ // 通过 id 获取
200
+ async findById(id: string, writerPubkey?: string, vaccount?: string): Promise<T | null> {
201
+ const key = id;
202
+ const [raw, err] = await this.ops.get(this.db, key, writerPubkey, vaccount);
203
+ if (err || !raw) return null;
204
+
205
+ const [json, extra] = extractRawValue(raw);
206
+ const obj = safeParseJSON<any>(json);
207
+ if (!obj) return null;
208
+ if (extra) {
209
+ const extraObj = safeParseJSON<any>(extra);
210
+ if (extraObj) Object.assign(obj, extraObj);
211
+ }
212
+
213
+
214
+ const inst = new this.entityCtor();
215
+ Object.assign(inst, obj);
216
+ return inst;
217
+ }
218
+
219
+
220
+
221
+
222
+ // 通过索引查询(单字段:indexKey=字段名;复合:indexKey=索引名,indexValue=JSON数组字符串)
223
+ async findByIndex(indexKey: string, indexValue: string, options: FindIndexOptions = {}): Promise<T[]> {
224
+ const [raw, err] = await this.ops.getWithIndex(
225
+ this.db,
226
+ indexKey,
227
+ indexValue,
228
+ sanitizeGetWithIndexOptions(options),
229
+ options.vaccount
230
+ );
231
+ if (err || !raw) return [];
232
+
233
+ const values = parseIndexResultValues(raw);
234
+ const out: T[] = [];
235
+ for (let val of values) {
236
+ // 兼容老格式 "key:value";新格式是纯 value,不会命中
237
+ val = maybeStripKeyPrefix(val);
238
+
239
+ const [json, extra] = extractRawValue(val);
240
+ const obj = safeParseJSON<any>(json);
241
+ if (!obj) continue;
242
+ if (extra) {
243
+ const extraObj = safeParseJSON<any>(extra);
244
+ if (extraObj) Object.assign(obj, extraObj);
245
+ }
246
+ const inst = new this.entityCtor();
247
+ Object.assign(inst, obj);
248
+ out.push(inst);
249
+ }
250
+ return out;
251
+ }
252
+
253
+ async findOneByIndex(indexKey: string, indexValue: string, options: Omit<FindIndexOptions, "limit"> = {}): Promise<T | null> {
254
+ const list = await this.findByIndex(indexKey, indexValue, { ...options, limit: 1 });
255
+ return list[0] ?? null;
256
+ }
257
+
258
+ // 按键前缀遍历(用于分页场景)
259
+ async getValues(keyPrefix: string, options: FindValuesOptions = {}): Promise<T[]> {
260
+ const [raw, err] = await this.ops.getValues(
261
+ this.db,
262
+ keyPrefix,
263
+ sanitizeGetValuesOptions(options),
264
+ options.vaccount
265
+ );
266
+ if (err || !raw) return [];
267
+
268
+ const list = safeParseJSON<string[]>(raw) ?? [];
269
+ const out: T[] = [];
270
+ for (const item of list) {
271
+ const [v,extra] = extractRawValue(String(item));
272
+ const obj = safeParseJSON<any>(v);
273
+ if (!obj) continue;
274
+ if (extra) {
275
+ const extraObj = safeParseJSON<any>(extra);
276
+ if (extraObj) Object.assign(obj, extraObj);
277
+ }
278
+ const inst = new this.entityCtor();
279
+ Object.assign(inst, obj);
280
+ out.push(inst);
281
+ }
282
+ return out;
283
+ }
284
+ }
@@ -0,0 +1,282 @@
1
+ import type { NodePath, Visitor } from "@babel/traverse";
2
+ import type * as T from "@babel/types";
3
+ import { loadBabel } from "./babel-browser";
4
+
5
+ export type PrintableSchema = {
6
+ entityName: string;
7
+ namespace?: string;
8
+ ttlSeconds?: number;
9
+ versioned?: boolean;
10
+ columns: Array<{
11
+ name: string;
12
+ type?: string;
13
+ required?: boolean;
14
+ unique?: boolean;
15
+ index?: boolean;
16
+ default?: unknown;
17
+ }>;
18
+ indexes: Array<{
19
+ name: string;
20
+ fields: Array<{ field: string; order: "asc" | "desc" }>;
21
+ unique?: boolean;
22
+ ttlSeconds?: number;
23
+ }>;
24
+ };
25
+
26
+ interface FileContentMap {
27
+ [fileName: string]: string;
28
+ }
29
+
30
+ export async function extractSchemasFromSources(sources: FileContentMap): Promise<PrintableSchema[]> {
31
+ const { parse, t, traverse } = await loadBabel();
32
+ const result: PrintableSchema[] = [];
33
+
34
+ // 解析源码(优先标准装饰器,失败回退 legacy)
35
+ function tryParse(code: string) {
36
+ const common = {
37
+ sourceType: "module" as const,
38
+ errorRecovery: true,
39
+ plugins: ["typescript"] as any[],
40
+ };
41
+ try {
42
+ return parse(code, {
43
+ ...common,
44
+ plugins: [
45
+ ["decorators", { decoratorsBeforeExport: true }],
46
+ "classProperties",
47
+ "classPrivateProperties",
48
+ ...common.plugins,
49
+ ],
50
+ } as any);
51
+ } catch {
52
+ return parse(code, {
53
+ ...common,
54
+ plugins: [
55
+ "decorators-legacy",
56
+ "classProperties",
57
+ "classPrivateProperties",
58
+ ...common.plugins,
59
+ ],
60
+ } as any);
61
+ }
62
+ }
63
+
64
+ // 将装饰器参数表达式转 JS 值(常见字面量/对象/数组)
65
+ function exprToValue(node: any): any {
66
+ if (!node) return undefined;
67
+ if (t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node)) return (node as any).value;
68
+ if (t.isNullLiteral(node)) return null;
69
+ if (t.isIdentifier(node) && (node.name === "undefined")) return undefined;
70
+ if (t.isObjectExpression(node)) {
71
+ const obj: any = {};
72
+ for (const p of node.properties) {
73
+ if (t.isObjectProperty(p)) {
74
+ const key = t.isIdentifier(p.key) ? p.key.name : t.isStringLiteral(p.key) ? p.key.value : undefined;
75
+ if (!key) continue;
76
+ obj[key] = exprToValue(p.value as any);
77
+ }
78
+ }
79
+ return obj;
80
+ }
81
+ if (t.isArrayExpression(node)) return node.elements.map((e: any) => exprToValue(e ));
82
+ return undefined;
83
+ }
84
+
85
+ // 规范化索引字段
86
+ function normIndexFields(fields: any, fallbackField?: string): Array<{ field: string; order: "asc" | "desc" }> {
87
+ const out: Array<{ field: string; order: "asc" | "desc" }> = [];
88
+ const arr = Array.isArray(fields) ? fields : (fields ? [fields] : []);
89
+ if (arr.length === 0 && fallbackField) return [{ field: fallbackField, order: "asc" }];
90
+ for (const it of arr) {
91
+ if (typeof it === "string") out.push({ field: it, order: "asc" });
92
+ else if (it && typeof it === "object" && typeof it.field === "string") {
93
+ out.push({ field: it.field, order: it.order === "desc" ? "desc" : "asc" });
94
+ }
95
+ }
96
+ return out;
97
+ }
98
+
99
+ // 默认索引名
100
+ function defaultIndexName(fields: Array<{ field: string; order: "asc" | "desc" }>) {
101
+ const seg = fields.map(f => f.field + (f.order === "desc" ? "_desc" : "")).join("_");
102
+ return `idx_${seg}`;
103
+ }
104
+
105
+ // 去重 key
106
+ function indexKey(name: string | undefined, fields: Array<{ field: string; order: "asc" | "desc" }>) {
107
+ const seg = fields.map(f => `${f.field}:${f.order}`).join(",");
108
+ return `${name ?? defaultIndexName(fields)}|${seg}`;
109
+ }
110
+
111
+ // 装饰器名/参数
112
+ function getDecoratorName(dec: any): string | undefined {
113
+ const ex = dec.expression;
114
+ if (t.isIdentifier(ex)) return ex.name;
115
+ if (t.isCallExpression(ex) && t.isIdentifier(ex.callee)) return ex.callee.name;
116
+ return undefined;
117
+ }
118
+ function getDecoratorArgs(dec: any): any[] {
119
+ const ex = dec.expression;
120
+ if (t.isCallExpression(ex)) return ex.arguments.map((arg: any) => exprToValue(arg as any));
121
+ return [];
122
+ }
123
+
124
+ // 统一处理类(ClassDeclaration / ClassExpression)
125
+ const visitClass = (path: NodePath<T.ClassDeclaration | T.ClassExpression>) => {
126
+ const node = path.node as any;
127
+ const className = (node.id && node.id.name) || "AnonymousClass";
128
+ const decorators: any[] = node.decorators ?? [];
129
+
130
+ // 解析 @Entity
131
+ let entityName = className;
132
+ let entityOpts: any = {};
133
+ for (const d of decorators) {
134
+ const name = getDecoratorName(d);
135
+ if (name === "Entity") {
136
+ const [opt] = getDecoratorArgs(d);
137
+ if (opt && typeof opt === "object") {
138
+ entityOpts = opt;
139
+ if (typeof opt.name === "string") entityName = opt.name;
140
+ }
141
+ }
142
+ }
143
+
144
+ // 字段与字段级装饰器
145
+ const columns: PrintableSchema["columns"] = [];
146
+ const fieldLevelIndexes: PrintableSchema["indexes"] = [];
147
+
148
+ for (const m of node.body.body as any[]) {
149
+ const isClassProp = (t as any).isClassProperty?.(m) || (t as any).isPropertyDefinition?.(m);
150
+ const isPrivateProp = (t as any).isClassPrivateProperty?.(m);
151
+ if (!isClassProp && !isPrivateProp) continue;
152
+
153
+ const keyNode = m.key;
154
+ const key =
155
+ t.isIdentifier(keyNode) ? keyNode.name :
156
+ t.isStringLiteral(keyNode) ? keyNode.value :
157
+ undefined;
158
+ if (!key) continue;
159
+
160
+ let columnOpts: any | undefined = undefined;
161
+ const explicitIndexFromDecorator: any[] = [];
162
+ const mDecos: any[] = m.decorators ?? [];
163
+
164
+ for (const d of mDecos) {
165
+ const dn = getDecoratorName(d);
166
+ const args = getDecoratorArgs(d);
167
+ if (dn === "Column") {
168
+ columnOpts = args[0] && typeof args[0] === "object" ? args[0] : {};
169
+ } else if (dn === "Index") {
170
+ explicitIndexFromDecorator.push(args);
171
+ }
172
+ }
173
+
174
+ const resolvedName = (columnOpts?.name as string) ?? key;
175
+ columns.push({
176
+ name: resolvedName,
177
+ ...(columnOpts?.type !== undefined ? { type: columnOpts.type } : {}),
178
+ ...(columnOpts?.required !== undefined ? { required: !!columnOpts.required } : {}),
179
+ ...(columnOpts?.unique !== undefined ? { unique: !!columnOpts.unique } : {}),
180
+ ...(columnOpts?.index !== undefined ? { index: !!columnOpts.index } : {}),
181
+ ...(columnOpts?.default !== undefined ? { default: columnOpts.default } : {}),
182
+ });
183
+
184
+ // Column 的 index/unique → 单列索引
185
+ if (columnOpts?.index || columnOpts?.unique) {
186
+ const fields = [{ field: resolvedName, order: "asc" as const }];
187
+ fieldLevelIndexes.push({
188
+ name: defaultIndexName(fields),
189
+ fields,
190
+ ...(columnOpts?.unique ? { unique: true } : {}),
191
+ });
192
+ }
193
+
194
+ // 字段级 @Index
195
+ for (const args of explicitIndexFromDecorator) {
196
+ let idxName: string | undefined;
197
+ let idxFields: any;
198
+ let idxUnique: boolean | undefined;
199
+ let idxTTL: number | undefined;
200
+ if (args.length === 0) {
201
+ idxFields = [resolvedName];
202
+ } else if (typeof args[0] === "string") {
203
+ idxName = args[0];
204
+ idxFields = args[1] ?? [resolvedName];
205
+ } else if (typeof args[0] === "object") {
206
+ idxName = args[0].name;
207
+ idxFields = args[0].fields ?? [resolvedName];
208
+ idxUnique = args[0].unique;
209
+ idxTTL = args[0].ttlSeconds;
210
+ }
211
+ const fields = normIndexFields(idxFields, resolvedName);
212
+ if (fields.length === 0) continue;
213
+ const name2 = idxName ?? defaultIndexName(fields);
214
+ fieldLevelIndexes.push({
215
+ name: name2,
216
+ fields,
217
+ ...(idxUnique !== undefined ? { unique: !!idxUnique } : {}),
218
+ ...(idxTTL !== undefined ? { ttlSeconds: idxTTL } : {}),
219
+ });
220
+ }
221
+ }
222
+
223
+ // 类级 @Index
224
+ const classLevelIndexes: PrintableSchema["indexes"] = [];
225
+ for (const d of decorators) {
226
+ const dn = getDecoratorName(d);
227
+ if (dn !== "Index") continue;
228
+ const args = getDecoratorArgs(d);
229
+ let idxName: string | undefined;
230
+ let idxFields: any;
231
+ let idxUnique: boolean | undefined;
232
+ let idxTTL: number | undefined;
233
+ if (args.length === 0) continue;
234
+ else if (typeof args[0] === "string") {
235
+ idxName = args[0]; idxFields = args[1] ?? [];
236
+ } else if (typeof args[0] === "object") {
237
+ idxName = args[0].name; idxFields = args[0].fields ?? [];
238
+ idxUnique = args[0].unique; idxTTL = args[0].ttlSeconds;
239
+ }
240
+ const fields = normIndexFields(idxFields);
241
+ if (fields.length === 0) continue;
242
+ const name2 = idxName ?? defaultIndexName(fields);
243
+ classLevelIndexes.push({
244
+ name: name2,
245
+ fields,
246
+ ...(idxUnique !== undefined ? { unique: !!idxUnique } : {}),
247
+ ...(idxTTL !== undefined ? { ttlSeconds: idxTTL } : {}),
248
+ });
249
+ }
250
+
251
+ // 合并去重
252
+ const allIndexes = [...fieldLevelIndexes, ...classLevelIndexes];
253
+ const dedup = new Map<string, PrintableSchema["indexes"][number]>();
254
+ for (const ix of allIndexes) {
255
+ const key = indexKey(ix.name, ix.fields);
256
+ if (!dedup.has(key)) dedup.set(key, ix);
257
+ }
258
+
259
+ const printable: PrintableSchema = {
260
+ entityName: (entityOpts?.name as string) ?? entityName,
261
+ ...(entityOpts?.namespace !== undefined ? { namespace: entityOpts.namespace } : {}),
262
+ ...(entityOpts?.ttlSeconds !== undefined ? { ttlSeconds: entityOpts.ttlSeconds } : {}),
263
+ ...(entityOpts?.versioned !== undefined ? { versioned: entityOpts.versioned } : {}),
264
+ columns,
265
+ indexes: Array.from(dedup.values()),
266
+ };
267
+
268
+ result.push(printable);
269
+ };
270
+
271
+ // 遍历每个文件的 AST
272
+ for (const [, content] of Object.entries(sources)) {
273
+ const ast = tryParse(content);
274
+ const visitors: Visitor = {
275
+ ClassDeclaration(path: NodePath<T.ClassDeclaration>) { visitClass(path); },
276
+ ClassExpression(path: NodePath<T.ClassExpression>) { visitClass(path); },
277
+ };
278
+ traverse(ast as any, visitors);
279
+ }
280
+
281
+ return result;
282
+ }