Skip to content

Commit

Permalink
Merge pull request #191 from rodrigodosanjosoliveira/feature/generate…
Browse files Browse the repository at this point in the history
…-id-field

feat: generate id field using uuid
  • Loading branch information
dalssoft authored Oct 30, 2024
2 parents 33b9f1e + dea93ab commit b460b3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/generators/src/infra/database/migrations/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ module.exports =
if (_type instanceof Boolean) return 'boolean'
if (_type instanceof Date) return 'timestamp'
}

if (Type === 'UUID') return 'uuid'
}

function getDBType(appDir) {
Expand All @@ -43,14 +45,23 @@ module.exports =
return config.database.herbsCLI
}

function createColumns(schema) {
function createColumns(schema, db) {
const columns = []
Object.keys(schema).forEach((prop) => {
const { name, type, options } = schema[prop]
columns.push(`table.${type2Str(type)}('${snakeCase(name)}')${options.isId ? '.primary()' : ''}`)
if (options.isId) {
if (type2Str(type) === 'uuid' && db.toLowerCase() === 'postgres') {
columns.push(`table.uuid('${snakeCase(name)}').primary().defaultTo(knex.raw('uuid_generate_v4()'))`)
} else {
columns.push(`table.${type2Str(type)}('${snakeCase(name)}').primary()`)
}
} else {
columns.push(`table.${type2Str(type)}('${snakeCase(name)}')`)
}
})
return columns
}


const db = getDBType(filesystem.cwd())
const migrationName = new Date()
Expand All @@ -59,7 +70,7 @@ module.exports =
.substring(0, 14)
const migrationFullPath = path.normalize(`${migrationsPath}/${migrationName}_${camelCase(name)}s.js`)

const columns = createColumns(schema)
const columns = createColumns(schema, db)

await generate({
template: `infra/data/database/${db.toLowerCase()}/migration.ejs`,
Expand Down
12 changes: 6 additions & 6 deletions src/templates/domain/useCases/create.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { usecase, step, Ok, Err, request } = require('@herbsjs/herbs')
const { herbarium } = require('@herbsjs/herbarium')
const { randomUUID } = require('crypto')
const <%- props.name.pascalCase %> = require('../../entities/<%- props.name.camelCase %>')
const <%- props.name.pascalCase %>Repository = require('../../../infra/data/repositories/<%- props.name.camelCase %>Repository')

Expand All @@ -22,17 +23,16 @@ const create<%- props.name.pascalCase %> = injection =>
//Step description and function
'Check if the <%- props.name.raw %> is valid': step(ctx => {
ctx.<%- props.name.camelCase %> = <%- props.name.pascalCase %>.fromJSON(ctx.req)
<% if(!props.mongo){ %>ctx.<%- props.name.camelCase %>.id = Math.floor(Math.random() * 100000).toString()<% } %>

if (!ctx.<%- props.name.camelCase %>.isValid())
ctx.<%- props.name.camelCase %>.id = randomUUID()
if (!ctx.<%- props.name.camelCase %>.isValid())
return Err.invalidEntity({
message: 'The <%- props.name.raw %> entity is invalid',
message: 'The <%- props.name.raw %> entity is invalid',
payload: { entity: '<%- props.name.raw %>' },
cause: ctx.<%- props.name.camelCase %>.errors
cause: ctx.<%- props.name.camelCase %>.errors
})

// returning Ok continues to the next step. Err stops the use case execution.
return Ok()
return Ok()
}),

'Save the <%- props.name.raw %>': step(async ctx => {
Expand Down

0 comments on commit b460b3f

Please sign in to comment.