These are chat archives for Automattic/mongoose

21st
Apr 2017
Stephen Charles Huey
@stephenhuey
Apr 21 2017 14:58

I'm rolling my own simple multi-tenant solution, e.g. Mongo collections such as

customer1-users
customer2-users
customer3-users

I was simply having my data access module store a hash of customer names to Mongoose schemas so that I don't compile the schema into a model more than once. However, in thinking about this a bit more, I'm wondering if it's better to store the model rather than the schema...?

collectionName = 'customer1-users';
schema = new Schema(attr, { collection: collectionName });
hash[collectionName] = schema;
MyModel = mongoose.model(collectionName, schema)

That's what I have now, but would there be any advantage to just storing MyModel, the result of mongoose.model(), in a hash instead?

Christian Roy
@roychri
Apr 21 2017 14:59
Are you asking if its better to have one collection per customers or a single collections for all customers?
Stephen Charles Huey
@stephenhuey
Apr 21 2017 15:00
No, we definitely want to one multiple collections (one per customer, per entity type).
@roychri We have a requirement in our industry to more explicitly separate customer data so each customer will get its own set of collections. Right now when I need a mongoose model I just pass the customer name and get a schema and call mongoose.model() on it again, but I'm wondering if it matters performance wise to store the generated models in the hash instead, or is mongoose.model() an inexpensive operation?
Christian Roy
@roychri
Apr 21 2017 15:09
You can benchmark it to evaluate that
Stephen Charles Huey
@stephenhuey
Apr 21 2017 15:33
@roychri Thanks, modeling seems to be about 4 times less expensive.