snd / mesa Goto Github PK
View Code? Open in Web Editor NEWsimple elegant sql for nodejs
License: MIT License
simple elegant sql for nodejs
License: MIT License
given link
is the object describing the include,
leftTable
is original table,
rightTable
is the table that is included,
leftRecord
is any record selected from the leftTable
during the include query,
rightRecord
is any record selected from the rightTable
during the include query.
leftRecord[link.left]?
rightRecord[link.right]?
rightRecord
there exists a leftRecord
such that leftRecord[link.left] == rightRecord[link.right]
causes confusion
would be nice to have an equivalent to mesa.exists
which would just negate the result of exists
.
The latest versions of node-pg require a call to a done method in order to return the connection back in the pool. The previous behaviour was to return the connection automatically into the pool.
Brian explained the changes for the 1.x version in a pull request
Do you have any plans to add this feature?
Would be great if there was support for schemas
CREATE SCHEMA someschema;
CREATE TABLE someschema.users (
id uuid primary key default uuid_generate_v4(),
name text,
email VARCHAR (355) UNIQUE NOT NULL
);
var userTable = mesaWithConnection.table('someschema.users');
userTable.
.attributes(['email'])
.insert({
email: '[email protected]'
}, function(err, id) {
});
Returns:
INSERT INTO "someschema.users" ("email") VALUES ($1) RETURNING id
Should return:
INSERT INTO someschema.users ("email") VALUES ($1) RETURNING id
[or]
INSERT INTO "someschema"."users" ("email") VALUES ($1) RETURNING id
Should this be fixed in https://github.com/snd/mesa/blob/master/src/mesa.coffee#L30-L31 or within the mohair._escape
or is it possible to be fixed somewhere else?
sometimes one needs to insert a value from the app passed through a postgres function
for example
INSERT INTO foo (bar) VALUES (pi()); -- without params
INSERT INTO foo (bar) VALUES (abs($1)); -- with one param
INSERT INTO foo (bar) VALUES (log($1,$2)); -- with n params
INSERT INTO foo (bar) VALUES (log(2,$1)); -- with some postgres values and some app params
Eventually it would be great to nest these arguments and functions
INSERT INTO foo (bar) VALUES (crypt($1, gen_salt('bf', $2)));
The new release candidate doesn't seem to bind the state of the current model to it's hooks and attached functions.
var assert = require('assert');
var mesa = require('mesa');
var model = mesa
.beforeInsert(function(data) {
assert(this.where, 'where should exist.')
return data;
});
model.attachedFunction = function() {
assert(this.where, 'where should exist.');
}
// both should not throw
model.insert({});
(function changeScope() { model.attachedFunction(); })()
CREATE TABLE "user" (
id SERIAL PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE "address" (
id SERIAL PRIMARY KEY,
user_id integer NOT NULL REFERENCES "user"(id),
city text NOT NULL
);
userTable = database.table('user');
addressTable = database.table('address');
userTable
.include({first: true}, addressTable)
.find(function(users) {
})
Unhandled rejection Error: `in` with empty array as right operand
at Object.dsl.(anonymous function).modifiers.(anonymous function) [as in] (/app/node_modules/fragments-postgres/node_modules/mesa/node_modules/mohair/node_modules/criterion/src/criterion.js:423:17)
at criterion (/app/node_modules/fragments-postgres/node_modules/mesa/node_modules/mohair/node_modules/criterion/src/criterion.js:603:21)
at Object.Mohair.where (/app/node_modules/fragments-postgres/node_modules/mesa/node_modules/mohair/lib/mohair.js:565:23)
at Object.Mesa.where (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:216:63)
at /app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:605:30
From previous event:
at reducer (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:601:20)
at Array.reduce (native)
at Object.Mesa.baseEmbed (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:635:21)
at Object.Mesa.embed (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:642:17)
at Object.bound (/app/node_modules/fragments-postgres/node_modules/mesa/node_modules/lodash/dist/lodash.js:957:21)
From previous event:
at reducer (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:458:20)
at Array.reduce (native)
at Object.Mesa.runQueue (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:460:18)
at Object.Mesa.afterQuery (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:475:17)
at /app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:568:19
From previous event:
at Object.Mesa.find (/app/node_modules/fragments-postgres/node_modules/mesa/lib/mesa.js:567:29)
....
at processImmediate [as _immediateCallback] (timers.js:367:17)
As the attributes in insert and update are escaped via mohair.escape
I expected the where
conditions to be escaped, too.
model.where({someColumn: true}); // doesn't escape
model.where({'"someColumn"': true}); // manual escaping is tedious
As a hotfix I used this method
model.where(escape({someColumn: true}));
function escape(where) {
if (Array.isArray(where)) {
return where.map(escape);
}
return _.object(_.map(where, function(value, key) {
var isSpecialKey = key[0] === '$';
var quotedKey = isSpecialKey ? key : '"' + key + '"';
return [quotedKey, isDictionary(value) ? escape(value) : value];
}));
function isDictionary(object) {
return _.isObject(object) && !_.isFunction(object);
}
}
Which currently works for normal keys, special keys ($or
, ...), but not yet for raw sql (where('foo && ?', foo)
)
Would be handsome if this would be baked into mesa or configurable.
Do you have any opinions or preferences?
this makes includes see the right database state in transactions
maybe only if connection was set explicitely to connection object on base table
maybe make this configurable
is there a reason getConnection is async?
Or do you prefer the cleaner error management with async callbacks?
Just thought about this:
There is no easy method to have an updated field in postgres without using an orm or some kind of hack with a trigger.
so it would be great if one could define some logic like: "if the model has an update field default it to new Date"
it would be great if this logic wouldn't be part of core mesa, but could be easily added with generic logic.
what do you think, could this be part of mesa?
This is a minor usability glitch I discovered while working with the api.
I shot myself several times with this error. It occurred one time to often and therefore I will report it.
Instead of model.find().then(postProcess)
I did model.find(postProcess)
which will swallow the postProcess
function and return something unexpected. As there will be no error it can get hard to detect the source of the problem
I would have 2 proposals to avoid this situation
a) assert to have no arguments for all functions without parameters (find
, first
, update
, delete
)
b) treat find(postProcess)
as find().then(postProcess)
Do you think this is an glitch which should be fixed? And if yes do you have a preference on how to solve this?
mesa = require 'mesa'
user = new mesa().table('user').connection(getConnection).attributes(['mail', 'password'])
user.insert {mail: 'foo', password: 'hashed'}, cb
returns this sql string
INSERT INTO user(mail, password) VALUES ($1, $2) RETURNING id
but should be
INSERT INTO "user" (mail, password) VALUES ($1, $2) RETURNING id
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.