Skip to content

Commit

Permalink
feat: DB_URI (#12)
Browse files Browse the repository at this point in the history
* feat: DB_URI

Allow connecting to the database using the environment variable
`DB_URI`.

* fix: Documentation for environment variables
  • Loading branch information
pettermachado authored Oct 8, 2024
1 parent 71890bb commit dd81e6b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 30 additions & 12 deletions lib/db/index.ts
Original file line number Diff line number Diff line change
@@ -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"];
Expand Down Expand Up @@ -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;

0 comments on commit dd81e6b

Please sign in to comment.