Skip to content

Commit a9e84e7

Browse files
committed
docs: add docs for the key-value resource adapter
AdminForth/1735/kv-adapters-and-storage-s3-com
1 parent 85774b2 commit a9e84e7

1 file changed

Lines changed: 98 additions & 3 deletions

File tree

adminforth/documentation/docs/tutorial/06-Adapters/07-key-value-adapters.md

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Key-value adapters are used to store data in a key-value format. They provide a
1111
## RAM Adapter
1212

1313
```bash
14-
pnpm i @adminforth/key-value-adapter-ram
14+
pnpm add @adminforth/key-value-adapter-ram
1515
```
1616

1717
The RAM adapter is a simple in-memory key-value store. It keeps data in process memory, so it is not suitable for multi-process deployments because each process would have its own isolated store. In that case you need a centralized KV adapter such as Redis.
@@ -27,7 +27,7 @@ Cons:
2727
## Redis Adapter
2828

2929
```bash
30-
pnpm i @adminforth/key-value-adapter-redis
30+
pnpm add @adminforth/key-value-adapter-redis
3131
```
3232

3333
Redis uses in-memory storage with $O(1)$ get complexity. It is a great fit for lightweight workloads that fit in RAM, and it also works well for multi-process or replica-based installations as a centralized store. If persistence across restarts is important, configure Redis persistence separately.
@@ -45,7 +45,7 @@ adapter.set('test-key', 'test-value', 120);
4545
## LevelDB Adapter
4646

4747
```bash
48-
pnpm i @adminforth/key-value-adapter-leveldb
48+
pnpm add @adminforth/key-value-adapter-leveldb
4949
```
5050

5151
LevelDB uses disk storage with $O(\log n)$ get complexity. It is a good fit for large or persistent KV datasets that still require fast lookups but do not efficiently fit in RAM. This is a single-process adapter only, so multiple admin processes should not point to the same LevelDB directory.
@@ -60,4 +60,99 @@ const adapter = new LevelDBKeyValueAdapter({
6060
});
6161

6262
adapter.set('test-key', 'test-value', 120);
63+
```
64+
65+
## Resource-based adapter
66+
```bash
67+
pnpm add @adminforth/key-value-adapter-resource
68+
```
69+
70+
Resource adapter uses adminforth resource to store key/value pairs.
71+
72+
### Setup
73+
74+
1) Add schema in `./schema.prisma` file
75+
76+
```
77+
model key_values {
78+
key String @id
79+
value String
80+
collection String?
81+
}
82+
```
83+
84+
2) Apply migration
85+
```bash
86+
pnpm makemigration --name add-adminforth-key-value-tables ; pnpm migrate:local
87+
```
88+
89+
3) Create resource
90+
91+
```ts title="./resources/key_value_resource.ts"
92+
import AdminForth, { AdminForthDataTypes } from 'adminforth';
93+
import type { AdminForthResourceInput, AdminUser } from 'adminforth';
94+
95+
async function allowedForSuperAdmins({ adminUser }: { adminUser: AdminUser }): Promise<boolean> {
96+
return adminUser.dbUser.role === 'superadmin';
97+
}
98+
99+
export default {
100+
dataSource: 'maindb',
101+
table: 'key_values',
102+
resourceId: 'key_values',
103+
label: 'Key values',
104+
columns: [
105+
{
106+
name: 'key',
107+
primaryKey: true,
108+
type: AdminForthDataTypes.STRING,
109+
},
110+
{
111+
name: 'value',
112+
type: AdminForthDataTypes.STRING,
113+
},
114+
{
115+
name: 'collection',
116+
type: AdminForthDataTypes.STRING,
117+
}
118+
],
119+
options: {
120+
allowedActions: {
121+
list: allowedForSuperAdmins,
122+
show: allowedForSuperAdmins,
123+
create: false,
124+
edit: false,
125+
delete: false,
126+
},
127+
},
128+
} as AdminForthResourceInput;
129+
```
130+
131+
4) Register resource
132+
133+
```ts title="./index.ts"
134+
import key_value_resource from './resources/key_value_resource.js';
135+
136+
export const admin = new AdminForth({
137+
...
138+
resources: [
139+
...
140+
key_value_resource,
141+
],
142+
...
143+
});
144+
145+
```
146+
147+
5) Create instance of adapter
148+
149+
```ts
150+
import ResourceKeyValueAdapter from '@adminforth/key-value-adapter-resource'
151+
152+
const adapter = new ResourceKeyValueAdapter({
153+
resourceId: 'key_values',
154+
keyField: 'key',
155+
valueField: 'value',
156+
collectionField: 'collection',
157+
})
63158
```

0 commit comments

Comments
 (0)