@stacksjs/cache 0.63.1 → 0.64.0
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/index.js +33 -2
- package/dist/index.js.map +503 -3
- package/package.json +4 -3
- package/src/drivers/dynamodb.ts +104 -119
- package/src/drivers/index.ts +1 -1
- package/src/drivers/type.ts +10 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/cache",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.64.0",
|
|
5
5
|
"description": "Caching the Stacks way.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -49,8 +49,9 @@
|
|
|
49
49
|
"prepublishOnly": "bun run build"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@aws-sdk/client-dynamodb": "^3.
|
|
53
|
-
"@stacksjs/config": "latest"
|
|
52
|
+
"@aws-sdk/client-dynamodb": "^3.637.0",
|
|
53
|
+
"@stacksjs/config": "latest",
|
|
54
|
+
"bentocache": "^1.0.0-beta.9"
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|
|
56
57
|
"@stacksjs/development": "latest"
|
package/src/drivers/dynamodb.ts
CHANGED
|
@@ -1,119 +1,104 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
// Key: {
|
|
106
|
-
// [keyAttribute]: {
|
|
107
|
-
// S: key,
|
|
108
|
-
// },
|
|
109
|
-
// },
|
|
110
|
-
// }
|
|
111
|
-
|
|
112
|
-
// await dynamodb.deleteItem(params)
|
|
113
|
-
// }
|
|
114
|
-
|
|
115
|
-
// function serialize(value: string | number) {
|
|
116
|
-
// return String(value)
|
|
117
|
-
// }
|
|
118
|
-
|
|
119
|
-
// export { set, get, remove, del, createTable }
|
|
1
|
+
import type { PutItemCommandInput } from '@aws-sdk/client-dynamodb'
|
|
2
|
+
import { DynamoDB, ListTablesCommand } from '@aws-sdk/client-dynamodb'
|
|
3
|
+
import { cache } from '@stacksjs/config'
|
|
4
|
+
import type { CacheDriver } from './type'
|
|
5
|
+
|
|
6
|
+
const valueAttribute = 'value'
|
|
7
|
+
const keyAttribute = 'key'
|
|
8
|
+
const tableName = cache.drivers?.dynamodb?.table
|
|
9
|
+
|
|
10
|
+
const client = new DynamoDB({ region: cache.drivers?.dynamodb?.region })
|
|
11
|
+
|
|
12
|
+
function getValueType(value: string | number) {
|
|
13
|
+
if (typeof value === 'string') return 'S'
|
|
14
|
+
if (typeof value === 'number') return 'N'
|
|
15
|
+
return 'S'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function serialize(value: string | number) {
|
|
19
|
+
return String(value)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const dynamodb: CacheDriver = {
|
|
23
|
+
async createTable() {
|
|
24
|
+
const tables = await client.send(new ListTablesCommand({}))
|
|
25
|
+
|
|
26
|
+
const tableExists = tables.TableNames?.includes('cache')
|
|
27
|
+
|
|
28
|
+
if (tableExists) return
|
|
29
|
+
|
|
30
|
+
const params = {
|
|
31
|
+
AttributeDefinitions: [
|
|
32
|
+
{
|
|
33
|
+
AttributeName: 'key',
|
|
34
|
+
AttributeType: 'S',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
KeySchema: [
|
|
38
|
+
{
|
|
39
|
+
AttributeName: 'key',
|
|
40
|
+
KeyType: 'HASH',
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
ProvisionedThroughput: {
|
|
44
|
+
ReadCapacityUnits: 5,
|
|
45
|
+
WriteCapacityUnits: 5,
|
|
46
|
+
},
|
|
47
|
+
TableName: tableName,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
client.createTable(params)
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
async set(key: string, value: string | number): Promise<void> {
|
|
54
|
+
const params: PutItemCommandInput = {
|
|
55
|
+
TableName: tableName,
|
|
56
|
+
Item: {
|
|
57
|
+
[keyAttribute]: {
|
|
58
|
+
S: key,
|
|
59
|
+
},
|
|
60
|
+
[valueAttribute]: {
|
|
61
|
+
[getValueType(value)]: serialize(value),
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
await client.putItem(params)
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
async get(key: string): Promise<string | undefined | null> {
|
|
70
|
+
const params = {
|
|
71
|
+
TableName: tableName,
|
|
72
|
+
Key: {
|
|
73
|
+
[keyAttribute]: {
|
|
74
|
+
S: key,
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const response = await client.getItem(params)
|
|
80
|
+
|
|
81
|
+
if (!response.Item) return null
|
|
82
|
+
|
|
83
|
+
return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
async remove(key: string): Promise<void> {
|
|
87
|
+
const params = {
|
|
88
|
+
TableName: tableName,
|
|
89
|
+
Key: {
|
|
90
|
+
[keyAttribute]: {
|
|
91
|
+
S: key,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
await client.deleteItem(params)
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
del(key: string): Promise<void> {
|
|
100
|
+
return this.remove(key)
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
client,
|
|
104
|
+
}
|
package/src/drivers/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { dynamodb } from './dynamodb'
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DynamoDB } from '@aws-sdk/client-dynamodb'
|
|
2
|
+
|
|
3
|
+
export type CacheDriver = {
|
|
4
|
+
createTable: () => Promise<void>
|
|
5
|
+
set: (key: string, value: string | number) => Promise<void>
|
|
6
|
+
get: (key: string) => Promise<string | undefined | null>
|
|
7
|
+
remove: (key: string) => Promise<void>
|
|
8
|
+
del: (key: string) => Promise<void>
|
|
9
|
+
client: DynamoDB
|
|
10
|
+
}
|