AbimongoClient
@abimongo/core / AbimongoClient
Class: AbimongoClient
Defined in: core/src/lib-core/AbimongoClient.ts:40
AbimongoClient is a MongoDB client wrapper that provides a simplified interface for connecting to and interacting with MongoDB databases. It supports multi-tenancy, connection pooling, and error handling. It also provides methods for connecting to a database, getting collections name and dropping databases.
Param
The MongoDB connection URI.
Param
Optional configuration options for the client.
Param
The name of the database to connect to.
Param
The name of the collection to use.
Param
An optional MongoDB client instance to use.
Param
An optional logger instance for logging messages. AbimongoClient
Extended by
Implements
Constructors
Constructor
new AbimongoClient(
uri,_options?):AbimongoClient
Defined in: core/src/lib-core/AbimongoClient.ts:52
Parameters
uri
string = AbimongoClient.defaultUri
_options?
Returns
AbimongoClient
Properties
_options?
optional_options:AbimongoClientOptions
Defined in: core/src/lib-core/AbimongoClient.ts:54
uri
uri:
string=AbimongoClient.defaultUri
Defined in: core/src/lib-core/AbimongoClient.ts:53
The MongoDB connection URI.
Implementation of
Accessors
client
Get Signature
get client():
MongoClient
Defined in: core/src/lib-core/AbimongoClient.ts:264
Gets the current MongoClient instance.
Returns
MongoClient
The connected MongoClient instance.
An optional MongoClient instance.
Implementation of
db
Get Signature
get db():
Db
Defined in: core/src/lib-core/AbimongoClient.ts:245
Gets the current database instance.
Throws
If the database connection is not established.
Returns
Db
The connected database instance.
Methods
close()
close():
Promise<void>
Defined in: core/src/lib-core/AbimongoClient.ts:455
Closes the MongoDB client connection.
Returns
Promise<void>
A promise that resolves when the client is closed.
collection()
collection<
T>(name):Collection<T>
Defined in: core/src/lib-core/AbimongoClient.ts:319
Retrieves a MongoDB collection by name.
Type Parameters
T
T extends Document
Parameters
name
string
The name of the collection to retrieve.
Returns
Collection<T>
The MongoDB collection instance.
Throws
If the database connection is not established.
connect()
connect():
Promise<Db>
Defined in: core/src/lib-core/AbimongoClient.ts:291
Establishes a connection to the MongoDB database.
Returns
Promise<Db>
A promise that resolves to the connected database instance.
connectDb()
connectDb(
uri,options?):Promise<AbimongoClient>
Defined in: core/src/lib-core/AbimongoClient.ts:108
Connects to the MongoDB database using the provided URI and options.
Parameters
uri
string
The MongoDB connection URI.
options?
Optional configuration options for the client.
Returns
Promise<AbimongoClient>
A promise that resolves to the connected AbimongoClient instance.
Throws
If the URI is not provided.
disconnect()
disconnect():
Promise<void>
Defined in: core/src/lib-core/AbimongoClient.ts:443
Disconnects from the MongoDB database.
Returns
Promise<void>
A promise that resolves when the client is disconnected.
dropCollection()
dropCollection():
Promise<void>
Defined in: core/src/lib-core/AbimongoClient.ts:408
Drops the specified collection from the database.
Returns
Promise<void>
A promise that resolves when the collection is dropped.
dropDatabase()
dropDatabase():
Promise<boolean>
Defined in: core/src/lib-core/AbimongoClient.ts:418
Drops the entire database.
Returns
Promise<boolean>
A promise that resolves to true if the database is dropped successfully, false otherwise.
getClusterInfo()
getClusterInfo():
Promise<{setName?:string;type:string; }>
Defined in: core/src/lib-core/AbimongoClient.ts:349
Retrieves information about the MongoDB cluster type (e.g., standalone, replica set, sharded).
Returns
Promise<{ setName?: string; type: string; }>
A promise that resolves to an object containing the cluster type and set name (if applicable).
getCollection()
getCollection<
T>(name):Collection<T>
Defined in: core/src/lib-core/AbimongoClient.ts:336
Retrieves a MongoDB collection by name, defaulting to the collection specified in the options if not provided.
Type Parameters
T
T extends Document
Parameters
name
string
The name of the collection to retrieve.
Returns
Collection<T>
The MongoDB collection instance.
Throws
If the database connection is not established.
isConnected()
isConnected():
boolean
Defined in: core/src/lib-core/AbimongoClient.ts:463
Checks if the MongoDB client is connected.
Returns
boolean
true if the client is connected, false otherwise.
useCollection()
useCollection(
collectionName):Promise<Collection<any>>
Defined in: core/src/lib-core/AbimongoClient.ts:391
Switches to a different collection at runtime.
Parameters
collectionName
string
The name of the collection to switch to.
Returns
Promise<Collection<any>>
A promise that resolves to the new collection instance.
Throws
If the client is not initialized or the collection name is not provided.
useDatabase()
useDatabase(
dbName):Promise<{client:MongoClient;db:Db; }>
Defined in: core/src/lib-core/AbimongoClient.ts:371
Switches to a different database at runtime (e.g., for multi-tenancy).
Parameters
dbName
string
The name of the database to switch to.
Returns
Promise<{ client: MongoClient; db: Db; }>
A promise that resolves to the new database instance.
Throws
If the client is not initialized or the database name is not provided.
validateUri()
validateUri(
uri):void
Defined in: core/src/lib-core/AbimongoClient.ts:273
Validates the MongoDB URI to ensure it starts with "mongodb://" or "mongodb+srv://".
Parameters
uri
string
The MongoDB connection URI.
Returns
void
Throws
If the URI is invalid.
getAllTenantDBs()
staticgetAllTenantDBs():Promise<Db[]>
Defined in: core/src/lib-core/AbimongoClient.ts:207
Returns
Promise<Db[]>
getDatabase()
staticgetDatabase(tenantId,uri):Promise<{client:MongoClient;db:Db; }>
Defined in: core/src/lib-core/AbimongoClient.ts:132
Retrieves the database connection for a specific tenant.
Parameters
tenantId
string
The ID of the tenant.
uri
string
The MongoDB connection URI.
Returns
Promise<{ client: MongoClient; db: Db; }>
A promise that resolves to the connected database instance.
Throws
If the MongoClient instance is undefined.
getRegisteredModel()
staticgetRegisteredModel(modelName,tenantId,schema?):GetTanantModelParams<Db> &object
Defined in: core/src/lib-core/AbimongoClient.ts:218
Parameters
modelName
string
tenantId
string
schema?
AbimongoSchema<any>
Returns
GetTanantModelParams<Db> & object
getTenantDB()
staticgetTenantDB(tenantId):Db
Defined in: core/src/lib-core/AbimongoClient.ts:186
Retrieves the database connection for a specific tenant.
Parameters
tenantId
string
The ID of the tenant.
Returns
Db
The connected database instance.
Throws
If the MongoClient instance is undefined.
handleTopologyEvent()
statichandleTopologyEvent(event):void
Defined in: core/src/lib-core/AbimongoClient.ts:490
Handles MongoDB topology events (e.g., opening, closing).
Parameters
event
The topology event to handle.
TopologyOpeningEvent | TopologyClosedEvent
Returns
void
init()
staticinit():string|Db
Defined in: core/src/lib-core/AbimongoClient.ts:77
Returns
string | Db
runGlobalGC()
staticrunGlobalGC():Promise<void>
Defined in: core/src/lib-core/AbimongoClient.ts:234
Returns
Promise<void>