Skip to content

Commit 33dfd84

Browse files
committed
chore: bump version
1 parent 739aa6e commit 33dfd84

10 files changed

+66
-73
lines changed

BUILD_SHA

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fcabbd1ee53b1c97b89337e657be1e62011eb716
1+
739aa6efb9c96e31554306b4761349550825fb2e

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const userSchema = schema({
2626
const userIndexes = [{ key: { name: 1 } }]
2727

2828
declare module 'fastify' {
29-
interface PaprModels {
30-
user: Model<typeof userSchema[0], Partial<typeof userSchema[1]>>
29+
interface FastifyPapr {
30+
user: user: Model<typeof userSchema[0], Partial<typeof userSchema[1]>>
3131
}
3232
}
3333

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

4040
await fastify.register(fastifyPaprPlugin, {
4141
db: fastify.mongo.client.db('test'),
42-
models: { user: asCollection('user', userSchema, userIndexes) },
42+
models: {
43+
user: asCollection('user', userSchema, userIndexes)
44+
},
4345
})
4446
},
4547
{ name: 'papr' },

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@inaiat/fastify-papr",
3-
"version": "6.0.0",
3+
"version": "7.0.0",
44
"description": "Fastify Papr Plugin Integration",
55
"type": "module",
66
"engines": {
@@ -61,7 +61,7 @@
6161
},
6262
"devDependencies": {
6363
"@eslint/js": "^9.4.0",
64-
"@types/node": "^20.14.0",
64+
"@types/node": "^20.14.1",
6565
"@types/semver": "^7.5.8",
6666
"c8": "^9.1.0",
6767
"dprint": "^0.46.1",

pnpm-lock.yaml

+18-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fastify-papr-plugin.ts

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import type { FastifyPluginAsync } from 'fastify'
22

3+
import fp from 'fastify-plugin'
34
import type { IndexDescription } from 'mongodb'
45
import type { BaseSchema, SchemaOptions } from 'papr'
56
import { paprHelper } from './papr-helper.js'
6-
import type { ColModel, FastifyPaprOptions, PaprModelItem } from './types.js'
7+
import type { FastifyPapr, FastifyPaprOptions, ModelRegistration } from './types.js'
8+
9+
declare module 'fastify' {
10+
interface FastifyInstance {
11+
papr: FastifyPapr
12+
}
13+
}
714

815
export const asCollection = <TSchema extends BaseSchema>(
9-
collectionName: string,
16+
name: string,
1017
// eslint-disable-next-line functional/prefer-immutable-types
11-
collectionSchema: [TSchema, SchemaOptions<Partial<TSchema>>],
18+
schema: [TSchema, SchemaOptions<Partial<TSchema>>],
1219
// eslint-disable-next-line functional/prefer-immutable-types
13-
collectionIndexes?: IndexDescription[],
14-
): PaprModelItem => ({
15-
collectionName,
16-
collectionSchema,
17-
collectionIndexes,
20+
indexes?: IndexDescription[],
21+
): ModelRegistration => ({
22+
name,
23+
schema,
24+
indexes,
1825
})
1926

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

39-
[dbName]: models as unknown as ColModel,
46+
[dbName]: models,
4047
}
4148
} else {
4249
mutable_fastify.decorate('papr', {
43-
[dbName]: models as unknown as ColModel,
50+
[dbName]: models,
4451
})
4552
}
4653
} else {
@@ -52,3 +59,8 @@ export const fastifyPaprPlugin: FastifyPluginAsync<FastifyPaprOptions> = async (
5259
}
5360
}
5461
}
62+
63+
export default fp(fastifyPaprPlugin, {
64+
name: 'fastify-papr-plugin',
65+
fastify: '4.x',
66+
})

src/index.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import fp from 'fastify-plugin'
2-
import { fastifyPaprPlugin } from './fastify-papr-plugin.js'
31
export { asCollection, fastifyPaprPlugin } from './fastify-papr-plugin.js'
42
export * from './simple-doc-failed-validation.js'
53
export * from './types.js'
64

7-
export default fp(fastifyPaprPlugin, {
8-
name: 'fastify-papr-plugin',
9-
fastify: '4.x',
10-
})
5+
export { fastifyPaprPlugin as default } from './fastify-papr-plugin.js'

src/papr-helper.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { FastifyInstance } from 'fastify'
22
import type { Db, IndexDescription } from 'mongodb'
33
import type { BaseSchema, SchemaOptions } from 'papr'
4-
import Papr, { type Model } from 'papr'
4+
import Papr from 'papr'
55
import type { ModelRegistrationPair } from './types.js'
66
import { type FastifyPapr } from './types.js'
77

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

2929
return {
30-
async register(models: ModelRegistrationPair<FastifyPapr>): Promise<FastifyPapr> {
30+
async register(models: ModelRegistrationPair<FastifyPapr>) {
3131
return Object.fromEntries(
3232
await Promise.all(
3333
Object.entries(models).map(async ([name, paprModel]) => {
34-
const model = await registerModel(paprModel.collectionName, paprModel.collectionSchema)
34+
const model = await registerModel(paprModel.name, paprModel.schema)
3535
fastify.log.info(`Model ${name} decorated`)
36-
if (paprModel.collectionIndexes) {
37-
const index = await registerIndexes(paprModel.collectionName, paprModel.collectionIndexes)
38-
fastify.log.info(`Indexes for ${paprModel.collectionName} => ${index.join(', ')} created.`)
36+
if (paprModel.indexes) {
37+
const index = await registerIndexes(paprModel.name, paprModel.indexes)
38+
fastify.log.info(`Indexes for ${paprModel.name} => ${index.join(', ')} created.`)
3939
}
4040

41-
return [name, model] as [string, Model<BaseSchema, SchemaOptions<Partial<BaseSchema>>>]
41+
return [name, model] as const
4242
}),
4343
),
4444
)

src/types.ts

+10-26
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,25 @@
11
import type { Db, IndexDescription } from 'mongodb'
22
import type { BaseSchema, Model, SchemaOptions } from 'papr'
33

4-
export type PaprModelItem = {
5-
collectionName: string
6-
collectionSchema: [BaseSchema, SchemaOptions<Partial<BaseSchema>>]
7-
collectionIndexes?: IndexDescription[]
8-
}
9-
10-
export type ModelRegistrationPair<T> = {
11-
[U in keyof T]: PaprModelItem
12-
}
13-
14-
export type IndexesRegistrationPair = {
15-
collectionName: string
16-
collectionIndexes: readonly IndexDescription[]
17-
}
18-
19-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
20-
export type ColModel<T extends BaseSchema = any, U extends SchemaOptions<Partial<T>> = any> = Model<T, U>
21-
224
// eslint-disable-next-line @typescript-eslint/no-explicit-any
235
export type FastifyPapr<T extends BaseSchema = any, U extends SchemaOptions<Partial<T>> = any> = Record<
246
string,
25-
ColModel<T, U> | Record<string, ColModel<T, U>>
7+
Model<T, U> | Record<string, Model<T, U>>
268
>
279

28-
export type PaprDb = Record<string, FastifyPapr>
10+
export type ModelRegistration = {
11+
name: string
12+
schema: [BaseSchema, SchemaOptions<Partial<BaseSchema>>]
13+
indexes?: IndexDescription[]
14+
}
15+
16+
export type ModelRegistrationPair<T> = {
17+
[U in keyof T]: ModelRegistration
18+
}
2919

3020
export type FastifyPaprOptions = {
3121
name?: string
3222
db: Db
3323
models: ModelRegistrationPair<FastifyPapr>
3424
disableSchemaReconciliation?: boolean
3525
}
36-
37-
declare module 'fastify' {
38-
interface FastifyInstance {
39-
papr: FastifyPapr
40-
}
41-
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)