@lovrabet/sdk 1.1.19 → 1.1.21-beta.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/README.md +160 -74
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/src/models/abstract-base-model.d.ts +2 -1
- package/dist/src/models/openapi-model.d.ts +6 -1
- package/dist/src/types/index.d.ts +102 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -246,7 +246,7 @@ SDK 提供了 `client.api` 命名空间,用于执行自定义 SQL 查询和其
|
|
|
246
246
|
|
|
247
247
|
```typescript
|
|
248
248
|
// 执行 SQL 查询,返回结果数组
|
|
249
|
-
const results = await client.api.executeSql(
|
|
249
|
+
const results = await client.api.executeSql("fc8e7777-06e3847d");
|
|
250
250
|
console.log(results);
|
|
251
251
|
// [
|
|
252
252
|
// { creation_date: '2025-08-13', page_count: 2 },
|
|
@@ -259,9 +259,9 @@ console.log(results);
|
|
|
259
259
|
|
|
260
260
|
```typescript
|
|
261
261
|
// 传递参数到 SQL
|
|
262
|
-
const results = await client.api.executeSql(
|
|
263
|
-
userId:
|
|
264
|
-
startDate:
|
|
262
|
+
const results = await client.api.executeSql("fc8e7777-xxxxx", {
|
|
263
|
+
userId: "123",
|
|
264
|
+
startDate: "2025-01-01",
|
|
265
265
|
});
|
|
266
266
|
```
|
|
267
267
|
|
|
@@ -275,9 +275,9 @@ interface PageStat {
|
|
|
275
275
|
}
|
|
276
276
|
|
|
277
277
|
// 使用泛型获得类型安全
|
|
278
|
-
const stats = await client.api.executeSql<PageStat>(
|
|
279
|
-
stats.forEach(stat => {
|
|
280
|
-
console.log(stat.creation_date);
|
|
278
|
+
const stats = await client.api.executeSql<PageStat>("fc8e7777-06e3847d");
|
|
279
|
+
stats.forEach((stat) => {
|
|
280
|
+
console.log(stat.creation_date); // TypeScript 自动补全
|
|
281
281
|
console.log(stat.page_count);
|
|
282
282
|
});
|
|
283
283
|
```
|
|
@@ -286,31 +286,33 @@ stats.forEach(stat => {
|
|
|
286
286
|
|
|
287
287
|
```typescript
|
|
288
288
|
// 获取第一条结果
|
|
289
|
-
const results = await client.api.executeSql<PageStat>(
|
|
289
|
+
const results = await client.api.executeSql<PageStat>("fc8e7777-xxxxx");
|
|
290
290
|
const firstResult = results[0];
|
|
291
291
|
|
|
292
292
|
if (firstResult) {
|
|
293
|
-
console.log(
|
|
293
|
+
console.log(
|
|
294
|
+
`日期: ${firstResult.creation_date}, 数量: ${firstResult.page_count}`
|
|
295
|
+
);
|
|
294
296
|
} else {
|
|
295
|
-
console.log(
|
|
297
|
+
console.log("无结果");
|
|
296
298
|
}
|
|
297
299
|
|
|
298
300
|
// 过滤结果
|
|
299
|
-
const activeItems = results.filter(item => item.status ===
|
|
301
|
+
const activeItems = results.filter((item) => item.status === "active");
|
|
300
302
|
|
|
301
303
|
// 查找特定结果
|
|
302
|
-
const targetItem = results.find(item => item.id ===
|
|
304
|
+
const targetItem = results.find((item) => item.id === "123");
|
|
303
305
|
```
|
|
304
306
|
|
|
305
307
|
#### 错误处理
|
|
306
308
|
|
|
307
309
|
```typescript
|
|
308
310
|
try {
|
|
309
|
-
const results = await client.api.executeSql(
|
|
311
|
+
const results = await client.api.executeSql("fc8e7777-xxxxx");
|
|
310
312
|
} catch (error) {
|
|
311
313
|
if (error instanceof LovrabetError) {
|
|
312
|
-
console.error(
|
|
313
|
-
console.error(
|
|
314
|
+
console.error("SQL执行失败:", error.message);
|
|
315
|
+
console.error("错误代码:", error.code);
|
|
314
316
|
}
|
|
315
317
|
}
|
|
316
318
|
```
|
|
@@ -352,6 +354,10 @@ import {
|
|
|
352
354
|
type TokenResult,
|
|
353
355
|
type ListResponse,
|
|
354
356
|
type ListParams,
|
|
357
|
+
type FilterParams,
|
|
358
|
+
type WhereCondition,
|
|
359
|
+
type FieldCondition,
|
|
360
|
+
type ConditionOperator,
|
|
355
361
|
type SortList,
|
|
356
362
|
type SelectOption,
|
|
357
363
|
type SelectOptionsParams,
|
|
@@ -424,10 +430,143 @@ const options = await client.models.users.getSelectOptions({
|
|
|
424
430
|
// 返回: [{ label: '张三', value: 'user001' }, { label: '李四', value: 'user002' }]
|
|
425
431
|
```
|
|
426
432
|
|
|
433
|
+
### 🔍 高级过滤查询(推荐)
|
|
434
|
+
|
|
435
|
+
`filter()` 方法提供比 `getList()` 更强大的查询能力,支持复杂的条件筛选、字段选择和排序。
|
|
436
|
+
|
|
437
|
+
#### 基础用法
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
// 简单条件查询
|
|
441
|
+
const result = await client.models.users.filter({
|
|
442
|
+
where: {
|
|
443
|
+
age: { $gte: 18 } // 年龄 >= 18
|
|
444
|
+
},
|
|
445
|
+
currentPage: 1,
|
|
446
|
+
pageSize: 20
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
console.log(result.tableData); // 数据列表
|
|
450
|
+
console.log(result.total); // 总数量
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
#### 复杂条件组合
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
import { SortOrder } from "@lovrabet/sdk";
|
|
457
|
+
|
|
458
|
+
const result = await client.models.users.filter({
|
|
459
|
+
where: {
|
|
460
|
+
$and: [
|
|
461
|
+
// 年龄区间
|
|
462
|
+
{ age: { $gte: 18, $lte: 45 } },
|
|
463
|
+
// 国家在集合内
|
|
464
|
+
{ country: { $in: ['中国', '美国', '日本'] } },
|
|
465
|
+
// VIP 字段非空
|
|
466
|
+
{ vip: { $ne: null } },
|
|
467
|
+
// 名称模糊匹配
|
|
468
|
+
{ name: { $contain: 'hello' } }
|
|
469
|
+
]
|
|
470
|
+
},
|
|
471
|
+
// 选择返回的字段
|
|
472
|
+
select: ['id', 'name', 'age', 'country', 'lastLoginAt'],
|
|
473
|
+
// 多字段排序:先按登录时间倒序,再按名称升序
|
|
474
|
+
orderBy: [
|
|
475
|
+
{ lastLoginAt: SortOrder.DESC },
|
|
476
|
+
{ name: SortOrder.ASC }
|
|
477
|
+
],
|
|
478
|
+
currentPage: 1,
|
|
479
|
+
pageSize: 20
|
|
480
|
+
});
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
#### 支持的操作符
|
|
484
|
+
|
|
485
|
+
| 操作符 | 说明 | 示例 |
|
|
486
|
+
|--------|------|------|
|
|
487
|
+
| `$eq` | 等于 | `{ status: { $eq: 'active' } }` |
|
|
488
|
+
| `$ne` | 不等于 | `{ status: { $ne: 'deleted' } }` |
|
|
489
|
+
| `$gte` / `$gteq` | 大于等于 | `{ age: { $gte: 18 } }` |
|
|
490
|
+
| `$lte` / `$lteq` | 小于等于 | `{ age: { $lte: 65 } }` |
|
|
491
|
+
| `$in` | 在集合内 | `{ country: { $in: ['中国', '美国'] } }` |
|
|
492
|
+
| `$contain` | 包含(模糊匹配) | `{ name: { $contain: 'test' } }` |
|
|
493
|
+
| `$startWith` | 以...开头 | `{ name: { $startWith: 'Mr.' } }` |
|
|
494
|
+
| `$endWith` | 以...结尾 | `{ email: { $endWith: '@example.com' } }` |
|
|
495
|
+
|
|
496
|
+
#### 逻辑连接符
|
|
497
|
+
|
|
498
|
+
```typescript
|
|
499
|
+
// $and - 所有条件都满足
|
|
500
|
+
const result = await client.models.users.filter({
|
|
501
|
+
where: {
|
|
502
|
+
$and: [
|
|
503
|
+
{ age: { $gte: 18 } },
|
|
504
|
+
{ status: { $eq: 'active' } }
|
|
505
|
+
]
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
// $or - 任一条件满足
|
|
510
|
+
const result = await client.models.users.filter({
|
|
511
|
+
where: {
|
|
512
|
+
$or: [
|
|
513
|
+
{ country: { $eq: '中国' } },
|
|
514
|
+
{ country: { $eq: '美国' } }
|
|
515
|
+
]
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
// 嵌套条件
|
|
520
|
+
const result = await client.models.users.filter({
|
|
521
|
+
where: {
|
|
522
|
+
$and: [
|
|
523
|
+
{ age: { $gte: 18 } },
|
|
524
|
+
{
|
|
525
|
+
$or: [
|
|
526
|
+
{ country: { $eq: '中国' } },
|
|
527
|
+
{ country: { $eq: '美国' } }
|
|
528
|
+
]
|
|
529
|
+
}
|
|
530
|
+
]
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
#### 字段选择
|
|
536
|
+
|
|
537
|
+
```typescript
|
|
538
|
+
// 只返回指定字段
|
|
539
|
+
const result = await client.models.users.filter({
|
|
540
|
+
select: ['id', 'name', 'email'],
|
|
541
|
+
currentPage: 1,
|
|
542
|
+
pageSize: 20
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
// 访问 JSON 字段
|
|
546
|
+
const result = await client.models.users.filter({
|
|
547
|
+
select: ['id', "value['name']", 'age'],
|
|
548
|
+
currentPage: 1,
|
|
549
|
+
pageSize: 20
|
|
550
|
+
});
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
#### filter vs getList
|
|
554
|
+
|
|
555
|
+
| 特性 | `filter()` | `getList()` |
|
|
556
|
+
|------|-----------|------------|
|
|
557
|
+
| 复杂条件查询 | ✅ 支持 $and, $or 等 | ❌ 仅支持简单参数 |
|
|
558
|
+
| 字段选择 | ✅ 支持 select | ❌ 返回所有字段 |
|
|
559
|
+
| 条件操作符 | ✅ 支持 11 种操作符 | ❌ 仅支持精确匹配 |
|
|
560
|
+
| 排序 | ✅ 支持多字段排序 | ✅ 支持多字段排序 |
|
|
561
|
+
| 分页 | ✅ 支持 | ✅ 支持 |
|
|
562
|
+
| 性能 | 更优(减少数据传输) | 一般 |
|
|
563
|
+
| 使用场景 | **推荐用于复杂查询** | 简单列表查询 |
|
|
564
|
+
|
|
427
565
|
### ⚠️ 操作限制
|
|
428
566
|
|
|
429
567
|
**OpenAPI 模式暂不支持以下操作:**
|
|
430
568
|
|
|
569
|
+
- ❌ `filter()` - 高级过滤查询(如需使用,请使用 WebAPI 模式)
|
|
431
570
|
- ❌ `delete()` - 删除操作(如需删除,请使用 WebAPI 模式)
|
|
432
571
|
- ❌ `getSelectOptions()` - 获取下拉选项(仅 WebAPI 模式支持)
|
|
433
572
|
|
|
@@ -507,23 +646,25 @@ const client = createClient({ token });
|
|
|
507
646
|
|
|
508
647
|
```typescript
|
|
509
648
|
// 执行 SQL 查询
|
|
510
|
-
const data = await client.api.executeSql(
|
|
649
|
+
const data = await client.api.executeSql("fc8e7777-06e3847d");
|
|
511
650
|
|
|
512
651
|
// 应用层检查执行结果
|
|
513
652
|
if (data.execSuccess && data.execResult) {
|
|
514
|
-
data.execResult.forEach(row => {
|
|
653
|
+
data.execResult.forEach((row) => {
|
|
515
654
|
console.log(row);
|
|
516
655
|
});
|
|
517
656
|
}
|
|
518
657
|
```
|
|
519
658
|
|
|
520
659
|
**核心特性:**
|
|
660
|
+
|
|
521
661
|
- 支持参数化查询,防止 SQL 注入
|
|
522
662
|
- 完整的 TypeScript 类型支持(泛型)
|
|
523
663
|
- 返回 `{ execSuccess: boolean, execResult?: T[] }` 结构
|
|
524
664
|
- 应用层负责检查 `execSuccess` 状态
|
|
525
665
|
|
|
526
666
|
**适用场景:**
|
|
667
|
+
|
|
527
668
|
- 复杂数据统计和聚合查询
|
|
528
669
|
- 跨表关联查询
|
|
529
670
|
- 自定义报表数据获取
|
|
@@ -531,66 +672,11 @@ const client = createClient({ token });
|
|
|
531
672
|
|
|
532
673
|
- 🏗️ **API 命名空间架构** - 新增 `client.api` 命名空间,用于管理通用 API 调用
|
|
533
674
|
|
|
534
|
-
|
|
535
|
-
const client = createClient({ /* ... */ });
|
|
536
|
-
|
|
537
|
-
// 通过 api 命名空间访问
|
|
538
|
-
const data = await client.api.executeSql('sqlCode');
|
|
539
|
-
```
|
|
540
|
-
|
|
541
|
-
- 统一的 API 调用入口
|
|
542
|
-
- 为未来扩展预留空间(executeFunction、callWebhook 等)
|
|
543
|
-
- 与 Model API 保持一致的设计风格
|
|
544
|
-
|
|
545
|
-
**类型定义 (Type Definitions):**
|
|
546
|
-
|
|
547
|
-
- 新增 `SqlExecuteRequest` 接口:
|
|
548
|
-
|
|
549
|
-
```typescript
|
|
550
|
-
interface SqlExecuteRequest {
|
|
551
|
-
sqlCode: string | number; // SQL 代码
|
|
552
|
-
params?: Record<string, string | number>; // SQL 参数
|
|
553
|
-
}
|
|
554
|
-
```
|
|
555
|
-
|
|
556
|
-
- 新增 `SqlExecuteResult<T>` 接口:
|
|
557
|
-
|
|
558
|
-
```typescript
|
|
559
|
-
interface SqlExecuteResult<T = Record<string, any>> {
|
|
560
|
-
execSuccess: boolean; // SQL 执行是否成功
|
|
561
|
-
execResult?: T[]; // 查询结果数组
|
|
562
|
-
}
|
|
563
|
-
```
|
|
564
|
-
|
|
565
|
-
- 导出 `ApiNamespace` 类供高级用户使用
|
|
566
|
-
|
|
567
|
-
**重要说明 (Important Notes):**
|
|
568
|
-
|
|
569
|
-
:::warning SDK 返回值设计原则
|
|
570
|
-
|
|
571
|
-
SDK 的 `executeSql()` 方法返回包含 `execSuccess` 和 `execResult` 的对象,**不直接返回结果数组**。
|
|
572
|
-
|
|
573
|
-
这是因为:
|
|
574
|
-
1. **分离职责** - SDK 负责 HTTP 通信,应用层负责业务逻辑判断
|
|
575
|
-
2. **错误处理** - `execSuccess: false` 表示 SQL 执行失败(业务错误),HTTP 失败才抛异常
|
|
576
|
-
3. **灵活性** - 应用层可以根据 `execSuccess` 自行决定如何处理
|
|
577
|
-
|
|
578
|
-
```typescript
|
|
579
|
-
// ✅ 正确使用方式
|
|
580
|
-
const data = await client.api.executeSql('sqlCode');
|
|
581
|
-
if (data.execSuccess && data.execResult) {
|
|
582
|
-
// 使用 execResult
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
// ❌ 错误使用方式
|
|
586
|
-
const data = await client.api.executeSql('sqlCode');
|
|
587
|
-
data.execResult.forEach(...); // 没有检查 execSuccess!
|
|
588
|
-
```
|
|
589
|
-
:::
|
|
675
|
+
详细说明参考:[说明文档](https://open.lovrabet.com/docs/lovrabet-sdk/sql-api)
|
|
590
676
|
|
|
591
677
|
---
|
|
592
678
|
|
|
593
|
-
### v1.1.
|
|
679
|
+
### v1.1.17 (2025-10-18)
|
|
594
680
|
|
|
595
681
|
**新增功能 (New Features):**
|
|
596
682
|
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,6 @@ export { ApiNamespace } from "./src/api/index";
|
|
|
8
8
|
export { AuthManager, } from "./src/auth/index";
|
|
9
9
|
export { generateOpenApiToken, TokenGenerator, isTokenExpiring, getTokenRemainingTime, type GenerateTokenParams, type TokenResult } from "./src/auth/index";
|
|
10
10
|
export type { ClientConfig, LovrabetClient, ModelConfig, ModelsConfig, } from "./src/types/index";
|
|
11
|
-
export type { ListParams, ListResponse, Environment, BaseModelMethods, SortList, SelectOption, SelectOptionsParams, } from "./src/types/index";
|
|
11
|
+
export type { ListParams, ListResponse, FilterParams, WhereCondition, FieldCondition, ConditionOperator, Environment, BaseModelMethods, SortList, SelectOption, SelectOptionsParams, } from "./src/types/index";
|
|
12
12
|
export type { SqlExecuteRequest, SqlExecuteResult, } from "./src/api/types";
|
|
13
13
|
export { SortOrder, } from "./src/types/index";
|