From dd81e6ba42c093d5b430d170dd24b9ed08f796a8 Mon Sep 17 00:00:00 2001 From: Petter Machado Date: Tue, 8 Oct 2024 15:19:29 +0200 Subject: [PATCH] feat: DB_URI (#12) * feat: DB_URI Allow connecting to the database using the environment variable `DB_URI`. * fix: Documentation for environment variables --- README.md | 14 ++++++++++++++ lib/db/index.ts | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 36a674b..ffff223 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,20 @@ cp .env.sample .env # Add your token to the .env file ``` +## Environment variables + +| Environment variable | Default | Description | +| ---------------------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `DB_CACHE` | - | When set to anything truthy cached values will be stored in the database. | +| `DB_URI` | - | Will be passed to `new Sequelize(dbUri)` when set. | +| `DB_STORAGE` | `db.sqlite` | Used in combination with the default databse setup. The name of the file that `sqlite` uses as database. | +| `SYNC_DB` | - | When set to anything truthy will sync all database tables. **Note: All your data will be deleted**. | +| `LOG_LEVEL` | `info` when `NODE_ENV=production` else `debug` | The log level passed to `pino({ level: logLevel })`. | +| `SOUNDTRACK_API_URL` | - | The url of the Soundtrack API. | +| `SOUNDTRACK_API_TOKEN` | - | The Soundtrack API token, used in all requests towards the Soundtrack API. | +| `REQUEST_LOG` | - | when set the anything truthy will enable http request logs. | +| `WORKER_INTERVAL` | `60` | The worker check interval in seconds. | + ## Deployment ### Security diff --git a/lib/db/index.ts b/lib/db/index.ts index ef59093..896dc17 100644 --- a/lib/db/index.ts +++ b/lib/db/index.ts @@ -1,12 +1,23 @@ import { addDays, addHours, addMinutes } from "date-fns"; -import { Sequelize, DataTypes, SyncOptions } from "sequelize"; +import { Sequelize, DataTypes, SyncOptions, Options } from "sequelize"; +import { getLogger } from "../logger"; -// TODO: Initialize your database -const _sequelize = new Sequelize({ - dialect: "sqlite", - storage: process.env.DB_STORAGE ?? "db.sqlite", - logging: false, -}); +const logger = getLogger("db/index"); + +const uri = process.env.DB_URI; +const commonOptions: Options = { logging: false }; + +const _sequelize = uri + ? new Sequelize(uri, commonOptions) + : new Sequelize({ + ...commonOptions, + dialect: "sqlite", + storage: process.env.DB_STORAGE ?? "db.sqlite", + }); + +logger.info( + `Created Sequelize with dialect: ${_sequelize.getDialect()}, database: ${_sequelize.getDatabaseName()}`, +); export type RepeatPart = "day" | "hour" | "minute"; export const repeatParts: RepeatPart[] = ["day", "hour", "minute"]; @@ -127,12 +138,19 @@ Action.belongsTo(Run); Event.hasMany(Action, { as: "actions" }); Action.belongsTo(Event); +/** + * Sync the databse schema. + * + * Note: All data in the database will be dropped. + * + * @param options Options passed to the models sync call. + */ export async function sync(options: SyncOptions) { - await Event.sync(options); - await ZoneEvent.sync(options); - await Run.sync(options); - await Action.sync(options); - await CacheEntry.sync(options); + const types = [Event, ZoneEvent, Run, Action, CacheEntry]; + types.forEach(async (type) => { + logger.info(`Syncing table name ${type.getTableName()} ...`); + await type.sync(options); + }); } export const sequelize = _sequelize;