typegoose: `useDB` fails before connecting the connection
Versions
- System: linux / macos / windows
- NodeJS: 14.17
- Typescript: 4.5.2
- Compiler / Transpiler: tsc / ts-node / ts-jest
- Typegoose(NPM): 0.0.0
- Typegoose(GIT): commithash
- mongoose: 6.0.11
- mongodb: 0.0.0
- nestjs / nestjs-typegoose: 0.0.0 / 0.0.0
- grahpql / type-graphql: 0.0.0 / 0.0.0
What is the Problem?
Schema has not been registered
Code Example
1. BaseUsers
import { getModelForClass, prop } from "@typegoose/typegoose";
export class BaseUsers {
@prop()
name: string;
@prop({ unique: true })
email: string;
@prop()
password: string;
@prop()
country: string;
@prop()
pin_code: string;
@prop()
mobile_number: string;
@prop()
can_be_deleted: boolean;
@prop()
avatar: string;
}
const BASE_USERS_MODEL = getModelForClass(BaseUsers, {
schemaOptions: {
collection: "users",
timestamps: true,
},
});
export default BASE_USERS_MODEL;
2. Clients
import { getModelForClass, Index, prop } from "@typegoose/typegoose";
export class SMTPSettings {
@prop()
host: string;
@prop()
email: string;
@prop()
from: string;
@prop()
password: string;
@prop()
port: string;
@prop()
isSSL: boolean;
}
export class SendGridSettings {
@prop()
host: string;
@prop()
email: string;
@prop()
from: string;
@prop()
password: string;
@prop()
port: string;
@prop()
isSSL: boolean;
}
@Index(
{ name: 1, alias_name: 1 },
{
unique: true,
}
)
export class BaseClients {
@prop()
name: string;
@prop()
alias_name: string;
@prop()
logo: string;
@prop()
akamai_cp_code: string;
@prop()
is_smtp: boolean;
@prop({ _id: false })
stmp_settings?: SMTPSettings;
@prop({ _id: false })
sendgrid_settings?: SendGridSettings;
}
const BASE_CLIENTS_MODEL = getModelForClass(BaseClients, {
schemaOptions: {
collection: "clients",
timestamps: true,
},
});
export default BASE_CLIENTS_MODEL;
3. Roles
import { getModelForClass, prop } from "@typegoose/typegoose";
export class Roles {
@prop()
name: string;
}
const RolesModel = getModelForClass(Roles, {
schemaOptions: {
collection: "roles",
},
});
export default RolesModel;
4. Clients Association
import { getModelForClass, mongoose, prop, Ref } from "@typegoose/typegoose";
import { BaseClients } from "./clients";
import { Roles } from "./roles";
import { BaseUsers } from "./users";
export class ClientIdAssociation {
@prop({ ref: () => BaseClients })
client_id?: Ref<BaseClients>;
@prop({ ref: () => BaseUsers })
user_id: Ref<BaseUsers>;
@prop({ ref: () => Roles, required: true })
roles!: Ref<Roles>[];
@prop({ type: mongoose.Types.ObjectId })
allowed_cms: mongoose.Types.ObjectId[];
@prop({ type: mongoose.Types.ObjectId })
allowed_crm: mongoose.Types.ObjectId[];
@prop({ type: mongoose.Types.ObjectId })
allowed_uit: mongoose.Types.ObjectId[];
@prop({ type: mongoose.Types.ObjectId })
allowed_iq: mongoose.Types.ObjectId[];
}
const CLIENT_ID_ASSOCIATION_MODEL = getModelForClass(ClientIdAssociation, {
schemaOptions: {
timestamps: true,
collection: "user_association_with_clients",
},
});
export default CLIENT_ID_ASSOCIATION_MODEL;
My DB Config looks like this
import {
AnyParamConstructor,
ReturnModelType
} from "@typegoose/typegoose/lib/types";
import Mongoose from "mongoose";
class DatabaseConfig {
public database: Mongoose.Connection;
public connectDatabase = (): any => {
const uri =
"mongodb://localhost:27017" ??
"";
if (this.database) {
return;
}
Mongoose.connect(uri);
this.database = Mongoose.connection;
this.database.once("open", async () => {
console.log("Connected to database");
});
this.database.on("error", () => {
console.log("Error connecting to database");
});
};
disconnectDatabase = () => {
if (!this.database) {
return;
}
Mongoose.disconnect();
};
getModelForDb<T extends AnyParamConstructor<any>>(
databaseName: string,
model: ReturnModelType<T>
): ReturnModelType<T> & T {
const db = Mongoose.connection.useDb(databaseName);
const DbModel = db.model(
model.modelName,
model.schema
) as ReturnModelType<T> & T;
return DbModel;
}
}
export default new DatabaseConfig();
Now when I try to get the reference populated it gives me error for roles while the client_id and user_id is correctly populated. I am using the below code for getting the results.
let client_id_association_model = databaseConfig.getModelForDb<
typeof ClientIdAssociation
>("ims_base_db", CLIENT_ID_ASSOCIATION_MODEL);
let found_users = await client_id_association_model
.find({
user_id: new mongoose.Types.ObjectId("6103c782f67f5162439f7d8e"),
})
.populate(["roles","client_id"]);
Kindly advise where is the issue.
Packages Version
"@typegoose/typegoose": "^9.2.0",
"express": "^4.17.1",
"mongoose": "^6.0.11"
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 24 (11 by maintainers)
Facing the same issue while using the generic model method to support multiple dynamic databases.
Was working fine with typegoose 7.6.3 and mongoose 5.10.18