I have following code for model.
import { DatabaseServer } from './database-server';
import { ProjectGroup } from './project-group';
import { ProjectUser } from './project-user';
import { prop, getModelForClass, ReturnModelType, modelOptions } from '@typegoose/typegoose';
import { defaultTransform, ModelBase } from '../general/model-base';
import { ObjectID } from 'mongodb';
import { Connection } from 'mongoose';
import { BeAnObject } from '@typegoose/typegoose/lib/types';
export class Datasource extends ModelBase {
@prop()
databaseServer?: DatabaseServer;
@prop()
databaseServerId?: ObjectID;
@prop()
datasource?: Datasource[];
@prop()
name?: string;
@prop()
projectGroups?: ProjectGroup[];
@prop()
projectUsers?: ProjectUser[];
}
const DatasourceModel = (
connection: Connection,
): ReturnModelType<typeof Datasource, BeAnObject> => {
return getModelForClass(Datasource, {
...defaultTransform,
...{
existingConnection: connection,
},
});
};
export { DatasourceModel };
And I am using above model as following.
await DatasourceModel(await this.masterContext).find({})
Where mastercontext is defined as below.
import {
Connection,
createConnection
} from 'mongoose';
export class MasterContext {
get context(): Promise<Connection> {
if (!this.m_context) {
this.m_context = createConnection('mongodb://localhost/Master', {
useNewUrlParser: true,
useUnifiedTopology: true,
});
}
return this.m_context;
}
private m_context: Promise<Connection>;
}
I am getting error as following.
Operation datasources.find() buffering timed out after 10000m
If I change class name from export class Datasource
to any other name (e.g. export class Datumsource
) then error is not throwing.
So is Datasource
reserved keyword in MongoDb or Mongoose or Typegoose?
from what i know, this error means that the connection is not connected, so the command (
find
) has an timeouti also would recommend to either cache the
DatasourceModel
or only run the function once (the connection does not need to be connected to create an model, it only needs to be connected to do commands (likefind
))so if you have an global connection, you should simply remove the function and just run
getModelForClass
, but if you have an "local" connection (like from an class property), then you should cache it there, example:Some other notes:
type
option, look here on whymongoose.Types.ObjectId
type should be used for anObjectId