|
- import { Sequelize, Dialect, } from 'sequelize'
- import * as ColumnTypes from './ColumnTypes'
-
- export enum DatabaseKind {
- MSSQL = 'mssql',
- SQLITE = 'sqlite',
- MYSQL = 'mysql',
- MARIADB = 'mariadb',
- POSTGRESQL = 'postgres',
- }
-
- type RepositoryParams = {
- url: string,
- kind: DatabaseKind,
- }
-
- export default class ORM {
- private readonly instance: Sequelize
-
- constructor(params: RepositoryParams) {
- const { kind, url, } = params
- switch (kind as DatabaseKind) {
- case DatabaseKind.SQLITE:
- this.instance = new Sequelize({
- dialect: kind as string as Dialect,
- storage: url,
- })
- return
- case DatabaseKind.POSTGRESQL:
- case DatabaseKind.MARIADB:
- case DatabaseKind.MSSQL:
- case DatabaseKind.MYSQL:
- this.instance = new Sequelize({
- dialect: kind as string as Dialect,
- host: url,
- })
- return
- // TODO add nosql dbs
- default:
- break
- }
- throw new Error(`Database kind "${kind as string}" not yet supported.`)
- }
-
- getRepository(model: ColumnTypes.Model) {
- if (this.instance instanceof Sequelize) {
- return this.instance.define(model.modelName, model.attributes, model.options)
- }
- throw new Error('Interface not yet initialized.')
- }
- }
|