typegoose: `instanceOf` errors in browser runtime

My @typegoose/typegoose ESM compliant local library has run wonderful without any debugging. I am fine tuning the library at this time and flushed out a stack trace.

Wanted to offer this up for any feedback on safer typescript or existing frail typescript

SUBJECT: @typegoose/typegoose built as local esm library receives runtime stack trace on bootstrap

CODE STACK TRACE CHRONOLOGY

simple consumer library MODELS has a barrel file index.ts export * from './lib/pojo/spinner.pojo'; and exports a pojo

spinner-pojo.ts

import { AbstractPojo } from '@nhhockeyplayer/shared-util-abstract'
import { ISpinnerPojo } from './interfaces/i-spinner.pojo'
import { OmitFunctions } from '@nhhockeyplayer/shared-util-abstract'
import { getModelForClass, modelOptions, prop, Severity } from '@typegoose/typegoose'
``
@modelOptions({ schemaOptions: { collection: 'spinner' }, options: { automaticName: true, allowMixed: Severity.ALLOW } })
export class SpinnerPojo extends AbstractPojo<SpinnerPojo> implements ISpinnerPojo, OmitFunctions<SpinnerPojo> {
    @prop()
    show?: boolean

    constructor(anatomy: Partial<SpinnerPojo> = {}) {
        super(anatomy)
    }
}

export const SpinnerPojoModel = getModelForClass(SpinnerPojo)

for this flavor instrumented ESM @typegoose/typegoose and of stack trace it seems the class SpinnerPojo is insufficient for the getModelForClass() implementation as its just a class not an actual instance and it is used for life-cycle in creating instances of the same.

Can anything be said about class vs instance for getModelForClass() ? why it’s failing? Is there a remedy? is instanceOf shakey code… should it be typeOf?

here is the logic from typegoose.ts previously the entrypoint for current release but now index.d.ts as exported std Nx barrel file.

how does it make sense to do instanceof to a class… should I provide an actual instance? or should I use typeof?

assertion(model.prototype instanceof mongooseModel, new NotValidModelError(model, 'addModelToTypegoose.model'));
  assertionIsClass(cl);

the failure lands on the above export export const SpinnerPojoModel = getModelForClass(SpinnerPojo) calling into question the durability of getModelForClass() which uses instanceOf

FAILURE LOG: core.mjs:6673 ERROR TypeError: Right-hand side of ‘instanceof’ is not an object at addModelToTypegoose (typegoose.ts:236:19) at getModelForClass (typegoose.ts:105:10) at libs/shared/util/isomorphic/src/lib/pojo/spinner.pojo.ts (spinner.pojo.ts:16:33) at __init (chunk-4IQVVUD4.js:37:56) at libs/shared/util/isomorphic/src/index.ts (index.ts:1:1) at __init (chunk-4IQVVUD4.js:37:56) at auth-facade.guard.ts:6:1

PROGNOSIS: as I stated library runs fine Im fine tuiing for package.json generation and interface entry points for esm and commonjs simultaneously for the final resulting distribution

Maybe there is a typescript compiler switch I can operate that will permit this code to build and run clean?

Any feedback is appreciated and helpful. I had hoped I didnt have to change anything but this code was taken moments ago from the github repo as-is and I just added a barrel to export everything as the entrypoint to the library (see attachments of the dist lib and the actual source code to the lib)

output to dist folder dist.libs.typegoose.zip

source code living in my monorepo for @typegoose/typegoose typegoose.zip

Should I re-tweek the line of code that does instanceOf ? to something else?

thanks for your interest, support and generosity

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 38 (16 by maintainers)

Commits related to this issue

Most upvoted comments

FYI, the mongoose PR https://github.com/Automattic/mongoose/pull/15277 has been merged and released with mongoose 8.12, which typegoose 12.13.0 uses.