Skip to content

Commit f0c1f70

Browse files
committed
feat(packages): add @stenodb/fastify
1 parent 19275b1 commit f0c1f70

File tree

11 files changed

+489
-51
lines changed

11 files changed

+489
-51
lines changed

examples/with-fastify/nodemon.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"watch": [
3+
"src"
4+
],
5+
"ignore": ["database", "dist"],
6+
"exec": "cross-env TS_NODE_TRANSPILE_ONLY=true NODE_ENV=development node --no-warnings --loader ts-node/esm --enable-source-maps --trace-warnings --inspect=0.0.0.0:9234 --nolazy src/index.ts",
7+
"ext": "ts"
8+
}

examples/with-fastify/package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "with-fastify",
3+
"type": "module",
4+
"private": true,
5+
"scripts": {
6+
"start": "pnpm build && node dist/index.js",
7+
"dev": "nodemon",
8+
"build": "rm -rf dist && tsc"
9+
},
10+
"dependencies": {
11+
"@stenodb/fastify": "workspace:*",
12+
"class-transformer": "0.5.1",
13+
"fastify": "4.13.0",
14+
"reflect-metadata": "0.1.13"
15+
},
16+
"devDependencies": {
17+
"@types/node": "18.11.19"
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Type } from 'class-transformer'
2+
3+
export class Users {
4+
@Type(() => User)
5+
users: User[]
6+
7+
constructor(...users: User[]) {
8+
this.users = users
9+
}
10+
}
11+
12+
export class User {
13+
username: string
14+
15+
@Type(() => Post)
16+
posts: Post[]
17+
18+
constructor(username: string, ...posts: Post[]) {
19+
this.username = username
20+
this.posts = posts
21+
}
22+
23+
addPost(post: Post) {
24+
this.posts.push(post)
25+
}
26+
}
27+
28+
export class Post {
29+
title: string
30+
31+
constructor(title: string) {
32+
this.title = title
33+
}
34+
35+
changeTitle(title: string) {
36+
this.title = title
37+
}
38+
}

examples/with-fastify/src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'reflect-metadata'
2+
import { dirname, resolve } from 'node:path'
3+
import { fileURLToPath } from 'node:url'
4+
import { FastifySteno } from '@stenodb/fastify'
5+
import Fastify from 'fastify'
6+
import { Post, User, Users } from './entities.js'
7+
8+
const fastify = Fastify()
9+
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db')
10+
11+
fastify.register(FastifySteno, { path })
12+
13+
fastify.get('/', async (req, rep) => {
14+
const users = fastify.steno.get<Users>('users')
15+
return users?.data
16+
})
17+
18+
fastify.listen({ host: '0.0.0.0', port: 3000 }, async (err, address) => {
19+
if (err) throw err
20+
21+
await fastify.steno.create('users', Users, new Users(new User('John')))
22+
23+
console.log(address)
24+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "@crashmax/tsconfig",
3+
"compilerOptions": {
4+
"experimentalDecorators": true,
5+
"emitDecoratorMetadata": true,
6+
"outDir": "dist"
7+
},
8+
"include": [
9+
"src"
10+
]
11+
}

packages/fastify/package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@stenodb/fastify",
3+
"version": "3.3.2",
4+
"exports": {
5+
".": {
6+
"types": "./dist/index.d.ts",
7+
"require": "./dist/index.js",
8+
"default": "./dist/index.mjs"
9+
}
10+
},
11+
"types": "./dist/index.d.ts",
12+
"main": "./dist/index.js",
13+
"module": "./dist/index.mjs",
14+
"files": [
15+
"dist",
16+
"src"
17+
],
18+
"scripts": {
19+
"dev": "pnpm build -- --watch src",
20+
"build": "tsup src/index.ts --format esm,cjs --dts --clean"
21+
},
22+
"dependencies": {
23+
"@stenodb/node": "workspace:3.3.2",
24+
"fastify-plugin": "4.5.0"
25+
},
26+
"devDependencies": {
27+
"@types/node": "18.11.19",
28+
"fastify": "^4.13.0"
29+
},
30+
"peerDependencies": {
31+
"@stenodb/node": "workspace:3.3.2",
32+
"fastify": "^4.0.0"
33+
},
34+
"engines": {
35+
"node": ">=14.16"
36+
}
37+
}

packages/fastify/src/index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
2+
import fp from 'fastify-plugin'
3+
import type { Steno } from '@stenodb/node'
4+
import type { FastifyInstance } from 'fastify'
5+
import './types.js'
6+
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, {
39+
name: '@stenodb/fastify',
40+
fastify: '4.x'
41+
})
42+
43+
export { FastifySteno }
44+
export default fastifyPlugin

packages/fastify/src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { NodeProvider, Steno } from '@stenodb/node'
2+
3+
declare module 'fastify' {
4+
export interface FastifyInstance {
5+
steno: {
6+
create: <T>(
7+
name: string,
8+
entity: Steno.Entity<T>,
9+
initialData?: T
10+
) => Promise<void>
11+
get: <T>(name: string) => Steno.NodeProvider<T> | undefined
12+
}
13+
}
14+
}

packages/fastify/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "@crashmax/tsconfig",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src"
8+
]
9+
}

0 commit comments

Comments
 (0)