@uisap/core
Version:
A modular Fastify-based framework inspired by Laravel
112 lines (100 loc) • 4.26 kB
JavaScript
import { ServiceProvider } from "../serviceProvider.js";
import { Sequelize } from "sequelize";
import { RedisKeyValueStore } from "../redisKeyValueStore.js";
export class DatabaseProvider extends ServiceProvider {
register() {
const dbConfig = this.app.config.database || {};
const dialect = dbConfig.default || "mysql";
const connectionConfig = dbConfig.connections?.[dialect] || {};
const dialectDefaults = {
mssql: { port: 1433, module: "tedious" },
mysql: { port: 3306, module: "mysql2" },
postgres: { port: 5432, module: "pg" },
sqlite: { port: null, module: "sqlite3" },
mariadb: { port: 3306, module: "mariadb" },
oracle: { port: 1521, module: "oracledb" },
};
const dialectConfig = dialectDefaults[dialect] || { port: null };
const sequelize = new Sequelize({
dialect: dialect,
host: connectionConfig.host || "localhost",
port: connectionConfig.port || dialectConfig.port,
username: connectionConfig.username || (dialect === "sqlite" ? null : "root"),
password: connectionConfig.password || "",
database: connectionConfig.database || "uisap_db",
storage: dialect === "sqlite" ? connectionConfig.storage || ":memory:" : undefined,
timezone: connectionConfig.timezone || "+00:00",
dialectOptions: {
connectTimeout: connectionConfig.connectTimeout || 10000,
useUTC: connectionConfig.useUTC || true,
dateStrings: connectionConfig.dateStrings || true,
typeCast: connectionConfig.typeCast || true,
},
pool: {
max: connectionConfig.pool?.max || 10,
min: connectionConfig.pool?.min || 0,
idle: connectionConfig.pool?.idle || 30000,
},
logging:
connectionConfig.logging ?? ((msg) => this.app.fastify.logger.debug(msg)),
});
this.app.container.singleton("sequelize", () => sequelize);
const keyValueDriver = dbConfig.defaultKeyValue || "redis";
const keyValueConfig = dbConfig.connections?.[keyValueDriver] || {};
this.app.container.singleton("keyValueStore", () => {
if (keyValueDriver === "redis") {
return new RedisKeyValueStore(this.app.fastify, keyValueConfig);
}
throw new Error(`Unsupported key-value driver: ${keyValueDriver}`);
});
// Explicitly decorate fastify with keyValue after singleton registration
const keyValueStore = this.app.container.make("keyValueStore");
this.app.fastify.decorate("keyValue", keyValueStore);
this.app.fastify.logger.debug("DatabaseProvider registered", {
dialect,
keyValueDriver,
});
}
async boot() {
const dbConfig = this.app.config.database || {};
const dialect = dbConfig.default || "mysql";
try {
const sequelize = this.app.container.make("sequelize");
await sequelize.authenticate();
this.app.fastify.logger.info("Veritabanı bağlantısı başarılı", {
dialect,
server: dbConfig.connections?.[dialect]?.host,
database: dbConfig.connections?.[dialect]?.database,
});
if (dbConfig.connections?.[dialect]?.sync) {
await sequelize.sync(dbConfig.connections[dialect].sync);
this.app.fastify.logger.debug("Tablolar senkronize edildi", {
sync: dbConfig.connections[dialect].sync,
});
}
} catch (err) {
this.app.fastify.logger.error("Veritabanı bağlantı hatası", err);
throw err;
}
// Key-value store bağlantısını test et
try {
const keyValueStore = this.app.container.make("keyValueStore");
this.app.fastify.logger.debug("KeyValueStore başlatıldı", {
driver: dbConfig.defaultKeyValue,
config: keyValueStore.config,
});
await keyValueStore.set("test", "connection_ok", 5);
const result = await keyValueStore.get("test");
if (result === "connection_ok") {
this.app.fastify.logger.info("Key-value store bağlantısı başarılı", {
driver: dbConfig.defaultKeyValue,
});
} else {
throw new Error("Key-value store test başarısız");
}
} catch (err) {
this.app.fastify.logger.error("Key-value store bağlantı hatası", err);
throw err;
}
}
}