Skip to content

Commit dc8a71c

Browse files
committed
refactor(with-fastify): dto
1 parent cb77081 commit dc8a71c

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

examples/with-fastify/src/api/posts/posts.controller.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Post, User } from '../../dto/users.dto.js'
1+
import { Post } from '../../dto/posts.dto.js'
2+
import { User } from '../../dto/users.dto.js'
23
import {
34
userIdAndPostIdParamsSchema,
45
userIdParamsSchema
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Exclude, Type } from 'class-transformer'
2+
import { IsNumber, IsString, Length } from 'class-validator'
3+
4+
export class Post {
5+
@Exclude({ toPlainOnly: true })
6+
@IsNumber()
7+
postId: number
8+
9+
@IsString()
10+
@Length(1, 128)
11+
subject: string
12+
13+
@Exclude({ toPlainOnly: true })
14+
@Type(() => Date)
15+
createdAt: Date
16+
17+
constructor(postId: number, subject: string, createdAt: Date) {
18+
this.postId = postId
19+
this.subject = subject
20+
this.createdAt = createdAt
21+
}
22+
}

examples/with-fastify/src/dto/users.dto.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { AsyncAdapter } from '@stenodb/fastify'
21
import { Exclude, Type } from 'class-transformer'
3-
import { IsNumber, IsOptional, IsString, Length } from 'class-validator'
2+
import { IsNumber, IsString, Length } from 'class-validator'
3+
import { Post } from './posts.dto.js'
44

55
export class Users {
66
@Type(() => User)
@@ -46,32 +46,3 @@ export class User {
4646
return this.posts.at(-1)!.postId
4747
}
4848
}
49-
50-
export class Post {
51-
@Exclude({ toPlainOnly: true })
52-
@IsNumber()
53-
postId: number
54-
55-
@IsString()
56-
@Length(1, 128)
57-
subject: string
58-
59-
@Exclude({ toPlainOnly: true })
60-
@Type(() => Date)
61-
createdAt: Date
62-
63-
constructor(postId: number, subject: string, createdAt: Date) {
64-
this.postId = postId
65-
this.subject = subject
66-
this.createdAt = createdAt
67-
}
68-
}
69-
70-
const initialData = new Users(
71-
new User(1, 'john', 18, new Post(1, 'Lorem ipsum', new Date())),
72-
new User(2, 'alice', 23)
73-
)
74-
75-
export const userEntities = [User, Post]
76-
77-
export const users = new AsyncAdapter('users', Users, initialData)

examples/with-fastify/src/index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import 'reflect-metadata'
22
import { dirname, resolve } from 'node:path'
33
import { fileURLToPath } from 'node:url'
4-
import { FastifySteno } from '@stenodb/fastify'
4+
import { AsyncAdapter, FastifySteno } from '@stenodb/fastify'
55
import Fastify from 'fastify'
66
import { postsController } from './api/posts/posts.controller.js'
77
import { userController } from './api/users/users.controller.js'
88
import { UserService } from './api/users/users.service.js'
9-
import { userEntities, users } from './dto/users.dto.js'
9+
import { Post } from './dto/posts.dto.js'
10+
import { User, Users } from './dto/users.dto.js'
1011

1112
const fastify = Fastify()
1213

14+
const usersInitialData = new Users(
15+
new User(1, 'john', 18, new Post(1, 'Lorem ipsum', new Date())),
16+
new User(2, 'alice', 23)
17+
)
18+
19+
const usersAdapter = new AsyncAdapter(
20+
'users',
21+
Users,
22+
usersInitialData
23+
)
24+
1325
fastify.register(FastifySteno, {
1426
path: resolve(dirname(fileURLToPath(import.meta.url)), '..', 'db'),
15-
entities: [...userEntities],
16-
adapters: [users]
27+
entities: [User, Post],
28+
adapters: [usersAdapter]
1729
})
1830

1931
fastify.register(

0 commit comments

Comments
 (0)