@wzyjs/utils 0.0.7 → 0.0.11
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/cjs/common/index.d.ts +5 -0
- package/dist/cjs/{tools → common/tools}/index.d.ts +0 -0
- package/dist/cjs/{tools → common/tools}/object.d.ts +0 -0
- package/dist/cjs/{tools → common/tools}/object.js +0 -0
- package/dist/cjs/common/tools/other.d.ts +14 -0
- package/dist/cjs/common/tools/other.js +50 -0
- package/dist/cjs/{tools → common/tools}/string.d.ts +0 -0
- package/dist/cjs/{tools → common/tools}/string.js +0 -0
- package/dist/cjs/{types → common/types}/index.d.ts +1 -1
- package/dist/cjs/fe/index.d.ts +15 -0
- package/dist/cjs/index.d.ts +2 -22
- package/dist/cjs/index_c.d.ts +2 -0
- package/dist/cjs/index_c.js +105 -0
- package/dist/cjs/rd/database/Collection.d.ts +22 -0
- package/dist/cjs/rd/database/Collection.js +202 -0
- package/dist/cjs/rd/database/Database.d.ts +10 -0
- package/dist/cjs/rd/database/Database.js +25 -0
- package/dist/cjs/rd/database/index.d.ts +2 -0
- package/dist/cjs/rd/database/types.d.ts +151 -0
- package/dist/cjs/rd/database/types.js +25 -0
- package/dist/cjs/rd/database/utils.d.ts +3 -0
- package/dist/cjs/rd/database/utils.js +30 -0
- package/dist/cjs/rd/index.d.ts +1 -0
- package/dist/esm/common/index.d.ts +5 -0
- package/dist/esm/{tools → common/tools}/index.d.ts +0 -0
- package/dist/esm/{tools → common/tools}/object.d.ts +0 -0
- package/dist/esm/{tools → common/tools}/object.js +0 -0
- package/dist/esm/common/tools/other.d.ts +14 -0
- package/dist/esm/common/tools/other.js +46 -0
- package/dist/esm/{tools → common/tools}/string.d.ts +0 -0
- package/dist/esm/{tools → common/tools}/string.js +0 -0
- package/dist/esm/{types → common/types}/index.d.ts +1 -1
- package/dist/esm/fe/index.d.ts +15 -0
- package/dist/esm/fe/index.js +14 -0
- package/dist/esm/index.d.ts +2 -22
- package/dist/esm/index.js +8 -18
- package/dist/esm/index_c.d.ts +2 -0
- package/dist/esm/rd/database/Collection.d.ts +22 -0
- package/dist/esm/rd/database/Database.d.ts +10 -0
- package/dist/esm/rd/database/index.d.ts +2 -0
- package/dist/esm/rd/database/types.d.ts +151 -0
- package/dist/esm/rd/database/utils.d.ts +3 -0
- package/dist/esm/rd/index.d.ts +1 -0
- package/package.json +6 -9
- package/dist/cjs/hooks/index.d.ts +0 -1
- package/dist/cjs/hooks/useRequestPro.d.ts +0 -11
- package/dist/cjs/hooks/useRequestPro.js +0 -37
- package/dist/cjs/index.js +0 -78
- package/dist/cjs/tools/other.d.ts +0 -6
- package/dist/cjs/tools/other.js +0 -24
- package/dist/esm/hooks/index.d.ts +0 -1
- package/dist/esm/hooks/useRequestPro.d.ts +0 -11
- package/dist/esm/hooks/useRequestPro.js +0 -35
- package/dist/esm/tools/other.d.ts +0 -6
- package/dist/esm/tools/other.js +0 -22
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError } from 'lodash';
|
|
2
|
+
export { cloneDeep, debounce, isBoolean, isEqual, isFunction, isNumber, isObject, isString, merge, omit, pick, uniq, uniqBy, uniqWith, isEmpty, isError, };
|
|
3
|
+
export * as consola from 'consola';
|
|
4
|
+
export * from './tools';
|
|
5
|
+
export * from './types';
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { KeyValue, OrderParams, Pagination, SortParams } from '../types';
|
|
2
|
+
export declare const handleParams: (params?: Pagination & KeyValue, sort?: SortParams) => {
|
|
3
|
+
page: Pagination;
|
|
4
|
+
where: KeyValue<string, string | number | boolean | RegExp>;
|
|
5
|
+
order?: OrderParams;
|
|
6
|
+
};
|
|
7
|
+
export declare const handleRes2List: <D>(reqPromise: any) => Promise<{
|
|
8
|
+
label: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}[]>;
|
|
11
|
+
export declare const executePromise: (promise: Promise<any>) => Promise<{
|
|
12
|
+
result: any;
|
|
13
|
+
time: number;
|
|
14
|
+
}>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// 处理 ProComponents 中 ProTable 参数中的 page 和 where 和 sort
|
|
4
|
+
const handleParams = (params, sort) => {
|
|
5
|
+
if (!params) {
|
|
6
|
+
return {
|
|
7
|
+
page: {},
|
|
8
|
+
where: {},
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
const { current, pageSize, ...other } = params;
|
|
12
|
+
return {
|
|
13
|
+
page: {
|
|
14
|
+
current: current || 1,
|
|
15
|
+
pageSize: pageSize || 10,
|
|
16
|
+
},
|
|
17
|
+
where: Object.entries(other).reduce((acc, [key, value]) => {
|
|
18
|
+
if (key && value) {
|
|
19
|
+
acc[key] = typeof value === 'string' ? new RegExp(value) : value;
|
|
20
|
+
}
|
|
21
|
+
return acc;
|
|
22
|
+
}, {}),
|
|
23
|
+
order: sort && {
|
|
24
|
+
field: Object.keys(sort)[0],
|
|
25
|
+
type: Object.values(sort)[0] === 'ascend' ? 'asc' : 'desc',
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
// 将 list 的数据处理为 { label: string, value: string }[]
|
|
30
|
+
const handleRes2List = async (reqPromise) => {
|
|
31
|
+
const { data } = await reqPromise();
|
|
32
|
+
return (data?.list || []).map((item) => ({
|
|
33
|
+
label: item.name,
|
|
34
|
+
value: item.key,
|
|
35
|
+
}));
|
|
36
|
+
};
|
|
37
|
+
// 计算执行 promise 花费的时间
|
|
38
|
+
const executePromise = async (promise) => {
|
|
39
|
+
const start = Date.now();
|
|
40
|
+
const result = await promise;
|
|
41
|
+
const time = Date.now() - start;
|
|
42
|
+
return {
|
|
43
|
+
result,
|
|
44
|
+
time,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
exports.executePromise = executePromise;
|
|
49
|
+
exports.handleParams = handleParams;
|
|
50
|
+
exports.handleRes2List = handleRes2List;
|
|
File without changes
|
|
File without changes
|
|
@@ -3,7 +3,7 @@ export interface RequestRes<D = any> {
|
|
|
3
3
|
message?: string;
|
|
4
4
|
data?: D;
|
|
5
5
|
}
|
|
6
|
-
export type KeyValue<D extends object | string = string, V =
|
|
6
|
+
export type KeyValue<D extends object | string = string, V = any> = Partial<Record<D extends string ? string : keyof D, V>>;
|
|
7
7
|
export interface Pagination {
|
|
8
8
|
current?: number;
|
|
9
9
|
pageSize?: number;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import md5 from 'md5';
|
|
2
|
+
import dayjs from 'dayjs';
|
|
3
|
+
import classnames from 'classnames';
|
|
4
|
+
import copy from 'copy-to-clipboard';
|
|
5
|
+
export { copy, classnames, dayjs, md5 };
|
|
6
|
+
export declare const localforage: {
|
|
7
|
+
config: {
|
|
8
|
+
(options: LocalForageOptions): boolean;
|
|
9
|
+
(options: string): any;
|
|
10
|
+
(): LocalForageOptions;
|
|
11
|
+
};
|
|
12
|
+
setItem: <T>(key: string, value: T, callback?: ((err: any, value: T) => void) | undefined) => Promise<T>;
|
|
13
|
+
getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
|
|
14
|
+
removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
|
|
15
|
+
};
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,22 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import classnames from 'classnames';
|
|
4
|
-
import consola from 'consola';
|
|
5
|
-
import md5 from 'md5';
|
|
6
|
-
export { copy, consola, classnames, dayjs, md5 };
|
|
7
|
-
export * from 'ahooks';
|
|
8
|
-
import { omit, pick, cloneDeep, isEqual, merge, isObject, isBoolean, isString, isNumber } from 'lodash';
|
|
9
|
-
export { omit, pick, cloneDeep, isEqual, merge, isObject, isBoolean, isString, isNumber };
|
|
10
|
-
export declare const localforage: {
|
|
11
|
-
config: {
|
|
12
|
-
(options: LocalForageOptions): boolean;
|
|
13
|
-
(options: string): any;
|
|
14
|
-
(): LocalForageOptions;
|
|
15
|
-
};
|
|
16
|
-
setItem: <T>(key: string, value: T, callback?: ((err: any, value: T) => void) | undefined) => Promise<T>;
|
|
17
|
-
getItem: <T_1>(key: string, callback?: ((err: any, value: T_1 | null) => void) | undefined) => Promise<T_1 | null>;
|
|
18
|
-
removeItem: (key: string, callback?: ((err: any) => void) | undefined) => Promise<void>;
|
|
19
|
-
};
|
|
20
|
-
export * from './tools';
|
|
21
|
-
export * from './hooks';
|
|
22
|
-
export * from './types';
|
|
1
|
+
export * from './common';
|
|
2
|
+
export * from './fe';
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lodash = require('lodash');
|
|
4
|
+
var consola = require('consola');
|
|
5
|
+
var string = require('./common/tools/string.js');
|
|
6
|
+
var object = require('./common/tools/object.js');
|
|
7
|
+
var other = require('./common/tools/other.js');
|
|
8
|
+
var Database = require('./rd/database/Database.js');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var consola__namespace = /*#__PURE__*/_interopNamespaceDefault(consola);
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Object.defineProperty(exports, 'cloneDeep', {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () { return lodash.cloneDeep; }
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(exports, 'debounce', {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
get: function () { return lodash.debounce; }
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(exports, 'isBoolean', {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
get: function () { return lodash.isBoolean; }
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(exports, 'isEmpty', {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () { return lodash.isEmpty; }
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(exports, 'isEqual', {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () { return lodash.isEqual; }
|
|
50
|
+
});
|
|
51
|
+
Object.defineProperty(exports, 'isError', {
|
|
52
|
+
enumerable: true,
|
|
53
|
+
get: function () { return lodash.isError; }
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(exports, 'isFunction', {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
get: function () { return lodash.isFunction; }
|
|
58
|
+
});
|
|
59
|
+
Object.defineProperty(exports, 'isNumber', {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
get: function () { return lodash.isNumber; }
|
|
62
|
+
});
|
|
63
|
+
Object.defineProperty(exports, 'isObject', {
|
|
64
|
+
enumerable: true,
|
|
65
|
+
get: function () { return lodash.isObject; }
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(exports, 'isString', {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
get: function () { return lodash.isString; }
|
|
70
|
+
});
|
|
71
|
+
Object.defineProperty(exports, 'merge', {
|
|
72
|
+
enumerable: true,
|
|
73
|
+
get: function () { return lodash.merge; }
|
|
74
|
+
});
|
|
75
|
+
Object.defineProperty(exports, 'omit', {
|
|
76
|
+
enumerable: true,
|
|
77
|
+
get: function () { return lodash.omit; }
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(exports, 'pick', {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
get: function () { return lodash.pick; }
|
|
82
|
+
});
|
|
83
|
+
Object.defineProperty(exports, 'uniq', {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
get: function () { return lodash.uniq; }
|
|
86
|
+
});
|
|
87
|
+
Object.defineProperty(exports, 'uniqBy', {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function () { return lodash.uniqBy; }
|
|
90
|
+
});
|
|
91
|
+
Object.defineProperty(exports, 'uniqWith', {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
get: function () { return lodash.uniqWith; }
|
|
94
|
+
});
|
|
95
|
+
exports.consola = consola__namespace;
|
|
96
|
+
exports.getChineseByStr = string.getChineseByStr;
|
|
97
|
+
exports.getStrLength = string.getStrLength;
|
|
98
|
+
exports.jsonParse = string.jsonParse;
|
|
99
|
+
exports.replaceAll = string.replaceAll;
|
|
100
|
+
exports.replaceByRules = string.replaceByRules;
|
|
101
|
+
exports.checkAttr = object.checkAttr;
|
|
102
|
+
exports.executePromise = other.executePromise;
|
|
103
|
+
exports.handleParams = other.handleParams;
|
|
104
|
+
exports.handleRes2List = other.handleRes2List;
|
|
105
|
+
exports.Database = Database.Database;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Database, IKeyValue } from '@cloudbase/node-sdk';
|
|
2
|
+
import { Add, Conditions, Count, Delete, Find, FindList, IdConditions, MethodParams, Update, WhereConditions } from './types';
|
|
3
|
+
export declare class Collection<D extends IKeyValue> {
|
|
4
|
+
private readonly db;
|
|
5
|
+
private readonly collection;
|
|
6
|
+
constructor(db: Database.Db, name: string);
|
|
7
|
+
private joinConditions;
|
|
8
|
+
private request;
|
|
9
|
+
private handleRes;
|
|
10
|
+
private handleData;
|
|
11
|
+
handleConditions(params: MethodParams<D>): MethodParams<D>;
|
|
12
|
+
private exec;
|
|
13
|
+
add(data: Add.Params<D>['data']): Promise<Add.Res>;
|
|
14
|
+
add(data: Add.Params<D>['data'], isReturn: true, conditions?: Pick<IdConditions, 'field'>): Promise<Find.Res<D>>;
|
|
15
|
+
delete(conditions: Conditions): Promise<Delete.Res>;
|
|
16
|
+
update(conditions: Conditions, data: Update.Params<D>['data'], isSet?: true): Promise<Update.Res<D>>;
|
|
17
|
+
update(conditions: IdConditions, data: Update.Params<D>['data'], isSet?: true): Promise<Update.Res<D>>;
|
|
18
|
+
update(conditions: IdConditions, data: Update.Params<D>['data'], isReturn: true, isSet?: true): Promise<Find.Res<D>>;
|
|
19
|
+
find(conditions?: Conditions): Promise<Find.Res<D>>;
|
|
20
|
+
findList(conditions?: WhereConditions): Promise<FindList.Res<D>>;
|
|
21
|
+
count(conditions: Conditions): Promise<Count.Res>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lodash = require('lodash');
|
|
4
|
+
var utils = require('./utils.js');
|
|
5
|
+
var types = require('./types.js');
|
|
6
|
+
|
|
7
|
+
class Collection {
|
|
8
|
+
db;
|
|
9
|
+
collection;
|
|
10
|
+
constructor(db, name) {
|
|
11
|
+
this.db = db;
|
|
12
|
+
this.collection = db.collection(name);
|
|
13
|
+
}
|
|
14
|
+
joinConditions(conditions) {
|
|
15
|
+
let record = this.collection;
|
|
16
|
+
if (!conditions) {
|
|
17
|
+
return record;
|
|
18
|
+
}
|
|
19
|
+
// 如果是根据 _id 查询
|
|
20
|
+
if ('_id' in conditions) {
|
|
21
|
+
record = record.doc(conditions._id);
|
|
22
|
+
// 如果是根据 条件 查询
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const { page, order, where, options } = conditions;
|
|
26
|
+
if (options) {
|
|
27
|
+
record = record.options(options);
|
|
28
|
+
}
|
|
29
|
+
if (where) {
|
|
30
|
+
record = record.where(where);
|
|
31
|
+
}
|
|
32
|
+
if (order) {
|
|
33
|
+
record = record.orderBy(order.field, order.type || 'desc');
|
|
34
|
+
}
|
|
35
|
+
if (page?.current && page?.size) {
|
|
36
|
+
record = record.skip((page.current - 1) * page.size);
|
|
37
|
+
}
|
|
38
|
+
if (page?.size) {
|
|
39
|
+
record = record.limit(page.size);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (Array.isArray(conditions.field)) {
|
|
43
|
+
record = record.field(conditions.field.reduce((o, i) => ({ ...o, [i]: true }), {}));
|
|
44
|
+
}
|
|
45
|
+
else if (conditions.field) {
|
|
46
|
+
record = record.field(conditions.field);
|
|
47
|
+
}
|
|
48
|
+
return record;
|
|
49
|
+
}
|
|
50
|
+
request(params) {
|
|
51
|
+
switch (params.method) {
|
|
52
|
+
case types.Method.Add:
|
|
53
|
+
return this.joinConditions().add(params.data);
|
|
54
|
+
case types.Method.Find:
|
|
55
|
+
case types.Method.FindList:
|
|
56
|
+
return this.joinConditions(params.conditions).get();
|
|
57
|
+
case types.Method.Delete:
|
|
58
|
+
return this.joinConditions(params.conditions).remove();
|
|
59
|
+
case types.Method.Update:
|
|
60
|
+
if (params.isSet && '_id' in params.conditions) {
|
|
61
|
+
return this.joinConditions(params.conditions).set(params.data);
|
|
62
|
+
}
|
|
63
|
+
return this.joinConditions(params.conditions).update(params.data);
|
|
64
|
+
case types.Method.Count:
|
|
65
|
+
return this.joinConditions(params.conditions).count();
|
|
66
|
+
default:
|
|
67
|
+
throw new Error('method is not exist');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// 处理出参
|
|
71
|
+
async handleRes(params, res) {
|
|
72
|
+
if (res) {
|
|
73
|
+
delete res.requestId;
|
|
74
|
+
}
|
|
75
|
+
let data = res;
|
|
76
|
+
switch (params.method) {
|
|
77
|
+
case types.Method.Add:
|
|
78
|
+
const addRes = res;
|
|
79
|
+
if (addRes.id && params.isReturn) {
|
|
80
|
+
data = await this.find({ _id: addRes.id, field: params.conditions?.field });
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
case types.Method.Update:
|
|
84
|
+
const updateRes = res;
|
|
85
|
+
if (updateRes.updated && params.isReturn && '_id' in params.conditions) {
|
|
86
|
+
data = await this.find({ _id: params.conditions._id, field: params.conditions?.field });
|
|
87
|
+
}
|
|
88
|
+
break;
|
|
89
|
+
case types.Method.Find:
|
|
90
|
+
const getRes = res;
|
|
91
|
+
data = getRes?.data[0] || null;
|
|
92
|
+
break;
|
|
93
|
+
case types.Method.FindList:
|
|
94
|
+
const getListRes = res;
|
|
95
|
+
const countRes = await this.request({
|
|
96
|
+
method: types.Method.Count,
|
|
97
|
+
conditions: params.conditions,
|
|
98
|
+
});
|
|
99
|
+
data = {
|
|
100
|
+
list: getListRes.data,
|
|
101
|
+
total: countRes.total,
|
|
102
|
+
};
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
return data;
|
|
106
|
+
}
|
|
107
|
+
// 处理数据
|
|
108
|
+
async handleData(params) {
|
|
109
|
+
switch (params.method) {
|
|
110
|
+
case types.Method.Add:
|
|
111
|
+
params.data = utils.appendTime(params.data, 'create');
|
|
112
|
+
params.data = utils.appendTime(params.data, 'update');
|
|
113
|
+
break;
|
|
114
|
+
case types.Method.Update:
|
|
115
|
+
if ('_id' in params.data) {
|
|
116
|
+
delete params.data._id;
|
|
117
|
+
}
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
return params;
|
|
121
|
+
}
|
|
122
|
+
// 处理条件
|
|
123
|
+
handleConditions(params) {
|
|
124
|
+
if (params.method !== types.Method.Add) {
|
|
125
|
+
params.conditions = lodash.merge(lodash.cloneDeep(utils.defaultConditions), params.conditions);
|
|
126
|
+
}
|
|
127
|
+
// 处理 where 条件
|
|
128
|
+
if (params.conditions && 'where' in params.conditions) {
|
|
129
|
+
const { command: _ } = this.db;
|
|
130
|
+
for (const key in params.conditions.where) {
|
|
131
|
+
const whereValue = params.conditions.where[key];
|
|
132
|
+
if (lodash.isObject(whereValue)) {
|
|
133
|
+
const { type, value } = whereValue;
|
|
134
|
+
switch (type) {
|
|
135
|
+
case types.Command.In:
|
|
136
|
+
params.conditions.where[key] = _.in(value);
|
|
137
|
+
break;
|
|
138
|
+
case types.Command.Nin:
|
|
139
|
+
params.conditions.where[key] = _.nin(value);
|
|
140
|
+
break;
|
|
141
|
+
case types.Command.Gt:
|
|
142
|
+
params.conditions.where[key] = _.gt(value);
|
|
143
|
+
break;
|
|
144
|
+
case types.Command.Gte:
|
|
145
|
+
params.conditions.where[key] = _.gte(value);
|
|
146
|
+
break;
|
|
147
|
+
case types.Command.Lt:
|
|
148
|
+
params.conditions.where[key] = _.lt(value);
|
|
149
|
+
break;
|
|
150
|
+
case types.Command.Lte:
|
|
151
|
+
params.conditions.where[key] = _.lte(value);
|
|
152
|
+
break;
|
|
153
|
+
case types.Command.Eq:
|
|
154
|
+
params.conditions.where[key] = _.eq(value);
|
|
155
|
+
break;
|
|
156
|
+
case types.Command.Neq:
|
|
157
|
+
params.conditions.where[key] = _.neq(value);
|
|
158
|
+
break;
|
|
159
|
+
case types.Command.Gtlt:
|
|
160
|
+
params.conditions.where[key] = _.and(_.gt(value[0]), _.lt(value[1]));
|
|
161
|
+
break;
|
|
162
|
+
default:
|
|
163
|
+
const check = type;
|
|
164
|
+
console.error(666, '处理 where 条件 出现未定义的情况', check);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return params;
|
|
171
|
+
}
|
|
172
|
+
async exec(params) {
|
|
173
|
+
params = await this.handleData(params);
|
|
174
|
+
params = await this.handleConditions(params);
|
|
175
|
+
const res = await this.request(params);
|
|
176
|
+
return this.handleRes(params, res);
|
|
177
|
+
}
|
|
178
|
+
add(data, isReturn, conditions) {
|
|
179
|
+
return this.exec({ method: types.Method.Add, conditions, data, isReturn });
|
|
180
|
+
}
|
|
181
|
+
// 删
|
|
182
|
+
delete(conditions) {
|
|
183
|
+
return this.exec({ method: types.Method.Delete, conditions });
|
|
184
|
+
}
|
|
185
|
+
update(conditions, data, isReturn, isSet) {
|
|
186
|
+
return this.exec({ method: types.Method.Update, conditions, data, isReturn, isSet });
|
|
187
|
+
}
|
|
188
|
+
// 查
|
|
189
|
+
find(conditions) {
|
|
190
|
+
return this.exec({ method: types.Method.Find, conditions });
|
|
191
|
+
}
|
|
192
|
+
// 查列表
|
|
193
|
+
findList(conditions) {
|
|
194
|
+
return this.exec({ method: types.Method.FindList, conditions });
|
|
195
|
+
}
|
|
196
|
+
// 查
|
|
197
|
+
count(conditions) {
|
|
198
|
+
return this.exec({ method: types.Method.Count, conditions });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
exports.Collection = Collection;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import tcb, { IKeyValue } from '@cloudbase/node-sdk';
|
|
2
|
+
import { Config } from './types';
|
|
3
|
+
import { Collection } from './Collection';
|
|
4
|
+
export declare class Database {
|
|
5
|
+
private readonly db;
|
|
6
|
+
private readonly instance;
|
|
7
|
+
constructor(config: Config);
|
|
8
|
+
collection<D extends IKeyValue>(name: string): Collection<D>;
|
|
9
|
+
createCollection(name: string): tcb.IBaseRes;
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var nodeSdk = require('@cloudbase/node-sdk');
|
|
4
|
+
var Collection = require('./Collection.js');
|
|
5
|
+
|
|
6
|
+
class Database {
|
|
7
|
+
db;
|
|
8
|
+
instance = {};
|
|
9
|
+
constructor(config) {
|
|
10
|
+
this.db = nodeSdk.init(config).database();
|
|
11
|
+
}
|
|
12
|
+
// 封装了较多功能的 collection
|
|
13
|
+
collection(name) {
|
|
14
|
+
if (!this.instance[name]) {
|
|
15
|
+
this.instance[name] = new Collection.Collection(this.db, name);
|
|
16
|
+
}
|
|
17
|
+
return this.instance[name];
|
|
18
|
+
}
|
|
19
|
+
// 创建 collection
|
|
20
|
+
createCollection(name) {
|
|
21
|
+
return this.db.createCollection(name);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exports.Database = Database;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Database, ICountRes } from '@cloudbase/node-sdk';
|
|
2
|
+
export interface Config {
|
|
3
|
+
env: string;
|
|
4
|
+
secretId: string;
|
|
5
|
+
secretKey: string;
|
|
6
|
+
}
|
|
7
|
+
export type IdConditions = {
|
|
8
|
+
_id: string;
|
|
9
|
+
field?: string[] | {
|
|
10
|
+
[key: string]: false;
|
|
11
|
+
};
|
|
12
|
+
options?: {
|
|
13
|
+
timeout: number;
|
|
14
|
+
multiple?: false;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare enum Command {
|
|
18
|
+
Eq = "eq",
|
|
19
|
+
Neq = "neq",
|
|
20
|
+
Lt = "lt",
|
|
21
|
+
Lte = "lte",
|
|
22
|
+
Gt = "gt",
|
|
23
|
+
Gte = "gte",
|
|
24
|
+
In = "in",
|
|
25
|
+
Nin = "nin",
|
|
26
|
+
Gtlt = "gtlt"
|
|
27
|
+
}
|
|
28
|
+
export type CommandWhere = {
|
|
29
|
+
type: Command.Eq;
|
|
30
|
+
value: any;
|
|
31
|
+
} | {
|
|
32
|
+
type: Command.Neq;
|
|
33
|
+
value: any;
|
|
34
|
+
} | {
|
|
35
|
+
type: Command.Gt;
|
|
36
|
+
value: number;
|
|
37
|
+
} | {
|
|
38
|
+
type: Command.Gte;
|
|
39
|
+
value: number;
|
|
40
|
+
} | {
|
|
41
|
+
type: Command.Lt;
|
|
42
|
+
value: number;
|
|
43
|
+
} | {
|
|
44
|
+
type: Command.Lte;
|
|
45
|
+
value: number;
|
|
46
|
+
} | {
|
|
47
|
+
type: Command.Gtlt;
|
|
48
|
+
value: number[];
|
|
49
|
+
} | {
|
|
50
|
+
type: Command.In;
|
|
51
|
+
value: any[];
|
|
52
|
+
} | {
|
|
53
|
+
type: Command.Nin;
|
|
54
|
+
value: any[];
|
|
55
|
+
};
|
|
56
|
+
export type WhereConditions = {
|
|
57
|
+
where?: {
|
|
58
|
+
[key: string]: any | CommandWhere;
|
|
59
|
+
};
|
|
60
|
+
field?: string[] | {
|
|
61
|
+
[key: string]: false;
|
|
62
|
+
};
|
|
63
|
+
page?: {
|
|
64
|
+
size?: number;
|
|
65
|
+
current?: number;
|
|
66
|
+
};
|
|
67
|
+
order?: {
|
|
68
|
+
field: string;
|
|
69
|
+
type?: 'asc' | 'desc';
|
|
70
|
+
};
|
|
71
|
+
options?: {
|
|
72
|
+
timeout?: number;
|
|
73
|
+
multiple?: boolean;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
export type Conditions = IdConditions | WhereConditions;
|
|
77
|
+
export declare enum Method {
|
|
78
|
+
Add = "add",
|
|
79
|
+
Find = "find",
|
|
80
|
+
FindList = "findList",
|
|
81
|
+
Delete = "delete",
|
|
82
|
+
Update = "update",
|
|
83
|
+
Count = "count"
|
|
84
|
+
}
|
|
85
|
+
export declare namespace Add {
|
|
86
|
+
type Params<D> = {
|
|
87
|
+
method: Method.Add;
|
|
88
|
+
data: D;
|
|
89
|
+
conditions?: Pick<IdConditions, 'field'>;
|
|
90
|
+
isReturn?: true;
|
|
91
|
+
};
|
|
92
|
+
type Res = {
|
|
93
|
+
id: string;
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
export declare namespace Find {
|
|
97
|
+
type Params = {
|
|
98
|
+
method: Method.Find;
|
|
99
|
+
conditions?: Conditions;
|
|
100
|
+
};
|
|
101
|
+
type Res<D> = Record<D> | undefined;
|
|
102
|
+
}
|
|
103
|
+
export declare namespace FindList {
|
|
104
|
+
type Params = {
|
|
105
|
+
method: Method.FindList;
|
|
106
|
+
conditions?: WhereConditions;
|
|
107
|
+
};
|
|
108
|
+
type Res<D> = {
|
|
109
|
+
list?: Record<D>[];
|
|
110
|
+
total?: number;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
export declare namespace Delete {
|
|
114
|
+
type Params = {
|
|
115
|
+
method: Method.Delete;
|
|
116
|
+
conditions: Conditions;
|
|
117
|
+
};
|
|
118
|
+
type Res = {
|
|
119
|
+
deleted: number | string;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
export declare namespace Update {
|
|
123
|
+
type Params<D> = {
|
|
124
|
+
method: Method.Update;
|
|
125
|
+
conditions: Conditions;
|
|
126
|
+
data: Partial<Record<D>>;
|
|
127
|
+
isReturn?: true;
|
|
128
|
+
isSet?: true;
|
|
129
|
+
};
|
|
130
|
+
type Res<D> = {
|
|
131
|
+
updated?: number;
|
|
132
|
+
doc?: D;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
export declare namespace Count {
|
|
136
|
+
type Params = {
|
|
137
|
+
method: Method.Count;
|
|
138
|
+
conditions?: WhereConditions;
|
|
139
|
+
};
|
|
140
|
+
type Res = {
|
|
141
|
+
total?: number;
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
export type MethodParams<D> = Add.Params<D> | Find.Params | FindList.Params | Delete.Params | Update.Params<D> | Count.Params;
|
|
145
|
+
export type MethodRes<D> = Add.Res | Find.Res<D> | FindList.Res<D> | Delete.Res | Update.Res<D> | Count.Res;
|
|
146
|
+
export type IRes = Database.IAddRes | Database.IGetRes | Database.IDeleteResult | Database.IUpdateResult | ICountRes;
|
|
147
|
+
export type Record<D> = D & {
|
|
148
|
+
_id: string;
|
|
149
|
+
createAt: number;
|
|
150
|
+
updateAt: number;
|
|
151
|
+
};
|