Monorepo containing core modules of Zeichen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

  1. import { Sequelize, Dialect, ModelAttributes, ModelOptions } from 'sequelize'
  2. export enum DatabaseKind {
  3. MSSQL = 'mssql',
  4. SQLITE = 'sqlite',
  5. MYSQL = 'mysql',
  6. MARIADB = 'mariadb',
  7. POSTGRESQL = 'postgres',
  8. }
  9. type RepositoryParams = {
  10. url: string,
  11. kind: DatabaseKind,
  12. }
  13. export default class ORM {
  14. private readonly instance: Sequelize
  15. constructor(params: RepositoryParams) {
  16. const { kind, url, } = params
  17. switch (kind as DatabaseKind) {
  18. case DatabaseKind.SQLITE:
  19. this.instance = new Sequelize({
  20. dialect: kind as string as Dialect,
  21. storage: url,
  22. })
  23. return
  24. case DatabaseKind.POSTGRESQL:
  25. case DatabaseKind.MARIADB:
  26. case DatabaseKind.MSSQL:
  27. case DatabaseKind.MYSQL:
  28. this.instance = new Sequelize({
  29. dialect: kind as string as Dialect,
  30. host: url,
  31. })
  32. return
  33. default:
  34. break
  35. }
  36. throw new Error(`Database kind "${kind as string}" not yet supported.`)
  37. }
  38. getRepository(model: { modelName: string, rawAttributes: ModelAttributes, options: ModelOptions }) {
  39. return this.instance.define(model.modelName, model.rawAttributes, model.options)
  40. }
  41. }