Skip to content

Commit 550f7f6

Browse files
authored
Merge pull request #10 from inaiat/feat/add_indexes_specficiation/omurilo
feat: add a possible to register indexes for models
2 parents 0000e0f + 789c2f0 commit 550f7f6

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@inaiat/fastify-papr",
3-
"version": "3.0.0",
3+
"version": "4.0.0",
44
"description": "Fastify Papr Plugin Integration",
55
"type": "module",
66
"engines": {

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fp from 'fastify-plugin'
22
import type {BaseSchema, Model, SchemaOptions} from 'papr'
3+
import { IndexDescription } from 'mongodb'
34
import type {FastifyPaprOptions, PaprModels} from './types.js'
45
import {paprHelper} from './papr-helper.js'
56

@@ -12,10 +13,12 @@ export const asModel = <TSchema extends BaseSchema>(
1213
collectionSchema,
1314
})
1415

16+
export const asIndexes = (collectionName: string, collectionIndexes: readonly IndexDescription[]) => ({collectionName, collectionIndexes})
17+
1518
const fastifyPaprPlugin = fp<FastifyPaprOptions>(
1619
async (mutable_fastify, options) => {
1720
const helper = paprHelper(mutable_fastify, options.db, options.disableSchemaReconciliation)
18-
const models = await helper.register(options.models)
21+
const models = await helper.register(options.models, options.indexes)
1922
const {name} = options
2023
if (name) {
2124
if (!mutable_fastify.papr) {

src/papr-helper.ts

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

66
export const paprHelper = (fastify: Readonly<FastifyInstance>, db: Db, disableSchemaReconciliation = false) => {
77
const papr = new Papr()
@@ -21,14 +21,23 @@ export const paprHelper = (fastify: Readonly<FastifyInstance>, db: Db, disableSc
2121
return model
2222
}
2323

24+
const registerIndexes = async (collectionName: string, indexes: readonly IndexDescription[]) => db.collection(collectionName).createIndexes(indexes as IndexDescription[])
25+
2426
return {
25-
register: async (schemas: ModelRegistrationPair<PaprModels>): Promise<PaprModels> =>
26-
Object.fromEntries(await Promise.all(
27+
async register(schemas: ModelRegistrationPair<PaprModels>, indexes: IndexesRegistrationPair[] = []): Promise<PaprModels> {
28+
indexes.map(async ({ collectionIndexes, collectionName}) => {
29+
const index = await registerIndexes(collectionName, collectionIndexes)
30+
fastify.log.info(`Indexes to ${collectionName} created`)
31+
return index
32+
})
33+
34+
return Object.fromEntries(await Promise.all(
2735
Object.entries(schemas).map(async ([modelName, modelObject]) => {
2836
const model = await registerModel(modelObject.collectionName, modelObject.collectionSchema)
2937
fastify.log.info(`Model ${modelName} decorated`)
3038
return [modelName, model] as [string, Model<BaseSchema, SchemaOptions<Partial<BaseSchema>>>]
31-
}))),
32-
39+
}),
40+
))
41+
},
3342
}
3443
}

src/types.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {Model} from 'papr'
2-
import type {Db} from 'mongodb'
2+
import type {Db, IndexDescription} from 'mongodb'
33
import type {asModel} from './index.js'
44

55
export type PaprModelItem = ReturnType<typeof asModel>
@@ -8,6 +8,11 @@ export type ModelRegistrationPair<T> = {
88
[U in keyof T]: PaprModelItem
99
}
1010

11+
export type IndexesRegistrationPair = {
12+
collectionName: string;
13+
collectionIndexes: IndexDescription[];
14+
}
15+
1116
export type PaprModels = Record<string, Model<any, any>>
1217

1318
export type FastifyPaprNestedObject = Record<string, PaprModels>
@@ -17,6 +22,7 @@ export type FastifyPaprOptions = {
1722
db: Db;
1823
models: ModelRegistrationPair<PaprModels>;
1924
disableSchemaReconciliation?: boolean;
25+
indexes?: IndexesRegistrationPair[];
2026
}
2127

2228
declare module 'fastify' {

0 commit comments

Comments
 (0)