ots-client 0.0.1
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/dist/adapters/type-adapter.d.ts +6 -0
- package/dist/client/base-client.d.ts +59 -0
- package/dist/client/index.d.ts +3 -0
- package/dist/client/query-builder.d.ts +23 -0
- package/dist/decorators/index.d.ts +56 -0
- package/dist/deps/acorn-BOl0sFTPW7-g7l5tn7d4.js +16 -0
- package/dist/deps/acorn-BOl0sFTPW7-hklkvjepo.js +3005 -0
- package/dist/deps/angular-XNXkkMW2x2-7du6op6fw.js +2533 -0
- package/dist/deps/angular-XNXkkMW2x2-we6wvxttc.js +3 -0
- package/dist/deps/babel-JmunAd9gx3-6dq643w5b.js +7217 -0
- package/dist/deps/babel-JmunAd9gx3-wao5c2v19.js +15 -0
- package/dist/deps/estree-QR0a7eAiZm-36ju20hxl.js +4528 -0
- package/dist/deps/estree-QR0a7eAiZm-t0kwe9gae.js +44 -0
- package/dist/deps/flow-GdDnVSvT5w-rgi5vp4qm.js +27411 -0
- package/dist/deps/flow-GdDnVSvT5w-rnla0vmdw.js +20 -0
- package/dist/deps/glimmer-Yu8RjFE1-a-5ivcwuhu1.js +37 -0
- package/dist/deps/glimmer-Yu8RjFE1-a-60xpuum9o.js +2703 -0
- package/dist/deps/graphql-b_914AArZY-uwrebifsb.js +28 -0
- package/dist/deps/graphql-b_914AArZY-yc77nhrwj.js +1221 -0
- package/dist/deps/html-6ymxY6PL6B-ge7hzyfqj.js +2791 -0
- package/dist/deps/html-6ymxY6PL6B-ug8ebf4sa.js +25 -0
- package/dist/deps/index-apktVSn0Wa-1pqh8j69b.js +7 -0
- package/dist/deps/index-apktVSn0Wa-m4m4s4pcy.js +24535 -0
- package/dist/deps/markdown-44cJkal_TC-rli9cqus2.js +3190 -0
- package/dist/deps/markdown-44cJkal_TC-t8vpmxjyl.js +62 -0
- package/dist/deps/meriyah-IwksJmaFJr-1kz9p5807.js +5 -0
- package/dist/deps/meriyah-IwksJmaFJr-aj8jxnzem.js +2645 -0
- package/dist/deps/postcss-gD1c7_GzEP-7h0ly93la.js +61 -0
- package/dist/deps/postcss-gD1c7_GzEP-kecpdijjx.js +4739 -0
- package/dist/deps/typescript-tYbKJGRdB5-3c3whaiss.js +12947 -0
- package/dist/deps/typescript-tYbKJGRdB5-4n3mbr7g0.js +21 -0
- package/dist/deps/yaml-LL7qhBUJFz-xb6k8do2g.js +4079 -0
- package/dist/deps/yaml-LL7qhBUJFz-zl5hfpq31.js +159 -0
- package/dist/generate.cjs +42349 -0
- package/dist/generate.d.ts +1 -0
- package/dist/generate.js +241530 -0
- package/dist/generator/index.d.ts +6 -0
- package/dist/generator/parser.d.ts +20 -0
- package/dist/generator/table-generator.d.ts +3 -0
- package/dist/generator/type-generator.d.ts +6 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +726 -0
- package/dist/init/index.d.ts +3 -0
- package/dist/types/client.d.ts +35 -0
- package/dist/types/index.d.ts +98 -0
- package/dist/utils/index.d.ts +31 -0
- package/package.json +47 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
var F = Object.defineProperty;
|
|
2
|
+
var U = (s, t, e) => t in s ? F(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
|
|
3
|
+
var E = (s, t, e) => U(s, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { T as y, t as f, a as O, f as D, v as _, b as q, c as Y } from "./deps/index-apktVSn0Wa-m4m4s4pcy.js";
|
|
5
|
+
import { V as Oe } from "./deps/index-apktVSn0Wa-m4m4s4pcy.js";
|
|
6
|
+
const N = "ots:table", k = "ots:primaryKey", M = "ots:partitionKey", z = "ots:field", B = "ots:createdAt", G = "ots:updatedAt";
|
|
7
|
+
function ae(s) {
|
|
8
|
+
return (t) => {
|
|
9
|
+
var e;
|
|
10
|
+
(e = Reflect.defineMetadata) == null || e.call(Reflect, N, s, t);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function oe(s) {
|
|
14
|
+
return (t, e) => {
|
|
15
|
+
var r;
|
|
16
|
+
(r = Reflect.defineMetadata) == null || r.call(Reflect, k, s || {}, t, e);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function le(s) {
|
|
20
|
+
return (t, e) => {
|
|
21
|
+
var r;
|
|
22
|
+
(r = Reflect.defineMetadata) == null || r.call(Reflect, M, s || {}, t, e);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function ue(s) {
|
|
26
|
+
return (t, e) => {
|
|
27
|
+
var r;
|
|
28
|
+
(r = Reflect.defineMetadata) == null || r.call(Reflect, z, s, t, e);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function ye(s) {
|
|
32
|
+
return (t, e) => {
|
|
33
|
+
var r;
|
|
34
|
+
(r = Reflect.defineMetadata) == null || r.call(Reflect, B, !0, t, e);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function de(s) {
|
|
38
|
+
return (t, e) => {
|
|
39
|
+
var r;
|
|
40
|
+
(r = Reflect.defineMetadata) == null || r.call(Reflect, G, !0, t, e);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function Q(s) {
|
|
44
|
+
if (!s || typeof s != "object")
|
|
45
|
+
return !1;
|
|
46
|
+
const t = Object.getPrototypeOf(s);
|
|
47
|
+
return t === null || t === Object.prototype || Object.getPrototypeOf(t) === null ? Object.prototype.toString.call(s) === "[object Object]" : !1;
|
|
48
|
+
}
|
|
49
|
+
const x = {
|
|
50
|
+
eq: y.ComparatorType.EQUAL,
|
|
51
|
+
not: y.ComparatorType.NOT_EQUAL,
|
|
52
|
+
lt: y.ComparatorType.LESS_THAN,
|
|
53
|
+
lte: y.ComparatorType.LESS_EQUAL,
|
|
54
|
+
gt: y.ComparatorType.GREATER_THAN,
|
|
55
|
+
gte: y.ComparatorType.GREATER_EQUAL
|
|
56
|
+
};
|
|
57
|
+
function V(s, t, e) {
|
|
58
|
+
var r, n;
|
|
59
|
+
if (Q(t)) {
|
|
60
|
+
const i = Object.entries(t);
|
|
61
|
+
if (i.length === 1) {
|
|
62
|
+
const [c, a] = i[0];
|
|
63
|
+
return new f.SingleColumnCondition(s, O.toOTS(a, (r = e == null ? void 0 : e[s]) == null ? void 0 : r.type), x[c]);
|
|
64
|
+
} else if (i.length > 1) {
|
|
65
|
+
const c = new y.CompositeCondition(y.LogicalOperator.AND);
|
|
66
|
+
return i.forEach(([a, o]) => {
|
|
67
|
+
var l;
|
|
68
|
+
c.addSubCondition(
|
|
69
|
+
new y.SingleColumnCondition(s, O.toOTS(o, (l = e == null ? void 0 : e[s]) == null ? void 0 : l.type), x[a])
|
|
70
|
+
);
|
|
71
|
+
}), c;
|
|
72
|
+
}
|
|
73
|
+
} else
|
|
74
|
+
return new f.SingleColumnCondition(s, O.toOTS(t, (n = e == null ? void 0 : e[s]) == null ? void 0 : n.type), x.eq);
|
|
75
|
+
}
|
|
76
|
+
function K(s, t, e) {
|
|
77
|
+
if (s === "NOT") {
|
|
78
|
+
const r = R(t, e);
|
|
79
|
+
if (r) {
|
|
80
|
+
const n = new y.CompositeCondition(y.LogicalOperator.NOT);
|
|
81
|
+
return n.addSubCondition(r), n;
|
|
82
|
+
}
|
|
83
|
+
} else if (s === "AND") {
|
|
84
|
+
if (Array.isArray(t)) {
|
|
85
|
+
if (t.length === 1)
|
|
86
|
+
return R(t[0], e);
|
|
87
|
+
if (t.length > 1) {
|
|
88
|
+
const r = t.map((n) => R(n, e)).filter((n) => !!n);
|
|
89
|
+
if (r.length === 1)
|
|
90
|
+
return r[0];
|
|
91
|
+
if (r.length > 1) {
|
|
92
|
+
const n = new y.CompositeCondition(y.LogicalOperator.AND);
|
|
93
|
+
return r.forEach((i) => n.addSubCondition(i)), n;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
} else if (s === "OR") {
|
|
98
|
+
if (Array.isArray(t)) {
|
|
99
|
+
if (t.length === 1)
|
|
100
|
+
return R(t[0], e);
|
|
101
|
+
if (t.length > 1) {
|
|
102
|
+
const r = t.map((n) => R(n, e)).filter((n) => !!n);
|
|
103
|
+
if (r.length === 1)
|
|
104
|
+
return r[0];
|
|
105
|
+
if (r.length > 1) {
|
|
106
|
+
const n = new y.CompositeCondition(y.LogicalOperator.OR);
|
|
107
|
+
return r.forEach((i) => n.addSubCondition(i)), n;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} else
|
|
112
|
+
return V(s, t, e);
|
|
113
|
+
}
|
|
114
|
+
function R(s, t) {
|
|
115
|
+
const e = Object.entries(s);
|
|
116
|
+
if (e.length === 1) {
|
|
117
|
+
const [r, n] = e[0];
|
|
118
|
+
return K(r, n, t);
|
|
119
|
+
} else if (e.length > 1) {
|
|
120
|
+
const r = e.map(([n, i]) => K(n, i, t)).filter((n) => !!n);
|
|
121
|
+
if (r.length === 1)
|
|
122
|
+
return r[0];
|
|
123
|
+
if (r.length > 1) {
|
|
124
|
+
const n = new y.CompositeCondition(y.LogicalOperator.AND);
|
|
125
|
+
return r.forEach((i) => n.addSubCondition(i)), n;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const $ = {
|
|
130
|
+
eq: [y.QueryType.TERM_QUERY, "term"],
|
|
131
|
+
in: [y.QueryType.TERMS_QUERY, "terms"],
|
|
132
|
+
lt: [y.QueryType.RANGE_QUERY, "rangeTo", { includeUpper: !1 }],
|
|
133
|
+
lte: [y.QueryType.RANGE_QUERY, "rangeTo", { includeUpper: !0 }],
|
|
134
|
+
gt: [y.QueryType.RANGE_QUERY, "rangeFrom", { includeLower: !1 }],
|
|
135
|
+
gte: [y.QueryType.RANGE_QUERY, "rangeFrom", { includeLower: !0 }],
|
|
136
|
+
contains: [y.QueryType.WILDCARD_QUERY, "value", {}, (s) => `*${s}*`],
|
|
137
|
+
startsWith: [y.QueryType.WILDCARD_QUERY, "value", {}, (s) => `${s}*`],
|
|
138
|
+
endsWith: [y.QueryType.WILDCARD_QUERY, "value", {}, (s) => `*${s}`]
|
|
139
|
+
};
|
|
140
|
+
function W(s, t, e) {
|
|
141
|
+
var r;
|
|
142
|
+
if (Q(t)) {
|
|
143
|
+
const n = Object.entries(t);
|
|
144
|
+
return {
|
|
145
|
+
queryType: y.QueryType.BOOL_QUERY,
|
|
146
|
+
mustQueries: n.map(([i, c]) => {
|
|
147
|
+
var h;
|
|
148
|
+
const [a, o, l, d] = $[i] ?? [], u = d ? d(c) : c;
|
|
149
|
+
return {
|
|
150
|
+
queryType: a,
|
|
151
|
+
[o]: O.toOTS(u, (h = e == null ? void 0 : e[s]) == null ? void 0 : h.type),
|
|
152
|
+
...l ?? {}
|
|
153
|
+
};
|
|
154
|
+
})
|
|
155
|
+
};
|
|
156
|
+
} else
|
|
157
|
+
return {
|
|
158
|
+
queryType: y.QueryType.TERM_QUERY,
|
|
159
|
+
query: {
|
|
160
|
+
fieldName: s,
|
|
161
|
+
term: O.toOTS(t, (r = e == null ? void 0 : e[s]) == null ? void 0 : r.type)
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function H(s, t, e) {
|
|
166
|
+
if (s === "NOT") {
|
|
167
|
+
const r = S(t, e);
|
|
168
|
+
return {
|
|
169
|
+
queryType: y.QueryType.BOOL_QUERY,
|
|
170
|
+
mustNotQueries: [r]
|
|
171
|
+
};
|
|
172
|
+
} else return s === "AND" ? Array.isArray(t) ? {
|
|
173
|
+
queryType: y.QueryType.BOOL_QUERY,
|
|
174
|
+
mustQueries: t.map((r) => S(r, e))
|
|
175
|
+
} : S(t, e) : s === "OR" ? Array.isArray(t) ? {
|
|
176
|
+
queryType: y.QueryType.BOOL_QUERY,
|
|
177
|
+
shouldQueries: t.map((r) => S(r, e)),
|
|
178
|
+
minimumShouldMatch: 1
|
|
179
|
+
} : S(t, e) : W(s, t, e);
|
|
180
|
+
}
|
|
181
|
+
function S(s, t) {
|
|
182
|
+
const e = Object.entries(s);
|
|
183
|
+
return {
|
|
184
|
+
queryType: y.QueryType.BOOL_QUERY,
|
|
185
|
+
query: {
|
|
186
|
+
mustQueries: e.map(([r, n]) => H(r, n, t))
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
const J = {
|
|
191
|
+
asc: y.SortOrder.SORT_ORDER_ASC,
|
|
192
|
+
desc: y.SortOrder.SORT_ORDER_DESC
|
|
193
|
+
};
|
|
194
|
+
class g {
|
|
195
|
+
static buildColumnCondition(t, e) {
|
|
196
|
+
return R(t ?? {}, e);
|
|
197
|
+
}
|
|
198
|
+
static buildSearchQuery(t, e) {
|
|
199
|
+
return !t || Object.keys(t).length === 0 ? {
|
|
200
|
+
queryType: y.QueryType.MATCH_ALL_QUERY
|
|
201
|
+
} : S(t, e);
|
|
202
|
+
}
|
|
203
|
+
static buildSearchSort(t) {
|
|
204
|
+
const e = Object.entries(t ?? {});
|
|
205
|
+
if (e.length > 0)
|
|
206
|
+
return { sorters: e.map(([n, i]) => ({
|
|
207
|
+
fieldSort: { fieldName: n, order: J[i == null ? void 0 : i.toLowerCase()] }
|
|
208
|
+
})) };
|
|
209
|
+
}
|
|
210
|
+
// ==================== 实时查询辅助函数 ====================
|
|
211
|
+
/**
|
|
212
|
+
* 条件匹配函数 - 用于内存中过滤数据
|
|
213
|
+
*/
|
|
214
|
+
static matchCondition(t, e) {
|
|
215
|
+
return !e || Object.keys(e).length === 0 ? !0 : Object.entries(e).every(([r, n]) => this.matchField(t, r, n));
|
|
216
|
+
}
|
|
217
|
+
static matchField(t, e, r) {
|
|
218
|
+
if (e === "NOT") return !this.matchCondition(t, r);
|
|
219
|
+
if (e === "AND") return Array.isArray(r) ? r.every((i) => this.matchCondition(t, i)) : this.matchCondition(t, r);
|
|
220
|
+
if (e === "OR") return Array.isArray(r) ? r.some((i) => this.matchCondition(t, i)) : this.matchCondition(t, r);
|
|
221
|
+
const n = t[e];
|
|
222
|
+
return typeof r == "object" && r !== null ? Object.entries(r).every(([i, c]) => this.compareValue(n, i, c)) : this.compareValue(n, "eq", r);
|
|
223
|
+
}
|
|
224
|
+
static compareValue(t, e, r) {
|
|
225
|
+
switch (e) {
|
|
226
|
+
case "eq":
|
|
227
|
+
return t === r;
|
|
228
|
+
case "not":
|
|
229
|
+
return t !== r;
|
|
230
|
+
case "lt":
|
|
231
|
+
return t < r;
|
|
232
|
+
case "lte":
|
|
233
|
+
return t <= r;
|
|
234
|
+
case "gt":
|
|
235
|
+
return t > r;
|
|
236
|
+
case "gte":
|
|
237
|
+
return t >= r;
|
|
238
|
+
case "in":
|
|
239
|
+
return Array.isArray(r) ? r.includes(t) : !1;
|
|
240
|
+
case "contains":
|
|
241
|
+
return typeof t == "string" && typeof r == "string" ? t.includes(r) : !1;
|
|
242
|
+
default:
|
|
243
|
+
return t === r;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
static extractSortValues(t, e) {
|
|
247
|
+
return !e || Object.keys(e).length === 0 ? [] : Object.keys(e).map((r) => t[r]);
|
|
248
|
+
}
|
|
249
|
+
static buildSortComparator(t) {
|
|
250
|
+
if (!t || Object.keys(t).length === 0) return () => 0;
|
|
251
|
+
const e = Object.keys(t);
|
|
252
|
+
return (r, n) => {
|
|
253
|
+
for (const i of e) {
|
|
254
|
+
const c = t[i], a = r[i], o = n[i];
|
|
255
|
+
if (a === o) continue;
|
|
256
|
+
let l = a == null ? 1 : o == null || a < o ? -1 : 1;
|
|
257
|
+
return c === "desc" ? -l : l;
|
|
258
|
+
}
|
|
259
|
+
return 0;
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
const b = class b {
|
|
264
|
+
constructor(t, e, r) {
|
|
265
|
+
E(this, "client");
|
|
266
|
+
E(this, "schemaMeta");
|
|
267
|
+
E(this, "context");
|
|
268
|
+
E(this, "fieldMetaMap", {});
|
|
269
|
+
E(this, "cacheTableInitialized", !1);
|
|
270
|
+
this.client = t, this.schemaMeta = e, this.context = r, this.fieldMetaMap = Object.entries(e).reduce((n, [i, c]) => (n[i] = c.columns.reduce((a, o) => (a[o.name] = o, a), {}), n), {});
|
|
271
|
+
}
|
|
272
|
+
getTableMeta(t) {
|
|
273
|
+
const e = this.schemaMeta[t];
|
|
274
|
+
if (!e) throw new Error(`Table '${t}' not found in schema metadata`);
|
|
275
|
+
return e;
|
|
276
|
+
}
|
|
277
|
+
fromOTS(t, e) {
|
|
278
|
+
if (!t) return null;
|
|
279
|
+
const r = {}, n = this.fieldMetaMap[e.tableName];
|
|
280
|
+
return Object.keys(t).forEach((i) => {
|
|
281
|
+
const c = t[i], a = n == null ? void 0 : n[i];
|
|
282
|
+
r[i] = a ? O.fromOTS(c, a.type) : c;
|
|
283
|
+
}), r;
|
|
284
|
+
}
|
|
285
|
+
recordToOTS(t, e) {
|
|
286
|
+
if (!t) return null;
|
|
287
|
+
const r = {}, n = this.fieldMetaMap[e.tableName];
|
|
288
|
+
return Object.keys(t).forEach((i) => {
|
|
289
|
+
const c = t[i], a = n == null ? void 0 : n[i];
|
|
290
|
+
r[i] = a ? O.toOTS(c, a.type) : c;
|
|
291
|
+
}), r;
|
|
292
|
+
}
|
|
293
|
+
transformRawData(t, e) {
|
|
294
|
+
var i, c;
|
|
295
|
+
const r = this.fieldMetaMap[t], n = {};
|
|
296
|
+
return (i = e == null ? void 0 : e.primaryKey) == null || i.forEach((a) => {
|
|
297
|
+
const o = r == null ? void 0 : r[a.name];
|
|
298
|
+
n[a.name] = o ? O.fromOTS(a.value, o.type) : a.value;
|
|
299
|
+
}), (c = e == null ? void 0 : e.attributes) == null || c.forEach((a) => {
|
|
300
|
+
const o = r == null ? void 0 : r[a.columnName];
|
|
301
|
+
n[a.columnName] = o ? O.fromOTS(a.columnValue, o.type) : a.columnValue;
|
|
302
|
+
}), n;
|
|
303
|
+
}
|
|
304
|
+
splitPkObject(t, e) {
|
|
305
|
+
const r = {}, n = {}, i = e.primaryKeys;
|
|
306
|
+
return Object.keys(t).forEach((c) => {
|
|
307
|
+
i.includes(c) ? r[c] = t[c] : n[c] = t[c];
|
|
308
|
+
}), [r, n];
|
|
309
|
+
}
|
|
310
|
+
getSelect(t) {
|
|
311
|
+
const e = Object.entries(t ?? {}).reduce((r, [n, i]) => (i && r.push(n), r), []);
|
|
312
|
+
if (e.length > 0)
|
|
313
|
+
return e;
|
|
314
|
+
}
|
|
315
|
+
pkToOts(t, e) {
|
|
316
|
+
return e.primaryKeys.map((r) => ({ [r]: t[r] }));
|
|
317
|
+
}
|
|
318
|
+
async findUnique(t, { where: e, select: r }) {
|
|
319
|
+
const n = this.getTableMeta(t), i = this.fieldMetaMap[t], c = D(e ?? {}, n.partitionKeyColumn, this.context);
|
|
320
|
+
_(c, n.partitionKeyColumn);
|
|
321
|
+
const [a, o] = this.splitPkObject(c, n), l = this.pkToOts(this.recordToOTS(a, n), n), d = g.buildColumnCondition(o, i);
|
|
322
|
+
try {
|
|
323
|
+
const u = await this.client.getRow({
|
|
324
|
+
tableName: t,
|
|
325
|
+
primaryKey: l,
|
|
326
|
+
columnFilter: d,
|
|
327
|
+
columnsToGet: this.getSelect(r)
|
|
328
|
+
});
|
|
329
|
+
return u.row ? this.transformRawData(t, u.row) : null;
|
|
330
|
+
} catch (u) {
|
|
331
|
+
throw console.error("findUnique error:", u), u;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async findUniqueOrThrow(t, { where: e, select: r }) {
|
|
335
|
+
const n = await this.findUnique(t, { where: e, select: r });
|
|
336
|
+
if (!n)
|
|
337
|
+
throw new Error("Data Not Found");
|
|
338
|
+
return n;
|
|
339
|
+
}
|
|
340
|
+
async findFirst(t, e) {
|
|
341
|
+
const { data: r } = await this.findManyFragment(t, { where: e == null ? void 0 : e.where, select: e == null ? void 0 : e.select, limit: 1 });
|
|
342
|
+
return r.length > 0 ? r[0] : null;
|
|
343
|
+
}
|
|
344
|
+
async findFirstOrThrow(t, e) {
|
|
345
|
+
const r = await this.findFirst(t, e);
|
|
346
|
+
if (!r)
|
|
347
|
+
throw new Error("Data Not Found");
|
|
348
|
+
return r;
|
|
349
|
+
}
|
|
350
|
+
// 由于表格存储限制最大limit为100,因此当第一次查询发现limit大于100且总条数超过时,需要分片查询最终组装结果
|
|
351
|
+
// !!!WARNING!!! 可能会有极小概率出现查询脏数据问题,谨慎使用
|
|
352
|
+
async findManyFragment(t, e) {
|
|
353
|
+
const r = this.getTableMeta(t), n = this.fieldMetaMap[t], i = g.buildSearchQuery(e == null ? void 0 : e.where, n), c = g.buildSearchSort(e == null ? void 0 : e.orderBy), a = this.getSelect(e == null ? void 0 : e.select);
|
|
354
|
+
try {
|
|
355
|
+
const o = await this.client.search({
|
|
356
|
+
tableName: t,
|
|
357
|
+
indexName: `${t}_index`,
|
|
358
|
+
// 表格存储限制最大limit为100
|
|
359
|
+
searchQuery: {
|
|
360
|
+
offset: (e == null ? void 0 : e.offset) ?? 0,
|
|
361
|
+
limit: (e == null ? void 0 : e.limit) ?? 100,
|
|
362
|
+
getTotalCount: !0,
|
|
363
|
+
query: i,
|
|
364
|
+
// @ts-ignore
|
|
365
|
+
sort: c
|
|
366
|
+
},
|
|
367
|
+
columnToGet: {
|
|
368
|
+
returnType: f.ColumnReturnType.RETURN_SPECIFIED,
|
|
369
|
+
returnNames: a ?? r.columnKeys
|
|
370
|
+
}
|
|
371
|
+
}), l = ((o == null ? void 0 : o.rows) ?? []).map((d) => this.transformRawData(t, d));
|
|
372
|
+
return { total: Number((o == null ? void 0 : o.totalCounts) ?? 0), data: l };
|
|
373
|
+
} catch (o) {
|
|
374
|
+
throw console.error("findManyFragment error:", o), o;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
async findManyCombination(t, e) {
|
|
378
|
+
const i = (e == null ? void 0 : e.limit) ?? 99e3;
|
|
379
|
+
if (i > 99e3)
|
|
380
|
+
throw new Error("limit can not oversize 99000");
|
|
381
|
+
let c = (e == null ? void 0 : e.offset) ?? 0, a = Math.min(i, 100), o = 0;
|
|
382
|
+
const l = [];
|
|
383
|
+
try {
|
|
384
|
+
for (; ; ) {
|
|
385
|
+
const { total: d, data: u } = await this.findManyFragment(t, {
|
|
386
|
+
offset: c,
|
|
387
|
+
limit: a,
|
|
388
|
+
select: e == null ? void 0 : e.select,
|
|
389
|
+
where: e == null ? void 0 : e.where,
|
|
390
|
+
orderBy: e == null ? void 0 : e.orderBy
|
|
391
|
+
});
|
|
392
|
+
l.push(...u), o += a;
|
|
393
|
+
const h = i - o;
|
|
394
|
+
if (h <= 0 || d <= o)
|
|
395
|
+
return { total: d, data: l };
|
|
396
|
+
a = Math.min(h, a), c = o;
|
|
397
|
+
}
|
|
398
|
+
} catch (d) {
|
|
399
|
+
throw console.error("findManyCombination error:", d), d;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
async findMany(t, e) {
|
|
403
|
+
try {
|
|
404
|
+
const { data: r } = await this.findManyCombination(t, e);
|
|
405
|
+
return r;
|
|
406
|
+
} catch (r) {
|
|
407
|
+
throw console.error("findMany error:", r), r;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async findPage(t, e) {
|
|
411
|
+
const n = (e == null ? void 0 : e.limit) || 10;
|
|
412
|
+
if (n > 100)
|
|
413
|
+
throw new Error("limit must be less than or equal to 100");
|
|
414
|
+
const i = e == null ? void 0 : e.where, c = e == null ? void 0 : e.orderBy, a = (e == null ? void 0 : e.page) || 1, o = e == null ? void 0 : e.select, l = (a - 1) * n, { total: d, data: u } = await this.findManyCombination(t, { where: i, orderBy: c, limit: n, offset: l, select: o }), h = Math.ceil(d / n);
|
|
415
|
+
return { data: u, total: d, page: a, pageCount: h, limit: n, hasPrev: a > 1, hasNext: a < h };
|
|
416
|
+
}
|
|
417
|
+
async create(t, { data: e }) {
|
|
418
|
+
var u;
|
|
419
|
+
const r = this.getTableMeta(t), n = this.fieldMetaMap[t];
|
|
420
|
+
let i = D(e ?? {}, r.partitionKeyColumn, this.context);
|
|
421
|
+
i = q(i, r, this.context), _(i, r.partitionKeyColumn);
|
|
422
|
+
const [c, a] = this.splitPkObject(i, r), o = this.pkToOts(this.recordToOTS(c, r), r), l = [];
|
|
423
|
+
Object.entries(a ?? {}).forEach(([h, w]) => {
|
|
424
|
+
var T;
|
|
425
|
+
const m = (T = n[h]) == null ? void 0 : T.type;
|
|
426
|
+
l.push({ [h]: O.toOTS(w, m) });
|
|
427
|
+
});
|
|
428
|
+
const d = new f.Condition(f.RowExistenceExpectation.EXPECT_NOT_EXIST, null);
|
|
429
|
+
try {
|
|
430
|
+
await this.client.putRow({
|
|
431
|
+
tableName: t,
|
|
432
|
+
primaryKey: o,
|
|
433
|
+
condition: d,
|
|
434
|
+
attributeColumns: l
|
|
435
|
+
});
|
|
436
|
+
const h = JSON.stringify(c);
|
|
437
|
+
this.writeCache(t, h, i, !1).catch(() => {
|
|
438
|
+
});
|
|
439
|
+
} catch (h) {
|
|
440
|
+
throw (u = h == null ? void 0 : h.message) != null && u.includes("OTSConditionCheckFail") ? (console.error("create error: Record already exists"), Error("Record already exists")) : (console.error("create error:", h), h);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
async update(t, { where: e, data: r }) {
|
|
444
|
+
const n = this.getTableMeta(t), i = this.fieldMetaMap[t], c = [], a = [];
|
|
445
|
+
if (Object.entries(r ?? {}).forEach(([m, T]) => {
|
|
446
|
+
var p;
|
|
447
|
+
const C = (p = i[m]) == null ? void 0 : p.type;
|
|
448
|
+
C === "long" && (T != null && T.$increment) ? a.push({ [m]: O.toOTS(T.$increment, C) }) : c.push({ [m]: O.toOTS(T, C) });
|
|
449
|
+
}), c.length === 0 && a.length === 0)
|
|
450
|
+
return;
|
|
451
|
+
Y(c, n);
|
|
452
|
+
const o = D(e ?? {}, n.partitionKeyColumn, this.context);
|
|
453
|
+
_(o, n.partitionKeyColumn);
|
|
454
|
+
const [l, d] = this.splitPkObject(o, n), u = this.pkToOts(this.recordToOTS(l, n), n), h = g.buildColumnCondition(d, i), w = new f.Condition(f.RowExistenceExpectation.IGNORE, h);
|
|
455
|
+
try {
|
|
456
|
+
await this.client.updateRow({
|
|
457
|
+
tableName: t,
|
|
458
|
+
primaryKey: u,
|
|
459
|
+
condition: w,
|
|
460
|
+
updateOfAttributeColumns: [
|
|
461
|
+
...c.length > 0 ? [{ PUT: c }] : [{}],
|
|
462
|
+
...a.length > 0 ? [{ INCREMENT: a }] : [{}]
|
|
463
|
+
]
|
|
464
|
+
});
|
|
465
|
+
const m = JSON.stringify(l), T = await this.findUnique(t, { where: l });
|
|
466
|
+
T && this.writeCache(t, m, T, !1).catch(() => {
|
|
467
|
+
});
|
|
468
|
+
} catch (m) {
|
|
469
|
+
throw console.error("update error:", m), m;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
async delete(t, { where: e }) {
|
|
473
|
+
const r = this.getTableMeta(t), n = this.fieldMetaMap[t], i = D(e ?? {}, r.partitionKeyColumn, this.context);
|
|
474
|
+
_(i, r.partitionKeyColumn);
|
|
475
|
+
const [c, a] = this.splitPkObject(i, r), o = this.pkToOts(this.recordToOTS(c, r), r), l = g.buildColumnCondition(a, n), d = new f.Condition(f.RowExistenceExpectation.IGNORE, l);
|
|
476
|
+
try {
|
|
477
|
+
await this.client.deleteRow({ tableName: t, primaryKey: o, condition: d });
|
|
478
|
+
const u = JSON.stringify(c);
|
|
479
|
+
this.writeCache(t, u, c, !0).catch(() => {
|
|
480
|
+
});
|
|
481
|
+
} catch (u) {
|
|
482
|
+
throw console.error("delete error:", u), u;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
async count(t, e) {
|
|
486
|
+
try {
|
|
487
|
+
const { total: r } = await this.findManyFragment(t, { where: e == null ? void 0 : e.where, limit: 0 });
|
|
488
|
+
return r;
|
|
489
|
+
} catch (r) {
|
|
490
|
+
throw console.error("count error:", r), r;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
async initCacheTable() {
|
|
494
|
+
var t, e;
|
|
495
|
+
if (!this.cacheTableInitialized) {
|
|
496
|
+
try {
|
|
497
|
+
await this.client.describeTable({ tableName: b.CACHE_TABLE_NAME }), this.cacheTableInitialized = !0;
|
|
498
|
+
return;
|
|
499
|
+
} catch (r) {
|
|
500
|
+
if ((t = r.message) != null && t.includes("OTSTableNotExist"))
|
|
501
|
+
try {
|
|
502
|
+
await this.client.createTable({
|
|
503
|
+
tableMeta: {
|
|
504
|
+
tableName: b.CACHE_TABLE_NAME,
|
|
505
|
+
primaryKey: [
|
|
506
|
+
{ name: "table", type: f.PrimaryKeyType.STRING },
|
|
507
|
+
{ name: "bizId", type: f.PrimaryKeyType.STRING }
|
|
508
|
+
]
|
|
509
|
+
},
|
|
510
|
+
tableOptions: { timeToLive: 5, maxVersions: 1e5 },
|
|
511
|
+
reservedThroughput: { capacityUnit: { read: 0, write: 0 } }
|
|
512
|
+
});
|
|
513
|
+
} catch (n) {
|
|
514
|
+
(e = n.message) != null && e.includes("OTSObjectAlreadyExist") || console.error("Failed to create cache table:", n);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
this.cacheTableInitialized = !0;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
async writeCache(t, e, r, n = !1) {
|
|
521
|
+
try {
|
|
522
|
+
const i = Date.now(), c = { isDeleted: n, data: r };
|
|
523
|
+
await this.client.putRow({
|
|
524
|
+
tableName: b.CACHE_TABLE_NAME,
|
|
525
|
+
primaryKey: [{ table: t }, { bizId: e }],
|
|
526
|
+
attributeColumns: [
|
|
527
|
+
// @ts-ignore - OTS SDK supports timestamp in attribute columns for versioning
|
|
528
|
+
{ data: { value: JSON.stringify(c), timestamp: i } }
|
|
529
|
+
],
|
|
530
|
+
condition: new f.Condition(f.RowExistenceExpectation.IGNORE, null)
|
|
531
|
+
});
|
|
532
|
+
} catch (i) {
|
|
533
|
+
console.warn("[writeCache] Failed:", i.message);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
async getCacheData(t, e, r, n) {
|
|
537
|
+
var i;
|
|
538
|
+
try {
|
|
539
|
+
const c = await this.client.getRow({
|
|
540
|
+
tableName: b.CACHE_TABLE_NAME,
|
|
541
|
+
primaryKey: [{ table: t }, { bizId: e }],
|
|
542
|
+
// 获取最多 100000 个版本
|
|
543
|
+
maxVersions: 1e5,
|
|
544
|
+
timeRange: { startTime: r, endTime: n },
|
|
545
|
+
columnsToGet: ["data"]
|
|
546
|
+
});
|
|
547
|
+
if (!((i = c.row) != null && i.attributes)) return null;
|
|
548
|
+
const a = c.row.attributes.filter((d) => d.columnName === "data");
|
|
549
|
+
let o = 0, l = null;
|
|
550
|
+
for (const d of a) {
|
|
551
|
+
const u = Number(d.timestamp);
|
|
552
|
+
if (u >= r && u <= n && u > o) {
|
|
553
|
+
o = u;
|
|
554
|
+
try {
|
|
555
|
+
l = JSON.parse(String(d.columnValue || ""));
|
|
556
|
+
} catch {
|
|
557
|
+
l = null;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return o > 0 && l ? {
|
|
562
|
+
bizId: e,
|
|
563
|
+
timestamp: o,
|
|
564
|
+
isDeleted: l.isDeleted ?? !1,
|
|
565
|
+
data: l.data ?? {}
|
|
566
|
+
} : null;
|
|
567
|
+
} catch (c) {
|
|
568
|
+
return console.warn(`[getCacheData] Failed to get row for bizId ${e}:`, c.message), null;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
aggregateCacheRecords(t) {
|
|
572
|
+
const e = /* @__PURE__ */ new Map();
|
|
573
|
+
for (const r of t) {
|
|
574
|
+
const n = e.get(r.bizId);
|
|
575
|
+
(!n || r.timestamp > n.timestamp) && e.set(r.bizId, r);
|
|
576
|
+
}
|
|
577
|
+
return e;
|
|
578
|
+
}
|
|
579
|
+
getPrimaryKeyValues(t, e) {
|
|
580
|
+
const r = this.getTableMeta(e), n = {};
|
|
581
|
+
for (const i of r.primaryKeys)
|
|
582
|
+
t[i] !== void 0 && (n[i] = t[i]);
|
|
583
|
+
return n;
|
|
584
|
+
}
|
|
585
|
+
async queryRealtime(t, e) {
|
|
586
|
+
await this.initCacheTable();
|
|
587
|
+
const r = Date.now(), n = r - b.CACHE_WINDOW_MS, i = r, c = e == null ? void 0 : e.bizId;
|
|
588
|
+
let a = null;
|
|
589
|
+
c && (a = await this.getCacheData(t, c, n, i));
|
|
590
|
+
let o = null, l = null;
|
|
591
|
+
a && (a.isDeleted ? o = a.bizId : l = { bizId: a.bizId, data: a.data });
|
|
592
|
+
const d = e == null ? void 0 : e.where;
|
|
593
|
+
let u = l ? [l] : [];
|
|
594
|
+
if (d && Object.keys(d).length > 0 && u.length > 0 && (u = u.filter((A) => g.matchCondition(A.data, d))), u.length === 0 && !o) {
|
|
595
|
+
const { total: A, data: P } = await this.findManyCombination(t, e);
|
|
596
|
+
return { data: P, total: A };
|
|
597
|
+
}
|
|
598
|
+
let h = d ?? {};
|
|
599
|
+
o && (h = { AND: [h, { NOT: { bizId: { in: [o] } } }] });
|
|
600
|
+
const w = (e == null ? void 0 : e.limit) ?? 100, { data: m, total: T } = await this.findManyCombination(t, {
|
|
601
|
+
where: h,
|
|
602
|
+
orderBy: e == null ? void 0 : e.orderBy,
|
|
603
|
+
limit: w,
|
|
604
|
+
offset: 0,
|
|
605
|
+
select: e == null ? void 0 : e.select
|
|
606
|
+
}), C = /* @__PURE__ */ new Map();
|
|
607
|
+
for (const A of m) C.set(JSON.stringify(this.getPrimaryKeyValues(A, t)), A);
|
|
608
|
+
u.length > 0 && C.set(u[0].bizId, u[0].data);
|
|
609
|
+
const p = Array.from(C.values());
|
|
610
|
+
e != null && e.orderBy && Object.keys(e.orderBy).length > 0 && p.sort(g.buildSortComparator(e.orderBy));
|
|
611
|
+
const I = T + u.length, j = (e == null ? void 0 : e.offset) ?? 0;
|
|
612
|
+
return { data: p.slice(j, j + w), total: I };
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
// ==================== 实时查询相关方法 ====================
|
|
616
|
+
E(b, "CACHE_TABLE_NAME", "_data_cache_"), E(b, "CACHE_WINDOW_MS", 3e3);
|
|
617
|
+
let L = b;
|
|
618
|
+
function he(s, t, e) {
|
|
619
|
+
const r = new L(s, t, e), n = {};
|
|
620
|
+
return Object.keys(t).forEach((i) => {
|
|
621
|
+
n[i] = {
|
|
622
|
+
findUnique: (c) => r.findUnique(i, c),
|
|
623
|
+
findUniqueOrThrow: (c) => r.findUniqueOrThrow(i, c),
|
|
624
|
+
findFirst: (c) => r.findFirst(i, c),
|
|
625
|
+
findFirstOrThrow: (c) => r.findFirstOrThrow(i, c),
|
|
626
|
+
findMany: (c) => r.findMany(i, c),
|
|
627
|
+
findPage: (c) => r.findPage(i, c),
|
|
628
|
+
create: (c) => r.create(i, c),
|
|
629
|
+
update: (c) => r.update(i, c),
|
|
630
|
+
delete: (c) => r.delete(i, c),
|
|
631
|
+
count: (c) => r.count(i, c),
|
|
632
|
+
queryRealtime: (c) => r.queryRealtime(i, c)
|
|
633
|
+
};
|
|
634
|
+
}), n;
|
|
635
|
+
}
|
|
636
|
+
async function fe(s, t) {
|
|
637
|
+
await Promise.all(Object.values(t).map(async (e) => X(s, e)));
|
|
638
|
+
}
|
|
639
|
+
async function X(s, t) {
|
|
640
|
+
await Z(s, t), await v(s, t.tableName, t);
|
|
641
|
+
}
|
|
642
|
+
async function Z(s, t) {
|
|
643
|
+
var e;
|
|
644
|
+
try {
|
|
645
|
+
await s.createTable({
|
|
646
|
+
tableMeta: {
|
|
647
|
+
tableName: t.tableName,
|
|
648
|
+
primaryKey: t.columns.filter((r) => r.isPrimaryKey).map((r) => ({ name: r.name, type: ee(r.type) }))
|
|
649
|
+
// definedColumn: tableMeta.columns.filter(c => !c.isPrimaryKey).map(c => ({ name: c.name, type: getDefinedColumnType(c.type) })),
|
|
650
|
+
},
|
|
651
|
+
tableOptions: {
|
|
652
|
+
timeToLive: -1,
|
|
653
|
+
maxVersions: 1
|
|
654
|
+
},
|
|
655
|
+
reservedThroughput: {
|
|
656
|
+
capacityUnit: { read: 0, write: 0 }
|
|
657
|
+
}
|
|
658
|
+
}), console.log(`Table ${t.tableName} created successfully`);
|
|
659
|
+
} catch (r) {
|
|
660
|
+
(e = r.message) != null && e.includes("OTSObjectAlreadyExist") ? console.log(`[Ignore]: Table ${t.tableName} already exists`) : console.log(`[Error]: ${r.message}`);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
async function v(s, t, e) {
|
|
664
|
+
var n;
|
|
665
|
+
const r = `${t}_index`;
|
|
666
|
+
try {
|
|
667
|
+
await s.createSearchIndex({
|
|
668
|
+
tableName: t,
|
|
669
|
+
indexName: r,
|
|
670
|
+
schema: {
|
|
671
|
+
fieldSchemas: (e.searchIndexes ?? []).map((i) => ({
|
|
672
|
+
fieldName: i.fieldName,
|
|
673
|
+
fieldType: re(i.indexType),
|
|
674
|
+
enableSortAndAgg: !0,
|
|
675
|
+
...ne(i.indexType, i.options)
|
|
676
|
+
}))
|
|
677
|
+
}
|
|
678
|
+
}), console.log(`Search index ${r} created successfully`);
|
|
679
|
+
} catch (i) {
|
|
680
|
+
(n = i.message) != null && n.includes("OTSObjectAlreadyExist") ? console.log(`[Index Error]: Search index ${r} already exists`) : console.log(`[Index Error]: ${i.message}`);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
function ee(s) {
|
|
684
|
+
switch (s) {
|
|
685
|
+
case "long":
|
|
686
|
+
return f.PrimaryKeyType.INTEGER;
|
|
687
|
+
case "binary":
|
|
688
|
+
return f.PrimaryKeyType.BINARY;
|
|
689
|
+
default:
|
|
690
|
+
return f.PrimaryKeyType.STRING;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
const te = {
|
|
694
|
+
number: f.FieldType.DOUBLE,
|
|
695
|
+
boolean: f.FieldType.BOOLEAN,
|
|
696
|
+
long: f.FieldType.LONG,
|
|
697
|
+
date: f.FieldType.DATE,
|
|
698
|
+
keyword: f.FieldType.KEYWORD,
|
|
699
|
+
text: f.FieldType.TEXT,
|
|
700
|
+
geopoint: f.FieldType.GEO_POINT,
|
|
701
|
+
vector: f.FieldType.VECTOR ?? 9
|
|
702
|
+
};
|
|
703
|
+
function re(s) {
|
|
704
|
+
return te[s];
|
|
705
|
+
}
|
|
706
|
+
function ne(s, t) {
|
|
707
|
+
var e;
|
|
708
|
+
return s === "vector" ? {
|
|
709
|
+
...t ?? {},
|
|
710
|
+
vectorOptions: { dataType: (e = f.VectorDataType) == null ? void 0 : e.VD_FLOAT_32, ...(t == null ? void 0 : t.vectorOptions) ?? {} }
|
|
711
|
+
} : s === "date" ? {
|
|
712
|
+
dateFormats: ["yyyy-MM-dd'T'HH:mm:ss.SSSX"],
|
|
713
|
+
...t ?? {}
|
|
714
|
+
} : t ?? {};
|
|
715
|
+
}
|
|
716
|
+
export {
|
|
717
|
+
ye as CreatedAt,
|
|
718
|
+
ue as Field,
|
|
719
|
+
le as PartitionKey,
|
|
720
|
+
oe as PrimaryKey,
|
|
721
|
+
ae as Table,
|
|
722
|
+
de as UpdatedAt,
|
|
723
|
+
Oe as VectorMetricType,
|
|
724
|
+
he as createOTSClient,
|
|
725
|
+
fe as initTables
|
|
726
|
+
};
|