Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify accessing mongoose models #262

Merged
merged 1 commit into from
Aug 22, 2023
Merged

Conversation

jrassa
Copy link
Member

@jrassa jrassa commented Aug 16, 2023

Currently mongoose models are accessed via db connection and require a typecast.

import { UserModel } from './user.model';

const User = dbs.admin.model('User') as UserModel;

Since model registry is tied to the db connection, there is no need to access it in this way. We can just import the model (w/ proper typing) directly since it is already being exported.

import { User } from './user.model';

For use cases where multiple db connections are used. Models should explicitly be registered on the appropriate connection.

// Register model on default/admin db connection
export const MyModel = model('MyModel', MyModelSchema);

// Register model on secondary db connection
export const OtherModel = dbs.other.model('OtherModel', OtherModelSchema);

Currently mongoose models are accessed via db connection and require a typecast.
```
import { UserModel } from './user.model';

const User = dbs.admin.model('User') as UserModel;
```

Since model registry is tied to the db connection, there is no need to access it in this way.  We can just import the model (w/ proper typing) directly since it is already being exported.

```
import { User } from './user.model';
```

For use cases where multiple db connections are used.  Models should explicitly be registered on the appropriate connection.
```
// Register model on default/admin db connection
export const MyModel = model('MyModel', MyModelSchema);

// Register model on secondary db connection
export const OtherModel = dbs.other.model('OtherModel', OtherModelSchema);
```
@jrassa jrassa merged commit 320341e into develop Aug 22, 2023
4 checks passed
@jrassa jrassa deleted the simplify-modal-access branch August 22, 2023 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant