@lovrabet/sdk 1.2.0-beta.2 → 1.2.3
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.md +159 -73
- package/dist/index.js +1 -1
- package/dist/src/client/client.d.ts +5 -3
- package/dist/src/config/model-registry.d.ts +1 -1
- package/dist/src/types/index.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,16 +31,15 @@ import { createClient } from "@lovrabet/sdk";
|
|
|
31
31
|
const client = createClient({
|
|
32
32
|
appCode: "your-app-code",
|
|
33
33
|
accessKey: process.env.LOVRABET_ACCESS_KEY,
|
|
34
|
-
models:
|
|
35
|
-
users:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
},
|
|
39
|
-
},
|
|
34
|
+
models: [
|
|
35
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users" },
|
|
36
|
+
{ tableName: "posts", datasetCode: "ghi789jkl012" },
|
|
37
|
+
],
|
|
40
38
|
});
|
|
41
39
|
|
|
42
|
-
// CRUD
|
|
43
|
-
const users = await client.models.users.getList();
|
|
40
|
+
// CRUD 操作(通过 alias 或 datasetCode 访问)
|
|
41
|
+
const users = await client.models.users.getList(); // 通过 alias
|
|
42
|
+
const posts = await client.models["dataset_ghi789jkl012"].getList(); // 通过 datasetCode
|
|
44
43
|
const user = await client.models.users.getOne("user-id");
|
|
45
44
|
const newUser = await client.models.users.create({ name: "John" });
|
|
46
45
|
```
|
|
@@ -79,12 +78,9 @@ const client = createClient({
|
|
|
79
78
|
appCode: "your-app-code",
|
|
80
79
|
token: token,
|
|
81
80
|
timestamp: timestamp,
|
|
82
|
-
models:
|
|
83
|
-
users:
|
|
84
|
-
|
|
85
|
-
datasetCode: "your-dataset-code",
|
|
86
|
-
},
|
|
87
|
-
},
|
|
81
|
+
models: [
|
|
82
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users" },
|
|
83
|
+
],
|
|
88
84
|
});
|
|
89
85
|
|
|
90
86
|
// 使用
|
|
@@ -99,12 +95,9 @@ import { createClient } from "@lovrabet/sdk";
|
|
|
99
95
|
// 不提供认证信息,自动使用 Cookie
|
|
100
96
|
const client = createClient({
|
|
101
97
|
appCode: "your-app-code",
|
|
102
|
-
models:
|
|
103
|
-
users:
|
|
104
|
-
|
|
105
|
-
datasetCode: "your-dataset-code",
|
|
106
|
-
},
|
|
107
|
-
},
|
|
98
|
+
models: [
|
|
99
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users" },
|
|
100
|
+
],
|
|
108
101
|
});
|
|
109
102
|
|
|
110
103
|
// 请求会自动带上 Cookie
|
|
@@ -173,20 +166,21 @@ console.log(`Token 剩余 ${remaining / 1000} 秒`);
|
|
|
173
166
|
```typescript
|
|
174
167
|
import { registerModels, createClient } from "@lovrabet/sdk";
|
|
175
168
|
|
|
176
|
-
//
|
|
169
|
+
// 注册配置(数组格式)
|
|
177
170
|
registerModels({
|
|
178
171
|
appCode: "your-app-code",
|
|
179
|
-
models:
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
172
|
+
models: [
|
|
173
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users", name: "用户" },
|
|
174
|
+
{ tableName: "posts", datasetCode: "ghi789jkl012", alias: "posts", name: "文章" },
|
|
175
|
+
],
|
|
183
176
|
});
|
|
184
177
|
|
|
185
178
|
// 创建客户端(无参数)
|
|
186
179
|
const client = createClient();
|
|
187
180
|
|
|
188
|
-
//
|
|
181
|
+
// 使用(通过 alias 访问)
|
|
189
182
|
const users = await client.models.users.getList();
|
|
183
|
+
const posts = await client.models.posts.getList();
|
|
190
184
|
```
|
|
191
185
|
|
|
192
186
|
### 多环境管理
|
|
@@ -198,9 +192,9 @@ import { registerModels, createClient } from "@lovrabet/sdk";
|
|
|
198
192
|
registerModels(
|
|
199
193
|
{
|
|
200
194
|
appCode: "prod-app",
|
|
201
|
-
models:
|
|
202
|
-
|
|
203
|
-
|
|
195
|
+
models: [
|
|
196
|
+
{ tableName: "users", datasetCode: "prod_abc123", alias: "users" },
|
|
197
|
+
],
|
|
204
198
|
},
|
|
205
199
|
"prod"
|
|
206
200
|
);
|
|
@@ -208,9 +202,9 @@ registerModels(
|
|
|
208
202
|
registerModels(
|
|
209
203
|
{
|
|
210
204
|
appCode: "dev-app",
|
|
211
|
-
models:
|
|
212
|
-
|
|
213
|
-
|
|
205
|
+
models: [
|
|
206
|
+
{ tableName: "users", datasetCode: "dev_abc123", alias: "users" },
|
|
207
|
+
],
|
|
214
208
|
},
|
|
215
209
|
"dev"
|
|
216
210
|
);
|
|
@@ -226,16 +220,16 @@ const devClient = createClient("dev");
|
|
|
226
220
|
// 创建客户端
|
|
227
221
|
const client = createClient({
|
|
228
222
|
appCode: "your-app-code",
|
|
229
|
-
models:
|
|
230
|
-
|
|
231
|
-
|
|
223
|
+
models: [
|
|
224
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users" },
|
|
225
|
+
],
|
|
232
226
|
});
|
|
233
227
|
|
|
234
228
|
// 后续更新 token
|
|
235
229
|
client.setToken(newToken, newTimestamp);
|
|
236
230
|
|
|
237
231
|
// 切换环境
|
|
238
|
-
client.
|
|
232
|
+
client.setEnvironment("daily");
|
|
239
233
|
```
|
|
240
234
|
|
|
241
235
|
### 执行自定义 SQL
|
|
@@ -245,24 +239,32 @@ SDK 提供了 `client.api` 命名空间,用于执行自定义 SQL 查询和其
|
|
|
245
239
|
#### 基础查询
|
|
246
240
|
|
|
247
241
|
```typescript
|
|
248
|
-
// 执行 SQL
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
//
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
//
|
|
255
|
-
//
|
|
242
|
+
// 执行 SQL 查询,返回 SqlExecuteResult 对象
|
|
243
|
+
const data = await client.api.executeSql("fc8e7777-06e3847d");
|
|
244
|
+
|
|
245
|
+
// 检查执行结果
|
|
246
|
+
if (data.execSuccess && data.execResult) {
|
|
247
|
+
console.log(data.execResult);
|
|
248
|
+
// [
|
|
249
|
+
// { creation_date: '2025-08-13', page_count: 2 },
|
|
250
|
+
// { creation_date: '2025-08-19', page_count: 3 },
|
|
251
|
+
// ...
|
|
252
|
+
// ]
|
|
253
|
+
}
|
|
256
254
|
```
|
|
257
255
|
|
|
258
256
|
#### 参数化查询
|
|
259
257
|
|
|
260
258
|
```typescript
|
|
261
259
|
// 传递参数到 SQL
|
|
262
|
-
const
|
|
260
|
+
const data = await client.api.executeSql("fc8e7777-xxxxx", {
|
|
263
261
|
userId: "123",
|
|
264
262
|
startDate: "2025-01-01",
|
|
265
263
|
});
|
|
264
|
+
|
|
265
|
+
if (data.execSuccess && data.execResult) {
|
|
266
|
+
// 处理结果
|
|
267
|
+
}
|
|
266
268
|
```
|
|
267
269
|
|
|
268
270
|
#### 带类型提示
|
|
@@ -275,33 +277,40 @@ interface PageStat {
|
|
|
275
277
|
}
|
|
276
278
|
|
|
277
279
|
// 使用泛型获得类型安全
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
const data = await client.api.executeSql<PageStat>("fc8e7777-06e3847d");
|
|
281
|
+
|
|
282
|
+
if (data.execSuccess && data.execResult) {
|
|
283
|
+
data.execResult.forEach((stat) => {
|
|
284
|
+
console.log(stat.creation_date); // TypeScript 自动补全
|
|
285
|
+
console.log(stat.page_count);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
283
288
|
```
|
|
284
289
|
|
|
285
290
|
#### 处理查询结果
|
|
286
291
|
|
|
287
292
|
```typescript
|
|
288
|
-
//
|
|
289
|
-
const
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
)
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
293
|
+
// 获取查询结果
|
|
294
|
+
const data = await client.api.executeSql<PageStat>("fc8e7777-xxxxx");
|
|
295
|
+
|
|
296
|
+
if (data.execSuccess && data.execResult) {
|
|
297
|
+
const results = data.execResult;
|
|
298
|
+
const firstResult = results[0];
|
|
299
|
+
|
|
300
|
+
if (firstResult) {
|
|
301
|
+
console.log(
|
|
302
|
+
`日期: ${firstResult.creation_date}, 数量: ${firstResult.page_count}`
|
|
303
|
+
);
|
|
304
|
+
} else {
|
|
305
|
+
console.log("无结果");
|
|
306
|
+
}
|
|
299
307
|
|
|
300
|
-
// 过滤结果
|
|
301
|
-
const activeItems = results.filter((item) => item.status === "active");
|
|
308
|
+
// 过滤结果
|
|
309
|
+
const activeItems = results.filter((item) => item.status === "active");
|
|
302
310
|
|
|
303
|
-
// 查找特定结果
|
|
304
|
-
const targetItem = results.find((item) => item.id === "123");
|
|
311
|
+
// 查找特定结果
|
|
312
|
+
const targetItem = results.find((item) => item.id === "123");
|
|
313
|
+
}
|
|
305
314
|
```
|
|
306
315
|
|
|
307
316
|
#### 错误处理
|
|
@@ -349,9 +358,25 @@ import {
|
|
|
349
358
|
// API 命名空间
|
|
350
359
|
ApiNamespace,
|
|
351
360
|
|
|
361
|
+
// 认证管理(高级用法)
|
|
362
|
+
AuthManager,
|
|
363
|
+
|
|
364
|
+
// 模型类(高级用法和扩展)
|
|
365
|
+
AbstractBaseModel,
|
|
366
|
+
OpenApiModel,
|
|
367
|
+
WebApiModel,
|
|
368
|
+
ModelFactory,
|
|
369
|
+
|
|
370
|
+
// 排序枚举
|
|
371
|
+
SortOrder,
|
|
372
|
+
|
|
352
373
|
// TypeScript 类型
|
|
353
374
|
type ClientConfig,
|
|
375
|
+
type LovrabetClient,
|
|
376
|
+
type ModelConfig,
|
|
377
|
+
type ModelsConfig,
|
|
354
378
|
type TokenResult,
|
|
379
|
+
type GenerateTokenParams,
|
|
355
380
|
type ListResponse,
|
|
356
381
|
type ListParams,
|
|
357
382
|
type FilterParams,
|
|
@@ -363,7 +388,8 @@ import {
|
|
|
363
388
|
type SelectOptionsParams,
|
|
364
389
|
type SqlExecuteRequest,
|
|
365
390
|
type SqlExecuteResult,
|
|
366
|
-
type
|
|
391
|
+
type Environment,
|
|
392
|
+
type BaseModelMethods,
|
|
367
393
|
} from "@lovrabet/sdk";
|
|
368
394
|
```
|
|
369
395
|
|
|
@@ -384,13 +410,13 @@ interface ClientConfig {
|
|
|
384
410
|
### CRUD 操作
|
|
385
411
|
|
|
386
412
|
```typescript
|
|
387
|
-
// 查询列表(返回
|
|
413
|
+
// 查询列表(返回 tableData/total/currentPage/pageSize)
|
|
388
414
|
const response = await client.models.users.getList({
|
|
389
415
|
currentPage: 1,
|
|
390
416
|
pageSize: 20,
|
|
391
417
|
});
|
|
392
|
-
console.log(response.
|
|
393
|
-
console.log(response.
|
|
418
|
+
console.log(response.total); // 总数量
|
|
419
|
+
console.log(response.tableData); // 数据列表
|
|
394
420
|
|
|
395
421
|
// 查询列表(带排序)
|
|
396
422
|
const sortedUsers = await client.models.users.getList(
|
|
@@ -575,9 +601,9 @@ try {
|
|
|
575
601
|
const webApiClient = createClient({
|
|
576
602
|
appCode: "your-app-code",
|
|
577
603
|
// 不提供 token/accessKey,自动使用 Cookie 认证
|
|
578
|
-
models:
|
|
579
|
-
|
|
580
|
-
|
|
604
|
+
models: [
|
|
605
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users" },
|
|
606
|
+
],
|
|
581
607
|
});
|
|
582
608
|
|
|
583
609
|
await webApiClient.models.users.delete("user-id"); // ✅ 可以使用
|
|
@@ -631,6 +657,64 @@ const client = createClient({ token });
|
|
|
631
657
|
|
|
632
658
|
## 📝 What's New
|
|
633
659
|
|
|
660
|
+
### v1.2.2 (2025-12-30)
|
|
661
|
+
|
|
662
|
+
**重大更新 (Major Changes):**
|
|
663
|
+
|
|
664
|
+
- 🏗️ **模型管理器重构** - 支持基于 `datasetCode` 的统一访问方式
|
|
665
|
+
|
|
666
|
+
```typescript
|
|
667
|
+
// 新格式:数组配置(推荐)
|
|
668
|
+
const client = createClient({
|
|
669
|
+
appCode: "your-app-code",
|
|
670
|
+
models: [
|
|
671
|
+
{ tableName: "users", datasetCode: "ds-001", alias: "users", name: "用户" },
|
|
672
|
+
{ tableName: "posts", datasetCode: "ds-002" }
|
|
673
|
+
]
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
// 多种访问方式
|
|
677
|
+
client.models.users.getList(); // 通过 alias 访问
|
|
678
|
+
client.models["dataset_ds-001"].getList(); // 通过 datasetCode 访问
|
|
679
|
+
client.getModel(0).getList(); // 通过索引访问
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
**核心改进:**
|
|
683
|
+
- 使用 `datasetCode` 作为模型的唯一标识符
|
|
684
|
+
- 支持 `dataset_{datasetCode}` 前缀格式的标准化访问
|
|
685
|
+
- 新增数组格式配置,同时保持对象格式向下兼容
|
|
686
|
+
- 老格式对象配置中的 key 自动作为默认 alias
|
|
687
|
+
|
|
688
|
+
- ✨ **模型管理器增强** - 新增多个辅助方法
|
|
689
|
+
|
|
690
|
+
```typescript
|
|
691
|
+
// 获取模型详细信息(用于 UI 展示)
|
|
692
|
+
const details = client.getModelListDetails();
|
|
693
|
+
// [
|
|
694
|
+
// { value: 'dataset_ds-001', label: 'users[用户]', alias: 'users', name: '用户' },
|
|
695
|
+
// { value: 'dataset_ds-002', label: 'dataset_ds-002' }
|
|
696
|
+
// ]
|
|
697
|
+
|
|
698
|
+
// 获取所有别名
|
|
699
|
+
const aliases = client.models.listAliases();
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
- 🔧 **环境管理方法更新**
|
|
703
|
+
|
|
704
|
+
```typescript
|
|
705
|
+
// 切换环境(方法名变更)
|
|
706
|
+
client.setEnvironment("daily"); // 新方法名
|
|
707
|
+
client.getEnvironment(); // 获取当前环境
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
**代码质量 (Code Quality):**
|
|
711
|
+
|
|
712
|
+
- 清理过期文档
|
|
713
|
+
- 增强类型安全
|
|
714
|
+
- 统一代码规范
|
|
715
|
+
|
|
716
|
+
---
|
|
717
|
+
|
|
634
718
|
### v1.1.24 (2025-12-03)
|
|
635
719
|
|
|
636
720
|
**新增功能 (New Features):**
|
|
@@ -667,7 +751,9 @@ const client = createClient({ token });
|
|
|
667
751
|
const client = createClient({
|
|
668
752
|
appCode: "your-app-code",
|
|
669
753
|
accessKey: process.env.LOVRABET_ACCESS_KEY, // OpenAPI 模式下使用 filter(v1.1.22+)
|
|
670
|
-
models:
|
|
754
|
+
models: [
|
|
755
|
+
{ tableName: "users", datasetCode: "abc123def456", alias: "users" },
|
|
756
|
+
],
|
|
671
757
|
});
|
|
672
758
|
|
|
673
759
|
// 现在可以使用 filter 进行复杂查询
|