Skip to content

Commit a733f9b

Browse files
committed
feat: register schemas
1 parent 1300ce2 commit a733f9b

File tree

8 files changed

+114
-51
lines changed

8 files changed

+114
-51
lines changed

examples/with-fastify/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dependencies": {
1111
"@stenodb/fastify": "workspace:*",
1212
"class-transformer": "0.5.1",
13+
"class-validator": "0.14.0",
1314
"fastify": "4.13.0",
1415
"reflect-metadata": "0.1.13"
1516
},

examples/with-fastify/src/entities.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Type } from 'class-transformer'
1+
import { Exclude, Type } from 'class-transformer'
2+
import { IsNumber, IsString, Length, MaxLength } from 'class-validator'
23

34
export class Users {
45
@Type(() => User)
@@ -10,13 +11,20 @@ export class Users {
1011
}
1112

1213
export class User {
14+
@IsString()
15+
@Length(3, 20)
1316
username: string
1417

18+
@IsNumber()
19+
age: number
20+
21+
@Exclude()
1522
@Type(() => Post)
1623
posts: Post[]
1724

18-
constructor(username: string, ...posts: Post[]) {
25+
constructor(username: string, age: number, ...posts: Post[]) {
1926
this.username = username
27+
this.age = age
2028
this.posts = posts
2129
}
2230

@@ -26,6 +34,8 @@ export class User {
2634
}
2735

2836
export class Post {
37+
@IsString()
38+
@MaxLength(128)
2939
title: string
3040

3141
constructor(title: string) {

examples/with-fastify/src/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import Fastify from 'fastify'
66
import { Post, User, Users } from './entities.js'
77

88
const fastify = Fastify()
9-
const path = resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db')
109

11-
fastify.register(FastifySteno, { path })
10+
fastify.register(FastifySteno, {
11+
path: resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db'),
12+
entities: [User, Post]
13+
})
1214

13-
fastify.get('/', async (req, rep) => {
14-
const users = fastify.steno.get<Users>('users')
15-
return users?.data
15+
fastify.post('/', { schema: { body: fastify.getSchema('User') } }, (req) => {
16+
return {
17+
body: req.body,
18+
schema: req.routeSchema
19+
}
1620
})
1721

1822
fastify.listen({ host: '0.0.0.0', port: 3000 }, async (err, address) => {
1923
if (err) throw err
20-
21-
await fastify.steno.create('users', Users, new Users(new User('John')))
22-
24+
console.log(fastify.getSchemas())
2325
console.log(address)
2426
})

examples/with-nest/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"@nestjs/platform-express": "^9.0.0",
2525
"@stenodb/nest": "workspace:*",
2626
"class-transformer": "0.5.1",
27-
"class-validator": "^0.14.0",
28-
"reflect-metadata": "^0.1.13",
27+
"class-validator": "0.14.0",
28+
"reflect-metadata": "0.1.13",
2929
"rimraf": "^3.0.2",
3030
"rxjs": "^7.2.0"
3131
},

packages/fastify/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"dependencies": {
2323
"@stenodb/node": "workspace:3.3.2",
24+
"class-validator-jsonschema": "5.0.0",
2425
"fastify-plugin": "4.5.0"
2526
},
2627
"devDependencies": {

packages/fastify/src/index.ts

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,10 @@
1-
import { AsyncAdapter, NodeProvider } from '@stenodb/node'
21
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'
63

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, {
395
name: '@stenodb/fastify',
406
fastify: '4.x'
417
})
428

439
export { FastifySteno }
44-
export default fastifyPlugin
10+
export default StenoPlugin.createInstance

packages/fastify/src/plugin.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { NodeProvider } from '@stenodb/node'
2+
import { targetConstructorToSchema } from 'class-validator-jsonschema'
3+
import type { Steno } from '@stenodb/node'
4+
import type { IOptions } from 'class-validator-jsonschema/build/options'
5+
import type { FastifyInstance } from 'fastify'
6+
7+
export interface StenoOptions extends Steno.NodeProviderOptions {
8+
entities: Steno.Entity<any>[]
9+
}
10+
11+
export class StenoPlugin {
12+
#fastify: FastifyInstance
13+
#options: StenoOptions
14+
#provider: NodeProvider
15+
16+
constructor(
17+
fastify: FastifyInstance,
18+
options: StenoOptions,
19+
next: () => void
20+
) {
21+
this.#fastify = fastify
22+
this.#options = options
23+
this.#provider = new NodeProvider(options)
24+
25+
this.#fastify.decorate('steno', {})
26+
this.registerSchemas()
27+
28+
next()
29+
}
30+
31+
static createInstance(
32+
fastify: FastifyInstance,
33+
options: StenoOptions,
34+
next: () => void
35+
): StenoPlugin {
36+
return new StenoPlugin(fastify, options, next)
37+
}
38+
39+
private registerSchemas(options?: IOptions): void {
40+
for (const entity of this.#options.entities) {
41+
const schema = targetConstructorToSchema(entity, options)
42+
this.#fastify.addSchema({ ...schema, $id: entity.name })
43+
}
44+
}
45+
}

pnpm-lock.yaml

Lines changed: 40 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)