Skip to content

Commit 1788530

Browse files
committed
chore: wip
1 parent 0fcd92c commit 1788530

File tree

10 files changed

+226
-137
lines changed

10 files changed

+226
-137
lines changed

.stacks/core/arrays/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141
"typecheck": "tsc --noEmit"
4242
},
4343
"peerDependencies": {
44+
"@stacksjs/types": "workspace:*",
4445
"@stacksjs/utils": "workspace:*"
4546
},
4647
"devDependencies": {
47-
"@stacksjs/development": "workspace:*"
48+
"@stacksjs/development": "workspace:*",
49+
"@stacksjs/types": "workspace:*"
4850
}
4951
}

.stacks/core/arrays/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Arrayable, Nullable } from '@stacksjs/types'
2-
import { clamp } from '../../utils/src'
2+
import { clamp } from '@stacksjs/utils'
33

44
export function contains(needle: string, haystack: string[]) {
55
return haystack.some(hay => needle.includes(hay))
Lines changed: 119 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,119 @@
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-
5-
const valueAttribute = 'value'
6-
const keyAttribute = 'key'
7-
8-
const tableName = cache.dynamodb.table
9-
10-
const dynamodb = new DynamoDB({ region: cache.dynamodb.region })
11-
12-
async function createTable() {
13-
const tables = await dynamodb.send(new ListTablesCommand({}))
14-
15-
const tableExists = tables.TableNames?.includes('cache')
16-
17-
if (tableExists)
18-
return
19-
20-
const params = {
21-
AttributeDefinitions: [
22-
{
23-
AttributeName: 'key',
24-
AttributeType: 'S',
25-
},
26-
],
27-
KeySchema: [
28-
{
29-
AttributeName: 'key',
30-
KeyType: 'HASH',
31-
},
32-
],
33-
ProvisionedThroughput: {
34-
ReadCapacityUnits: 5,
35-
WriteCapacityUnits: 5,
36-
},
37-
TableName: tableName,
38-
}
39-
40-
await dynamodb.createTable(params)
41-
}
42-
43-
async function set(key: string, value: string | number): Promise<void> {
44-
const params: PutItemCommandInput = {
45-
TableName: tableName,
46-
Item: {
47-
[keyAttribute]: {
48-
S: key,
49-
},
50-
[valueAttribute]: {
51-
[getValueType(value)]: serialize(value),
52-
},
53-
},
54-
}
55-
56-
await dynamodb.putItem(params)
57-
}
58-
59-
async function get(key: string): Promise<string | undefined | null> {
60-
const params = {
61-
TableName: tableName,
62-
Key: {
63-
[keyAttribute]: {
64-
S: key,
65-
},
66-
},
67-
}
68-
69-
const response = await dynamodb.getItem(params)
70-
71-
if (!response.Item)
72-
return null
73-
74-
return response.Item[valueAttribute].S ?? response.Item[valueAttribute].N
75-
}
76-
77-
function getValueType(value: string | number) {
78-
if (typeof value === 'string')
79-
return 'S'
80-
81-
if (typeof value === 'number')
82-
return 'N'
83-
84-
return 'S'
85-
}
86-
87-
async function remove(key: string): Promise<void> {
88-
const params = {
89-
TableName: tableName,
90-
Key: {
91-
[keyAttribute]: {
92-
S: key,
93-
},
94-
},
95-
}
96-
97-
await dynamodb.deleteItem(params)
98-
}
99-
100-
async function del(key: string): Promise<void> {
101-
const params = {
102-
TableName: tableName,
103-
Key: {
104-
[keyAttribute]: {
105-
S: key,
106-
},
107-
},
108-
}
109-
110-
await dynamodb.deleteItem(params)
111-
}
112-
113-
function serialize(value: string | number) {
114-
return String(value)
115-
}
116-
117-
export { set, get, remove, del, createTable }
1+
export {}
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 }
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import type { Client } from 'memjs'
2-
import { Memcached } from 'memjs'
1+
export {}
32

4-
const client: Client = Memcached.Client.create('127.0.0.1:11211')
3+
// import { Client } from 'memjs'
54

6-
async function set(key: string, value: any, duration: number): Promise<void> {
7-
await client.set(key, value, { expires: duration })
8-
}
5+
// const client = Client.create('127.0.0.1:11211')
96

10-
async function get(key: string): Promise<any> {
11-
const value = await client.get(key)
7+
// async function set(key: string, value: any, duration: number): Promise<void> {
8+
// await client.set(key, value, { expires: duration })
9+
// }
1210

13-
return value.toString()
14-
}
11+
// async function get(key: string): Promise<any> {
12+
// const value = await client.get(key)
1513

16-
async function del(key: string): Promise<void> {
17-
await client.delete(key)
18-
}
14+
// return value.toString()
15+
// }
1916

20-
export { set, get, del }
17+
// async function del(key: string): Promise<void> {
18+
// await client.delete(key)
19+
// }
20+
21+
// export { set, get, del }

.stacks/core/cache/src/drivers/redis.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ const client: RedisClientType = createClient({
1010
password: '',
1111
})
1212

13-
await client.connect()
13+
// await client.connect()
14+
// client.on('error', (error) => {
15+
// console.error(error)
16+
// })
1417

15-
async function set(key, value): Promise<void> {
18+
async function set(key: string, value: any): Promise<void> {
1619
await client.set(key, value)
1720
}
1821

.stacks/core/types/src/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,3 @@ export interface AppOptions {
128128

129129
// inspect: <InspectOptions>{},
130130
}
131-
132-
export interface CacheOptions {}

.stacks/core/types/src/cache.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export interface CacheOptions {
2+
/**
3+
* **Cache Driver**
4+
*
5+
* This value determines the cache driver that will be used by your application to store
6+
* cached data. By default, Stacks uses the "redis" driver, which stores cached data in
7+
* in a self-configufed Redis instance. You may use any of the other drivers provided
8+
* by Stacks or write your own custom driver.
9+
*
10+
* @default "redis"
11+
* @example "redis"
12+
* @example "memcached"
13+
* @example "dynamodb"
14+
*
15+
* @see https://stacks.js.org/docs/cache
16+
*/
17+
driver: string
18+
19+
/**
20+
* **Cache Prefix**
21+
*
22+
* This value determines the prefix that will be used when storing items in the cache. This
23+
* prefix may be useful when multiple applications are sharing the same cache driver so that
24+
* they may avoid collisions when attempting to retrieve items from the cache.
25+
*
26+
* @default string "stacks"
27+
* @example "stacks"
28+
*
29+
* @see https://stacks.js.org/docs/cache
30+
*/
31+
prefix: string
32+
33+
/**
34+
* **Cache TTL**
35+
*
36+
* This value determines the default time to live for items stored in the cache. This value
37+
* may be overridden when storing items in the cache. If no value is specified, the cache
38+
* driver will default to a reasonable time to live for the given item.
39+
*
40+
* @default number 3600
41+
* @example 3600
42+
* @example 3600 * 24
43+
* @example -1 (never expires)
44+
*/
45+
ttl: number
46+
47+
redis: {
48+
connection: string
49+
50+
/**
51+
* **Redis Host**
52+
*
53+
* This value determines the host that will be used to connect to the Redis server.
54+
*
55+
* @default string "localhost"
56+
* @example "localhost"
57+
*/
58+
host: string
59+
60+
/**
61+
* **Redis Port**
62+
*
63+
* This value determines the port that will be used to connect to the Redis server.
64+
*
65+
* @default number 6379
66+
* @example 6379
67+
*/
68+
port: number
69+
}
70+
71+
memcached: {
72+
}
73+
74+
dynamodb: {
75+
}
76+
77+
}

.stacks/core/types/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './app'
22
export * from './auto-imports'
33
export * from './build'
44
export * from './components'
5+
export * from './cache'
56
export * from './cli'
67
export * from './cron-jobs'
78
export * from './database'

0 commit comments

Comments
 (0)