From 0466fc4e18af88916989532e25c5bc492609e243 Mon Sep 17 00:00:00 2001 From: LoneRifle Date: Thu, 22 Apr 2021 15:38:39 +0800 Subject: [PATCH] fix(mongoose): use official discriminator definitions Now that mongoose properly defines discriminator(), drop our custom definition of DocumentArray in favour of the official one - pick up the latest version of mongoose - go back to casting the return value of `FormSchema.path()` as a DocumentArray, as opposed to our custom definition of it - remove custom definitions of mongoose types, no longer used --- package-lock.json | 21 +++++++++++------- package.json | 2 +- src/app/models/form.server.model.ts | 8 +++---- src/types/vendor/mongoose.d.ts | 33 ----------------------------- 4 files changed, 18 insertions(+), 46 deletions(-) delete mode 100644 src/types/vendor/mongoose.d.ts diff --git a/package-lock.json b/package-lock.json index b0ee6bd6d3..7a3a665016 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17522,14 +17522,14 @@ "integrity": "sha1-D3ca0W9IOuZfQoeWlCjp+8SqYYE=" }, "mongoose": { - "version": "5.12.3", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.12.3.tgz", - "integrity": "sha512-frsSR9yeldaRpSUeTegXCSB0Tu5UGq8sHuHBuEV31Jk3COyxlKFQPL7UsdMhxPUCmk74FpOYSmNwxhWBEqgzQg==", + "version": "5.12.5", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.12.5.tgz", + "integrity": "sha512-VVoqiELZcoI2HhHDuPpfN3qmExrtIeXSWNb1nihf4w1SJoWGXilU/g2cQgeeSMc2vAHSZd5Nv2sNPvbZHFw+pg==", "requires": { "@types/mongodb": "^3.5.27", "bson": "^1.1.4", "kareem": "2.3.2", - "mongodb": "3.6.5", + "mongodb": "3.6.6", "mongoose-legacy-pluralize": "1.0.2", "mpath": "0.8.3", "mquery": "3.2.5", @@ -17546,14 +17546,14 @@ "integrity": "sha512-EvVNVeGo4tHxwi8L6bPj3y3itEvStdwvvlojVxxbyYfoaxJ6keLgrTuKdyfEAszFK+H3olzBuafE0yoh0D1gdg==" }, "mongodb": { - "version": "3.6.5", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.5.tgz", - "integrity": "sha512-mQlYKw1iGbvJJejcPuyTaytq0xxlYbIoVDm2FODR+OHxyEiMR021vc32bTvamgBjCswsD54XIRwhg3yBaWqJjg==", + "version": "3.6.6", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.6.tgz", + "integrity": "sha512-WlirMiuV1UPbej5JeCMqE93JRfZ/ZzqE7nJTwP85XzjAF4rRSeq2bGCb1cjfoHLOF06+HxADaPGqT0g3SbVT1w==", "requires": { "bl": "^2.2.1", "bson": "^1.1.4", "denque": "^1.4.1", - "require_optional": "^1.0.1", + "optional-require": "^1.0.2", "safe-buffer": "^5.1.2", "saslprep": "^1.0.0" } @@ -18356,6 +18356,11 @@ "last-call-webpack-plugin": "^3.0.0" } }, + "optional-require": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/optional-require/-/optional-require-1.0.3.tgz", + "integrity": "sha512-RV2Zp2MY2aeYK5G+B/Sps8lW5NHAzE5QClbFP15j+PWmP+T9PxlJXBOOLoSAdgwFvS4t0aMR4vpedMkbHfh0nA==" + }, "optionator": { "version": "0.8.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.3.tgz", diff --git a/package.json b/package.json index 24df9080d4..e3e2a95703 100644 --- a/package.json +++ b/package.json @@ -124,7 +124,7 @@ "lodash": "^4.17.21", "moment-timezone": "0.5.33", "mongodb-uri": "^0.9.7", - "mongoose": "^5.12.3", + "mongoose": "^5.12.5", "multiparty": ">=4.2.2", "neverthrow": "^4.2.1", "ng-infinite-scroll": "^1.3.0", diff --git a/src/app/models/form.server.model.ts b/src/app/models/form.server.model.ts index a88bb0d1c5..b1d356b831 100644 --- a/src/app/models/form.server.model.ts +++ b/src/app/models/form.server.model.ts @@ -351,7 +351,7 @@ const compileFormModel = (db: Mongoose): IFormModel => { // Add discriminators for the various field types. const FormFieldPath = FormSchema.path( 'form_fields', - ) as Schema.Types.DocumentArrayWithLooseDiscriminator + ) as Schema.Types.DocumentArray const TableFieldSchema = createTableFieldSchema() @@ -385,7 +385,7 @@ const compileFormModel = (db: Mongoose): IFormModel => { FormFieldPath.discriminator(BasicField.Table, TableFieldSchema) const TableColumnPath = TableFieldSchema.path( 'columns', - ) as Schema.Types.DocumentArrayWithLooseDiscriminator + ) as Schema.Types.DocumentArray TableColumnPath.discriminator( BasicField.ShortText, createShortTextFieldSchema(), @@ -398,13 +398,13 @@ const compileFormModel = (db: Mongoose): IFormModel => { // Discriminator defines all possible values of startPage.logo const StartPageLogoPath = FormSchema.path( 'startPage.logo', - ) as Schema.Types.DocumentArrayWithLooseDiscriminator + ) as Schema.Types.DocumentArray StartPageLogoPath.discriminator(FormLogoState.Custom, CustomFormLogoSchema) // Discriminator defines different logic types const FormLogicPath = FormSchema.path( 'form_logics', - ) as Schema.Types.DocumentArrayWithLooseDiscriminator + ) as Schema.Types.DocumentArray FormLogicPath.discriminator(LogicType.ShowFields, ShowFieldsLogicSchema) FormLogicPath.discriminator(LogicType.PreventSubmit, PreventSubmitLogicSchema) diff --git a/src/types/vendor/mongoose.d.ts b/src/types/vendor/mongoose.d.ts deleted file mode 100644 index 917356050f..0000000000 --- a/src/types/vendor/mongoose.d.ts +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Additional type declarations for mongoose to fit our use case, - * to accommodate the non-standard but compatible use of types - * in schema registration - */ -declare module 'mongoose' { - namespace Schema { - namespace Types { - /** - * A DocumentArray with a discriminator function that takes in a - * type-generic Schema. - */ - class DocumentArrayWithLooseDiscriminator extends DocumentArray { - /** - * In the built-in type declarations in - * version 5.12 of mongoose, discriminator() expects a Schema - * (and hence Schema). This does not work; a Schema - * for a subtype of Document is not a Schema for a Document, as - * Schema.methods expect to operate on the Schema's document type, - * which is not necessarily a Document. - * - * Address this by overriding the definition and provide a type-generic - * Schema argument. - */ - discriminator( - name: string, - schema: Schema, - tag?: string, - ): unknown - } - } - } -}