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
1020 B
47 lines
1020 B
import { readdir } from 'node:fs/promises'
|
|
import path from 'path'
|
|
|
|
/**
|
|
* runMigrationSqlite
|
|
*
|
|
* @param {string} path
|
|
* @param {object} db
|
|
*
|
|
*/
|
|
async function runMigrationSqlite(filePath: string, db: object): void {
|
|
|
|
const SQL_EXTENSION = '.sql'
|
|
|
|
// resolve path
|
|
filePath = path.resolve(__dirname, filePath)
|
|
|
|
// getting files
|
|
const files = await readdir(filePath)
|
|
|
|
for (const fileName of files) {
|
|
|
|
// skip if extension not sql
|
|
if (path.extname(fileName).toLowerCase() !== SQL_EXTENSION) {
|
|
continue
|
|
}
|
|
|
|
const file = Bun.file(filePath + '/' + fileName)
|
|
|
|
try {
|
|
const sql = await file.text()
|
|
|
|
if (!sql.trim()) {
|
|
throw new Error(fileName + ' is empty!')
|
|
}
|
|
|
|
db.query(sql.trim()).run()
|
|
|
|
console.log('migrated ' + path.basename(fileName, SQL_EXTENSION))
|
|
} catch(error) {
|
|
console.log(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default runMigrationSqlite
|