diff --git a/app/Middleware/ConvertEmptyStringsToNull.js b/app/Middleware/ConvertEmptyStringsToNull.js new file mode 100644 index 0000000..a5750cc --- /dev/null +++ b/app/Middleware/ConvertEmptyStringsToNull.js @@ -0,0 +1,17 @@ +'use strict' + +class ConvertEmptyStringsToNull { + async handle ({ request }, next) { + if (Object.keys(request.body).length) { + request.body = Object.assign( + ...Object.keys(request.body).map(key => ({ + [key]: request.body[key] !== '' ? request.body[key] : null + })) + ) + } + + await next() + } +} + +module.exports = ConvertEmptyStringsToNull diff --git a/app/Models/Traits/NoTimestamp.js b/app/Models/Traits/NoTimestamp.js new file mode 100644 index 0000000..45f210e --- /dev/null +++ b/app/Models/Traits/NoTimestamp.js @@ -0,0 +1,15 @@ +'use strict' + +class NoTimestamp { + register (Model) { + Object.defineProperties(Model, 'createdAtColumn', { + get () { return null } + }) + + Object.defineProperties(Model, 'updatedAtColumn', { + get () { return null } + }) + } +} + +module.exports = NoTimestamp diff --git a/start/kernel.js b/start/kernel.js index cba76b4..b4f2720 100644 --- a/start/kernel.js +++ b/start/kernel.js @@ -16,7 +16,8 @@ const globalMiddleware = [ 'Adonis/Middleware/BodyParser', 'Adonis/Middleware/Session', 'Adonis/Middleware/Shield', - 'Adonis/Middleware/AuthInit' + 'Adonis/Middleware/AuthInit', + 'App/Middleware/ConvertEmptyStringsToNull', ] /*