Skip to content

Commit 65464cf

Browse files
authored
Merge pull request #8 from inaiat/chore/papr_v_9_x
chore: upgrade papr to version 9.x
2 parents f03e63d + 1d50899 commit 65464cf

12 files changed

+9658
-6994
lines changed

BUILD_SHA

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
aa68fc93f7a0f7e18f1cf03c28cfd2abfa94876d
1+
f03e63dab105a2d15f6c54b9c69a515d6b2eaa9e

package-lock.json

+9,249-6,620
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+70-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@inaiat/fastify-papr",
3-
"version": "2.1.0",
3+
"version": "3.0.0",
44
"description": "Fastify Papr Plugin Integration",
55
"type": "module",
66
"engines": {
@@ -46,28 +46,31 @@
4646
],
4747
"homepage": "https://github.com/inaiat/fastify-papr.git",
4848
"dependencies": {
49-
"fastify-plugin": "^4.2.1",
50-
"papr": "^8.2.0"
49+
"fastify-plugin": "^4.4.0",
50+
"papr": "^9.2.1"
5151
},
5252
"peerDependencies": {
53-
"fastify": "^4.5.3",
54-
"mongodb": "^4.9.1"
53+
"fastify": "^4.10.2",
54+
"mongodb": "^4.12.1"
5555
},
5656
"devDependencies": {
5757
"@swc/cli": "^0.1.57",
58-
"@swc/core": "^1.2.245",
59-
"@swc/helpers": "^0.4.11",
60-
"@types/node": "^18.7.14",
61-
"@types/semver": "^7.3.12",
62-
"ava": "^4.3.3",
58+
"@swc/core": "^1.3.23",
59+
"@swc/helpers": "^0.4.14",
60+
"@types/node": "^18.11.16",
61+
"@types/semver": "^7.3.13",
62+
"ava": "^5.1.0",
6363
"c8": "^7.12.0",
64-
"istanbul-badges-readme": "^1.8.2",
65-
"mongodb-memory-server": "^8.9.0",
64+
"istanbul-badges-readme": "^1.8.4",
65+
"mongodb-memory-server": "^8.10.2",
6666
"rimraf": "^3.0.2",
67-
"semver": "^7.3.7",
67+
"semver": "^7.3.8",
6868
"ts-node": "^10.9.1",
69-
"typescript": "^4.8.2",
70-
"xo": "^0.52.3"
69+
"typescript": "^4.9.4",
70+
"xo": "^0.53.1",
71+
"eslint-plugin-unused-imports": "^2.0.0",
72+
"eslint-plugin-functional": "4.4.1",
73+
"eslint-plugin-prettier": "4.2.1"
7174
},
7275
"files": [
7376
"dist"
@@ -92,28 +95,61 @@
9295
},
9396
"xo": {
9497
"semicolon": false,
95-
"ignores": [
96-
"documentation/examples/*"
98+
"space": 2,
99+
"plugins": [
100+
"prettier",
101+
"functional",
102+
"unused-imports"
103+
],
104+
"extends": [
105+
"plugin:functional/no-mutations",
106+
"plugin:functional/stylistic",
107+
"plugin:functional/no-exceptions",
108+
"plugin:functional/external-recommended"
97109
],
98110
"rules": {
99-
"@typescript-eslint/no-empty-function": "off",
100-
"n/no-deprecated-api": "off",
101-
"n/prefer-global/url": "off",
102-
"n/prefer-global/url-search-params": "off",
103-
"@typescript-eslint/no-implicit-any-catch": "off",
104-
"unicorn/prefer-node-protocol": "off",
105-
"ava/assertion-arguments": "off",
106-
"@typescript-eslint/no-unsafe-member-access": "off",
107-
"@typescript-eslint/no-unsafe-return": "off",
108-
"@typescript-eslint/no-unsafe-assignment": "off",
109-
"@typescript-eslint/no-unsafe-call": "off",
110-
"@typescript-eslint/await-thenable": "off",
111-
"@typescript-eslint/no-redundant-type-constituents": "off",
112-
"no-lone-blocks": "off",
113-
"unicorn/no-await-expression-member": "off",
111+
"capitalized-comments": "off",
112+
"unicorn/prevent-abbreviations": "off",
113+
"unicorn/no-array-callback-reference": "off",
114+
"unicorn/no-array-method-this-argument": "off",
115+
"new-cap": "off",
116+
"import/no-extraneous-dependencies": "off",
117+
"@typescript-eslint/member-delimiter-style": "off",
118+
"@typescript-eslint/prefer-readonly-parameter-types": "off",
114119
"@typescript-eslint/naming-convention": "off",
115-
"@typescript-eslint/consistent-type-definitions": "off"
116-
}
120+
"@typescript-eslint/consistent-type-imports": "off",
121+
"@typescript-eslint/consistent-type-definitions": "off",
122+
"@typescript-eslint/no-unsafe-assignment": "warn",
123+
"@typescript-eslint/no-empty-function": "warn",
124+
"@typescript-eslint/no-unsafe-call": "off",
125+
"@typescript-eslint/object-curly-spacing": "off",
126+
"@typescript-eslint/no-unused-vars": "error",
127+
"unused-imports/no-unused-imports": "error",
128+
"functional/no-let": [
129+
"error",
130+
{
131+
"allowLocalMutation": true
132+
}
133+
],
134+
"functional/no-method-signature": "off",
135+
"functional/prefer-readonly-type": "off"
136+
},
137+
"overrides": [
138+
{
139+
"files": "tests/**/*.ts",
140+
"rules": {
141+
"neverthrow/must-use-result": "off",
142+
"functional/no-method-signature": "off",
143+
"@typescript-eslint/prefer-readonly-parameter-types": "off",
144+
"@typescript-eslint/no-unsafe-assignment": "off",
145+
"functional/prefer-readonly-type": "off",
146+
"functional/immutable-data": "off",
147+
"ava/use-test": "off",
148+
"unicorn/no-await-expression-member": "off",
149+
"prefer-promise-reject-errors": "off"
150+
}
151+
}
152+
]
117153
},
118154
"config": {
119155
"mongodbMemoryServer": {

src/index.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,37 @@ import type {FastifyPaprOptions, PaprModels} from './types.js'
44
import {paprHelper} from './papr-helper.js'
55

66
export const asModel = <TSchema extends BaseSchema>(
7-
collectionName: string,
8-
collectionSchema: [TSchema, SchemaOptions<Partial<TSchema>>],
7+
collectionName: string,
8+
collectionSchema: [TSchema, SchemaOptions<Partial<TSchema>>],
99
) => ({
10-
collectionName,
11-
collectionSchema,
10+
collectionName,
11+
collectionSchema,
1212
})
1313

1414
const fastifyPaprPlugin = fp<FastifyPaprOptions>(
15-
async (fastify, options) => {
16-
const helper = paprHelper(fastify, options.db, options.disableSchemaReconciliation)
17-
const models = await helper.register(options.models)
18-
const {name} = options
19-
if (name) {
20-
if (!fastify.papr) {
21-
fastify.decorate('papr', models)
22-
}
15+
async (fastify, options) => {
16+
const helper = paprHelper(fastify, options.db, options.disableSchemaReconciliation)
17+
const models = await helper.register(options.models)
18+
const {name} = options
19+
if (name) {
20+
if (!fastify.papr) {
21+
fastify.decorate('papr', models)
22+
}
2323

24-
if (fastify.papr[name]) {
25-
throw new Error(`Connection name already registered: ${name}`)
26-
}
24+
if (fastify.papr[name]) {
25+
// eslint-disable-next-line functional/no-throw-statement
26+
throw new Error(`Connection name already registered: ${name}`)
27+
}
2728

28-
fastify.papr[name] = models as Model<any, any> & PaprModels
29-
}
29+
// eslint-disable-next-line functional/immutable-data
30+
fastify.papr[name] = models as Model<any, any> & PaprModels
31+
}
3032

31-
if (!fastify.papr) {
32-
fastify.decorate('papr', models)
33-
}
34-
},
35-
{fastify: '4.x', name: 'fastify-papr-plugin'},
33+
if (!fastify.papr) {
34+
fastify.decorate('papr', models)
35+
}
36+
},
37+
{fastify: '4.x', name: 'fastify-papr-plugin'},
3638
)
3739

3840
export default fastifyPaprPlugin

src/papr-helper.ts

+26-29
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
import type {FastifyInstance} from 'fastify'
22
import type {Db} from 'mongodb'
3+
import Papr, {type Model} from 'papr'
34
import type {BaseSchema, SchemaOptions} from 'papr'
4-
import Papr from 'papr'
5-
import type {ModelRegistrationPair, PaprModels} from './types.js'
5+
import {type PaprModels} from './types.js'
6+
import type {ModelRegistrationPair} from './types.js'
67

7-
export const paprHelper = (fastify: FastifyInstance, db: Db, disableSchemaReconciliation: boolean = false) => {
8-
const papr = new Papr()
9-
papr.initialize(db)
8+
export const paprHelper = (fastify: FastifyInstance, db: Db, disableSchemaReconciliation = false) => {
9+
const papr = new Papr()
10+
papr.initialize(db)
1011

11-
const registerModel = async <TSchema extends BaseSchema, TOptions extends SchemaOptions<TSchema>>(
12-
collectionName: string,
13-
collectionSchema: [TSchema, TOptions],
14-
) => {
15-
const model = papr.model(collectionName, collectionSchema)
12+
const registerModel = async <TSchema extends BaseSchema, TOptions extends SchemaOptions<TSchema>>(
13+
collectionName: string,
14+
collectionSchema: [TSchema, TOptions],
15+
) => {
16+
const model = papr.model(collectionName, collectionSchema)
1617

17-
if (!disableSchemaReconciliation) {
18-
await papr.updateSchema(model)
19-
}
18+
if (!disableSchemaReconciliation) {
19+
await papr.updateSchema(model)
20+
}
2021

21-
return model
22-
}
22+
return model
23+
}
2324

24-
return {
25-
async register(schemas: ModelRegistrationPair<PaprModels>) {
26-
const models: PaprModels = {}
27-
await Promise.all(
28-
Object.entries(schemas).map(async ([modelName, modelObject]) => {
29-
const model = await registerModel(modelObject.collectionName, modelObject.collectionSchema)
30-
models[modelName] = model
31-
fastify.log.info(`Model ${modelName} decorated`)
32-
return model
33-
}),
34-
)
35-
return models
36-
},
37-
}
25+
return {
26+
register: async (schemas: ModelRegistrationPair<PaprModels>): Promise<PaprModels> =>
27+
Object.fromEntries(await Promise.all(
28+
Object.entries(schemas).map(async ([modelName, modelObject]) => {
29+
const model = await registerModel(modelObject.collectionName, modelObject.collectionSchema)
30+
fastify.log.info(`Model ${modelName} decorated`)
31+
return [modelName, model] as [string, Model<BaseSchema, SchemaOptions<Partial<BaseSchema>>>]
32+
}))),
33+
34+
}
3835
}

src/simple-doc-failed-validation.ts

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
import {MongoServerError} from 'mongodb'
22

33
export type PropertyDetail = {
4-
operatorName?: string;
5-
specifiedAs?: Record<string, any>;
6-
reason?: string;
7-
consideredValue?: any;
8-
consideredType?: any;
4+
operatorName?: string;
5+
specifiedAs?: Record<string, any>;
6+
reason?: string;
7+
consideredValue?: any;
8+
consideredType?: any;
99
}
1010

1111
export type PropertiesNotSatisfied = {
12-
propertyName: string;
13-
details: PropertyDetail[];
12+
propertyName: string;
13+
details: PropertyDetail[];
1414
}
1515

1616
export type DocumentFailedValidation = {
17-
failingDocumentId?: string;
18-
details?: {
19-
operatorName?: string;
20-
schemaRulesNotSatisfied?: [
21-
{
22-
operatorName: string;
23-
propertiesNotSatisfied: PropertiesNotSatisfied[];
24-
},
25-
];
26-
};
17+
failingDocumentId?: string;
18+
details?: {
19+
operatorName?: string;
20+
schemaRulesNotSatisfied?: [
21+
{
22+
operatorName: string;
23+
propertiesNotSatisfied: PropertiesNotSatisfied[];
24+
},
25+
];
26+
};
2727
}
2828

2929
export type SimpleDocFailedValidation = Array<Record<string, Array<Record<string, PropertyDetail[]>>>> | undefined
3030

3131
const prettyPropertiesNotSatisfied = (propertiesNotSatisfied: PropertiesNotSatisfied[]) =>
32-
propertiesNotSatisfied?.map(i => ({[i.propertyName]: i.details}))
32+
propertiesNotSatisfied?.map(i => ({[i.propertyName]: i.details}))
3333

3434
export function tryExtractSimpleDocFailedValidation(error: MongoServerError): SimpleDocFailedValidation | undefined {
35-
if (error instanceof MongoServerError && error.code === 121) {
36-
const schemaRulesNotSatisfied = (error?.errInfo as DocumentFailedValidation)?.details?.schemaRulesNotSatisfied
35+
if (error instanceof MongoServerError && error.code === 121) {
36+
const schemaRulesNotSatisfied = (error?.errInfo as DocumentFailedValidation)?.details?.schemaRulesNotSatisfied
3737

38-
const result: SimpleDocFailedValidation = schemaRulesNotSatisfied?.map(i => ({
39-
[i.operatorName]: prettyPropertiesNotSatisfied(i.propertiesNotSatisfied),
40-
}))
38+
const result: SimpleDocFailedValidation = schemaRulesNotSatisfied?.map(i => ({
39+
[i.operatorName]: prettyPropertiesNotSatisfied(i.propertiesNotSatisfied),
40+
}))
4141

42-
return result?.length === 0 ? undefined : result
43-
}
42+
return result?.length === 0 ? undefined : result
43+
}
4444

45-
return undefined
45+
return undefined
4646
}
4747

4848
export class SimpleDocFailedValidationError extends Error {
49-
schemaRulesNotSatisfied?: SimpleDocFailedValidation
50-
documentFailedValidation: boolean
51-
52-
constructor(mongoError: MongoServerError) {
53-
super(mongoError.message, mongoError)
54-
this.schemaRulesNotSatisfied = tryExtractSimpleDocFailedValidation(mongoError)
55-
this.documentFailedValidation = this.schemaRulesNotSatisfied !== undefined
56-
}
57-
58-
schemaRulesNotSatisfiedAsString(): string | undefined {
59-
return this.documentFailedValidation ? JSON.stringify(this.schemaRulesNotSatisfied) : undefined
60-
}
49+
schemaRulesNotSatisfied?: SimpleDocFailedValidation
50+
documentFailedValidation: boolean
51+
52+
constructor(mongoError: MongoServerError) {
53+
super(mongoError.message, mongoError)
54+
this.schemaRulesNotSatisfied = tryExtractSimpleDocFailedValidation(mongoError)
55+
this.documentFailedValidation = this.schemaRulesNotSatisfied !== undefined
56+
}
57+
58+
schemaRulesNotSatisfiedAsString(): string | undefined {
59+
return this.documentFailedValidation ? JSON.stringify(this.schemaRulesNotSatisfied) : undefined
60+
}
6161
}

src/types.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import type {asModel} from './index.js'
55
export type PaprModelItem = ReturnType<typeof asModel>
66

77
export type ModelRegistrationPair<T> = {
8-
[U in keyof T]: PaprModelItem
8+
[U in keyof T]: PaprModelItem
99
}
1010

1111
export type PaprModels = Record<string, Model<any, any>>
1212

1313
export type FastifyPaprNestedObject = Record<string, PaprModels>
1414

1515
export type FastifyPaprOptions = {
16-
name?: string;
17-
db: Db;
18-
models: ModelRegistrationPair<PaprModels>;
19-
disableSchemaReconciliation?: boolean;
16+
name?: string;
17+
db: Db;
18+
models: ModelRegistrationPair<PaprModels>;
19+
disableSchemaReconciliation?: boolean;
2020
}
2121

2222
declare module 'fastify' {
23-
interface FastifyInstance {
24-
papr: PaprModels & FastifyPaprNestedObject;
25-
}
23+
interface FastifyInstance {
24+
papr: PaprModels & FastifyPaprNestedObject;
25+
}
2626
}

0 commit comments

Comments
 (0)