1
- import { DynamoDB , ListTablesCommand } from '@aws-sdk/client-dynamodb'
2
- import { expect , it } from '@stacksjs/testing'
3
-
4
- const dynamodb = new DynamoDB ( { region : 'us-east-1' } )
5
-
6
- describe ( 'dynamoDB test' , ( ) => {
7
- it ( 'it should create a table for dynamodb cache' , async ( ) => {
8
- await createTable ( )
9
- } )
10
-
11
- it ( 'it should set dynamodb cache' , async ( ) => {
12
- await set ( 'foo' , 'bar' )
13
- const value = await get ( 'foo' )
14
- expect ( value ) . toBe ( 'bar' )
15
- } )
16
-
17
- it ( 'it should get dynamodb cache' , async ( ) => {
18
- await set ( 'foo' , 'bar' )
19
- const value = await get ( 'foo' )
20
- expect ( value ) . toBe ( 'bar' )
21
- } )
22
-
23
- it ( 'it should delete dynamodb cache' , async ( ) => {
24
- await set ( 'foo' , 'bar' )
25
- await remove ( 'foo' )
26
- const value = await get ( 'foo' )
27
- expect ( value ) . toBe ( null )
28
- } )
29
- } )
30
-
31
- async function createTable ( ) {
32
- const tables = await dynamodb . send ( new ListTablesCommand ( { } ) )
33
-
34
- const tableExists = tables . TableNames ?. includes ( 'cache' )
35
-
36
- if ( tableExists )
37
- return
38
-
39
- const params = {
40
- AttributeDefinitions : [
41
- {
42
- AttributeName : 'key' ,
43
- AttributeType : 'S' ,
44
- } ,
45
- ] ,
46
- KeySchema : [
47
- {
48
- AttributeName : 'key' ,
49
- KeyType : 'HASH' ,
50
- } ,
51
- ] ,
52
- ProvisionedThroughput : {
53
- ReadCapacityUnits : 5 ,
54
- WriteCapacityUnits : 5 ,
55
- } ,
56
- TableName : 'cache' ,
57
- }
58
-
59
- await dynamodb . createTable ( params )
60
- }
61
-
62
- // TODO: needs to be imported to cache package
63
- async function set ( key : string , value : string | number ) : Promise < void > {
64
- // eslint-disable-next-line no-console
65
- console . log ( 'set' , key , value )
66
- return Promise . resolve ( )
67
- // const valueAttribute = 'value'
68
- // const keyAttribute = 'key'
69
- //
70
- // const params: PutItemCommandInput = {
71
- // TableName: 'cache',
72
- // Item: {
73
- // [keyAttribute]: {
74
- // S: key,
75
- // },
76
- // [valueAttribute]: {
77
- // [getValueType(value)]: serialize(value),
78
- // },
79
- // },
80
- // }
81
- //
82
- // await dynamodb.putItem(params)
83
- }
84
-
85
- // TODO: needs to be imported to cache package
86
- async function get ( key : string ) : Promise < string | undefined | null > {
87
- const valueAttribute = 'value'
88
- const keyAttribute = 'key'
89
-
90
- const params = {
91
- TableName : 'cache' ,
92
- Key : {
93
- [ keyAttribute ] : {
94
- S : key ,
95
- } ,
96
- } ,
97
- }
98
-
99
- const response = await dynamodb . getItem ( params )
100
-
101
- if ( ! response . Item )
102
- return null
103
-
104
- return response . Item [ valueAttribute ] . S ?? response . Item [ valueAttribute ] . N
105
- }
106
-
107
- // TODO: needs to be imported to cache package
108
- async function remove ( key : string ) : Promise < void > {
109
- const keyAttribute = 'key'
110
-
111
- const params = {
112
- TableName : 'cache' ,
113
- Key : {
114
- [ keyAttribute ] : {
115
- S : key ,
116
- } ,
117
- } ,
118
- }
119
-
120
- await dynamodb . deleteItem ( params )
121
- }
122
-
123
- // TODO: needs to be imported to cache package
124
- // async function del(key: string): Promise<void> {
1
+ // import { DynamoDB, ListTablesCommand } from '@aws-sdk/client-dynamodb'
2
+ // import { expect, it } from '@stacksjs/testing'
3
+ //
4
+ // const dynamodb = new DynamoDB({ region: 'us-east-1' })
5
+ //
6
+ // describe('dynamoDB test', () => {
7
+ // it('it should create a table for dynamodb cache', async () => {
8
+ // await createTable()
9
+ // })
10
+ //
11
+ // it('it should set dynamodb cache', async () => {
12
+ // await set('foo', 'bar')
13
+ // const value = await get('foo')
14
+ // expect(value).toBe('bar')
15
+ // })
16
+ //
17
+ // it('it should get dynamodb cache', async () => {
18
+ // await set('foo', 'bar')
19
+ // const value = await get('foo')
20
+ // expect(value).toBe('bar')
21
+ // })
22
+ //
23
+ // it('it should delete dynamodb cache', async () => {
24
+ // await set('foo', 'bar')
25
+ // await remove('foo')
26
+ // const value = await get('foo')
27
+ // expect(value).toBe(null)
28
+ // })
29
+ // })
30
+ //
31
+ // async function createTable() {
32
+ // const tables = await dynamodb.send(new ListTablesCommand({}))
33
+ //
34
+ // const tableExists = tables.TableNames?.includes('cache')
35
+ //
36
+ // if (tableExists)
37
+ // return
38
+ //
39
+ // const params = {
40
+ // AttributeDefinitions: [
41
+ // {
42
+ // AttributeName: 'key',
43
+ // AttributeType: 'S',
44
+ // },
45
+ // ],
46
+ // KeySchema: [
47
+ // {
48
+ // AttributeName: 'key',
49
+ // KeyType: 'HASH',
50
+ // },
51
+ // ],
52
+ // ProvisionedThroughput: {
53
+ // ReadCapacityUnits: 5,
54
+ // WriteCapacityUnits: 5,
55
+ // },
56
+ // TableName: 'cache',
57
+ // }
58
+ //
59
+ // await dynamodb.createTable(params)
60
+ // }
61
+ //
62
+ // // TODO: needs to be imported to cache package
63
+ // async function set(key: string, value: string | number): Promise<void> {
64
+ // // eslint-disable-next-line no-console
65
+ // console.log('set', key, value)
66
+ // return Promise.resolve()
67
+ // // const valueAttribute = 'value'
68
+ // // const keyAttribute = 'key'
69
+ // //
70
+ // // const params: PutItemCommandInput = {
71
+ // // TableName: 'cache',
72
+ // // Item: {
73
+ // // [keyAttribute]: {
74
+ // // S: key,
75
+ // // },
76
+ // // [valueAttribute]: {
77
+ // // [getValueType(value)]: serialize(value),
78
+ // // },
79
+ // // },
80
+ // // }
81
+ // //
82
+ // // await dynamodb.putItem(params)
83
+ // }
84
+ //
85
+ // // TODO: needs to be imported to cache package
86
+ // async function get(key: string): Promise<string | undefined | null> {
87
+ // const valueAttribute = 'value'
88
+ // const keyAttribute = 'key'
89
+ //
90
+ // const params = {
91
+ // TableName: 'cache',
92
+ // Key: {
93
+ // [keyAttribute]: {
94
+ // S: key,
95
+ // },
96
+ // },
97
+ // }
98
+ //
99
+ // const response = await dynamodb.getItem(params)
100
+ //
101
+ // if (!response.Item)
102
+ // return null
103
+ //
104
+ // return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N
105
+ // }
106
+ //
107
+ // // TODO: needs to be imported to cache package
108
+ // async function remove(key: string): Promise<void> {
109
+ // const keyAttribute = 'key'
110
+ //
125
111
// const params = {
126
112
// TableName: 'cache',
127
113
// Key: {
@@ -130,6 +116,20 @@ async function remove(key: string): Promise<void> {
130
116
// },
131
117
// },
132
118
// }
133
-
119
+ //
134
120
// await dynamodb.deleteItem(params)
135
121
// }
122
+ //
123
+ // // TODO: needs to be imported to cache package
124
+ // // async function del(key: string): Promise<void> {
125
+ // // const params = {
126
+ // // TableName: 'cache',
127
+ // // Key: {
128
+ // // [keyAttribute]: {
129
+ // // S: key,
130
+ // // },
131
+ // // },
132
+ // // }
133
+ //
134
+ // // await dynamodb.deleteItem(params)
135
+ // // }
0 commit comments