2
2
3
3
const assert = require ( 'assert' )
4
4
const util = require ( 'util' )
5
- const AWS = require ( 'aws-sdk' )
6
5
7
6
class Plugin {
8
7
constructor ( serverless , options ) {
9
8
this . serverless = serverless
10
9
this . options = options || { }
10
+ this . provider = serverless . getProvider ( 'aws' )
11
11
12
12
this . hooks = {
13
13
'after:deploy:deploy' : this . afterDeploy . bind ( this )
@@ -26,23 +26,11 @@ class Plugin {
26
26
assert ( this . list ( ) . length > 0 , 'No configuration found' )
27
27
}
28
28
29
- configure ( ) {
30
- this . region = this . serverless . service . provider . region
31
-
32
- if ( this . options && this . options . region ) {
33
- this . region = this . options . region
34
- }
35
-
36
- this . dynamodb = new AWS . DynamoDB ( { region : this . region } )
37
- }
38
-
39
29
afterDeploy ( ) {
40
30
return Promise . resolve ( ) . then (
41
31
this . validate . bind ( this )
42
32
) . then (
43
- this . configure . bind ( this )
44
- ) . then (
45
- ( ) => this . serverless . cli . log ( util . format ( 'Enabling TTL setting(s) for DynamoDB (%s)' , this . region ) )
33
+ ( ) => this . serverless . cli . log ( util . format ( 'Enabling TTL setting(s) for DynamoDB (%s)' , this . options . region ) )
46
34
) . then (
47
35
( ) => this . list ( ) . map (
48
36
data => this . check ( data . table ) . then (
@@ -55,25 +43,21 @@ class Plugin {
55
43
}
56
44
57
45
check ( table ) {
58
- return this . dynamodb . describeTimeToLive (
59
- {
60
- TableName : table
61
- }
62
- ) . promise ( ) . then (
46
+ return this . provider . request ( 'DynamoDB' , 'describeTimeToLive' , {
47
+ TableName : table
48
+ } ) . then (
63
49
res => res . TimeToLiveDescription . TimeToLiveStatus === 'ENABLED'
64
50
)
65
51
}
66
52
67
53
enable ( data ) {
68
- return this . dynamodb . updateTimeToLive (
69
- {
70
- TableName : data . table ,
71
- TimeToLiveSpecification : {
72
- AttributeName : data . field ,
73
- Enabled : true
74
- }
54
+ return this . provider . request ( 'DynamoDB' , 'updateTimeToLive' , {
55
+ TableName : data . table ,
56
+ TimeToLiveSpecification : {
57
+ AttributeName : data . field ,
58
+ Enabled : true
75
59
}
76
- ) . promise ( )
60
+ } )
77
61
}
78
62
79
63
list ( ) {
0 commit comments