API Reference
Complete API reference for all @inceptools/db classes and methods.
DBService
The main service for managing multiple database connections.
Constructor
new DBService(config: {
[key: string]: {
type: SUPPORTED_DBS;
connectionString: string;
models?: any;
options?: any;
configOptions?: any;
logging?: boolean;
};
})
Methods
Method | Description | Returns |
---|---|---|
connect() | Connects to all configured databases | Promise<void> |
closeConnection() | Closes all database connections | Promise<void> |
getService(name: string) | Gets a specific database service by name | MongoService | SQLService | RedisService |
Properties
The DBService instance has dynamic properties for each configured database service. For example, if you configured a MongoDB service with the key "mongodb", you can access it as dbService.mongodb
.
MongoService
Service for managing MongoDB connections.
Constructor
new MongoService({
type: SUPPORTED_DBS.MONGO_DB;
connectionString: string;
models?: {
[key: string]: mongoose.Schema;
};
options?: mongoose.ConnectOptions;
logging?: boolean;
})
Methods
Method | Description | Returns |
---|---|---|
connect() | Connects to MongoDB | Promise<void> |
closeConnection() | Closes the MongoDB connection | Promise<void> |
getModel(name: string) | Gets a Mongoose model by name | mongoose.Model<any> |
startSession() | Starts a MongoDB session for transactions | Promise<mongoose.ClientSession> |
Properties
Property | Description | Type |
---|---|---|
connection | The Mongoose connection instance | mongoose.Connection |
mongoose | The Mongoose instance | typeof mongoose |
migrationService | Service for managing MongoDB migrations | MongoMigrationService |
[modelName] | Dynamic properties for each model | mongoose.Model<any> |
SQLService
Service for managing SQL database connections.
Constructor
new SQLService({
type: SUPPORTED_DBS.SQL;
connectionString: string;
models?: {
[key: string]: (sequelize: Sequelize) => any;
};
configOptions?: SequelizeOptions;
logging?: boolean;
})
Methods
Method | Description | Returns |
---|---|---|
connect() | Connects to the SQL database | Promise<void> |
closeConnection() | Closes the SQL database connection | Promise<void> |
getModel(name: string) | Gets a Sequelize model by name | any |
transaction(options?: TransactionOptions) | Starts a transaction | Promise<Transaction> |
Properties
Property | Description | Type |
---|---|---|
sequelize | The Sequelize instance | Sequelize |
migrationService | Service for managing SQL migrations | SQLMigrationService |
[modelName] | Dynamic properties for each model | Model<any> |
RedisService
Service for managing Redis connections.
Constructor
new RedisService({
type: SUPPORTED_DBS.REDIS;
connectionString: string;
options?: RedisOptions;
logging?: boolean;
})
Methods
Method | Description | Returns |
---|---|---|
connect() | Connects to Redis | Promise<void> |
closeConnection() | Closes the Redis connection | Promise<void> |
get(key: string) | Gets a value by key | Promise<string | null> |
set(key: string, value: string, ...args: any[]) | Sets a key-value pair | Promise<string> |
del(key: string) | Deletes a key | Promise<number> |
exists(key: string) | Checks if a key exists | Promise<number> |
expire(key: string, seconds: number) | Sets a key's expiration time | Promise<number> |
incr(key: string) | Increments a key's value | Promise<number> |
decr(key: string) | Decrements a key's value | Promise<number> |
The RedisService also supports all other Redis commands through direct method calls. For example, you can use redisService.hset()
, redisService.lpush()
, etc.
Migration Services
Services for managing database migrations.
MongoMigrationService
Method | Description | Returns |
---|---|---|
init() | Initializes the migration service | Promise<void> |
generateMigration(name: string) | Generates a new migration file | Promise<string> |
migrate() | Applies pending migrations | Promise<void> |
rollback() | Rolls back the last applied migration | Promise<void> |
status() | Gets the migration status | Promise<MigrationStatus> |
SQLMigrationService
Method | Description | Returns |
---|---|---|
init() | Initializes the migration service | Promise<void> |
generateMigration(name: string) | Generates a new migration file | Promise<string> |
migrate() | Applies pending migrations | Promise<void> |
rollback() | Rolls back the last applied migration | Promise<void> |
status() | Gets the migration status | Promise<MigrationStatus> |
Enums
Enums used in @inceptools/db.
SUPPORTED_DBS
enum SUPPORTED_DBS {
MONGO_DB = "mongodb",
SQL = "sql",
REDIS = "redis",
}
Interfaces
Interfaces used in @inceptools/db.
MigrationStatus
interface MigrationStatus {
applied: string[];
pending: string[];
}