v-js
A schema:
$ psql -U cardinal_li -d db -h localhostpsql (9.4.9)Type "help" for help. SLAPTCHA=# \d List of relations Schema | Name | Type | Owner --------+--------------------------------------------+----------+---------- public | users | table | postgres SLAPTCHA=# \d users Table "public.users" Column | Type | Modifiers --------+---------+--------------------- email | text | not null title | text | not null virtue | integer | not null default 10Indexes: "users_email_key" UNIQUE CONSTRAINT, btree (email)Check constraints: "title_length" CHECK (length(title) < 20) "valid_email" CHECK (email ~~ '%_@_%'::text) "valid_virtue" CHECK (virtue >= 0)
A command that extracts the schema information and transpiles constraints and defaults to JavaScript:
npm -g i v-jsv-js -U postgres -d db -h localhost --out schema.v.js
Now, without touching the database:
var schema = /* -> {error: "constraint_violated", violated: [{ name: "valid_email", columns: ["email"]}, { name: "valid_virtue", columns: ["virtue"]}]} */
Building
API interface:
v =
The CLI is a yargs wrapper around the API, with additional --file and --out options:
v-js --client.username surveillant_mohammed --client.password mhmm --schema wards --tables.stimuli --tables.detergents name,quantity --file nothing_masters.sql --out schema.v.js
Shorthands are defined here:
v-js -U surveillant_mohammed -p mhmm --schema wards -t.stimuli -t.detergents name,quantity -f nothing_masters.sql --out schema.v.js
Validating
Using the first example schema:
var schema = // {success: true}/* -> {error: "constraint_violated", violated: [{ name: "not_null", columns: ["title"]}]} */// {success: true}// {success: true}// {error: "unknown_field", violated: "hope"}// {success: true}// {email: undefined, title: undefined, virtue: 10}/* -> {error: "constraint_violated", violated: [{ name: "not_null", columns: ["virtue"]}]} */// {success: true}// {success: true}
Limitations & Troubleshooting & Things to Do
Limitations of v-js:
- Limited support for major databases other than Postgres (Alasql and v-js aim to be SQL-compliant; v-js favors Postgres when other incompatibilities arise)
- Limited implementation of Postgres functions (causes
TypeError: alasql.fn.function_you_called is not a function
errors; checkfn.js
if the function you're calling already has an implementation. If not, add your own!) - No type checking
Limitations of Alasql, the project v-js builds on:
- Limited support for non-expression SQL syntax
- Limited support for casting values
And much more.