@cloudbase/manager-node 4.2.4 → 4.2.5

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.
Files changed (85) hide show
  1. package/lib/access/index.js +141 -0
  2. package/lib/access/types.js +2 -0
  3. package/lib/billing/index.js +36 -0
  4. package/lib/cam/index.js +77 -0
  5. package/lib/cloudBaseRun/index.js +36 -0
  6. package/lib/cloudBaseRun/types.js +2 -0
  7. package/lib/common/index.js +39 -0
  8. package/lib/constant.js +55 -0
  9. package/lib/context.js +14 -0
  10. package/lib/database/index.js +244 -0
  11. package/lib/debug.js +34 -0
  12. package/lib/env/index.js +289 -0
  13. package/lib/environment.js +124 -0
  14. package/lib/environmentManager.js +44 -0
  15. package/lib/error.js +16 -0
  16. package/lib/function/index.js +1019 -0
  17. package/lib/function/packer.js +129 -0
  18. package/lib/function/types.js +2 -0
  19. package/lib/hosting/index.js +461 -0
  20. package/lib/index.js +83 -0
  21. package/lib/interfaces/base.interface.js +2 -0
  22. package/lib/interfaces/billing.interface.js +2 -0
  23. package/lib/interfaces/cam.interface.js +2 -0
  24. package/lib/interfaces/flexdb.interface.js +2 -0
  25. package/lib/interfaces/function.interface.js +2 -0
  26. package/lib/interfaces/index.js +19 -0
  27. package/lib/interfaces/storage.interface.js +2 -0
  28. package/lib/interfaces/tcb.interface.js +2 -0
  29. package/lib/storage/index.js +1051 -0
  30. package/lib/third/index.js +18 -0
  31. package/lib/user/index.js +136 -0
  32. package/lib/user/types.js +2 -0
  33. package/lib/utils/auth.js +97 -0
  34. package/lib/utils/cloud-api-request.js +212 -0
  35. package/lib/utils/cloudbase-request.js +69 -0
  36. package/lib/utils/envLazy.js +18 -0
  37. package/lib/utils/fs.js +64 -0
  38. package/lib/utils/http-request.js +44 -0
  39. package/lib/utils/index.js +103 -0
  40. package/lib/utils/parallel.js +69 -0
  41. package/lib/utils/runenv.js +8 -0
  42. package/lib/utils/uuid.js +18 -0
  43. package/package.json +1 -1
  44. package/types/access/index.d.ts +38 -0
  45. package/types/access/types.d.ts +42 -0
  46. package/types/billing/index.d.ts +21 -0
  47. package/types/cam/index.d.ts +63 -0
  48. package/types/cloudBaseRun/index.d.ts +12 -0
  49. package/types/cloudBaseRun/types.d.ts +21 -0
  50. package/types/common/index.d.ts +18 -0
  51. package/types/constant.d.ts +44 -0
  52. package/types/context.d.ts +17 -0
  53. package/types/database/index.d.ts +66 -0
  54. package/types/debug.d.ts +1 -0
  55. package/types/env/index.d.ts +127 -0
  56. package/types/environment.d.ts +51 -0
  57. package/types/environmentManager.d.ts +13 -0
  58. package/types/error.d.ts +18 -0
  59. package/types/function/index.d.ts +379 -0
  60. package/types/function/packer.d.ts +37 -0
  61. package/types/function/types.d.ts +154 -0
  62. package/types/hosting/index.d.ts +253 -0
  63. package/types/index.d.ts +52 -0
  64. package/types/interfaces/base.interface.d.ts +7 -0
  65. package/types/interfaces/billing.interface.d.ts +18 -0
  66. package/types/interfaces/cam.interface.d.ts +24 -0
  67. package/types/interfaces/flexdb.interface.d.ts +67 -0
  68. package/types/interfaces/function.interface.d.ts +65 -0
  69. package/types/interfaces/index.d.ts +7 -0
  70. package/types/interfaces/storage.interface.d.ts +26 -0
  71. package/types/interfaces/tcb.interface.d.ts +305 -0
  72. package/types/storage/index.d.ts +324 -0
  73. package/types/third/index.d.ts +11 -0
  74. package/types/user/index.d.ts +52 -0
  75. package/types/user/types.d.ts +20 -0
  76. package/types/utils/auth.d.ts +8 -0
  77. package/types/utils/cloud-api-request.d.ts +21 -0
  78. package/types/utils/cloudbase-request.d.ts +14 -0
  79. package/types/utils/envLazy.d.ts +1 -0
  80. package/types/utils/fs.d.ts +7 -0
  81. package/types/utils/http-request.d.ts +2 -0
  82. package/types/utils/index.d.ts +20 -0
  83. package/types/utils/parallel.d.ts +17 -0
  84. package/types/utils/runenv.d.ts +1 -0
  85. package/types/utils/uuid.d.ts +2 -0
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.AccessService = void 0;
10
+ const utils_1 = require("../utils");
11
+ class AccessService {
12
+ constructor(environment) {
13
+ this.environment = environment;
14
+ this.tcbService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
15
+ }
16
+ // 创建云接入路径
17
+ async createAccess(options) {
18
+ const { path, name, type = 1, auth } = options;
19
+ const { envId } = this.getEnvInfo();
20
+ return this.tcbService.request('CreateCloudBaseGWAPI', {
21
+ ServiceId: envId,
22
+ Path: path,
23
+ Type: type,
24
+ Name: name,
25
+ AuthSwitch: auth ? 1 : 2
26
+ });
27
+ }
28
+ // 获取云接入自定义域名列表
29
+ async getDomainList() {
30
+ const { envId } = this.getEnvInfo();
31
+ return this.tcbService.request('DescribeCloudBaseGWService', {
32
+ ServiceId: envId
33
+ });
34
+ }
35
+ // 获取云接入服务列表
36
+ async getAccessList(options = {}) {
37
+ const { path, name, offset, limit } = options;
38
+ const { envId } = this.getEnvInfo();
39
+ return this.tcbService.request('DescribeCloudBaseGWAPI', {
40
+ ServiceId: envId,
41
+ Path: path,
42
+ Name: name,
43
+ Offset: offset,
44
+ limit: limit
45
+ });
46
+ }
47
+ // 切换云接入服务开关:开启/关闭
48
+ async switchAuth(auth) {
49
+ const { envId } = this.getEnvInfo();
50
+ return this.tcbService.request('ModifyCloudBaseGWPrivilege', {
51
+ ServiceId: envId,
52
+ EnableService: auth,
53
+ Options: [
54
+ {
55
+ Key: 'serviceswitch',
56
+ Value: auth ? 'true' : 'false'
57
+ }
58
+ ]
59
+ });
60
+ }
61
+ // 更新云接入路径鉴权
62
+ async switchPathAuth(options) {
63
+ const { apiIds, auth } = options;
64
+ const { envId } = this.getEnvInfo();
65
+ return this.tcbService.request('ModifyCloudBaseGWAPIPrivilegeBatch', {
66
+ ServiceId: envId,
67
+ APIIdSet: apiIds,
68
+ Options: [
69
+ {
70
+ Key: 'authswitch',
71
+ Value: auth ? 'true' : 'false'
72
+ }
73
+ ]
74
+ });
75
+ }
76
+ // 删除云接入服务
77
+ async deleteAccess(options) {
78
+ const { name, type = 1, apiId } = options;
79
+ const { envId } = this.getEnvInfo();
80
+ return this.tcbService.request('DeleteCloudBaseGWAPI', {
81
+ ServiceId: envId,
82
+ Name: name,
83
+ Type: type,
84
+ APIId: apiId
85
+ });
86
+ }
87
+ // 添加自定义域名
88
+ async addCustomDomain(options) {
89
+ const { domain, certId } = options;
90
+ const { envId } = this.getEnvInfo();
91
+ return this.tcbService.request('BindCloudBaseGWDomain', {
92
+ Domain: domain,
93
+ ServiceId: envId,
94
+ CertId: certId
95
+ });
96
+ }
97
+ // 删除自定义域名
98
+ async deleteCustomDomain(domain) {
99
+ const { envId } = this.getEnvInfo();
100
+ return this.tcbService.request('DeleteCloudBaseGWDomain', {
101
+ Domain: domain,
102
+ ServiceId: envId
103
+ });
104
+ }
105
+ getEnvInfo() {
106
+ var _a;
107
+ const envConfig = this.environment.lazyEnvironmentConfig;
108
+ const appId = (_a = envConfig.Storages[0]) === null || _a === void 0 ? void 0 : _a.AppId;
109
+ const { proxy } = this.environment.cloudBaseContext;
110
+ return {
111
+ appId,
112
+ proxy,
113
+ envId: envConfig.EnvId
114
+ };
115
+ }
116
+ }
117
+ __decorate([
118
+ utils_1.preLazy()
119
+ ], AccessService.prototype, "createAccess", null);
120
+ __decorate([
121
+ utils_1.preLazy()
122
+ ], AccessService.prototype, "getDomainList", null);
123
+ __decorate([
124
+ utils_1.preLazy()
125
+ ], AccessService.prototype, "getAccessList", null);
126
+ __decorate([
127
+ utils_1.preLazy()
128
+ ], AccessService.prototype, "switchAuth", null);
129
+ __decorate([
130
+ utils_1.preLazy()
131
+ ], AccessService.prototype, "switchPathAuth", null);
132
+ __decorate([
133
+ utils_1.preLazy()
134
+ ], AccessService.prototype, "deleteAccess", null);
135
+ __decorate([
136
+ utils_1.preLazy()
137
+ ], AccessService.prototype, "addCustomDomain", null);
138
+ __decorate([
139
+ utils_1.preLazy()
140
+ ], AccessService.prototype, "deleteCustomDomain", null);
141
+ exports.AccessService = AccessService;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BillingService = void 0;
4
+ const utils_1 = require("../utils");
5
+ class BillingService {
6
+ constructor(context) {
7
+ this.billService = new utils_1.CloudService(context, BillingService.billServiceVersion.service, BillingService.billServiceVersion.version);
8
+ }
9
+ /**
10
+ * 创建订单
11
+ * @param {Array<IGoodItem>} goods
12
+ * @returns {Promise<IGenerateDealsRes>}
13
+ * @memberof BillingService
14
+ */
15
+ async GenerateDeals(goods) {
16
+ return this.billService.request('GenerateDeals', {
17
+ Goods: goods
18
+ });
19
+ }
20
+ /**
21
+ * 支付订单
22
+ * @param {Array<string>} orderIds
23
+ * @returns {Promise<IPayDealsRes>}
24
+ * @memberof BillingService
25
+ */
26
+ async PayDeals(orderIds) {
27
+ return this.billService.request('PayDeals', {
28
+ OrderIds: orderIds
29
+ });
30
+ }
31
+ }
32
+ exports.BillingService = BillingService;
33
+ BillingService.billServiceVersion = {
34
+ service: 'billing',
35
+ version: '2018-07-09'
36
+ };
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CamService = void 0;
4
+ const utils_1 = require("../utils");
5
+ class CamService {
6
+ constructor(context) {
7
+ this.camService = new utils_1.CloudService(context, CamService.camServiceVersion.service, CamService.camServiceVersion.version);
8
+ }
9
+ /**
10
+ * 查询账户角色列表
11
+ * @param {number} page
12
+ * @param {number} rp
13
+ * @returns {Promise<IRoleListRes>}
14
+ * @memberof CamService
15
+ */
16
+ async describeRoleList(page, rp) {
17
+ return this.camService.request('DescribeRoleList', {
18
+ Page: page,
19
+ Rp: rp
20
+ });
21
+ }
22
+ /**
23
+ * 获取角色详情
24
+ * @param {string} roleName
25
+ * @returns {Promise<IGetRoleRes>}
26
+ * @memberof CamService
27
+ */
28
+ async getRole(roleName) {
29
+ return this.camService.request('GetRole', {
30
+ RoleName: roleName
31
+ });
32
+ }
33
+ /**
34
+ * 创建角色
35
+ * @param {{
36
+ * RoleName: string
37
+ * PolicyDocument: string
38
+ * Description: string
39
+ * }} param
40
+ * @returns {Promise<ICreateRoleRes>}
41
+ * @memberof CamService
42
+ */
43
+ async createRole(param) {
44
+ return this.camService.request('CreateRole', param);
45
+ }
46
+ /**
47
+ * 绑定角色策略
48
+ * @param {{
49
+ * PolicyId: number
50
+ * AttachRoleName: string
51
+ * }} param
52
+ * @returns {Promise<IResponseInfo>}
53
+ * @memberof CamService
54
+ */
55
+ async attachRolePolicy(param) {
56
+ return this.camService.request('AttachRolePolicy', param);
57
+ }
58
+ async attachRolePolicies(param) {
59
+ return this.camService.request('AttachRolePolicies', param);
60
+ }
61
+ /**
62
+ * 删除角色
63
+ * @param {string} roleName
64
+ * @returns {Promise<IResponseInfo>}
65
+ * @memberof CamService
66
+ */
67
+ async deleteRole(roleName) {
68
+ return this.camService.request('DeleteRole', {
69
+ RoleName: roleName
70
+ });
71
+ }
72
+ }
73
+ exports.CamService = CamService;
74
+ CamService.camServiceVersion = {
75
+ service: 'cam',
76
+ version: '2019-01-16'
77
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.CloudBaseRunService = void 0;
10
+ const utils_1 = require("../utils");
11
+ class CloudBaseRunService {
12
+ constructor(environment) {
13
+ this.environment = environment;
14
+ this.tcbService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
15
+ }
16
+ // 修改容器内版本流量配置
17
+ async modifyServerFlow(options) {
18
+ const { envId } = this.getEnvInfo();
19
+ return this.tcbService.request('ModifyCloudBaseRunServerFlowConf', {
20
+ EnvId: envId,
21
+ ServerName: options.serverName,
22
+ VersionFlowItems: utils_1.upperCaseObjKey(options.versionFlowItems)
23
+ // TrafficType: options.trafficType
24
+ });
25
+ }
26
+ getEnvInfo() {
27
+ const envConfig = this.environment.lazyEnvironmentConfig;
28
+ return {
29
+ envId: envConfig.EnvId
30
+ };
31
+ }
32
+ }
33
+ __decorate([
34
+ utils_1.preLazy()
35
+ ], CloudBaseRunService.prototype, "modifyServerFlow", null);
36
+ exports.CloudBaseRunService = CloudBaseRunService;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommonService = void 0;
4
+ const utils_1 = require("../utils");
5
+ const error_1 = require("../error");
6
+ /**
7
+ * 公共的云api调用方法 透传用户参数 无业务逻辑处理
8
+ * @export
9
+ * @class CommonService
10
+ */
11
+ const ActionVersionMap = {
12
+ tcb: '2018-06-08',
13
+ flexdb: '2018-11-27',
14
+ scf: '2018-04-16',
15
+ sts: '2018-04-16',
16
+ cam: '2018-04-16',
17
+ lowcode: '2021-01-08'
18
+ };
19
+ class CommonService {
20
+ constructor(environment, serviceType, serviceVersion) {
21
+ this.environment = environment;
22
+ this.commonService = new utils_1.CloudService(environment.cloudBaseContext, serviceType, serviceVersion || ActionVersionMap[serviceType]);
23
+ }
24
+ /**
25
+ * 公共方法调用
26
+ * @param {ICommonApiServiceParam} param
27
+ * @returns {Promise<any>}
28
+ * @memberof CommonService
29
+ */
30
+ async call(options) {
31
+ const { Action, Param = {} } = options;
32
+ if (!Action) {
33
+ throw new error_1.CloudBaseError('缺少必填参数 Action');
34
+ }
35
+ const res = await this.commonService.request(Action, Object.assign({}, Param));
36
+ return res;
37
+ }
38
+ }
39
+ exports.CommonService = CommonService;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ // // cloudbase cli 配置的字段名
3
+ // export class ConfigItems {
4
+ // static credentail = 'credential'
5
+ // }
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.COS_SDK_KEEPALIVE = exports.COS_SDK_PROTOCOL = exports.USE_INTERNAL_ENDPOINT = exports.SCF_STATUS = exports.ROLE_NAME = exports.PUBLIC_RSA_KEY = exports.ERROR = exports.SERVICE_TYPE = exports.ENDPOINT = exports.RUN_ENV = exports.SDK_VERSION = exports.ENV_NAME = void 0;
8
+ exports.ENV_NAME = {
9
+ ENV_SECRETID: 'TENCENTCLOUD_SECRETID',
10
+ ENV_SECRETKEY: 'TENCENTCLOUD_SECRETKEY',
11
+ ENV_SESSIONTOKEN: 'TENCENTCLOUD_SESSIONTOKEN',
12
+ ENV_TCB_ENV_ID: 'TENCENTCLOUD_TCB_ENVID',
13
+ ENV_RUNENV: 'TENCENTCLOUD_RUNENV',
14
+ ENV_RUNENV_SCF: 'TENCENTCLOUD_RUNENV=SCF'
15
+ };
16
+ exports.SDK_VERSION = 'TCB-NODE-MANAGER/1.0.O';
17
+ exports.RUN_ENV = {
18
+ SCF: 'SCF'
19
+ };
20
+ exports.ENDPOINT = {
21
+ TCB: 'tcb.tencentcloudapi.com',
22
+ SCF: 'scf.tencentcloudapi.com',
23
+ COS: 'cos.tencentcloudapi.com',
24
+ FLEXDB: 'flexdb.tencentcloudapi.com'
25
+ };
26
+ exports.SERVICE_TYPE = {
27
+ TCB: 'tcb'
28
+ };
29
+ exports.ERROR = {
30
+ MISS_SECRET_INFO_IN_ENV: 'MISS_SECRET_INFO_IN_ENV',
31
+ MISS_SECRET_INFO_IN_ARGS: 'MISS_SECRET_INFO_IN_ARGS',
32
+ CURRENT_ENVIRONMENT_IS_NULL: 'CURRENT_ENVIRONMENT_IS_NULL',
33
+ ENV_ID_NOT_EXISTS: 'ENV_ID_NOT_EXISTS'
34
+ };
35
+ exports.PUBLIC_RSA_KEY = `
36
+ -----BEGIN PUBLIC KEY-----
37
+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC0ZLB0ZpWWFsHPnDDw++Nc2wI3
38
+ nl2uyOrIJ5FUfxt4GAmt1Faf5pgMxAnL9exEUrrUDUX8Ri1R0KyfnHQQwCvKt8T8
39
+ bgILIJe9UB8e9dvFqgqH2oA8Vqwi0YqDcvFLFJk2BJbm/0QYtZ563FumW8LEXAgu
40
+ UeHi/0OZN9vQ33jWMQIDAQAB
41
+ -----END PUBLIC KEY-----
42
+ `;
43
+ exports.ROLE_NAME = {
44
+ TCB: 'TCB_QcsRole'
45
+ };
46
+ exports.SCF_STATUS = {
47
+ ACTIVE: 'Active',
48
+ CREATING: 'Creating',
49
+ UPDATING: 'Updating',
50
+ CREATE_FAILED: 'CreateFailed'
51
+ };
52
+ // 是否使用内网域名
53
+ exports.USE_INTERNAL_ENDPOINT = "USE_INTERNAL_ENDPOINT" in process.env;
54
+ exports.COS_SDK_PROTOCOL = process.env.COS_SDK_PROTOCOL;
55
+ exports.COS_SDK_KEEPALIVE = process.env.COS_SDK_KEEPALIVE;
package/lib/context.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CloudBaseContext = void 0;
4
+ class CloudBaseContext {
5
+ constructor({ secretId = '', secretKey = '', token = '', proxy = '', region = '', envType = '' }) {
6
+ this.secretId = secretId;
7
+ this.secretKey = secretKey;
8
+ this.token = token;
9
+ this.proxy = proxy;
10
+ this.region = region;
11
+ this.envType = envType;
12
+ }
13
+ }
14
+ exports.CloudBaseContext = CloudBaseContext;
@@ -0,0 +1,244 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __importDefault = (this && this.__importDefault) || function (mod) {
9
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DatabaseService = void 0;
13
+ const path_1 = __importDefault(require("path"));
14
+ const error_1 = require("../error");
15
+ const utils_1 = require("../utils");
16
+ function preLazy() {
17
+ return function (target, propertyKey, descriptor) {
18
+ let oldFunc = descriptor.value;
19
+ descriptor.value = async function () {
20
+ // 检查当前环境对象上是否已加载好环境信息
21
+ const currentEnvironment = this.environment;
22
+ if (!currentEnvironment.inited) {
23
+ await currentEnvironment.lazyInit();
24
+ }
25
+ let result = await oldFunc.apply(this, arguments);
26
+ return result;
27
+ };
28
+ };
29
+ }
30
+ class DatabaseService {
31
+ constructor(environment) {
32
+ this.DEFAULT_MGO_OFFSET = 0;
33
+ this.DEFAULT_MGO_LIMIT = 100;
34
+ this.environment = environment;
35
+ this.envId = environment.getEnvId();
36
+ this.dbOpService = new utils_1.CloudService(environment.cloudBaseContext, DatabaseService.tcbServiceVersion.service, DatabaseService.tcbServiceVersion.version);
37
+ this.collOpService = new utils_1.CloudService(environment.cloudBaseContext, DatabaseService.flexdbServiceVersion.service, DatabaseService.flexdbServiceVersion.version);
38
+ }
39
+ getCurrEnvironment() {
40
+ return this.environment;
41
+ }
42
+ getDatabaseConfig() {
43
+ const currEnv = this.environment;
44
+ const { Databases } = currEnv.lazyEnvironmentConfig;
45
+ return {
46
+ Tag: Databases[0].InstanceId
47
+ };
48
+ }
49
+ async checkCollectionExists(collectionName) {
50
+ try {
51
+ const result = await this.describeCollection(collectionName);
52
+ return {
53
+ RequestId: result.RequestId,
54
+ Exists: true
55
+ };
56
+ }
57
+ catch (e) {
58
+ return {
59
+ RequestId: e.requestId,
60
+ Msg: e.message,
61
+ Exists: false
62
+ };
63
+ }
64
+ }
65
+ async createCollection(collectionName) {
66
+ let { Tag } = this.getDatabaseConfig();
67
+ const res = await this.collOpService.request('CreateTable', {
68
+ Tag,
69
+ TableName: collectionName
70
+ });
71
+ return res;
72
+ }
73
+ async deleteCollection(collectionName) {
74
+ // 先检查当前集合是否存在
75
+ const existRes = await this.checkCollectionExists(collectionName);
76
+ if (existRes.Exists) {
77
+ let { Tag } = this.getDatabaseConfig();
78
+ const res = await this.collOpService.request('DeleteTable', {
79
+ Tag,
80
+ TableName: collectionName
81
+ });
82
+ return res;
83
+ }
84
+ else {
85
+ return existRes;
86
+ }
87
+ }
88
+ async updateCollection(collectionName, indexiesInfo) {
89
+ let { Tag } = this.getDatabaseConfig();
90
+ const res = await this.collOpService.request('UpdateTable', Object.assign({ Tag, TableName: collectionName }, indexiesInfo));
91
+ return res;
92
+ }
93
+ async describeCollection(collectionName) {
94
+ let { Tag } = this.getDatabaseConfig();
95
+ return this.collOpService.request('DescribeTable', {
96
+ Tag,
97
+ TableName: collectionName
98
+ });
99
+ }
100
+ // 获取
101
+ async listCollections(options = {
102
+ MgoLimit: this.DEFAULT_MGO_LIMIT,
103
+ MgoOffset: this.DEFAULT_MGO_OFFSET
104
+ }) {
105
+ let { Tag } = this.getDatabaseConfig();
106
+ if (options.MgoLimit === undefined) {
107
+ options.MgoLimit = this.DEFAULT_MGO_LIMIT;
108
+ }
109
+ if (options.MgoOffset === undefined) {
110
+ options.MgoOffset = this.DEFAULT_MGO_OFFSET;
111
+ }
112
+ const res = await this.collOpService.request('ListTables', Object.assign({ Tag }, options));
113
+ if (res.Tables === null) {
114
+ // 无集合
115
+ res.Collections = [];
116
+ }
117
+ else {
118
+ // 云 API 返回转换为与TCB一致
119
+ res.Collections = res.Tables.map(item => {
120
+ item.CollectionName = item.TableName;
121
+ delete item.TableName;
122
+ return item;
123
+ });
124
+ }
125
+ delete res.Tables;
126
+ return res;
127
+ }
128
+ async createCollectionIfNotExists(collectionName) {
129
+ const existRes = await this.checkCollectionExists(collectionName);
130
+ let res;
131
+ if (!existRes.Exists) {
132
+ res = await this.createCollection(collectionName);
133
+ return {
134
+ RequestId: res.RequestId,
135
+ IsCreated: true,
136
+ ExistsResult: existRes
137
+ };
138
+ }
139
+ else {
140
+ return {
141
+ RequestId: '',
142
+ IsCreated: false,
143
+ ExistsResult: existRes
144
+ };
145
+ }
146
+ }
147
+ // 检查集合中是否存在某索引
148
+ async checkIndexExists(collectionName, indexName) {
149
+ const result = await this.describeCollection(collectionName);
150
+ let exists = result.Indexes.some(item => {
151
+ return item.Name === indexName;
152
+ });
153
+ return {
154
+ RequestId: result.RequestId,
155
+ Exists: exists
156
+ };
157
+ }
158
+ // 查询DB的数据存储分布
159
+ async distribution() {
160
+ const res = await this.dbOpService.request('DescribeDbDistribution', {
161
+ EnvId: this.envId
162
+ });
163
+ return res;
164
+ }
165
+ // 查询DB 迁移进度
166
+ async migrateStatus(jobId) {
167
+ const res = await this.dbOpService.request('DatabaseMigrateQueryInfo', {
168
+ EnvId: this.envId,
169
+ JobId: jobId
170
+ });
171
+ return res;
172
+ }
173
+ // 数据库导入数据
174
+ async import(collectionName, file, options) {
175
+ let filePath;
176
+ let fileType;
177
+ if (file['FilePath']) {
178
+ let temp = 'tmp/db-imports/';
179
+ if (options['ObjectKeyPrefix']) {
180
+ temp = options['ObjectKeyPrefix'];
181
+ delete options['ObjectKeyPrefix'];
182
+ }
183
+ filePath = path_1.default.join(temp, path_1.default.basename(file['FilePath']));
184
+ // 调用cos接口 上传文件 todo
185
+ await this.environment.getStorageService().uploadFile({
186
+ localPath: file['FilePath'],
187
+ cloudPath: filePath
188
+ });
189
+ fileType = path_1.default.extname(filePath).substring(1);
190
+ }
191
+ else if (file['ObjectKey']) {
192
+ delete options['ObjectKeyPrefix'];
193
+ filePath = file['ObjectKey'];
194
+ fileType = path_1.default.extname(filePath).substring(1);
195
+ }
196
+ else {
197
+ throw new error_1.CloudBaseError('Miss file.filePath or file.objectKey');
198
+ }
199
+ if (file['FileType']) {
200
+ fileType = file['FileType'];
201
+ }
202
+ return this.dbOpService.request('DatabaseMigrateImport', Object.assign({ CollectionName: collectionName, FilePath: filePath, FileType: fileType, EnvId: this.envId }, options));
203
+ }
204
+ // 数据库导出数据
205
+ async export(collectionName, file, options) {
206
+ let filePath;
207
+ let fileType;
208
+ if (file['ObjectKey']) {
209
+ filePath = file['ObjectKey'];
210
+ fileType = path_1.default.extname(filePath).substring(1);
211
+ }
212
+ else {
213
+ throw new error_1.CloudBaseError('Miss file.filePath or file.objectKey');
214
+ }
215
+ if (file['FileType']) {
216
+ fileType = file['FileType'];
217
+ }
218
+ return this.dbOpService.request('DatabaseMigrateExport', Object.assign({ CollectionName: collectionName, FilePath: filePath, FileType: fileType, EnvId: this.envId }, options));
219
+ }
220
+ }
221
+ DatabaseService.tcbServiceVersion = {
222
+ service: 'tcb',
223
+ version: '2018-06-08'
224
+ };
225
+ DatabaseService.flexdbServiceVersion = {
226
+ service: 'flexdb',
227
+ version: '2018-11-27'
228
+ };
229
+ __decorate([
230
+ preLazy()
231
+ ], DatabaseService.prototype, "createCollection", null);
232
+ __decorate([
233
+ preLazy()
234
+ ], DatabaseService.prototype, "deleteCollection", null);
235
+ __decorate([
236
+ preLazy()
237
+ ], DatabaseService.prototype, "updateCollection", null);
238
+ __decorate([
239
+ preLazy()
240
+ ], DatabaseService.prototype, "describeCollection", null);
241
+ __decorate([
242
+ preLazy()
243
+ ], DatabaseService.prototype, "listCollections", null);
244
+ exports.DatabaseService = DatabaseService;
package/lib/debug.js ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const index_1 = __importDefault(require("./index"));
7
+ // import { cloudBaseConfig } from '../test/config'
8
+ // const app = new CloudBase(cloudBaseConfig)
9
+ const app = new index_1.default({});
10
+ async function test() {
11
+ // const hosting = await app.hosting.getInfo()
12
+ // const { Bucket, Regoin } = hosting[0]
13
+ // const res = await app.storage.uploadFilesCustom({
14
+ // files: [
15
+ // {
16
+ // localPath: 'test/storage/test_data/data.txt',
17
+ // cloudPath: 'test/storage/test_data/data.txt'
18
+ // },
19
+ // {
20
+ // localPath: 'test/storage/test_data/download.txt',
21
+ // cloudPath: 'test/storage/test_data/download.txt'
22
+ // }
23
+ // ],
24
+ // region: Regoin,
25
+ // bucket: Bucket
26
+ // })
27
+ const res = await app.hosting.uploadFiles({
28
+ localPath: '/Users/wuyiqing/Desktop/cloudbase-demo/functions/node-sdk',
29
+ cloudPath: '',
30
+ onProgress: console.log
31
+ });
32
+ console.log(res);
33
+ }
34
+ test().catch(console.log);