@cloudcome/utils-uni 1.12.0 → 1.14.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.
@@ -0,0 +1,122 @@
1
+ import { DbMutateCommand, DbQueryCommand } from './_command.class';
2
+ /**
3
+ * 数据库查询命令对象,提供各种查询操作符
4
+ */
5
+ export declare const dbQuery: {
6
+ /**
7
+ * 等于操作符
8
+ * @param value 比较值
9
+ * @returns DbQueryCommand 查询命令对象
10
+ */
11
+ eq: (value: unknown) => DbQueryCommand;
12
+ /**
13
+ * 不等于操作符
14
+ * @param value 比较值
15
+ * @returns DbQueryCommand 查询命令对象
16
+ */
17
+ neq: (value: unknown) => DbQueryCommand;
18
+ /**
19
+ * 大于操作符
20
+ * @param value 比较值
21
+ * @returns DbQueryCommand 查询命令对象
22
+ */
23
+ gt: (value: unknown) => DbQueryCommand;
24
+ /**
25
+ * 大于等于操作符
26
+ * @param value 比较值
27
+ * @returns DbQueryCommand 查询命令对象
28
+ */
29
+ gte: (value: unknown) => DbQueryCommand;
30
+ /**
31
+ * 小于操作符
32
+ * @param value 比较值
33
+ * @returns DbQueryCommand 查询命令对象
34
+ */
35
+ lt: (value: unknown) => DbQueryCommand;
36
+ /**
37
+ * 小于等于操作符
38
+ * @param value 比较值
39
+ * @returns DbQueryCommand 查询命令对象
40
+ */
41
+ lte: (value: unknown) => DbQueryCommand;
42
+ /**
43
+ * 包含在数组中操作符
44
+ * @param value 值数组
45
+ * @returns DbQueryCommand 查询命令对象
46
+ */
47
+ in: (value: unknown[]) => DbQueryCommand;
48
+ /**
49
+ * 不包含在数组中操作符
50
+ * @param value 值数组
51
+ * @returns DbQueryCommand 查询命令对象
52
+ */
53
+ nin: (value: unknown[]) => DbQueryCommand;
54
+ /**
55
+ * 逻辑与操作符
56
+ * @param conditions 查询条件参数
57
+ * @returns DbQueryCommand 查询命令对象
58
+ */
59
+ and: (conditions: unknown[]) => DbQueryCommand;
60
+ /**
61
+ * 逻辑或操作符
62
+ * @param conditions 查询条件参数
63
+ * @returns DbQueryCommand 查询命令对象
64
+ */
65
+ or: (conditions: unknown[]) => DbQueryCommand;
66
+ /**
67
+ * 数组长度匹配操作符
68
+ * @param size 数组长度
69
+ * @returns DbQueryCommand 查询命令对象
70
+ */
71
+ size: (size: number) => DbQueryCommand;
72
+ };
73
+ /**
74
+ * 数据库变更命令对象,提供各种数据更新操作符
75
+ */
76
+ export declare const dbMutate: {
77
+ /**
78
+ * 数值增加操作符
79
+ * @param value 增加的数值
80
+ * @returns DbMutateCommand 变更命令对象
81
+ */
82
+ inc: (value: number) => DbMutateCommand;
83
+ /**
84
+ * 数值乘法操作符
85
+ * @param value 乘数
86
+ * @returns DbMutateCommand 变更命令对象
87
+ */
88
+ mul: (value: number) => DbMutateCommand;
89
+ /**
90
+ * 设置字段值操作符
91
+ * @param value 设置的值
92
+ * @returns DbMutateCommand 变更命令对象
93
+ */
94
+ set: (value: unknown) => DbMutateCommand;
95
+ /**
96
+ * 向数组末尾添加元素操作符
97
+ * @param value 添加的值
98
+ * @returns DbMutateCommand 变更命令对象
99
+ */
100
+ push: (value: unknown) => DbMutateCommand;
101
+ /**
102
+ * 向数组开头添加元素操作符
103
+ * @param value 添加的值
104
+ * @returns DbMutateCommand 变更命令对象
105
+ */
106
+ unshift: (value: unknown) => DbMutateCommand;
107
+ /**
108
+ * 从数组末尾移除元素操作符
109
+ * @returns DbMutateCommand 变更命令对象
110
+ */
111
+ pop: () => DbMutateCommand;
112
+ /**
113
+ * 从数组开头移除元素操作符
114
+ * @returns DbMutateCommand 变更命令对象
115
+ */
116
+ shift: () => DbMutateCommand;
117
+ /**
118
+ * 移除字段操作符
119
+ * @returns DbMutateCommand 变更命令对象
120
+ */
121
+ remove: () => DbMutateCommand;
122
+ };
@@ -0,0 +1,17 @@
1
+ import { Db, DbSelect } from './_db.class';
2
+ /**
3
+ * DbProxy 类型定义,用于创建数据库代理对象
4
+ * @template D1 - 主表数据
5
+ * @template S1 - 主表筛选
6
+ */
7
+ export type DbProxy<D1, S1 extends DbSelect<D1> = {}> = Db<D1, S1> & {
8
+ _isProxy: true;
9
+ };
10
+ /**
11
+ * 创建一个数据库代理对象,用于延迟实例化数据库操作类
12
+ * @template D1 - 主表数据
13
+ * @template S1 - 主表筛选
14
+ * @param name - 数据库表名
15
+ * @returns 返回一个代理对象,该对象会将属性访问转发到实际的数据库操作实例
16
+ */
17
+ export declare function dbProxy<D1, S1 extends DbSelect<D1> = {}>(name: string): DbProxy<D1, S1>;
@@ -0,0 +1,23 @@
1
+ import { Db } from './_db.class';
2
+ import { DbProxy } from './proxy';
3
+ type _WithTransaction = <D1>(table: DbProxy<D1>) => Db<D1>;
4
+ /**
5
+ * 在数据库事务中执行操作
6
+ *
7
+ * @template K - 事务操作返回值类型
8
+ * @param transacting - 事务执行函数,接收事务数据库实例作为参数
9
+ * @param _mockDatabase - 用于测试的模拟数据库对象
10
+ * @param _mockDbInstance - 用于测试的模拟数据库实例
11
+ * @returns 事务操作的返回结果
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const result = await dbTransaction(async (withTransaction) => {
16
+ * const userId = await withTransaction(db.table('user')).create({ name: 'John' });
17
+ * const order = await withTransaction(db.table('orders')).create({ userId, amount: 100 });
18
+ * return { user, order };
19
+ * });
20
+ * ```
21
+ */
22
+ export declare function dbTransaction<K>(transacting: (withTransaction: _WithTransaction) => Promise<K>, _mockDatabase?: any, _mockDbInstance?: any): Promise<K>;
23
+ export {};
@@ -1,4 +1,4 @@
1
- export type UniClientDatabaseOutput<T> = {
1
+ export type ClientDatabaseOutput<T> = {
2
2
  result: T & {
3
3
  code?: number | string;
4
4
  errCode?: number | string;
@@ -6,145 +6,4 @@ export type UniClientDatabaseOutput<T> = {
6
6
  message?: string;
7
7
  };
8
8
  };
9
- export type UniCloudDatabaseOutput<T> = T;
10
- export type UniDatabaseQueryCommand = {
11
- _: never;
12
- };
13
- export type UniDatabaseMutateCommand = {
14
- _: never;
15
- };
16
- /**
17
- * UniDatabaseCommand 数据库操作命令类型定义
18
- */
19
- export type UniDatabaseCommand = {
20
- /**
21
- * 聚合表达式操作符
22
- * @param expr 表达式参数
23
- * @returns 返回聚合表达式结果
24
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-expr expr 文档}
25
- */
26
- expr: (expr: unknown) => UniDatabaseQueryCommand;
27
- /**
28
- * 等于操作符
29
- * @param value 比较值
30
- * @returns 返回查询条件
31
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-eq eq 文档}
32
- */
33
- eq: (value: unknown) => UniDatabaseQueryCommand;
34
- /**
35
- * 不等于操作符
36
- * @param value 比较值
37
- * @returns 返回查询条件
38
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-neq neq 文档}
39
- */
40
- neq: (value: unknown) => UniDatabaseQueryCommand;
41
- /**
42
- * 大于操作符
43
- * @param value 比较值
44
- * @returns 返回查询条件
45
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-gt gt 文档}
46
- */
47
- gt: (value: unknown) => UniDatabaseQueryCommand;
48
- /**
49
- * 大于等于操作符
50
- * @param value 比较值
51
- * @returns 返回查询条件
52
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-gte gte 文档}
53
- */
54
- gte: (value: unknown) => UniDatabaseQueryCommand;
55
- /**
56
- * 小于操作符
57
- * @param value 比较值
58
- * @returns 返回查询条件
59
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-lt lt 文档}
60
- */
61
- lt: (value: unknown) => UniDatabaseQueryCommand;
62
- /**
63
- * 小于等于操作符
64
- * @param value 比较值
65
- * @returns 返回查询条件
66
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-lte lte 文档}
67
- */
68
- lte: (value: unknown) => UniDatabaseQueryCommand;
69
- /**
70
- * 包含在数组内操作符
71
- * @param value 包含的值数组
72
- * @returns 返回查询条件
73
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-in in 文档}
74
- */
75
- in: (value: unknown[]) => UniDatabaseQueryCommand;
76
- /**
77
- * 不包含在数组内操作符
78
- * @param value 不包含的值数组
79
- * @returns 返回查询条件
80
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-nin nin 文档}
81
- */
82
- nin: (value: unknown[]) => UniDatabaseQueryCommand;
83
- /**
84
- * 逻辑与操作符
85
- * @param args 多个查询条件
86
- * @returns 返回逻辑与查询条件
87
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-and and 文档}
88
- */
89
- and: (...args: unknown[]) => UniDatabaseQueryCommand;
90
- /**
91
- * 逻辑或操作符
92
- * @param args 多个查询条件
93
- * @returns 返回逻辑或查询条件
94
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#dbcmd-or or 文档}
95
- */
96
- or: (...args: unknown[]) => UniDatabaseQueryCommand;
97
- /**
98
- * 自增操作符
99
- * @param value 增加的数值
100
- * @returns 返回更新操作
101
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-inc inc 文档}
102
- */
103
- inc: (value: number) => UniDatabaseMutateCommand;
104
- /**
105
- * 自乘操作符
106
- * @param value 相乘的数值
107
- * @returns 返回更新操作
108
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-mul mul 文档}
109
- */
110
- mul: (value: number) => UniDatabaseMutateCommand;
111
- /**
112
- * 设置字段值操作符
113
- * @param value 设置的值
114
- * @returns 返回更新操作
115
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-set set 文档}
116
- */
117
- set: (value: unknown) => UniDatabaseMutateCommand;
118
- /**
119
- * 数组末尾添加元素操作符
120
- * @param value 添加的值
121
- * @returns 返回更新操作
122
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-push push 文档}
123
- */
124
- push: (value: unknown) => UniDatabaseMutateCommand;
125
- /**
126
- * 数组开头添加元素操作符
127
- * @param value 添加的值
128
- * @returns 返回更新操作
129
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-unshift unshift 文档}
130
- */
131
- unshift: (value: unknown) => UniDatabaseMutateCommand;
132
- /**
133
- * 删除数组末尾元素操作符
134
- * @returns 返回更新操作
135
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-pop pop 文档}
136
- */
137
- pop: () => UniDatabaseMutateCommand;
138
- /**
139
- * 删除数组开头元素操作符
140
- * @returns 返回更新操作
141
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-shift shift 文档}
142
- */
143
- shift: () => UniDatabaseMutateCommand;
144
- /**
145
- * 删除字段操作符
146
- * @returns 返回更新操作
147
- * @see {@link https://doc.dcloud.net.cn/uniCloud/cf-database.html#operator-remove remove 文档}
148
- */
149
- remove: () => UniDatabaseMutateCommand;
150
- };
9
+ export type CloudDatabaseOutput<T> = T;
@@ -0,0 +1,27 @@
1
+ import { DbCreate, DbSelect, DbUpdate, DbWhere } from './_db.class';
2
+ import { DbProxy } from './proxy';
3
+ export type DbUniqueOptions<T, S extends DbSelect<T>, C extends DbCreate<T>, U extends DbUpdate<T>> = {
4
+ /** 查询条件 */
5
+ where: DbWhere<T>;
6
+ /** 创建数据 */
7
+ create: C;
8
+ /** 创建前回调函数 */
9
+ onBeforeCreate?: () => unknown;
10
+ /**
11
+ * 创建后回调函数
12
+ * @param id 创建的文档ID
13
+ */
14
+ onAfterCreate?: (id: string) => unknown;
15
+ /** 用于测试的模拟数据库实例 */
16
+ _mockDbInstance?: any;
17
+ };
18
+ /**
19
+ * 数据库 upsert 操作的返回结果类型
20
+ */
21
+ export type DbUniqueOutput = {
22
+ /** 操作的文档ID */
23
+ id: string;
24
+ /** 是否为创建操作 */
25
+ created: boolean;
26
+ };
27
+ export declare function dbUnique<T, S extends DbSelect<T>, C extends DbCreate<T>, U extends DbUpdate<T>>(dbProxy: DbProxy<T>, options: DbUniqueOptions<T, S, C, U>): Promise<DbUniqueOutput>;
@@ -1,5 +1,6 @@
1
- import { AnyObject, Exact } from '@cloudcome/utils-core/types';
2
- import { Db, DbCreate, DbProxy, DbQuery, DbSelect, DbUpdate, DbWhere } from './db';
1
+ import { Exact } from '@cloudcome/utils-core/types';
2
+ import { DbCreate, DbQuery, DbSelect, DbUpdate, DbWhere } from './_db.class';
3
+ import { DbProxy } from './proxy';
3
4
  export type DbUpsertOptions<T, S extends DbSelect<T>, C extends DbCreate<T>, U extends DbUpdate<T>> = {
4
5
  /** 查询条件 */
5
6
  where: DbWhere<T>;
@@ -45,25 +46,4 @@ export type DbUpsertOutput = {
45
46
  /** 是否为更新操作 */
46
47
  updated: boolean;
47
48
  };
48
- export declare function dbUpsert<T, S extends DbSelect<T>, C extends DbCreate<T>, U extends DbUpdate<T>>(dbProxy: DbProxy<T>, options: DbUpsertOptions<T, S, C, U>): Promise<DbUpsertOutput>;
49
- type _WithTransaction = <T, S extends DbSelect<T>, R extends AnyObject>(table: DbProxy<T, S, R>) => Db<T, S, R>;
50
- /**
51
- * 在数据库事务中执行操作
52
- *
53
- * @template T - 事务操作返回值类型
54
- * @param transacting - 事务执行函数,接收事务数据库实例作为参数
55
- * @param _mockDatabase - 用于测试的模拟数据库对象
56
- * @param _mockDbInstance - 用于测试的模拟数据库实例
57
- * @returns 事务操作的返回结果
58
- *
59
- * @example
60
- * ```typescript
61
- * const result = await dbTransaction(async (withTransaction) => {
62
- * const userId = await withTransaction(db.table('user')).create({ name: 'John' });
63
- * const order = await withTransaction(db.table('orders')).create({ userId, amount: 100 });
64
- * return { user, order };
65
- * });
66
- * ```
67
- */
68
- export declare function dbTransaction<K>(transacting: (withTransaction: _WithTransaction) => Promise<K>, _mockDatabase?: any, _mockDbInstance?: any): Promise<K>;
69
- export {};
49
+ export declare function dbUpsert<D1, S1 extends DbSelect<D1>, C extends DbCreate<D1>, U extends DbUpdate<D1>>(dbProxy: DbProxy<D1>, options: DbUpsertOptions<D1, S1, C, U>): Promise<DbUpsertOutput>;