@cloudcome/utils-uni 1.3.1 → 1.5.0
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/_helpers.cjs +10 -0
- package/dist/_helpers.cjs.map +1 -0
- package/dist/_helpers.d.ts +8 -0
- package/dist/_helpers.mjs +11 -0
- package/dist/_helpers.mjs.map +1 -0
- package/dist/client.cjs +26 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.ts +49 -0
- package/dist/client.mjs +26 -0
- package/dist/client.mjs.map +1 -0
- package/dist/cloud/error.d.ts +4 -0
- package/dist/cloud/expose.d.ts +48 -0
- package/dist/cloud/object.d.ts +132 -0
- package/dist/cloud/respond.d.ts +3 -0
- package/dist/cloud/uni-id.d.ts +14 -0
- package/dist/cloud.cjs +90 -19
- package/dist/cloud.cjs.map +1 -1
- package/dist/cloud.d.ts +5 -53
- package/dist/cloud.mjs +90 -19
- package/dist/cloud.mjs.map +1 -1
- package/dist/database/db.d.ts +182 -0
- package/dist/database/fns.d.ts +73 -0
- package/dist/database/types.d.ts +9 -0
- package/dist/database.cjs +259 -0
- package/dist/database.cjs.map +1 -0
- package/dist/database.d.ts +3 -0
- package/dist/database.mjs +259 -0
- package/dist/database.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +21 -4
package/dist/cloud.mjs
CHANGED
|
@@ -1,25 +1,96 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { errorAssign, errorNormalize } from "@cloudcome/utils-core/error";
|
|
2
|
+
import { p as parseCloudObjectOutput } from "./_helpers.mjs";
|
|
3
|
+
import { objectDefaults } from "@cloudcome/utils-core/object";
|
|
4
|
+
import { tryFlatten } from "@cloudcome/utils-core/try";
|
|
5
|
+
import { isFunction } from "@cloudcome/utils-core/type";
|
|
6
|
+
function createCloudObjectError(message, code) {
|
|
7
|
+
return errorAssign(new Error(message), {
|
|
8
|
+
errCode: code,
|
|
9
|
+
errMsg: message
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
async function respondCloudObject(fn, requestId) {
|
|
13
|
+
try {
|
|
14
|
+
const data = await fn();
|
|
15
|
+
return {
|
|
16
|
+
requestId,
|
|
17
|
+
data,
|
|
18
|
+
errCode: 0,
|
|
19
|
+
errMsg: ""
|
|
20
|
+
};
|
|
21
|
+
} catch (err) {
|
|
22
|
+
const err2 = errorNormalize(err);
|
|
23
|
+
return {
|
|
24
|
+
requestId,
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
data: null,
|
|
27
|
+
errCode: err2.errCode || -1,
|
|
28
|
+
errMsg: err2.errMsg || err2.message || ""
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function buildCloudObjectExposeCreator(options) {
|
|
33
|
+
const buildOptions = objectDefaults(options || {}, {
|
|
34
|
+
requiredUserErrCode: "uni-id-check-token-failed",
|
|
35
|
+
requiredUserErrMsg: "需要登录后才能进行此操作"
|
|
36
|
+
});
|
|
37
|
+
const createCloudObjectExpose = (arg0, arg1, arg2) => {
|
|
38
|
+
const optionsSource = isFunction(arg0) ? arg1 : arg2;
|
|
39
|
+
const createOptions = objectDefaults(optionsSource || {}, {
|
|
40
|
+
requiredUser: false
|
|
41
|
+
});
|
|
42
|
+
return async function(input) {
|
|
43
|
+
return await respondCloudObject(async () => {
|
|
44
|
+
var _a, _b;
|
|
45
|
+
const user = await parseAppendUser(this, options == null ? void 0 : options.uniIdCloudObject);
|
|
46
|
+
const append = {
|
|
47
|
+
options: createOptions,
|
|
48
|
+
user
|
|
49
|
+
};
|
|
50
|
+
const context = Object.assign(this, append);
|
|
51
|
+
if (createOptions.requiredUser && !user.id) {
|
|
52
|
+
throw createCloudObjectError(buildOptions.requiredUserErrMsg, buildOptions.requiredUserErrCode);
|
|
53
|
+
}
|
|
54
|
+
if (isFunction(arg0)) {
|
|
55
|
+
return await arg0(context);
|
|
56
|
+
}
|
|
57
|
+
const parsed = arg0.safeParse(input);
|
|
58
|
+
if (!parsed.success) {
|
|
59
|
+
console.log(parsed.error.issues);
|
|
60
|
+
const issue0 = (_b = (_a = parsed.error) == null ? void 0 : _a.issues) == null ? void 0 : _b[0];
|
|
61
|
+
if ((issue0 == null ? void 0 : issue0.code) === "custom") throw issue0.message;
|
|
62
|
+
throw new Error("请求数据不正确");
|
|
63
|
+
}
|
|
64
|
+
return await arg1(context, parsed.data);
|
|
65
|
+
}, this.getUniCloudRequestId());
|
|
66
|
+
};
|
|
10
67
|
};
|
|
11
|
-
return
|
|
68
|
+
return createCloudObjectExpose;
|
|
12
69
|
}
|
|
13
|
-
function
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
70
|
+
async function parseAppendUser(objectThis, uniIdCloudObject) {
|
|
71
|
+
const appendUser = {
|
|
72
|
+
id: "",
|
|
73
|
+
role: [],
|
|
74
|
+
permission: [],
|
|
75
|
+
isAdmin: false
|
|
76
|
+
};
|
|
77
|
+
if (!uniIdCloudObject) return appendUser;
|
|
78
|
+
const uniId = uniIdCloudObject.createInstance({
|
|
79
|
+
clientInfo: objectThis.getClientInfo()
|
|
80
|
+
});
|
|
81
|
+
const [err1, user] = await tryFlatten(uniId.checkToken(objectThis.getUniIdToken() || ""));
|
|
82
|
+
if (!user) return appendUser;
|
|
83
|
+
const [err2, userData] = tryFlatten(() => parseCloudObjectOutput(user));
|
|
84
|
+
if (!userData) return appendUser;
|
|
85
|
+
appendUser.id = userData.uid || "";
|
|
86
|
+
appendUser.role = userData.role || [];
|
|
87
|
+
appendUser.permission = userData.permission || [];
|
|
88
|
+
appendUser.isAdmin = appendUser.role.includes("admin") && appendUser.permission.length === 0;
|
|
89
|
+
return appendUser;
|
|
20
90
|
}
|
|
21
91
|
export {
|
|
22
|
-
|
|
23
|
-
|
|
92
|
+
buildCloudObjectExposeCreator,
|
|
93
|
+
createCloudObjectError,
|
|
94
|
+
respondCloudObject
|
|
24
95
|
};
|
|
25
96
|
//# sourceMappingURL=cloud.mjs.map
|
package/dist/cloud.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cloud.mjs","sources":["../src/cloud.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"cloud.mjs","sources":["../src/cloud/error.ts","../src/cloud/respond.ts","../src/cloud/expose.ts"],"sourcesContent":["import { errorAssign } from '@cloudcome/utils-core/error';\n\nexport function createCloudObjectError(message: string, code?: number | string) {\n return errorAssign(new Error(message), {\n errCode: code,\n errMsg: message,\n });\n}\n","import { errorNormalize } from '@cloudcome/utils-core/error';\nimport type { MaybePromise } from '@cloudcome/utils-core/types';\nimport type { UniCloudObjectOutput } from './object';\n\nexport async function respondCloudObject<O>(\n fn: () => MaybePromise<O>,\n requestId?: string,\n): Promise<UniCloudObjectOutput<O>> {\n try {\n const data = await fn();\n\n return {\n requestId,\n data,\n errCode: 0,\n errMsg: '',\n };\n } catch (err) {\n const err2 = errorNormalize(err as Error & { errCode?: number | string; errMsg?: string });\n\n return {\n requestId,\n // @ts-ignore\n data: null,\n errCode: err2.errCode || -1,\n errMsg: err2.errMsg || err2.message || '',\n };\n }\n}\n","import { parseCloudObjectOutput } from '@/_helpers';\nimport { objectDefaults } from '@cloudcome/utils-core/object';\nimport { tryFlatten } from '@cloudcome/utils-core/try';\nimport { isFunction } from '@cloudcome/utils-core/type';\nimport type { MaybePromise } from '@cloudcome/utils-core/types';\nimport type z from 'zod';\nimport type { ZodObject } from 'zod';\nimport { createCloudObjectError } from './error';\nimport type { UniCloudObject, UniCloudObjectThis } from './object';\nimport { respondCloudObject } from './respond';\nimport type { UniIdCloudObject } from './uni-id';\n\nexport type UniCloudObjectThisAppendUser = {\n id: string;\n role: string[];\n permission: string[];\n isAdmin: boolean;\n};\n\nexport type UniCloudObjectThisAppend = {\n options: Required<CreateCloudObjectOptions>;\n user: UniCloudObjectThisAppendUser;\n};\n\nexport type UniCloudObjectContext = UniCloudObjectThis & UniCloudObjectThisAppend;\n\n/**\n * 构建云函数暴露创建器的选项配置\n * 用于配置云函数暴露创建器的行为,目前支持传入UniIdCloudObject实例\n */\nexport type BuildCloudExposeCreatorOptions = {\n /**\n * 可选的UniIdCloudObject实例\n * 用于处理用户身份验证和权限管理相关功能\n */\n uniIdCloudObject?: UniIdCloudObject;\n\n /**\n * 需要用户登录态的错误码\n * @default 'uni-id-check-token-failed'\n */\n requiredUserErrCode?: number | string;\n\n /**\n * 需要用户登录态的错误消息\n * @default '需要登录后才能进行此操作'\n */\n requiredUserErrMsg?: string;\n};\n\nexport type CreateCloudObjectOptions = {\n /**\n * 是否需要用户登录态\n * @default false\n */\n requiredUser?: boolean;\n};\n\nexport type CreateCloudObjectExpose = {\n <S extends ZodObject, O>(\n schema: S,\n fn: (context: UniCloudObjectContext, input: z.infer<S>) => MaybePromise<O>,\n options?: CreateCloudObjectOptions,\n ): UniCloudObject<z.infer<S>, O>;\n <O>(\n fn: (context: UniCloudObjectContext) => MaybePromise<O>,\n options?: CreateCloudObjectOptions,\n ): UniCloudObject<void, O>;\n};\n\nexport function buildCloudObjectExposeCreator(options?: BuildCloudExposeCreatorOptions) {\n const buildOptions = objectDefaults(options || {}, {\n requiredUserErrCode: 'uni-id-check-token-failed',\n requiredUserErrMsg: '需要登录后才能进行此操作',\n }) as Required<BuildCloudExposeCreatorOptions>;\n\n // @ts-ignore\n const createCloudObjectExpose: CreateCloudObjectExpose = (arg0, arg1, arg2) => {\n // 选项来源\n const optionsSource = (isFunction(arg0) ? arg1 : arg2) as CreateCloudObjectOptions | undefined;\n // 设置默认选项值\n const createOptions = objectDefaults(optionsSource || {}, {\n requiredUser: false,\n }) as Required<CreateCloudObjectOptions>;\n\n return async function (input) {\n // 处理云函数响应逻辑\n return await respondCloudObject(async () => {\n // 构建附加的上下文信息\n const user = await parseAppendUser(this, options?.uniIdCloudObject);\n const append: UniCloudObjectThisAppend = {\n options: createOptions,\n user: user,\n };\n const context = Object.assign(this, append) as UniCloudObjectContext;\n\n if (createOptions.requiredUser && !user.id) {\n throw createCloudObjectError(buildOptions.requiredUserErrMsg, buildOptions.requiredUserErrCode);\n }\n\n // 无入参\n if (isFunction(arg0)) {\n return await arg0(context);\n }\n\n // 单入参\n // 验证输入数据\n const parsed = arg0.safeParse(input);\n\n if (!parsed.success) {\n console.log(parsed.error.issues);\n\n const issue0 = parsed.error?.issues?.[0];\n\n // 处理验证错误\n if (issue0?.code === 'custom') throw issue0.message;\n throw new Error('请求数据不正确');\n }\n\n return await arg1(context, parsed.data);\n }, this.getUniCloudRequestId());\n };\n };\n\n return createCloudObjectExpose;\n}\n\nasync function parseAppendUser(\n objectThis: UniCloudObjectThis,\n uniIdCloudObject?: UniIdCloudObject,\n): Promise<UniCloudObjectThisAppendUser> {\n const appendUser: UniCloudObjectThisAppendUser = {\n id: '',\n role: [],\n permission: [],\n isAdmin: false,\n };\n\n if (!uniIdCloudObject) return appendUser;\n\n const uniId = uniIdCloudObject.createInstance({\n clientInfo: objectThis.getClientInfo(),\n });\n\n // 忽略错误1\n const [err1, user] = await tryFlatten(uniId.checkToken(objectThis.getUniIdToken() || ''));\n if (!user) return appendUser;\n\n // 忽略错误2\n const [err2, userData] = tryFlatten(() => parseCloudObjectOutput(user));\n if (!userData) return appendUser;\n\n appendUser.id = userData.uid || '';\n appendUser.role = userData.role || [];\n appendUser.permission = userData.permission || [];\n appendUser.isAdmin = appendUser.role.includes('admin') && appendUser.permission.length === 0;\n\n return appendUser;\n}\n"],"names":[],"mappings":";;;;;AAEgB,SAAA,uBAAuB,SAAiB,MAAwB;AAC9E,SAAO,YAAY,IAAI,MAAM,OAAO,GAAG;AAAA,IACrC,SAAS;AAAA,IACT,QAAQ;AAAA,EAAA,CACT;AACH;ACHsB,eAAA,mBACpB,IACA,WACkC;AAC9B,MAAA;AACI,UAAA,OAAO,MAAM,GAAG;AAEf,WAAA;AAAA,MACL;AAAA,MACA;AAAA,MACA,SAAS;AAAA,MACT,QAAQ;AAAA,IACV;AAAA,WACO,KAAK;AACN,UAAA,OAAO,eAAe,GAA6D;AAElF,WAAA;AAAA,MACL;AAAA;AAAA,MAEA,MAAM;AAAA,MACN,SAAS,KAAK,WAAW;AAAA,MACzB,QAAQ,KAAK,UAAU,KAAK,WAAW;AAAA,IACzC;AAAA,EAAA;AAEJ;AC0CO,SAAS,8BAA8B,SAA0C;AACtF,QAAM,eAAe,eAAe,WAAW,IAAI;AAAA,IACjD,qBAAqB;AAAA,IACrB,oBAAoB;AAAA,EAAA,CACrB;AAGD,QAAM,0BAAmD,CAAC,MAAM,MAAM,SAAS;AAE7E,UAAM,gBAAiB,WAAW,IAAI,IAAI,OAAO;AAEjD,UAAM,gBAAgB,eAAe,iBAAiB,IAAI;AAAA,MACxD,cAAc;AAAA,IAAA,CACf;AAED,WAAO,eAAgB,OAAO;AAErB,aAAA,MAAM,mBAAmB,YAAY;;AAE1C,cAAM,OAAO,MAAM,gBAAgB,MAAM,mCAAS,gBAAgB;AAClE,cAAM,SAAmC;AAAA,UACvC,SAAS;AAAA,UACT;AAAA,QACF;AACA,cAAM,UAAU,OAAO,OAAO,MAAM,MAAM;AAE1C,YAAI,cAAc,gBAAgB,CAAC,KAAK,IAAI;AAC1C,gBAAM,uBAAuB,aAAa,oBAAoB,aAAa,mBAAmB;AAAA,QAAA;AAI5F,YAAA,WAAW,IAAI,GAAG;AACb,iBAAA,MAAM,KAAK,OAAO;AAAA,QAAA;AAKrB,cAAA,SAAS,KAAK,UAAU,KAAK;AAE/B,YAAA,CAAC,OAAO,SAAS;AACX,kBAAA,IAAI,OAAO,MAAM,MAAM;AAE/B,gBAAM,UAAS,kBAAO,UAAP,mBAAc,WAAd,mBAAuB;AAGtC,eAAI,iCAAQ,UAAS,SAAU,OAAM,OAAO;AACtC,gBAAA,IAAI,MAAM,SAAS;AAAA,QAAA;AAG3B,eAAO,MAAM,KAAK,SAAS,OAAO,IAAI;AAAA,MAAA,GACrC,KAAK,sBAAsB;AAAA,IAChC;AAAA,EACF;AAEO,SAAA;AACT;AAEA,eAAe,gBACb,YACA,kBACuC;AACvC,QAAM,aAA2C;AAAA,IAC/C,IAAI;AAAA,IACJ,MAAM,CAAC;AAAA,IACP,YAAY,CAAC;AAAA,IACb,SAAS;AAAA,EACX;AAEI,MAAA,CAAC,iBAAyB,QAAA;AAExB,QAAA,QAAQ,iBAAiB,eAAe;AAAA,IAC5C,YAAY,WAAW,cAAc;AAAA,EAAA,CACtC;AAGD,QAAM,CAAC,MAAM,IAAI,IAAI,MAAM,WAAW,MAAM,WAAW,WAAW,cAAmB,KAAA,EAAE,CAAC;AACpF,MAAA,CAAC,KAAa,QAAA;AAGZ,QAAA,CAAC,MAAM,QAAQ,IAAI,WAAW,MAAM,uBAAuB,IAAI,CAAC;AAClE,MAAA,CAAC,SAAiB,QAAA;AAEX,aAAA,KAAK,SAAS,OAAO;AACrB,aAAA,OAAO,SAAS,QAAQ,CAAC;AACzB,aAAA,aAAa,SAAS,cAAc,CAAC;AACrC,aAAA,UAAU,WAAW,KAAK,SAAS,OAAO,KAAK,WAAW,WAAW,WAAW;AAEpF,SAAA;AACT;"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { AnyObject, LowercaseStartString } from '@cloudcome/utils-core/types';
|
|
2
|
+
import { UniClientDatabaseOutput, UniCloudDatabaseOutput } from './types';
|
|
3
|
+
type DbSelectPositive = {
|
|
4
|
+
[key: LowercaseStartString]: true;
|
|
5
|
+
};
|
|
6
|
+
type DbSelectNegative = {
|
|
7
|
+
_id?: false;
|
|
8
|
+
};
|
|
9
|
+
export type DbSelect = Omit<DbSelectPositive, '_id'> & DbSelectNegative;
|
|
10
|
+
export type DbOrder = Record<string, 'asc' | 'desc'>;
|
|
11
|
+
export type DbRecord<S extends Record<string, boolean>> = {
|
|
12
|
+
[K in keyof S as S[K] extends true ? K : never]: unknown;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* 数据库操作符命令
|
|
16
|
+
*/
|
|
17
|
+
export declare const dbCmd: UniCloud.QueryCommand;
|
|
18
|
+
/**
|
|
19
|
+
* 数据库聚合操作符命令
|
|
20
|
+
*/
|
|
21
|
+
export declare const dbAgg: UniCloud.AggregateCommand;
|
|
22
|
+
export type DbOptions = {
|
|
23
|
+
/**
|
|
24
|
+
* 集合名称
|
|
25
|
+
*/
|
|
26
|
+
collection: string;
|
|
27
|
+
/**
|
|
28
|
+
* 事务对象,用于事务操作
|
|
29
|
+
*/
|
|
30
|
+
transaction?: any;
|
|
31
|
+
/**
|
|
32
|
+
* 模拟数据库,用于单元测试
|
|
33
|
+
*/
|
|
34
|
+
_mockDatabase?: any;
|
|
35
|
+
};
|
|
36
|
+
export declare class Db {
|
|
37
|
+
#private;
|
|
38
|
+
/**
|
|
39
|
+
* 构造函数,初始化数据库集合引用
|
|
40
|
+
* @param collection 集合名称
|
|
41
|
+
* @param _mockDatabase 模拟数据库,用于单元测试
|
|
42
|
+
*/
|
|
43
|
+
constructor(options: DbOptions);
|
|
44
|
+
/**
|
|
45
|
+
* 创建聚合操作实例
|
|
46
|
+
* @returns 聚合操作实例
|
|
47
|
+
*/
|
|
48
|
+
aggregate(): UniCloud.AggregateReference;
|
|
49
|
+
/**
|
|
50
|
+
* 设置查询条件
|
|
51
|
+
* @param where 查询条件对象
|
|
52
|
+
* @returns 当前Db实例,支持链式执行
|
|
53
|
+
*/
|
|
54
|
+
where(where: AnyObject): this;
|
|
55
|
+
/**
|
|
56
|
+
* 指定要返回的字段
|
|
57
|
+
* @param fields 要返回的字段对象,true表示返回,false表示不返回
|
|
58
|
+
* @returns 当前Db实例,支持链式执行
|
|
59
|
+
*/
|
|
60
|
+
select(fields: DbSelect): this;
|
|
61
|
+
/**
|
|
62
|
+
* 设置排序规则
|
|
63
|
+
* @param order 排序规则对象,key为字段名,value为"asc"或"desc"
|
|
64
|
+
* @returns 当前Db实例,支持链式执行
|
|
65
|
+
*/
|
|
66
|
+
order(order: DbOrder): this;
|
|
67
|
+
/**
|
|
68
|
+
* 跳过指定数量的记录
|
|
69
|
+
* @param skip 要跳过的记录数
|
|
70
|
+
* @returns 当前Db实例,支持链式执行
|
|
71
|
+
*/
|
|
72
|
+
skip(skip: number): this;
|
|
73
|
+
/**
|
|
74
|
+
* 限制返回的记录数量
|
|
75
|
+
* @param limit 最大返回记录数
|
|
76
|
+
* @returns 当前Db实例,支持链式执行
|
|
77
|
+
*/
|
|
78
|
+
limit(limit: number): this;
|
|
79
|
+
/**
|
|
80
|
+
* 创建新记录
|
|
81
|
+
* @param data 要创建的数据
|
|
82
|
+
* @returns 创建结果
|
|
83
|
+
*/
|
|
84
|
+
create(data: AnyObject): Promise<{
|
|
85
|
+
id: string;
|
|
86
|
+
} | Omit<{
|
|
87
|
+
id: string;
|
|
88
|
+
} & {
|
|
89
|
+
code?: number | string;
|
|
90
|
+
errCode?: number | string;
|
|
91
|
+
errMsg?: string;
|
|
92
|
+
message?: string;
|
|
93
|
+
}, "errCode" | "errMsg" | "code" | "message">>;
|
|
94
|
+
/**
|
|
95
|
+
* 执行查询操作
|
|
96
|
+
* @returns 查询结果
|
|
97
|
+
*/
|
|
98
|
+
query<T>(): Promise<{
|
|
99
|
+
data: T[];
|
|
100
|
+
} | Omit<{
|
|
101
|
+
data: T[];
|
|
102
|
+
} & {
|
|
103
|
+
code?: number | string;
|
|
104
|
+
errCode?: number | string;
|
|
105
|
+
errMsg?: string;
|
|
106
|
+
message?: string;
|
|
107
|
+
}, "errCode" | "errMsg" | "code" | "message">>;
|
|
108
|
+
/**
|
|
109
|
+
* 只查询一条,自动添加 limit(1) 条件
|
|
110
|
+
* @param ignoreMiss 是否忽略没有匹配到记录
|
|
111
|
+
* @returns 查询结果
|
|
112
|
+
*/
|
|
113
|
+
queryOne<T>(): Promise<T>;
|
|
114
|
+
queryOne<T>(ignoreMiss: false): Promise<T>;
|
|
115
|
+
queryOne<T>(ignoreMiss: true): Promise<T | undefined>;
|
|
116
|
+
/**
|
|
117
|
+
* 获取匹配记录的数量
|
|
118
|
+
* @returns 记录总数
|
|
119
|
+
*/
|
|
120
|
+
count(): Promise<{
|
|
121
|
+
total: number;
|
|
122
|
+
} | Omit<{
|
|
123
|
+
total: number;
|
|
124
|
+
} & {
|
|
125
|
+
code?: number | string;
|
|
126
|
+
errCode?: number | string;
|
|
127
|
+
errMsg?: string;
|
|
128
|
+
message?: string;
|
|
129
|
+
}, "errCode" | "errMsg" | "code" | "message">>;
|
|
130
|
+
/**
|
|
131
|
+
* 更新记录
|
|
132
|
+
* @param data 要更新的数据
|
|
133
|
+
* @returns 更新结果
|
|
134
|
+
*/
|
|
135
|
+
update(data: AnyObject): Promise<{
|
|
136
|
+
updated: number;
|
|
137
|
+
} | Omit<{
|
|
138
|
+
updated: number;
|
|
139
|
+
} & {
|
|
140
|
+
code?: number | string;
|
|
141
|
+
errCode?: number | string;
|
|
142
|
+
errMsg?: string;
|
|
143
|
+
message?: string;
|
|
144
|
+
}, "errCode" | "errMsg" | "code" | "message">>;
|
|
145
|
+
/**
|
|
146
|
+
* 删除记录
|
|
147
|
+
* @returns 删除结果
|
|
148
|
+
*/
|
|
149
|
+
remove(): Promise<{
|
|
150
|
+
deleted: number;
|
|
151
|
+
} | Omit<{
|
|
152
|
+
deleted: number;
|
|
153
|
+
} & {
|
|
154
|
+
code?: number | string;
|
|
155
|
+
errCode?: number | string;
|
|
156
|
+
errMsg?: string;
|
|
157
|
+
message?: string;
|
|
158
|
+
}, "errCode" | "errMsg" | "code" | "message">>;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 数据库操作对象
|
|
162
|
+
*/
|
|
163
|
+
export declare const db: {
|
|
164
|
+
/**
|
|
165
|
+
* 获取指定名称的数据库集合实例
|
|
166
|
+
* @param collection 集合名称
|
|
167
|
+
* @returns Db类实例,用于执行数据库操作
|
|
168
|
+
*/
|
|
169
|
+
collection(collection: string): Db;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* 解析数据库执行结果
|
|
173
|
+
* @param res 客户端、云端响应结果
|
|
174
|
+
* @returns 处理后的结果
|
|
175
|
+
*/
|
|
176
|
+
export declare function parseDatabaseOutput<T>(res: UniClientDatabaseOutput<T> | UniCloudDatabaseOutput<T>): T | Omit<T & {
|
|
177
|
+
code?: number | string;
|
|
178
|
+
errCode?: number | string;
|
|
179
|
+
errMsg?: string;
|
|
180
|
+
message?: string;
|
|
181
|
+
}, "errCode" | "errMsg" | "code" | "message">;
|
|
182
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AnyObject } from '@cloudcome/utils-core/types';
|
|
2
|
+
import { Db, DbSelect } from './db';
|
|
3
|
+
/**
|
|
4
|
+
* 数据库 upsert 操作的配置选项
|
|
5
|
+
*
|
|
6
|
+
* @template W - 查询条件类型
|
|
7
|
+
* @template S - 查询返回字段类型
|
|
8
|
+
* @template C - 创建数据类型
|
|
9
|
+
* @template U - 更新数据类型
|
|
10
|
+
* @template R - 查询结果类型
|
|
11
|
+
*/
|
|
12
|
+
export type DbUpsertOptions<W extends AnyObject, S extends DbSelect, C extends AnyObject, U extends AnyObject, R extends AnyObject> = {
|
|
13
|
+
/** 集合名称 */
|
|
14
|
+
collection: string;
|
|
15
|
+
/** 查询条件 */
|
|
16
|
+
where: W;
|
|
17
|
+
/** 查询返回字段 */
|
|
18
|
+
select?: S;
|
|
19
|
+
/** 创建数据 */
|
|
20
|
+
create: C;
|
|
21
|
+
/**
|
|
22
|
+
* 更新数据,可以是对象或根据查询结果生成更新对象的函数
|
|
23
|
+
* @param row 查询到的文档数据,仅在传入函数时可用
|
|
24
|
+
*/
|
|
25
|
+
update: U | ((row: R) => U);
|
|
26
|
+
/** 创建前回调函数 */
|
|
27
|
+
onBeforeCreate?: () => unknown;
|
|
28
|
+
/**
|
|
29
|
+
* 创建后回调函数
|
|
30
|
+
* @param id 创建的文档ID
|
|
31
|
+
*/
|
|
32
|
+
onAfterCreate?: (id: string) => unknown;
|
|
33
|
+
/**
|
|
34
|
+
* 更新前回调函数
|
|
35
|
+
* @param row 查询到的文档
|
|
36
|
+
*/
|
|
37
|
+
onBeforeUpdate?: (row: R) => unknown;
|
|
38
|
+
/** 更新后回调函数 */
|
|
39
|
+
onAfterUpdate?: () => unknown;
|
|
40
|
+
/** 用于测试的模拟数据库实例 */
|
|
41
|
+
_mockDb?: (collection: string) => any;
|
|
42
|
+
};
|
|
43
|
+
export declare function dbUpsert<W extends AnyObject, S extends DbSelect, C extends AnyObject, U extends AnyObject, R extends AnyObject>(options: DbUpsertOptions<W, S, C, U, R>): Promise<{
|
|
44
|
+
updated: number;
|
|
45
|
+
} | {
|
|
46
|
+
id: string;
|
|
47
|
+
}>;
|
|
48
|
+
type _TransactionDb = {
|
|
49
|
+
startTransaction: () => Promise<_Transaction>;
|
|
50
|
+
};
|
|
51
|
+
type _Transaction = {
|
|
52
|
+
commit: () => Promise<unknown>;
|
|
53
|
+
rollback: () => Promise<unknown>;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* 在数据库事务中执行操作
|
|
57
|
+
*
|
|
58
|
+
* @template T - 事务操作返回值类型
|
|
59
|
+
* @param transact - 事务执行函数,接收事务数据库实例作为参数
|
|
60
|
+
* @param _mockDatabase - 用于测试的模拟数据库实例
|
|
61
|
+
* @returns 事务操作的返回结果
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```typescript
|
|
65
|
+
* const result = await dbTransaction(async (ta) => {
|
|
66
|
+
* const user = await ta.collection('users').create({ name: 'John' });
|
|
67
|
+
* const order = await ta.collection('orders').create({ userId: user.id, amount: 100 });
|
|
68
|
+
* return { user, order };
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare function dbTransaction<T>(transact: (ta: Db) => Promise<T>, _mockDatabase?: _TransactionDb): Promise<T>;
|
|
73
|
+
export {};
|