Skip to content

Instantly share code, notes, and snippets.

@hos
Last active January 31, 2020 18:43
Show Gist options
  • Save hos/29e997e7afbf9a688ff3b1088fbc6e15 to your computer and use it in GitHub Desktop.
Save hos/29e997e7afbf9a688ff3b1088fbc6e15 to your computer and use it in GitHub Desktop.
Script to create schema.sql from sequelize sync logs. This will only help with getting started there must be some other changes to do manually for using with graphile-migrate.
import fs from 'fs'
export function getLogger (path) {
const stream = fs.createWriteStream(path)
process.on('beforeExit', () => stream.close())
return msg => {
const _ = msg + ''
if (!/ select /i.test(_)) {
stream.write(
_.replace('Executing (default): ', '\n').replace(/;/g, ';\n')
)
// the rest formating can be done with pg formatter or
// with some online free tools. I used https://sqlformat.org
}
}
}
/*
* After sequelize is synced the rest can be edited manually in schema.sql
* export const sequelize = new Sequelize(database.connectionString, {
* // ...
* logging: getLogger('./schema.sql')
* })
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment