Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SQL Server and mongodb with Repository pattern in node.js

I try to implement dependency injection with repository pattern in expressJS. I want to achieve that using two different repository types (mongodb and SQL Server ) but one controller.

When app starts, I should choose one repository type (for example SQL Server repository) which is used by application. So I don't want to create different controllers for every repository types.

How can I do that? Is there any suggestion?

like image 318
emreturan Avatar asked Jun 25 '26 11:06

emreturan


1 Answers

I suspect you are coming from a statically-typed languages like C# or Java in which using one class over another is somewhat convoluted since their interface must match. Keep in mind that in languages like JavaScript that are dynamically typed using one class or another is easy; there is little required ceremony to use one or the other, it's mostly up to you to make sure both classes implement the required functionality.

var dbProv = (config == "sql") ? new SqlProv() : new mongoProv();
dbProv.getAll();
like image 64
Hector Correa Avatar answered Jun 27 '26 02:06

Hector Correa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!