Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DB_URI #12

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;