-
Notifications
You must be signed in to change notification settings - Fork 29
feat: including relationships in migrations #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
f5be364
c6cc551
4f10d6b
3fcb965
1d7d1b3
335b92d
962252f
a55258c
d8a12c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ const { snakeCase, camelCase } = require('lodash') | |
| const fs = require("fs") | ||
| const glob = require('glob') | ||
| const path = require('path') | ||
| const { requireHerbarium } = require('../../../../utils') | ||
| const { requireHerbarium, requireHerbs } = require('../../../../utils') | ||
|
|
||
| module.exports = | ||
| async ({ template: { generate }, filesystem }, command) => | ||
|
|
@@ -12,6 +12,7 @@ module.exports = | |
| process.stdout.write(`Generating Migration\n`) | ||
|
|
||
| const herbarium = requireHerbarium(command, filesystem.cwd()) | ||
| const herbs = requireHerbs(filesystem.cwd()) | ||
| const entities = herbarium.entities.all | ||
|
|
||
| const cwd = filesystem.cwd().replace(new RegExp('\\\\', 'g'), '/') | ||
|
|
@@ -26,15 +27,11 @@ module.exports = | |
| } | ||
|
|
||
| function type2Str(Type) { | ||
| const nativeTypes = [Boolean, Number, String, Array, Object, Date, Function] | ||
|
|
||
| if (nativeTypes.includes(Type)) { | ||
| const _type = new Type() | ||
| if (_type instanceof String) return 'string' | ||
| if (_type instanceof Number) return 'integer' | ||
| if (_type instanceof Boolean) return 'boolean' | ||
| if (_type instanceof Date) return 'timestamp' | ||
| } | ||
| const _type = new Type() | ||
| if (_type instanceof String) return 'string' | ||
| if (_type instanceof Number) return 'integer' | ||
| if (_type instanceof Boolean) return 'boolean' | ||
| if (_type instanceof Date) return 'timestamp' | ||
| } | ||
|
|
||
| function getDBType(appDir) { | ||
|
|
@@ -47,11 +44,35 @@ module.exports = | |
| const columns = [] | ||
| Object.keys(schema).forEach((prop) => { | ||
| const { name, type, options } = schema[prop] | ||
| columns.push(`table.${type2Str(type)}('${snakeCase(name)}')${options.isId ? '.primary()' : ''}`) | ||
|
|
||
| const nativeTypes = [Boolean, Number, String, Date] | ||
| if (!nativeTypes.includes(type)) return | ||
|
|
||
| const typeString = type2Str(type) | ||
| columns.push(`table.${typeString}('${snakeCase(name)}')${options.isId ? '.primary()' : ''}`) | ||
| }) | ||
| return columns | ||
| } | ||
|
|
||
| function isArrayWithType(value) { | ||
| return Array.isArray(value) && value.length === 1 | ||
| } | ||
|
|
||
| function identifyRef(schema) { | ||
| const refs = [] | ||
| Object.values(schema).forEach(({ type, name }) => { | ||
| if (herbs.entity.isEntity(type)) { | ||
| const typeSchema = type.prototype.meta.schema | ||
| const idRef = Object.values(typeSchema).find(column => column.options.isId)?.name | ||
| refs.push({ id: idRef, columnName: `${camelCase(name)}Id`, table: `${camelCase(type.name)}s`, relationship: 'One-to-One' }) | ||
| } | ||
|
|
||
| if (isArrayWithType(type) && herbs.entity.isEntity(type[0])) | ||
| refs.push({ table: `${camelCase(type[0].name)}s`, relationship: 'One-to-Many' }) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both table and columns name should be snake_cases for DB to keep with the current standard. ex: |
||
| }) | ||
| return refs | ||
| } | ||
|
|
||
| const db = getDBType(filesystem.cwd()) | ||
| const migrationName = new Date() | ||
| .toISOString() | ||
|
|
@@ -60,11 +81,14 @@ module.exports = | |
| const migrationFullPath = path.normalize(`${migrationsPath}/${migrationName}_${camelCase(name)}s.js`) | ||
|
|
||
| const columns = createColumns(schema) | ||
| const ref = identifyRef(schema) | ||
| const idColumn = Object.values(schema).find(column => column.options.isId)?.name | ||
|
|
||
|
|
||
| await generate({ | ||
| template: `infra/data/database/${db.toLowerCase()}/migration.ejs`, | ||
| target: migrationFullPath, | ||
| props: { table: `${camelCase(name)}s`, columns: columns } | ||
| props: { table: `${camelCase(name)}s`, externalColumnName: `${camelCase(name)}Id`, columns, ref, idColumn } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both table and columns name should be snake_cases for DB to keep with the current standard. ex: |
||
| }) | ||
| process.stdout.write(` New: ${migrationFullPath}\n`) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| const { herbarium } = require('@herbsjs/herbarium') | ||
| module.exports = herbarium | ||
| const herbs = require('@herbsjs/herbs') | ||
| module.exports = { herbarium, herbs } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,44 @@ | ||
|
|
||
| exports.up = async function (knex) { | ||
| knex.schema.hasTable('<%- props.table %>') | ||
| .then(function (exists) { | ||
| if (exists) return | ||
| return knex.schema | ||
| .createTable('<%- props.table %>', function (table) {<% for(colum of props.columns) { %> | ||
| <%- colum %><% } %> | ||
| table.timestamps() | ||
| }) | ||
| }) | ||
| return Promise.all([ | ||
| knex.schema.hasTable('<%- props.table %>') | ||
| .then(function (exists) { | ||
| if (exists) return | ||
| return knex.schema | ||
| .createTable('<%- props.table %>', function (table) { | ||
| <% for(colum of props.columns) {%> | ||
| <%- colum %> <%}%> | ||
| <%if (props.ref) { | ||
| props.ref.forEach(function(link){ | ||
| if (link.relationship === 'One-to-One') { %> | ||
| // table.integer('<%-link.columnName %>').unsigned().nullable() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment is necessary?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the idea are to not create a migration that does not work, due to not has an order of migrations when we write the files. These relationships throw errors due the dependence dont exist yet ... |
||
| // table.foreign('<%-link.columnName %>').references('<%- link.id %>').inTable('<%- link.table %>') | ||
| <% } | ||
| }) | ||
| } %> | ||
| table.timestamps() | ||
| }) | ||
| }), | ||
| <%if (props.ref) { | ||
| props.ref.forEach(function(link){ | ||
| if (link.relationship === 'One-to-Many') { %> | ||
| // knex.schema.table('<%- link.table %>', function (table) { | ||
| // table.integer('<%-props.externalColumnName %>').unsigned().index().references('<%- props.idColumn %>').inTable('<%- props.table %>') | ||
| // }) | ||
| <% } | ||
| }) | ||
| } %> | ||
| ]) | ||
| } | ||
|
|
||
| exports.down = function (knex) { | ||
| return knex.schema | ||
| .dropTableIfExists('<%- props.table %>') | ||
| return Promise.all([ | ||
| knex.schema | ||
| .dropTableIfExists('<%- props.table %>'), | ||
| <%if (props.ref) { | ||
| props.ref.forEach(function(link){ | ||
| if (link.relationship === 'One-to-Many') { %> | ||
| // knex.schema.table('<%- link.table %>', function (table) { | ||
| // table.dropColumn('<%-props.externalColumnName %>')}) | ||
| <% }})} %> | ||
| ]) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both table and columns name should be snake_cases for DB to keep with the current standard.
ex:
userId->user_id