adapter.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Path from 'node:path'
  2. import { db } from '../../app/src/infrastructure/mongodb.js'
  3. import { fileURLToPath } from 'node:url'
  4. const __filename = fileURLToPath(import.meta.url)
  5. const __dirname = Path.dirname(__filename)
  6. class Adapter {
  7. constructor(params) {
  8. if (
  9. !process.env.SKIP_TAG_CHECK &&
  10. !process.argv.includes('create') &&
  11. !(process.argv.includes('-t') || process.argv.includes('--tags'))
  12. ) {
  13. console.error("ERROR: must pass tags using '-t' or '--tags', exiting")
  14. process.exit(1)
  15. }
  16. this.params = params || {}
  17. }
  18. getTemplatePath() {
  19. return Path.resolve(__dirname, 'template.mjs')
  20. }
  21. async connect() {
  22. return { db }
  23. }
  24. disconnect() {
  25. return Promise.resolve()
  26. }
  27. async getExecutedMigrationNames() {
  28. const migrations = await db.migrations
  29. .find({}, { sort: { migratedAt: 1 }, projection: { name: 1 } })
  30. .toArray()
  31. return migrations.map(migration => migration.name)
  32. }
  33. async markExecuted(name) {
  34. return db.migrations.insertOne({
  35. name,
  36. migratedAt: new Date(),
  37. })
  38. }
  39. async unmarkExecuted(name) {
  40. return db.migrations.deleteOne({
  41. name,
  42. })
  43. }
  44. }
  45. export default Adapter