Skip to content

Commit 3406e8c

Browse files
committed
feat: add memory adapter
1 parent 8de0c1c commit 3406e8c

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"test:push-schema": "prisma db push --schema ./tests/prisma/schema.prisma",
1010
"build": "tsc -p tsconfig.build.json",
1111
"test": "vitest",
12-
"lint": "prettier --check . && eslint .",
12+
"lint": "prettier --write . && eslint .",
1313
"format": "prettier --write .",
1414
"postinstall": "husky install",
1515
"commit": "git-cz"

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,6 @@ export function cache(opts: CacheOptions) {
161161
});
162162
});
163163
}
164+
165+
export * from './types';
166+
export * from './storage';

src/storage/acebase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AceBase } from 'acebase';
2+
import type { Adapter } from '.';
23
import hash from 'object-hash';
3-
import { Adapter } from '.';
44

55
export function adapterAceBase(acebase: AceBase): Adapter {
66
return ({ logger }) => ({

src/storage/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ export interface Database {
2222
clear(model: string): Promise<void>;
2323
clearAll(): Promise<void>;
2424
}
25+
26+
export * from './acebase';
27+
export * from './memory';

src/storage/memory.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { Adapter } from '.';
2+
import hash from 'object-hash';
3+
4+
export function adapterMemory(): Adapter {
5+
const storage = new Map<string, string>();
6+
return ({ logger }) => ({
7+
async get({ model, operation, args }) {
8+
const queryHash = hash({ operation, args });
9+
const key = `${model}:${queryHash}`;
10+
11+
logger.module('memory').log(`Get key ${key}`);
12+
const value = storage.get(key);
13+
if (value) {
14+
logger.module('memory').log(`Found value:`, value);
15+
return JSON.parse(value);
16+
}
17+
return null;
18+
},
19+
async set({ model, operation, args }, value) {
20+
const queryHash = hash({ operation, args });
21+
const key = `${model}:${queryHash}`;
22+
23+
logger.module('memory').log(`Set key ${key}`);
24+
storage.set(key, JSON.stringify(value));
25+
},
26+
async clear(model) {
27+
for (const [key] of storage.entries()) {
28+
if (key.startsWith(`${model}:`)) {
29+
storage.delete(key);
30+
}
31+
}
32+
},
33+
async clearAll() {
34+
storage.clear();
35+
}
36+
});
37+
}

tests/memory.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { adapterMemory } from '@/storage';
2+
import { testAdapter } from './adapter-tests';
3+
4+
const adapter = adapterMemory();
5+
6+
await testAdapter(adapter);

tests/prisma/dev.db

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)