-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ariga/sequelize_schema
js: add way to load sequelize models programmatically as js script
- Loading branch information
Showing
8 changed files
with
118 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const loadSequelizeModels = require("./src/sequelize_schema"); | ||
|
||
module.exports = loadSequelizeModels; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const Sequelize = require("sequelize"); | ||
const DataTypes = require("sequelize/lib/data-types"); | ||
|
||
const validDialects = ["mysql", "postgres", "sqlite", "mariadb", "mssql"]; | ||
|
||
// gets dialect and models that are functions of the form (sequelize, DataTypes) => Model | ||
// returns DDL string describing the models. | ||
const loadSequelizeModels = (dialect, ...models) => { | ||
if (!validDialects.includes(dialect)) { | ||
throw new Error("invalid dialect: " + dialect); | ||
} | ||
let sequelize = new Sequelize({ | ||
dialect: dialect, | ||
}); | ||
const db = {}; | ||
for (const model of models) { | ||
const m = model(sequelize, DataTypes); | ||
if (m?.name) { | ||
db[m.name] = m; | ||
} | ||
} | ||
// create associations between models | ||
for (const modelName of Object.keys(db)) { | ||
if (db[modelName]?.associate) { | ||
db[modelName].associate(db); | ||
} | ||
} | ||
const modelsOrdered = sequelize.modelManager | ||
.getModelsTopoSortedByForeignKey() | ||
.reverse(); | ||
let sql = ""; | ||
for (const model of modelsOrdered) { | ||
const def = sequelize.modelManager.getModel(model.name); | ||
const attr = sequelize | ||
.getQueryInterface() | ||
.queryGenerator.attributesToSQL(def.getAttributes(), { ...def.options }); | ||
sql += | ||
sequelize | ||
.getQueryInterface() | ||
.queryGenerator.createTableQuery(def.tableName, attr, { | ||
...def.options, | ||
}) + "\n"; | ||
} | ||
return sql; | ||
}; | ||
|
||
module.exports = loadSequelizeModels; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
variable "dialect" { | ||
type = string | ||
} | ||
|
||
locals { | ||
dev_url = { | ||
mysql = "docker://mysql/8/dev" | ||
postgres = "docker://postgres/15" | ||
sqlite = "sqlite://file::memory:?cache=shared" | ||
}[var.dialect] | ||
} | ||
|
||
data "external_schema" "sequelize" { | ||
program = [ | ||
"node", | ||
"load-models.js", | ||
var.dialect, | ||
] | ||
} | ||
|
||
env "sequelize" { | ||
src = data.external_schema.sequelize.url | ||
dev = local.dev_url | ||
migration { | ||
dir = "file://migrations/${var.dialect}" | ||
} | ||
format { | ||
migrate { | ||
diff = "{{ sql . \" \" }}" | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env node | ||
|
||
const ingredient = require("./models/ingredient"); | ||
const recipe = require("./models/recipe"); | ||
const recipeIngredient = require("./models/recipe-ingredient"); | ||
const loadModels = require("../index"); | ||
|
||
// parse the second argument as the dialect | ||
const dialect = process.argv[2]; | ||
|
||
// load the models | ||
console.log(loadModels(dialect, ingredient, recipe, recipeIngredient)); |
File renamed without changes.