@stacksjs/cache 0.63.1 → 0.64.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/cache",
3
3
  "type": "module",
4
- "version": "0.63.1",
4
+ "version": "0.64.1",
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.632.0",
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"
@@ -1,119 +1,104 @@
1
- export type {}
2
-
3
- // import type { PutItemCommandInput } from '@aws-sdk/client-dynamodb'
4
- // import { DynamoDB, ListTablesCommand } from '@aws-sdk/client-dynamodb'
5
- // import { cache } from '@stacksjs/config'
6
-
7
- // const valueAttribute = 'value'
8
- // const keyAttribute = 'key'
9
-
10
- // const tableName = cache.dynamodb.table
11
-
12
- // const dynamodb = new DynamoDB({ region: cache.dynamodb.region })
13
-
14
- // async function createTable() {
15
- // const tables = await dynamodb.send(new ListTablesCommand({}))
16
-
17
- // const tableExists = tables.TableNames?.includes('cache')
18
-
19
- // if (tableExists)
20
- // return
21
-
22
- // const params = {
23
- // AttributeDefinitions: [
24
- // {
25
- // AttributeName: 'key',
26
- // AttributeType: 'S',
27
- // },
28
- // ],
29
- // KeySchema: [
30
- // {
31
- // AttributeName: 'key',
32
- // KeyType: 'HASH',
33
- // },
34
- // ],
35
- // ProvisionedThroughput: {
36
- // ReadCapacityUnits: 5,
37
- // WriteCapacityUnits: 5,
38
- // },
39
- // TableName: tableName,
40
- // }
41
-
42
- // await dynamodb.createTable(params)
43
- // }
44
-
45
- // async function set(key: string, value: string | number): Promise<void> {
46
- // const params: PutItemCommandInput = {
47
- // TableName: tableName,
48
- // Item: {
49
- // [keyAttribute]: {
50
- // S: key,
51
- // },
52
- // [valueAttribute]: {
53
- // [getValueType(value)]: serialize(value),
54
- // },
55
- // },
56
- // }
57
-
58
- // await dynamodb.putItem(params)
59
- // }
60
-
61
- // async function get(key: string): Promise<string | undefined | null> {
62
- // const params = {
63
- // TableName: tableName,
64
- // Key: {
65
- // [keyAttribute]: {
66
- // S: key,
67
- // },
68
- // },
69
- // }
70
-
71
- // const response = await dynamodb.getItem(params)
72
-
73
- // if (!response.Item)
74
- // return null
75
-
76
- // return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N
77
- // }
78
-
79
- // function getValueType(value: string | number) {
80
- // if (typeof value === 'string')
81
- // return 'S'
82
-
83
- // if (typeof value === 'number')
84
- // return 'N'
85
-
86
- // return 'S'
87
- // }
88
-
89
- // async function remove(key: string): Promise<void> {
90
- // const params = {
91
- // TableName: tableName,
92
- // Key: {
93
- // [keyAttribute]: {
94
- // S: key,
95
- // },
96
- // },
97
- // }
98
-
99
- // await dynamodb.deleteItem(params)
100
- // }
101
-
102
- // async function del(key: string): Promise<void> {
103
- // const params = {
104
- // TableName: tableName,
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
+ }
@@ -1 +1 @@
1
- export * as dynamodb from './dynamodb'
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
+ }