|
| 1 | +import mongoose, { Schema, UpdateQuery, ModelUpdateOptions } from 'mongoose'; |
| 2 | +import { createError } from 'api/utils'; |
| 3 | + |
| 4 | +import { WithId, UwaziFilterQuery } from './models'; |
| 5 | +import { tenants, Tenant } from './tenantContext'; |
| 6 | +import { DB } from './DB'; |
| 7 | + |
| 8 | +class MultiTenantMongooseModel<T> { |
| 9 | + dbs: { [k: string]: mongoose.Model<WithId<T> & mongoose.Document> }; |
| 10 | + |
| 11 | + constructor(collectionName: string, schema: Schema) { |
| 12 | + this.dbs = {}; |
| 13 | + |
| 14 | + Object.keys(tenants.tenants).forEach(tenantName => { |
| 15 | + if (!this.dbs[tenantName]) { |
| 16 | + this.dbs[tenantName] = DB.connectionForDB(tenants.tenants[tenantName].dbName).model< |
| 17 | + WithId<T> & mongoose.Document |
| 18 | + >(collectionName, schema); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + tenants.on('newTenant', (tenant: Tenant) => { |
| 23 | + if (!this.dbs[tenant.name]) { |
| 24 | + this.dbs[tenant.name] = DB.connectionForDB(tenant.dbName).model<WithId<T> & mongoose.Document>( |
| 25 | + collectionName, |
| 26 | + schema |
| 27 | + ); |
| 28 | + } |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + dbForCurrentTenant() { |
| 33 | + const currentTenantName = tenants.current().name; |
| 34 | + if (!this.dbs[currentTenantName]) { |
| 35 | + throw createError(`tenant "${currentTenantName}" db connection is not available`, 503); |
| 36 | + } |
| 37 | + |
| 38 | + return this.dbs[currentTenantName]; |
| 39 | + } |
| 40 | + |
| 41 | + findById(id: any | string | number, select = {}) { |
| 42 | + return this.dbForCurrentTenant().findById(id, select, { lean: true }); |
| 43 | + } |
| 44 | + |
| 45 | + find(query: UwaziFilterQuery<T>, select = '', options = {}) { |
| 46 | + return this.dbForCurrentTenant().find(query, select, options); |
| 47 | + } |
| 48 | + |
| 49 | + async findOneAndUpdate( |
| 50 | + query: UwaziFilterQuery<T>, |
| 51 | + update: Readonly<Partial<T>> & { _id?: any }, |
| 52 | + options: any = {} |
| 53 | + ) { |
| 54 | + return this.dbForCurrentTenant().findOneAndUpdate(query, update, options); |
| 55 | + } |
| 56 | + |
| 57 | + async create(data: Readonly<Partial<T>> & { _id?: any }) { |
| 58 | + return this.dbForCurrentTenant().create(data); |
| 59 | + } |
| 60 | + |
| 61 | + async _updateMany( |
| 62 | + conditions: UwaziFilterQuery<T>, |
| 63 | + doc: UpdateQuery<T>, |
| 64 | + options: ModelUpdateOptions = {} |
| 65 | + ) { |
| 66 | + return this.dbForCurrentTenant().updateMany(conditions, doc, options); |
| 67 | + } |
| 68 | + |
| 69 | + async findOne(conditions: UwaziFilterQuery<T>, projection: any) { |
| 70 | + return this.dbForCurrentTenant().findOne(conditions, projection); |
| 71 | + } |
| 72 | + |
| 73 | + async replaceOne(conditions: UwaziFilterQuery<T>, replacement: any) { |
| 74 | + return this.dbForCurrentTenant().replaceOne(conditions, replacement); |
| 75 | + } |
| 76 | + |
| 77 | + async countDocuments(query: UwaziFilterQuery<T> = {}) { |
| 78 | + return this.dbForCurrentTenant().countDocuments(query); |
| 79 | + } |
| 80 | + |
| 81 | + async distinct(field: string, query: UwaziFilterQuery<T> = {}) { |
| 82 | + return this.dbForCurrentTenant().distinct(field, query); |
| 83 | + } |
| 84 | + |
| 85 | + async deleteMany(query: UwaziFilterQuery<T>) { |
| 86 | + return this.dbForCurrentTenant().deleteMany(query); |
| 87 | + } |
| 88 | + |
| 89 | + async aggregate(aggregations?: any[]) { |
| 90 | + return this.dbForCurrentTenant().aggregate(aggregations); |
| 91 | + } |
| 92 | + |
| 93 | + async updateOne(conditions: UwaziFilterQuery<T>, doc: UpdateQuery<T>) { |
| 94 | + return this.dbForCurrentTenant().updateOne(conditions, doc); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export { MultiTenantMongooseModel }; |
0 commit comments