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

Define models to be used across multiple databases #727

Open
kartsims opened this issue Jan 7, 2020 · 8 comments
Open

Define models to be used across multiple databases #727

kartsims opened this issue Jan 7, 2020 · 8 comments

Comments

@kartsims
Copy link

kartsims commented Jan 7, 2020

Sorry if this doesn't belong here but I am having an issue to re-use models across multiple databases. I think this is related to sequelize-typescript.

When I define my model I am able to add it to a sequelize instance, but when I add it to another instance it fails.

StackOverflow post

@RobinBuschmann
Copy link
Member

Hey @kartsims, you could use the respositoryMode for that. This mode lets you re-use your models. See https://github.com/RobinBuschmann/sequelize-typescript#how-to-enable-repository-mode for details.

@kangzj
Copy link

kangzj commented Feb 25, 2020

@RobinBuschmann hey awesome work man.
Just got a question here as well. I found out how to create / find using repository mode, but how to do updates and deletes?
Can I please have some examples? As sequelize static update/delete are all for bulk operations.
Thanks.

@RobinBuschmann
Copy link
Member

Hey @kangzj , save and update should still work on your instances (instance.save();) So the created objects are still active records.
Otherwise you have to do it like you said: Using repository.update/delete like so:
await MyModel.update(instance.toJSON(), {where: {id: instance.id}});

Hope this helps.

@redevill
Copy link

redevill commented May 4, 2021

Still on the topic of the re-use models / repository mode:
My understanding (and it seems to work in practice) is the following.

const myRepo1 = sequelizeConnection1.getRepository(MyModel);
const myRepo2 = sequelizeConnection2.getRepository(MyModel);

Now when I do something like myRepo1.findAll() the data is distinct from myRepo2.findAll() according to the respective databases that are connected to the instances of sequelize above.

Now, I was wondering, how to protect against someone writing this code in the repository mode:
MyModel.findAll() ?? not using the repository but the Model directly...
Not sure where the data would be sourced for this? or if it would work?

@redevill
Copy link

redevill commented May 4, 2021

... AAAhhh - clever:
With respect to my own question above, I did a test (MyModel.findAll() ), and got the following error.
"Error: Model not initialized: Member "findAll" cannot be called. "MyModel" needs to be added to a Sequelize instance."
In repository mode, the models that were defined previously, no longer respond, as don't appear to be registered with the sequelize instance.

@redevill
Copy link

Still on the topic of the re-use models / repository mode:
(Maybe I get to answer my own question again ;))

Can transactions be used with the repository model. So far, did not have much luck. Is there a trick?

@ghost
Copy link

ghost commented Aug 25, 2021

Can transactions be used with the repository model. So far, did not have much luck. Is there a trick?

Start the transaction using the sequelizeConnection object rather than the repository instance.

@redevill
Copy link

That's the trick - THANK YOU.

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

No branches or pull requests

4 participants