|
1 | | -import { AsyncAdapter, NodeProvider } from '@stenodb/node' |
2 | 1 | import fp from 'fastify-plugin' |
3 | | -import type { Steno } from '@stenodb/node' |
4 | | -import type { FastifyInstance } from 'fastify' |
5 | | -import './types.js' |
| 2 | +import { StenoPlugin } from './plugin.js' |
6 | 3 |
|
7 | | -function fastifyPlugin( |
8 | | - fastify: FastifyInstance, |
9 | | - options: Steno.NodeProviderOptions, |
10 | | - next: () => void |
11 | | -) { |
12 | | - const database = new Map<string, Steno.NodeProvider<unknown>>() |
13 | | - const provider = new NodeProvider(options) |
14 | | - |
15 | | - async function createDatabase<T>( |
16 | | - name: string, |
17 | | - entity: Steno.Entity<T>, |
18 | | - initialData?: T |
19 | | - ): Promise<void> { |
20 | | - const adapter = new AsyncAdapter(name, entity, initialData) |
21 | | - const db = await provider.create(adapter) |
22 | | - await db.read() |
23 | | - database.set(name, db) |
24 | | - } |
25 | | - |
26 | | - function getDatabase<T>(name: string): Steno.NodeProvider<T> | undefined { |
27 | | - return database.get(name) as Steno.NodeProvider<T> |
28 | | - } |
29 | | - |
30 | | - fastify.decorate('steno', { |
31 | | - create: createDatabase, |
32 | | - get: getDatabase |
33 | | - }) |
34 | | - |
35 | | - next() |
36 | | -} |
37 | | - |
38 | | -const FastifySteno = fp(fastifyPlugin, { |
| 4 | +const FastifySteno = fp(StenoPlugin.createInstance, { |
39 | 5 | name: '@stenodb/fastify', |
40 | 6 | fastify: '4.x' |
41 | 7 | }) |
42 | 8 |
|
43 | 9 | export { FastifySteno } |
44 | | -export default fastifyPlugin |
| 10 | +export default StenoPlugin.createInstance |
0 commit comments