Skip to content

Instantly share code, notes, and snippets.

@Flamefork
Created January 8, 2011 23:07
Show Gist options
  • Save Flamefork/771230 to your computer and use it in GitHub Desktop.
Save Flamefork/771230 to your computer and use it in GitHub Desktop.
Queryl
/*
Usage examples
*/
// select * from users where active = true and last_logged_in > '2010-12-01' and type in ('admin, 'moderator') and expires > now() limit 1
table('users').
where({active: true}).
where({'last_logged_in >': new Date(2010, 12, 1)}).
where({type: ['admin', 'moderator']}).
where('expires > now()').
first(function (row) {});
var Users = table('users');
Users.active = Users.where({active: true});
// select users.login from users where active = true
Users.active.
select('login').
all(function (rows) {});
// select users.login, comments.text from users left join comments on users.id = comments.user_id order by last_logged_in desc
Users.
order('last_logged_in desc').
left_join(
table('comments'),
'id', 'user_id').
select('login', 'comments.text').
limit(20, 20).
all(function (rows) {});
// insert into users (login, active) values ('flameork', false)
Users.insert({
login: 'flameork',
active: false
}, function (id) {});
// update users set active = false
Users.update({
active: false
}, function (updated_rows) {});
// update users set login = 'flamefork', active = true where id = 123
Users.where({id: 123}).update({
login: 'flamefork',
active: true
}, function (updated_rows) {});
/*
API mockup
*/
function table(name) { return new Table(name); }
var Table = function (name) { this.name = name; };
Table.prototype.where = function (conditions) { return this; };
Table.prototype.limit = function (limit, offset) { return this; };
Table.prototype.order = function (clause) { return this; };
Table.prototype.left_join = function (clause) { return this; };
Table.prototype.all = function (callback) { callback([{}]); };
Table.prototype.first = function (callback) { callback({}); };
Table.prototype.insert = function (data, callback) { callback(345); };
Table.prototype.update = function (data, callback) { callback(1); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment