Skip to content

Commit

Permalink
chore: bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
inaiat committed Jun 4, 2024
1 parent 739aa6e commit 33dfd84
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 73 deletions.
2 changes: 1 addition & 1 deletion BUILD_SHA
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fcabbd1ee53b1c97b89337e657be1e62011eb716
739aa6efb9c96e31554306b4761349550825fb2e
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const userSchema = schema({
const userIndexes = [{ key: { name: 1 } }]

declare module 'fastify' {
interface PaprModels {
user: Model<typeof userSchema[0], Partial<typeof userSchema[1]>>
interface FastifyPapr {
user: user: Model<typeof userSchema[0], Partial<typeof userSchema[1]>>
}
}

Expand All @@ -39,7 +39,9 @@ export default fp<FastifyPaprOptions>(

await fastify.register(fastifyPaprPlugin, {
db: fastify.mongo.client.db('test'),
models: { user: asCollection('user', userSchema, userIndexes) },
models: {
user: asCollection('user', userSchema, userIndexes)
},
})
},
{ name: 'papr' },
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@inaiat/fastify-papr",
"version": "6.0.0",
"version": "7.0.0",
"description": "Fastify Papr Plugin Integration",
"type": "module",
"engines": {
Expand Down Expand Up @@ -61,7 +61,7 @@
},
"devDependencies": {
"@eslint/js": "^9.4.0",
"@types/node": "^20.14.0",
"@types/node": "^20.14.1",
"@types/semver": "^7.5.8",
"c8": "^9.1.0",
"dprint": "^0.46.1",
Expand Down
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions src/fastify-papr-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import type { FastifyPluginAsync } from 'fastify'

import fp from 'fastify-plugin'
import type { IndexDescription } from 'mongodb'
import type { BaseSchema, SchemaOptions } from 'papr'
import { paprHelper } from './papr-helper.js'
import type { ColModel, FastifyPaprOptions, PaprModelItem } from './types.js'
import type { FastifyPapr, FastifyPaprOptions, ModelRegistration } from './types.js'

declare module 'fastify' {
interface FastifyInstance {
papr: FastifyPapr
}
}

export const asCollection = <TSchema extends BaseSchema>(
collectionName: string,
name: string,
// eslint-disable-next-line functional/prefer-immutable-types
collectionSchema: [TSchema, SchemaOptions<Partial<TSchema>>],
schema: [TSchema, SchemaOptions<Partial<TSchema>>],
// eslint-disable-next-line functional/prefer-immutable-types
collectionIndexes?: IndexDescription[],
): PaprModelItem => ({
collectionName,
collectionSchema,
collectionIndexes,
indexes?: IndexDescription[],
): ModelRegistration => ({
name,
schema,
indexes,
})

export const fastifyPaprPlugin: FastifyPluginAsync<FastifyPaprOptions> = async (mutable_fastify, options) => {
Expand All @@ -36,11 +43,11 @@ export const fastifyPaprPlugin: FastifyPluginAsync<FastifyPaprOptions> = async (
mutable_fastify.papr = {
...mutable_fastify.papr,

[dbName]: models as unknown as ColModel,
[dbName]: models,
}
} else {
mutable_fastify.decorate('papr', {
[dbName]: models as unknown as ColModel,
[dbName]: models,
})
}
} else {
Expand All @@ -52,3 +59,8 @@ export const fastifyPaprPlugin: FastifyPluginAsync<FastifyPaprOptions> = async (
}
}
}

export default fp(fastifyPaprPlugin, {
name: 'fastify-papr-plugin',
fastify: '4.x',
})
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import fp from 'fastify-plugin'
import { fastifyPaprPlugin } from './fastify-papr-plugin.js'
export { asCollection, fastifyPaprPlugin } from './fastify-papr-plugin.js'
export * from './simple-doc-failed-validation.js'
export * from './types.js'

export default fp(fastifyPaprPlugin, {
name: 'fastify-papr-plugin',
fastify: '4.x',
})
export { fastifyPaprPlugin as default } from './fastify-papr-plugin.js'
14 changes: 7 additions & 7 deletions src/papr-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FastifyInstance } from 'fastify'
import type { Db, IndexDescription } from 'mongodb'
import type { BaseSchema, SchemaOptions } from 'papr'
import Papr, { type Model } from 'papr'
import Papr from 'papr'
import type { ModelRegistrationPair } from './types.js'
import { type FastifyPapr } from './types.js'

Expand All @@ -27,18 +27,18 @@ export const paprHelper = (fastify: Readonly<FastifyInstance>, db: Db, disableSc
db.collection(collectionName).createIndexes(indexes as IndexDescription[])

return {
async register(models: ModelRegistrationPair<FastifyPapr>): Promise<FastifyPapr> {
async register(models: ModelRegistrationPair<FastifyPapr>) {
return Object.fromEntries(
await Promise.all(
Object.entries(models).map(async ([name, paprModel]) => {
const model = await registerModel(paprModel.collectionName, paprModel.collectionSchema)
const model = await registerModel(paprModel.name, paprModel.schema)
fastify.log.info(`Model ${name} decorated`)
if (paprModel.collectionIndexes) {
const index = await registerIndexes(paprModel.collectionName, paprModel.collectionIndexes)
fastify.log.info(`Indexes for ${paprModel.collectionName} => ${index.join(', ')} created.`)
if (paprModel.indexes) {
const index = await registerIndexes(paprModel.name, paprModel.indexes)
fastify.log.info(`Indexes for ${paprModel.name} => ${index.join(', ')} created.`)
}

return [name, model] as [string, Model<BaseSchema, SchemaOptions<Partial<BaseSchema>>>]
return [name, model] as const
}),
),
)
Expand Down
36 changes: 10 additions & 26 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
import type { Db, IndexDescription } from 'mongodb'
import type { BaseSchema, Model, SchemaOptions } from 'papr'

export type PaprModelItem = {
collectionName: string
collectionSchema: [BaseSchema, SchemaOptions<Partial<BaseSchema>>]
collectionIndexes?: IndexDescription[]
}

export type ModelRegistrationPair<T> = {
[U in keyof T]: PaprModelItem
}

export type IndexesRegistrationPair = {
collectionName: string
collectionIndexes: readonly IndexDescription[]
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ColModel<T extends BaseSchema = any, U extends SchemaOptions<Partial<T>> = any> = Model<T, U>

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type FastifyPapr<T extends BaseSchema = any, U extends SchemaOptions<Partial<T>> = any> = Record<
string,
ColModel<T, U> | Record<string, ColModel<T, U>>
Model<T, U> | Record<string, Model<T, U>>
>

export type PaprDb = Record<string, FastifyPapr>
export type ModelRegistration = {
name: string
schema: [BaseSchema, SchemaOptions<Partial<BaseSchema>>]
indexes?: IndexDescription[]
}

export type ModelRegistrationPair<T> = {
[U in keyof T]: ModelRegistration
}

export type FastifyPaprOptions = {
name?: string
db: Db
models: ModelRegistrationPair<FastifyPapr>
disableSchemaReconciliation?: boolean
}

declare module 'fastify' {
interface FastifyInstance {
papr: FastifyPapr
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit 33dfd84

Please sign in to comment.