@lovrabet/sdk 1.1.18 → 1.1.19
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 +181 -7
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/src/api/index.d.ts +51 -0
- package/dist/src/api/types.d.ts +18 -0
- package/dist/src/client/client.d.ts +5 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ const client = createClient({
|
|
|
34
34
|
models: {
|
|
35
35
|
users: {
|
|
36
36
|
tableName: "users",
|
|
37
|
-
|
|
37
|
+
datasetCode: "your-dataset-code",
|
|
38
38
|
},
|
|
39
39
|
},
|
|
40
40
|
});
|
|
@@ -82,7 +82,7 @@ const client = createClient({
|
|
|
82
82
|
models: {
|
|
83
83
|
users: {
|
|
84
84
|
tableName: "users",
|
|
85
|
-
|
|
85
|
+
datasetCode: "your-dataset-code",
|
|
86
86
|
},
|
|
87
87
|
},
|
|
88
88
|
});
|
|
@@ -102,7 +102,7 @@ const client = createClient({
|
|
|
102
102
|
models: {
|
|
103
103
|
users: {
|
|
104
104
|
tableName: "users",
|
|
105
|
-
|
|
105
|
+
datasetCode: "your-dataset-code",
|
|
106
106
|
},
|
|
107
107
|
},
|
|
108
108
|
});
|
|
@@ -177,8 +177,8 @@ import { registerModels, createClient } from "@lovrabet/sdk";
|
|
|
177
177
|
registerModels({
|
|
178
178
|
appCode: "your-app-code",
|
|
179
179
|
models: {
|
|
180
|
-
users: { tableName: "users",
|
|
181
|
-
posts: { tableName: "posts",
|
|
180
|
+
users: { tableName: "users", datasetCode: "ds-001" },
|
|
181
|
+
posts: { tableName: "posts", datasetCode: "ds-002" },
|
|
182
182
|
},
|
|
183
183
|
});
|
|
184
184
|
|
|
@@ -238,6 +238,83 @@ client.setToken(newToken, newTimestamp);
|
|
|
238
238
|
client.switchEnv("daily");
|
|
239
239
|
```
|
|
240
240
|
|
|
241
|
+
### 执行自定义 SQL
|
|
242
|
+
|
|
243
|
+
SDK 提供了 `client.api` 命名空间,用于执行自定义 SQL 查询和其他通用 API 调用。
|
|
244
|
+
|
|
245
|
+
#### 基础查询
|
|
246
|
+
|
|
247
|
+
```typescript
|
|
248
|
+
// 执行 SQL 查询,返回结果数组
|
|
249
|
+
const results = await client.api.executeSql('fc8e7777-06e3847d');
|
|
250
|
+
console.log(results);
|
|
251
|
+
// [
|
|
252
|
+
// { creation_date: '2025-08-13', page_count: 2 },
|
|
253
|
+
// { creation_date: '2025-08-19', page_count: 3 },
|
|
254
|
+
// ...
|
|
255
|
+
// ]
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
#### 参数化查询
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// 传递参数到 SQL
|
|
262
|
+
const results = await client.api.executeSql('fc8e7777-xxxxx', {
|
|
263
|
+
userId: '123',
|
|
264
|
+
startDate: '2025-01-01'
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
#### 带类型提示
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
// 定义结果类型
|
|
272
|
+
interface PageStat {
|
|
273
|
+
creation_date: string;
|
|
274
|
+
page_count: number;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// 使用泛型获得类型安全
|
|
278
|
+
const stats = await client.api.executeSql<PageStat>('fc8e7777-06e3847d');
|
|
279
|
+
stats.forEach(stat => {
|
|
280
|
+
console.log(stat.creation_date); // TypeScript 自动补全
|
|
281
|
+
console.log(stat.page_count);
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
#### 处理查询结果
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
// 获取第一条结果
|
|
289
|
+
const results = await client.api.executeSql<PageStat>('fc8e7777-xxxxx');
|
|
290
|
+
const firstResult = results[0];
|
|
291
|
+
|
|
292
|
+
if (firstResult) {
|
|
293
|
+
console.log(`日期: ${firstResult.creation_date}, 数量: ${firstResult.page_count}`);
|
|
294
|
+
} else {
|
|
295
|
+
console.log('无结果');
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 过滤结果
|
|
299
|
+
const activeItems = results.filter(item => item.status === 'active');
|
|
300
|
+
|
|
301
|
+
// 查找特定结果
|
|
302
|
+
const targetItem = results.find(item => item.id === '123');
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
#### 错误处理
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
try {
|
|
309
|
+
const results = await client.api.executeSql('fc8e7777-xxxxx');
|
|
310
|
+
} catch (error) {
|
|
311
|
+
if (error instanceof LovrabetError) {
|
|
312
|
+
console.error('SQL执行失败:', error.message);
|
|
313
|
+
console.error('错误代码:', error.code);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
241
318
|
## 📖 API 文档
|
|
242
319
|
|
|
243
320
|
### 主要导出
|
|
@@ -267,6 +344,9 @@ import {
|
|
|
267
344
|
// 错误处理
|
|
268
345
|
LovrabetError,
|
|
269
346
|
|
|
347
|
+
// API 命名空间
|
|
348
|
+
ApiNamespace,
|
|
349
|
+
|
|
270
350
|
// TypeScript 类型
|
|
271
351
|
type ClientConfig,
|
|
272
352
|
type TokenResult,
|
|
@@ -275,6 +355,8 @@ import {
|
|
|
275
355
|
type SortList,
|
|
276
356
|
type SelectOption,
|
|
277
357
|
type SelectOptionsParams,
|
|
358
|
+
type SqlExecuteRequest,
|
|
359
|
+
type SqlExecuteResult,
|
|
278
360
|
|
|
279
361
|
// 枚举
|
|
280
362
|
SortOrder,
|
|
@@ -298,12 +380,13 @@ interface ClientConfig {
|
|
|
298
380
|
### CRUD 操作
|
|
299
381
|
|
|
300
382
|
```typescript
|
|
301
|
-
//
|
|
383
|
+
// 查询列表(返回 paging/tableData/tableColumns)
|
|
302
384
|
const response = await client.models.users.getList({
|
|
303
385
|
currentPage: 1,
|
|
304
386
|
pageSize: 20,
|
|
305
|
-
// 其他查询参数
|
|
306
387
|
});
|
|
388
|
+
console.log(response.paging.totalCount);
|
|
389
|
+
console.log(response.tableColumns);
|
|
307
390
|
|
|
308
391
|
// 查询列表(带排序)
|
|
309
392
|
import { SortOrder } from "@lovrabet/sdk";
|
|
@@ -416,6 +499,97 @@ const client = createClient({ token });
|
|
|
416
499
|
|
|
417
500
|
## 📝 What's New
|
|
418
501
|
|
|
502
|
+
### v1.1.19 (2025-11-10)
|
|
503
|
+
|
|
504
|
+
**新增功能 (New Features):**
|
|
505
|
+
|
|
506
|
+
- ✨ **SQL API 支持** - 新增 `client.api.executeSql()` 方法,支持执行自定义 SQL 查询
|
|
507
|
+
|
|
508
|
+
```typescript
|
|
509
|
+
// 执行 SQL 查询
|
|
510
|
+
const data = await client.api.executeSql('fc8e7777-06e3847d');
|
|
511
|
+
|
|
512
|
+
// 应用层检查执行结果
|
|
513
|
+
if (data.execSuccess && data.execResult) {
|
|
514
|
+
data.execResult.forEach(row => {
|
|
515
|
+
console.log(row);
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
**核心特性:**
|
|
521
|
+
- 支持参数化查询,防止 SQL 注入
|
|
522
|
+
- 完整的 TypeScript 类型支持(泛型)
|
|
523
|
+
- 返回 `{ execSuccess: boolean, execResult?: T[] }` 结构
|
|
524
|
+
- 应用层负责检查 `execSuccess` 状态
|
|
525
|
+
|
|
526
|
+
**适用场景:**
|
|
527
|
+
- 复杂数据统计和聚合查询
|
|
528
|
+
- 跨表关联查询
|
|
529
|
+
- 自定义报表数据获取
|
|
530
|
+
- 灵活的数据分析需求
|
|
531
|
+
|
|
532
|
+
- 🏗️ **API 命名空间架构** - 新增 `client.api` 命名空间,用于管理通用 API 调用
|
|
533
|
+
|
|
534
|
+
```typescript
|
|
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
|
+
:::
|
|
590
|
+
|
|
591
|
+
---
|
|
592
|
+
|
|
419
593
|
### v1.1.18 (2025-10-18)
|
|
420
594
|
|
|
421
595
|
**新增功能 (New Features):**
|
package/dist/index.d.ts
CHANGED
|
@@ -4,8 +4,10 @@ export { getApiEndpoint, getAvailableEnvironments, } from "./src/config/endpoint
|
|
|
4
4
|
export { CONFIG_NAMES, ENVIRONMENTS, DEFAULTS, HTTP_STATUS, ERROR_MESSAGES, API_PATHS, } from "./src/config/constants";
|
|
5
5
|
export { LovrabetError } from "./src/utils/errors";
|
|
6
6
|
export { AbstractBaseModel, OpenApiModel, WebApiModel, ModelFactory, } from "./src/models/index";
|
|
7
|
+
export { ApiNamespace } from "./src/api/index";
|
|
7
8
|
export { AuthManager, } from "./src/auth/index";
|
|
8
9
|
export { generateOpenApiToken, TokenGenerator, isTokenExpiring, getTokenRemainingTime, type GenerateTokenParams, type TokenResult } from "./src/auth/index";
|
|
9
10
|
export type { ClientConfig, LovrabetClient, ModelConfig, ModelsConfig, } from "./src/types/index";
|
|
10
11
|
export type { ListParams, ListResponse, Environment, BaseModelMethods, SortList, SelectOption, SelectOptionsParams, } from "./src/types/index";
|
|
12
|
+
export type { SqlExecuteRequest, SqlExecuteResult, } from "./src/api/types";
|
|
11
13
|
export { SortOrder, } from "./src/types/index";
|