diff --git a/src/generators/src/infra/database/migrations/migrations.js b/src/generators/src/infra/database/migrations/migrations.js index 39eddd1..936fb94 100644 --- a/src/generators/src/infra/database/migrations/migrations.js +++ b/src/generators/src/infra/database/migrations/migrations.js @@ -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) { @@ -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() @@ -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`, diff --git a/src/templates/domain/useCases/create.ejs b/src/templates/domain/useCases/create.ejs index 5f0f7ca..13d658b 100644 --- a/src/templates/domain/useCases/create.ejs +++ b/src/templates/domain/useCases/create.ejs @@ -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') @@ -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 => {