{"version":3,"file":"resolver.mjs","sources":["../../src/migrations/resolver.ts"],"sourcesContent":["import path from 'path';\n\nimport fse from 'fs-extra';\n\nimport { wrapTransaction } from './common';\n\nimport type { Context, MigrationResolver } from './common';\n\n// TODO: check multiple commands in one sql statement\nexport const migrationResolver: MigrationResolver = ({ name, path: migrationPath, context }) => {\n  const { db } = context;\n\n  if (!migrationPath) {\n    throw new Error(`Migration ${name} has no path`);\n  }\n\n  // if sql file run with knex raw\n  if (migrationPath.match(/\\.sql$/)) {\n    const sql = fse.readFileSync(migrationPath, 'utf8');\n\n    return {\n      name,\n      path: migrationPath,\n      up: wrapTransaction(db)((knex) => knex.raw(sql)),\n      async down() {\n        throw new Error('Down migration is not supported for sql files');\n      },\n    };\n  }\n\n  // NOTE: we can add some ts register if we want to handle ts migration files at some point\n  // eslint-disable-next-line @typescript-eslint/no-var-requires\n  const required = require(migrationPath);\n  // Compiled TypeScript migrations (`export default { up, down }`) expose the\n  // migration object on `default`; plain CommonJS files export it directly.\n  const migration = required?.default?.up ? required.default : required;\n  return {\n    name,\n    path: migrationPath,\n    up: wrapTransaction(db)(migration.up),\n    down: wrapTransaction(db)(migration.down),\n  };\n};\n\nexport const resolveMigrationFiles = (filepaths: string[], context: Context) => {\n  return filepaths.map((filepath) => {\n    const name = path.basename(filepath);\n    return migrationResolver({ name, path: filepath, context });\n  });\n};\n"],"names":["migrationResolver","name","path","migrationPath","context","db","Error","match","sql","fse","readFileSync","up","wrapTransaction","knex","raw","down","required","require","migration","default","resolveMigrationFiles","filepaths","map","filepath","basename"],"mappings":";;;;AAQA;AACO,MAAMA,iBAAAA,GAAuC,CAAC,EAAEC,IAAI,EAAEC,IAAAA,EAAMC,aAAa,EAAEC,OAAO,EAAE,GAAA;IACzF,MAAM,EAAEC,EAAE,EAAE,GAAGD,OAAAA;AAEf,IAAA,IAAI,CAACD,aAAAA,EAAe;AAClB,QAAA,MAAM,IAAIG,KAAAA,CAAM,CAAC,UAAU,EAAEL,IAAAA,CAA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