Zeichen's app for both server and client.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ORM.ts 1.3 KiB

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