简体   繁体   中英

Knex.js with Joi binding

I'm using Knex.js (along with Bookshelf.js) with a PostreSQL DB and I'm trying to "extract" the SQL schema in order to use it with something like Joi or Checkit .

Here just a simple example:
The schema written with knex:

return knex.schema.createTable('users', table => {
  table.string('username').notNullable()
  table.string('password')
})

And what I would like to automatically get:

const schema = Joi.object().keys({
  username: Joi.string().required(),
  password: Joi.string()
})

The point is simply to avoid writting the schema twice and having a programmatically way to validated the objects, before passing it to bookshelf/knex. Are there any built-ins ways to do it? Or any best practice?

You need to do this manually or write your own schema builder which outputs Joi schema and knex migrations...

Usually people just make migrations separately in knex and then updates Joi validations to be in sync with changed DB schema.

If people are looking for a similar service, than the one asked. I just released a library that converts a Joi schema into a SQL table with Knex .

https://github.com/Fantasim/joi-to-sql

For the moment, the library only supports MySQL and essential features of it, but I plan to add more like auto-migration (on MySQL). If you need other features or other DB support, pull requests are welcomed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM