Configuration
Learn how to configure InceptTools libraries for your specific needs.
Configuring @inceptools/db
@inceptools/db provides a flexible configuration system that allows you to customize your database connections.
Basic Configuration
Here's a basic configuration example for connecting to MongoDB:
import { MongoService, SUPPORTED_DBS } from "@inceptools/db";
import mongoose from "mongoose";
// Define your MongoDB schemas
const schemas = {
users: new mongoose.Schema({
name: String,
email: String,
createdAt: { type: Date, default: Date.now },
}),
};
// Create a MongoDB service instance
const mongoService = new MongoService({
type: SUPPORTED_DBS.MONGO_DB,
connectionString: "mongodb://localhost:27017/myapp",
models: schemas,
options: {
useNewUrlParser: true,
useUnifiedTopology: true,
},
logging: true,
});
Multiple Database Configuration
You can configure multiple databases using the DBService:
import { DBService, SUPPORTED_DBS } from "@inceptools/db";
import mongoose from "mongoose";
import { Sequelize, DataTypes } from "sequelize";
// Define your database schemas/models
const db = {
mongodb: {
users: new mongoose.Schema({
name: String,
email: String,
}),
},
postgres: {
contacts: (sequelize: Sequelize) => {
return sequelize.define("Contact", {
name: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
});
},
},
};
// Configure your database connections
const config = {
mongodb: {
type: SUPPORTED_DBS.MONGO_DB,
connectionString: "mongodb://localhost:27017/myapp",
models: db.mongodb,
options: {
useNewUrlParser: true,
useUnifiedTopology: true,
},
},
postgres: {
type: SUPPORTED_DBS.SQL,
connectionString: "postgresql://user:password@localhost:5432/myapp",
models: db.postgres,
configOptions: {
dialect: "postgres",
},
},
redis: {
type: SUPPORTED_DBS.REDIS,
connectionString: "redis://localhost:6379",
},
};
// Create a database service instance
const dbService = new DBService(config);
Configuration Options
MongoDB Options
You can pass any valid Mongoose connection options to the options field:
const mongoService = new MongoService({
type: SUPPORTED_DBS.MONGO_DB,
connectionString: "mongodb://localhost:27017/myapp",
models: schemas,
options: {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 10,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
},
});
SQL Options
For SQL databases, you can pass Sequelize options to the configOptions field:
const sqlService = new SQLService({
type: SUPPORTED_DBS.SQL,
connectionString: "postgresql://user:password@localhost:5432/myapp",
models: models,
configOptions: {
dialect: "postgres",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
logging: false
},
});
Redis Options
For Redis, you can configure various connection options:
const redisService = new RedisService({
type: SUPPORTED_DBS.REDIS,
connectionString: "redis://localhost:6379",
options: {
retryStrategy: (times) => Math.min(times * 50, 2000),
maxRetriesPerRequest: 3,
},
});
Environment Variables
It's recommended to use environment variables for sensitive information like connection strings:
MONGODB_URI=mongodb://localhost:27017/myapp
POSTGRES_URI=postgresql://user:password@localhost:5432/myapp
REDIS_URI=redis://localhost:6379