Comparing version 0.3.3 to 0.3.4
@@ -8,3 +8,7 @@ #!/usr/bin/env node | ||
argv = require('optimist').usage('Usage: plv8x {OPTIONS}').wrap(80).option('db', { | ||
alias: 'd', | ||
desc: 'database connection string' | ||
}).option('json', { | ||
alias: 'j', | ||
desc: 'Use json for output' | ||
}).option('list', { | ||
@@ -21,2 +25,11 @@ alias: 'l', | ||
desc: 'Inject plv8 function from bundles or string' | ||
}).option('query', { | ||
alias: 'c', | ||
desc: 'Execute query' | ||
}).option('eval', { | ||
alias: 'e', | ||
desc: 'Eval the given expression in plv8x context' | ||
}).option('require', { | ||
alias: 'r', | ||
desc: 'Require the given file and eval in plv8x context' | ||
}).option('help', { | ||
@@ -34,4 +47,10 @@ alias: 'h', | ||
plv8x['new'](argv.db, function(plx){ | ||
var done, ref$, spec, source; | ||
done = function(){ | ||
var done, ref$, spec, source, code; | ||
done = function(output){ | ||
if (output) { | ||
if (argv.json) { | ||
output = JSON.stringify(output); | ||
} | ||
console.log(output); | ||
} | ||
return plx.end(); | ||
@@ -61,9 +80,26 @@ }; | ||
return plx.list(function(res){ | ||
var i$, len$, ref$, name, length; | ||
for (i$ = 0, len$ = res.length; i$ < len$; ++i$) { | ||
ref$ = res[i$], name = ref$.name, length = ref$.length; | ||
console.log(name + ": " + length + " bytes"); | ||
var name, length; | ||
if (!argv.json) { | ||
res = (function(){ | ||
var i$, ref$, len$, ref1$, results$ = []; | ||
for (i$ = 0, len$ = (ref$ = res).length; i$ < len$; ++i$) { | ||
ref1$ = ref$[i$], name = ref1$.name, length = ref1$.length; | ||
results$.push(name + ": " + length + " bytes"); | ||
} | ||
return results$; | ||
}()).join('\n'); | ||
} | ||
return done(); | ||
return done(res); | ||
}); | ||
case !argv.query: | ||
return plx.query(argv.query, done); | ||
case !argv.eval: | ||
code = plv8x.xpressionToBody(argv.eval); | ||
return plx.eval("(" + code + ")()", done); | ||
case !argv.require: | ||
code = fs.readFileSync(argv.require, 'utf8'); | ||
if (/\.ls$/.exec(argv.require)) { | ||
code = plv8x.compileLivescript(code); | ||
} | ||
return plx.eval(code, done); | ||
default: | ||
@@ -70,0 +106,0 @@ console.log("Unknown command: " + argv._[0]); |
@@ -1,4 +0,4 @@ | ||
var ref$, _mk_func, compileCoffeescript, compileLivescript, xpressionToBody, plv8xSql, _eval, _apply, _require, _mk_json_eval, _mk_json_eval_ls, _boot; | ||
var ref$, _mk_func, compileCoffeescript, compileLivescript, xpressionToBody, plv8xSql, operatorsSql, _eval, _apply, _require, _mk_json_eval, _mk_json_eval_ls, _boot; | ||
ref$ = require('..'), _mk_func = ref$._mk_func, compileCoffeescript = ref$.compileCoffeescript, compileLivescript = ref$.compileLivescript, xpressionToBody = ref$.xpressionToBody; | ||
plv8xSql = require('./sql').plv8xSql; | ||
ref$ = require('./sql'), plv8xSql = ref$.plv8xSql, operatorsSql = ref$.operatorsSql; | ||
module.exports = function(drop, cascade, done){ | ||
@@ -86,3 +86,3 @@ var this$ = this; | ||
})); | ||
y$.query('DROP OPERATOR IF EXISTS |> (NONE, text); CREATE OPERATOR |> (\n RIGHTARG = text,\n PROCEDURE = plv8x.json_eval\n);\nDROP OPERATOR IF EXISTS |> (plv8x.json, text); CREATE OPERATOR |> (\n LEFTARG = plv8x.json,\n RIGHTARG = text,\n COMMUTATOR = <|,\n PROCEDURE = plv8x.json_eval\n);\nDROP OPERATOR IF EXISTS <| (text, plv8x.json); CREATE OPERATOR <| (\n LEFTARG = text,\n RIGHTARG = plv8x.json,\n COMMUTATOR = |>,\n PROCEDURE = plv8x.json_eval\n);\n\nDROP OPERATOR IF EXISTS ~> (NONE, text); CREATE OPERATOR ~> (\n RIGHTARG = text,\n PROCEDURE = plv8x.json_eval_ls\n);\nDROP OPERATOR IF EXISTS ~> (plv8x.json, text); CREATE OPERATOR ~> (\n LEFTARG = plv8x.json,\n RIGHTARG = text,\n COMMUTATOR = <~,\n PROCEDURE = plv8x.json_eval_ls\n);\nDROP OPERATOR IF EXISTS <~ (text, plv8x.json); CREATE OPERATOR <~ (\n LEFTARG = text,\n RIGHTARG = plv8x.json,\n COMMUTATOR = ~>,\n PROCEDURE = plv8x.json_eval_ls\n);'); | ||
y$.query(operatorsSql()); | ||
r = y$.query("select plv8x.boot()"); | ||
@@ -89,0 +89,0 @@ r.on('end', done); |
@@ -124,3 +124,3 @@ var PLX, slice$ = [].slice, this$ = this, out$ = typeof exports != 'undefined' && exports || this; | ||
body = pkg | ||
? (plv8xLift(pkg, expression), boot = true) | ||
? (boot = true, plv8xLift(pkg, expression)) | ||
: xpressionToBody(expression); | ||
@@ -154,2 +154,5 @@ return this.query(_mk_func(name, paramObj, rettype, body, { | ||
var conn, plx; | ||
if (db.indexOf('/') < 0) { | ||
db = "tcp://localhost/" + db; | ||
} | ||
conn = connect(db); | ||
@@ -156,0 +159,0 @@ plx = new PLX(conn); |
@@ -13,2 +13,6 @@ var out$ = typeof exports != 'undefined' && exports || this; | ||
return defineSchema('plv8x', 'Out-of-table for loading plv8 modules', drop, cascade) + "\nCREATE TABLE IF NOT EXISTS plv8x.code (\n name text,\n code text,\n load_seq int,\n updated timestamp\n);"; | ||
} | ||
out$.operatorsSql = operatorsSql; | ||
function operatorsSql(){ | ||
return 'DROP OPERATOR IF EXISTS |> (NONE, text); CREATE OPERATOR |> (\n RIGHTARG = text,\n PROCEDURE = plv8x.json_eval\n);\nDROP OPERATOR IF EXISTS |> (plv8x.json, text); CREATE OPERATOR |> (\n LEFTARG = plv8x.json,\n RIGHTARG = text,\n COMMUTATOR = <|,\n PROCEDURE = plv8x.json_eval\n);\nDROP OPERATOR IF EXISTS <| (text, plv8x.json); CREATE OPERATOR <| (\n LEFTARG = text,\n RIGHTARG = plv8x.json,\n COMMUTATOR = |>,\n PROCEDURE = plv8x.json_eval\n);\n\nDROP OPERATOR IF EXISTS ~> (NONE, text); CREATE OPERATOR ~> (\n RIGHTARG = text,\n PROCEDURE = plv8x.json_eval_ls\n);\nDROP OPERATOR IF EXISTS ~> (plv8x.json, text); CREATE OPERATOR ~> (\n LEFTARG = plv8x.json,\n RIGHTARG = text,\n COMMUTATOR = <~,\n PROCEDURE = plv8x.json_eval_ls\n);\nDROP OPERATOR IF EXISTS <~ (text, plv8x.json); CREATE OPERATOR <~ (\n LEFTARG = text,\n RIGHTARG = plv8x.json,\n COMMUTATOR = ~>,\n PROCEDURE = plv8x.json_eval_ls\n);'; | ||
} |
@@ -10,3 +10,3 @@ { | ||
"description": "Use JavaScript expressions and modules in PostgreSQL plv8", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"keywords": [ | ||
@@ -37,3 +37,3 @@ "postgres", | ||
"resolve": "0.3.x", | ||
"one": "2.0.x", | ||
"one": ">= 2.0.10", | ||
"tmp": "0.0.x", | ||
@@ -40,0 +40,0 @@ "LiveScript": "1.1.1", |
@@ -30,10 +30,58 @@ plv8x | ||
Enable plv8x for your database: | ||
plv8x --db tcp://localhost/test -l | ||
[INFO] console - plv8x: 491425 bytes | ||
% createdb test | ||
% plv8x -d test -l | ||
plv8x: 491425 bytes | ||
Now try: | ||
Now create some test data with json columns: (example table from [Postgres 9.3 feature highlight: JSON operators](http://michael.otacoo.com/postgresql-2/postgres-9-3-feature-highlight-json-operators/)) | ||
% psql test | ||
test=# CREATE TABLE aa (a int, b json); | ||
CREATE TABLE | ||
test=# INSERT INTO aa VALUES (1, '{"f1":1,"f2":true,"f3":"Hi I''m \"Daisy\""}'); | ||
INSERT 0 1 | ||
test=# INSERT INTO aa VALUES (2, '{"f1":{"f11":11,"f12":12},"f2":2}'); | ||
INSERT 0 1 | ||
test=# INSERT INTO aa VALUES (3, '{"f1":[1,"Robert \"M\"",true],"f2":[2,"Kevin \"K\"",false]}'); | ||
INSERT 0 1 | ||
Instead of `b->'f1'`, we use `b~>'this.f1'`, which means bind `b` as `this` and evaluate the right hand side (`this.f1`): | ||
test=# SELECT b~>'this.f1' AS f1, b~>'this.f3' AS f3 FROM aa WHERE a = 1; | ||
f1 | f3 | ||
----+-------------------- | ||
1 | "Hi I'm \"Daisy\"" | ||
If you like coffee, `@` works too: | ||
test=# SELECT b~>'@f1' AS f1, b~>'@f3' AS f3 FROM aa WHERE a = 1; | ||
f1 | f3 | ||
----+-------------------- | ||
1 | "Hi I'm \"Daisy\"" | ||
For multiple keys, you can of course do `b~>'@f1'~>'@f12'`, but single expression will do: | ||
test=# SELECT b~>'@f1'~>'@f12' as f12_long, b~>'@f1.f12' AS f12 FROM aa WHERE a = 2; | ||
f12_long | f12 | ||
----------+----- | ||
12 | 12 | ||
Ditto for arrays: | ||
postgres=# SELECT b~>'@f1[0]' as f1_0 FROM aa WHERE a = 3; | ||
f1_0 | ||
------ | ||
1 | ||
Unary `~>` for just evaluating the expression: | ||
test=# SELECT ~>'[1 to 10]' AS f1 | ||
f1 | ||
------------------------ | ||
[1,2,3,4,5,6,7,8,9,10] | ||
`~>` is actually a shorthand for `|> '~>...'`. Using raw `|>` for plain | ||
old javascript: | ||
test=# select '{"foo": [1,2,3]}'::json |> 'function() { return this.foo[1] }'; | ||
@@ -58,11 +106,2 @@ ?column? | ||
Actually it was LiveScript: (please send pullreqs for coffeescript support!) | ||
test=# select '{"foo": [1,2,3]}'::json |> '@foo.1 * 5'; | ||
?column? | ||
---------- | ||
10 | ||
```<|``` is ```|>``` reversed: | ||
@@ -82,4 +121,32 @@ | ||
# Importing nodejs modules and creating user functions | ||
Let's try reusing some existing npm modules: | ||
% plv8x -d test --import qs:./node_modules/qs/package.json | ||
% psql test | ||
# parse a query string | ||
test=# select ~>'require("qs").parse("foo=bar&baz=1")' as qs; | ||
qs | ||
------------------------- | ||
{"foo":"bar","baz":"1"} | ||
# actually use the parsed query string as json | ||
test=# select qs~>'@foo' as foo from (select ~>'require("qs").parse("foo=bar&baz=1")' as qs) a; | ||
foo | ||
------- | ||
"bar" | ||
# create a user function from qs so we don't have to require it: | ||
% plv8x -d test --inject 'plv8x.json parse_qs(text)=qs:parse' | ||
ok plv8x.json parse_qs(text) | ||
# Now parse_qs is a postgresql function: | ||
test=# select parse_qs('foo=bar&baz=1') as qs; | ||
qs | ||
------------------------- | ||
{"foo":"bar","baz":"1"} | ||
# License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
68706
25
1108
150
+ Addedone@6.0.1(transitive)
- Removedboxcars@0.0.6(transitive)
- Removedcombiner@1.2.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedfunctools@1.1.01.4.0(transitive)
- Removedhogan.js@2.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.1(transitive)
- Removedlog4js@0.6.38(transitive)
- Removedone@2.0.11(transitive)
- Removedopts@1.2.1(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedsemver@4.3.6(transitive)
- Removedstring_decoder@0.10.31(transitive)
Updatedone@>= 2.0.10