Create Joi validation code for MySQL databases
JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
test Added timestamp field for date mapping Sep 23, 2017
.gitignore Initial commit Apr 24, 2015
.travis.yml Thing about modernizing is you have to let Travis know Sep 18, 2017
bin.js Split CLI into its own file, modernize code Sep 18, 2017
build.js Added timestamp field for date mapping Sep 23, 2017
fetch.js Split CLI into its own file, modernize code Sep 18, 2017
index.js Added connection recycling Sep 23, 2017
package-lock.json 1.3.0 Oct 2, 2017
package.json
readme.md Slim down readme explanation of the connection property Oct 2, 2017

readme.md

joi-sql

Point it at your local database, and it spits out a Joi object validator.

Build Status

Install

npm install joi-sql

(npm install -g joi-sql if you want to use it from the command-line)

Usage

CLI

joi-sql --host=localhost --user=root --password=abc123 --schema=awesomedb --table=customer --camel

host, user, password, and camel are all optional. schema and table are not.

If camel is set, then column names are converted to cammel case identifiers.

Spits out something like:

Joi.object({
	projectId: Joi.required().invalid(null).number().integer().max(4294967295).min(0),
	contactId: Joi.required().invalid(null).number().integer().max(4294967295).min(0),
	dateCreated: Joi.required().invalid(null).date(),
	engineerId: Joi.number().integer().max(4294967295).min(0),
	name: Joi.required().invalid(null).string().max(200),
	engineeringProject: Joi.required().invalid(null).boolean(),
	printingProject: Joi.required().invalid(null).boolean(),
	activeProjectStateId: Joi.required().invalid(null).number().integer().max(4294967295).min(0),
	startDate: Joi.date(),
	done: Joi.required().invalid(null).boolean(),
	doneDate: Joi.date(),
	deadReason: Joi.string().max(65535),
	quotedEngineeringHours: Joi.number().precision(1).less(10000),
	actualEngineeringHours: Joi.number().precision(1).less(10000),
	engineeringDueDate: Joi.date(),
	printParts: Joi.string().max(65535),
	printQuantity: Joi.number().integer().max(8388607).min(-8388608),
	printTimeHours: Joi.number().precision(1).less(10000),
	printDueDate: Joi.date(),
	paymentReceived: Joi.required().invalid(null).boolean(),
	contactDate: Joi.date(),
	replyDate: Joi.date(),
	quoteDate: Joi.date(),
	followUpDate: Joi.date(),
	notes: Joi.string().max(65535),
	version: Joi.required().invalid(null).number().integer().max(4294967295).min(0)
})

Programmatic

Returns a promise that resolves to a string containing the code snippet above.

const joiSql = require('joi-sql')

joiSql({
    host: 'localhost',
    user: 'root',
    password: 'abc123',
    schema: 'awesomedb',
    table: 'customer',
    camel: true
}).then(result => {
	typeof result // => 'string'
})

You may also pass in an optional connection property which must be a mysql connection instance.

Pull requests welcome.

License

WTFPL