1- import Tablestore20201209 , * as ots from '@alicloud/tablestore20201209' ;
1+ import Client , {
2+ DeleteInstanceRequest ,
3+ CreateInstanceRequestTags ,
4+ CreateInstanceRequest ,
5+ GetInstanceRequest ,
6+ } from '@alicloud/tablestore20201209' ;
27import { Config } from '@alicloud/credentials' ;
38import TableStore from 'tablestore' ;
49import * as teaUtil from '@alicloud/tea-util' ;
510import { Credentials } from '../tyes' ;
611import { formatOtsTableParam , logger } from '../common' ;
712
8- const loadAlicloudTableStoreClient = (
9- region : string ,
10- credentials : Credentials ,
11- ) : Tablestore20201209 => {
13+ const loadAlicloudTableStoreClient = ( region : string , credentials : Credentials ) : Client => {
1214 const credentialsConfig = new Config ( { ...credentials , type : 'access_key' } ) ;
1315 credentialsConfig . endpoint = `tablestore.${ region } .aliyuncs.com` ;
1416
15- return new Tablestore20201209 ( credentialsConfig ) ;
17+ return new Client ( credentialsConfig ) ;
1618} ;
1719
1820const loadTableStore = (
@@ -46,12 +48,12 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
4648 tags ?: Array < { key : string ; value : string } > ;
4749 } ) => {
4850 const tags = rawTags ?. map (
49- ( tag ) => new ots . CreateInstanceRequestTags ( { key : tag . key , value : tag . value } ) ,
51+ ( tag ) => new CreateInstanceRequestTags ( { key : tag . key , value : tag . value } ) ,
5052 ) ;
5153 const networkTypeACL =
5254 network . type === 'PRIVATE' ? [ 'VPC' ] : network . type === 'PUBLIC' ? [ 'PUBLIC' ] : [ ] ;
5355
54- const createInstanceRequest = new ots . CreateInstanceRequest ( {
56+ const createInstanceRequest = new CreateInstanceRequest ( {
5557 instanceName : instanceName ,
5658 clusterType : clusterType ,
5759 networkTypeACL,
@@ -93,7 +95,7 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
9395 const runtime = new teaUtil . RuntimeOptions ( { } ) ;
9496 const headers : { [ key : string ] : string } = { } ;
9597 const response = await alicloudTableStoreClient . getInstanceWithOptions (
96- new ots . GetInstanceRequest ( { instanceName } ) ,
98+ new GetInstanceRequest ( { instanceName } ) ,
9799 headers ,
98100 runtime ,
99101 ) ;
@@ -161,6 +163,7 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
161163 const {
162164 tableMeta : { tableName, primaryKey, definedColumn : columns } ,
163165 reservedThroughput,
166+ tableOptions,
164167 } = formatOtsTableParam ( params ) ;
165168
166169 const table = await client . describeTable ( { tableName } ) ;
@@ -191,10 +194,10 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
191194
192195 if ( ! matchingCol ) {
193196 // Column exists in table but not in new columns - delete
194- acc . deletedColumns . push ( existingCol ) ;
197+ acc . deletedColumns ! . push ( existingCol ) ;
195198 } else if ( matchingCol . type !== existingCol . type ) {
196199 // Column exists in both but type is different
197- acc . typeChangedColumns . push ( matchingCol ) ;
200+ acc . modifiedColumns ! . push ( matchingCol ) ;
198201 }
199202
200203 return acc ;
@@ -219,7 +222,7 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
219222
220223 // @TODO : Handle column deletion and addition if needed
221224
222- const result = await client . updateTable ( tableParam ) ;
225+ const result = await client . updateTable ( { tableName , tableOptions , reservedThroughput } ) ;
223226
224227 logger . info ( result , `Table updated successfully` ) ;
225228
@@ -238,9 +241,40 @@ export const loadTableStoreClient = (regionId: string, credentials: Credentials)
238241 tableName : string ;
239242 } ) => {
240243 const client = loadTableStore ( instanceName , regionId , credentials ) ;
241- const result = ( await client . deleteTable ( { tableName } ) ) as { RequestId : string } ;
244+ try {
245+ const result = ( await client . deleteTable ( { tableName } ) ) as { RequestId : string } ;
246+ return { instanceName, tableName, requestId : result ?. RequestId } ;
247+ } catch ( error ) {
248+ logger . error ( { instanceName, tableName, error } , `Failed to delete table` ) ;
249+ if ( ( error as { code : number } ) . code === 404 ) {
250+ return { instanceName, tableName } ;
251+ }
252+ throw error ;
253+ }
254+ } ,
255+
256+ deleteInstance : async ( instanceName : string ) => {
257+ const client = loadTableStore ( instanceName , regionId , credentials ) ;
258+
259+ const tables = await client . listTable ( { instanceName } ) ;
260+ if ( tables . tableNames . length != 0 ) {
261+ return ;
262+ }
263+ try {
264+ await alicloudTableStoreClient . deleteInstance ( new DeleteInstanceRequest ( { instanceName } ) ) ;
265+ } catch ( error ) {
266+ logger . error ( { instanceName, error } , `Failed to delete instance` ) ;
267+ const { statusCode, code } = error as { statusCode : number ; code : string } ;
268+ if ( ( statusCode === 400 && code === 'IllegalOp' ) || statusCode === 404 ) {
269+ return { instanceName } ;
270+ }
271+
272+ throw error ;
273+ }
274+
275+ logger . info ( { instanceName } , `Instance deleted successfully` ) ;
242276
243- return { instanceName, tableName , requestId : result ?. RequestId } ;
277+ return { instanceName } ;
244278 } ,
245279 } ;
246280} ;
0 commit comments