@stacksjs/cache 0.39.1 → 0.40.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.
@@ -0,0 +1,5 @@
1
+ declare function set(key: string, value: string | number): Promise<void>;
2
+ declare function get(key: string): Promise<string | undefined | null>;
3
+ declare function remove(key: string): Promise<void>;
4
+ declare function del(key: string): Promise<void>;
5
+ export { set, get, remove, del };
@@ -0,0 +1,66 @@
1
+ import { DynamoDB } from "@aws-sdk/client-dynamodb";
2
+ import { cache } from "@stacksjs/config";
3
+ const valueAttribute = "value";
4
+ const keyAttribute = "key";
5
+ const dynamodb = new DynamoDB({ region: cache.dynamodb.region });
6
+ async function set(key, value) {
7
+ const params = {
8
+ TableName: "cache",
9
+ Item: {
10
+ [keyAttribute]: {
11
+ S: key
12
+ },
13
+ [valueAttribute]: {
14
+ [getValueType(value)]: serialize(value)
15
+ }
16
+ }
17
+ };
18
+ await dynamodb.putItem(params);
19
+ }
20
+ async function get(key) {
21
+ const params = {
22
+ TableName: "cache",
23
+ Key: {
24
+ [keyAttribute]: {
25
+ S: key
26
+ }
27
+ }
28
+ };
29
+ const response = await dynamodb.getItem(params);
30
+ if (!response.Item)
31
+ return null;
32
+ return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N;
33
+ }
34
+ function getValueType(value) {
35
+ if (typeof value === "string")
36
+ return "S";
37
+ if (typeof value === "number")
38
+ return "N";
39
+ return "S";
40
+ }
41
+ async function remove(key) {
42
+ const params = {
43
+ TableName: "cache",
44
+ Key: {
45
+ [keyAttribute]: {
46
+ S: key
47
+ }
48
+ }
49
+ };
50
+ await dynamodb.deleteItem(params);
51
+ }
52
+ async function del(key) {
53
+ const params = {
54
+ TableName: "cache",
55
+ Key: {
56
+ [keyAttribute]: {
57
+ S: key
58
+ }
59
+ }
60
+ };
61
+ await dynamodb.deleteItem(params);
62
+ }
63
+ function serialize(value) {
64
+ return String(value);
65
+ }
66
+ export { set, get, remove, del };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * as redis from './drivers/redis';
2
2
  export * as memcached from './drivers/memcached';
3
+ export * as dynamodb from './drivers/dynamodb';
package/dist/index.mjs CHANGED
@@ -1,2 +1,3 @@
1
1
  export * as redis from "./drivers/redis.mjs";
2
2
  export * as memcached from "./drivers/memcached.mjs";
3
+ export * as dynamodb from "./drivers/dynamodb.mjs";
@@ -0,0 +1,75 @@
1
+ import { DynamoDB } from "@aws-sdk/client-dynamodb";
2
+ import { describe, expect, it } from "vitest";
3
+ const dynamodb = new DynamoDB({ region: "us-east-1" });
4
+ describe("DynamoDB test", () => {
5
+ it("it should set dynamodb cache", async () => {
6
+ await set("foo", "bar");
7
+ const value = await get("foo");
8
+ expect(value).toBe("bar");
9
+ });
10
+ it("it should get dynamodb cache", async () => {
11
+ await set("foo", "bar");
12
+ const value = await get("foo");
13
+ expect(value).toBe("bar");
14
+ });
15
+ it("it should delete dynamodb cache", async () => {
16
+ await set("foo", "bar");
17
+ await remove("foo");
18
+ const value = await get("foo");
19
+ expect(value).toBe(null);
20
+ });
21
+ });
22
+ async function set(key, value) {
23
+ const valueAttribute = "value";
24
+ const keyAttribute = "key";
25
+ const params = {
26
+ TableName: "cache",
27
+ Item: {
28
+ [keyAttribute]: {
29
+ S: key
30
+ },
31
+ [valueAttribute]: {
32
+ [getValueType(value)]: serialize(value)
33
+ }
34
+ }
35
+ };
36
+ await dynamodb.putItem(params);
37
+ }
38
+ async function get(key) {
39
+ const valueAttribute = "value";
40
+ const keyAttribute = "key";
41
+ const params = {
42
+ TableName: "cache",
43
+ Key: {
44
+ [keyAttribute]: {
45
+ S: key
46
+ }
47
+ }
48
+ };
49
+ const response = await dynamodb.getItem(params);
50
+ if (!response.Item)
51
+ return null;
52
+ return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N;
53
+ }
54
+ function getValueType(value) {
55
+ if (typeof value === "string")
56
+ return "S";
57
+ if (typeof value === "number")
58
+ return "N";
59
+ return "S";
60
+ }
61
+ async function remove(key) {
62
+ const keyAttribute = "key";
63
+ const params = {
64
+ TableName: "cache",
65
+ Key: {
66
+ [keyAttribute]: {
67
+ S: key
68
+ }
69
+ }
70
+ };
71
+ await dynamodb.deleteItem(params);
72
+ }
73
+ function serialize(value) {
74
+ return String(value);
75
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@stacksjs/cache",
3
3
  "type": "module",
4
- "version": "0.39.1",
5
- "packageManager": "pnpm@7.15.0",
4
+ "version": "0.40.0",
5
+ "packageManager": "pnpm@7.16.0",
6
6
  "description": "The Stacks way to test.",
7
7
  "author": "Chris Breuer",
8
8
  "license": "MIT",
@@ -36,22 +36,24 @@
36
36
  ],
37
37
  "engines": {
38
38
  "node": ">=v18.12.1",
39
- "pnpm": ">=7.15.0"
39
+ "pnpm": ">=7.16.0"
40
40
  },
41
41
  "peerDependencies": {
42
- "@stacksjs/config": "0.39.1",
42
+ "@stacksjs/config": "0.40.0",
43
43
  "memcached": "^2.2.2",
44
44
  "redis": "^4.5.0",
45
- "vitest": "^0.25.1"
45
+ "vitest": "^0.25.2"
46
46
  },
47
47
  "dependencies": {
48
+ "@aws-sdk/client-dynamodb": "^3.209.0",
49
+ "aws-sdk": "^2.1253.0",
48
50
  "memcached": "^2.2.2",
49
51
  "memjs": "^1.3.0"
50
52
  },
51
53
  "devDependencies": {
52
54
  "mkdist": "^0.4.0",
53
55
  "typescript": "^4.8.4",
54
- "vitest": "^0.25.1"
56
+ "vitest": "^0.25.2"
55
57
  },
56
58
  "scripts": {
57
59
  "build": "mkdist -d",