These are chat archives for Automattic/mongoose

12th
Oct 2018
Eswan
@Eswan
Oct 12 2018 11:32
Hi guys,
i have multiple clients that want to use an application i develop and i would like to know what would be the best solution to seperate the data for each client.
I would like each client to get his owns collections, i was thinking about make a unique database by client, but it means my API will have to connect to the database on each request, that's weird.
Thanks
Kev
@lineus
Oct 12 2018 11:37
@Eswan when mongoose.createConnection is called multiple times to the same host ( with different dbs ) it might share the same socket. It would be cool if it did, I'll test it out and see.
Eswan
@Eswan
Oct 12 2018 11:38
So you think i can just make an interceptor and call createConnection before each request without having perf issues ?
they say it is a better solution to use multiple connections
Kev
@lineus
Oct 12 2018 12:05
I'm wondering if you use createConnection() multiple times in a process with the same host/port but with different databases, will mongodb's native driver use the same socket for all of them. This would allow you to create a new connection one time for each database without having to disconnect and reconnect for every request. If you change the default connection with mongoose.connect() for every request, not only are you going to have the extra overhead of connecting every time, but if you have concurrent requests for different databases, someone's connection is probably going to be overwritten.
Eswan
@Eswan
Oct 12 2018 12:10
i will try with createConnection, thank you for your help
Kev
@lineus
Oct 12 2018 12:17
for the record, multiple connections to the same host/port combination with different databases using createConnection results in mutliple TCP connections, not just one.
Eswan
@Eswan
Oct 12 2018 12:18
i think about something, is it possible to have my database, and a collection for each client ?
database
  • client1
    --- users
    --- products
  • client2
    --- users
    --- products
maybe easier than multiple databases
Kev
@lineus
Oct 12 2018 12:23
Yeah, you can store different document types in the same collection using two different models that point to the same collection. The officially supported way to do this is with discriminators, but you can also implement it manually with some clever pre hooks on each schema.
Eswan
@Eswan
Oct 12 2018 12:24
or maybe have customer.collectionName ?
each collection has customerName prefix
so in my database i will have : customer1.users, customer2.users
Kev
@lineus
Oct 12 2018 12:25
that's an even better idea, it's a more elegant solution for sure.
Eswan
@Eswan
Oct 12 2018 12:25
you think it's better than a database by user ?
Kev
@lineus
Oct 12 2018 12:30
I think it's easier to implement than separate databases. Since I can't think of a meaningful reason to use separate databases in this case, I think using prefixed collections is the better approach.
Eswan
@Eswan
Oct 12 2018 12:30
ok, any idea how i can do that ? ^^'
User.findOne({
    _id: id
  }).exec(function (err, user) {
I have no idea how i can tell to mongoose to add a prefix on this request :/
or maybe it works to do User = moogose.model('prefix.User') ?
This message was deleted
Eswan
@Eswan
Oct 12 2018 12:46
It's working ! i just added var HolidayModel = mongoose.models[req.query.db + '.Holiday'] || mongoose.model(req.query.db + '.Holiday', HolidaySchema); before my mongoose request