Skip to content

Commit

Permalink
move schema loader to schemas/index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchoz49 committed Oct 20, 2024
1 parent ec5b005 commit f914114
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
4 changes: 1 addition & 3 deletions .cursorrules
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
This a starter project to build libraries for node.js.

You are a programmer that uses several tools and best practices.

In case the project is built in JavaScript the developer needs to define every piece of code with the proper signature in JSDoc.
Expand All @@ -8,7 +6,7 @@ The project is set up to use:

- node:test for the testing framework
- node:assert for the assertions
- prefers to use `describe` and `it` for the tests
- prefers to use `describe` and `test` for the tests
- JavaScript with stricter JSDoc or TypeScript
- eslint for linting
- eslint-config-standard-ext for the style guide
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ temp/
.Trashes
ehthumbs.db
Thumbs.db
serve
4 changes: 3 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export const createApp = (options: AppOptions = {}) => {
app.register(import('./plugins/auth.js'))
app.register(import('./plugins/documentation.js'))
app.register(import('./plugins/drizzle.js'))
app.register(import('./plugins/schema-loader.js'))

// schemas
app.register(import('./schemas/index.js'))

// routes
app.register(import('./routes/index.js'), { prefix: '/api' })
Expand Down
20 changes: 0 additions & 20 deletions src/plugins/schema-loader.ts

This file was deleted.

30 changes: 28 additions & 2 deletions src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
export * from './posts.js'
export * from './users.js'
import fp from 'fastify-plugin'

import type { App } from '~/app.js'

import * as posts from './posts.js'
import * as users from './users.js'

const schemas = {
...posts,
...users,
}

async function schemaLoaderPlugin(app: App) {
Object.values(schemas).forEach((schema) => {
app.addSchema(schema)
})
app.decorate('schemas', schemas)
}

export default fp(schemaLoaderPlugin, {
name: 'core:schema-loader',
})

declare module 'fastify' {
interface FastifyInstance {
schemas: typeof schemas
}
}

0 comments on commit f914114

Please sign in to comment.