Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC/WIP: Add query execution features and column definition shorthands #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
.idea
yarn.lock
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"node": true,
"esversion": 8,
"globals": {
"describe" : true,
"it" : true,
Expand Down
Empty file added .prettierrc
Empty file.
18 changes: 18 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,21 @@ module.exports = new Sql(DEFAULT_DIALECT, {});
module.exports.create = create;
module.exports.Sql = Sql;
module.exports.Table = Table;

function specializeColumn(dataType) {
let columMaker = (o) => Object.assign({}, o, { dataType: dataType });
return columMaker;
}

module.exports.column = {
text: specializeColumn('text'),
varchar: specializeColumn('varchar'),
uuid: specializeColumn('uuid'),
boolean: specializeColumn('boolean'),
timestamp: specializeColumn('timestamp'),
json: (def) => Object.assign({}, def, { dataType: 'json' }),
jsonb: (def) => Object.assign({}, def, { dataType: 'jsonb' }),
bytea: specializeColumn('bytea'),
integer: specializeColumn('integer'),
custom: (def) => Object.assign({}, def),
};
32 changes: 32 additions & 0 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ Node.prototype.toQuery = function(dialect) {
return initializeDialect(Dialect, this).getQuery(this);
};

Node.prototype._exec = function(queryable) {
let q = this.toQuery();
return queryable.queryAsync(q);
};

Node.prototype.execAsync = function(queryable) {
return this._exec(queryable).then(() => {});
};

Node.prototype.toArrayAsync = function(queryable) {
return this._exec(queryable);
};

Node.prototype.getAsync = function(queryable) {
return this._exec(queryable).then(res => res.length > 0 ? res[0] : null);
};

Node.prototype.firstAsync = function(queryable) {
return this._exec(queryable).then(res => {
if (res.length < 1) throw new Error('No result found');
return res[0];
});
};

Node.prototype.singleAsync = function(queryable) {
return this._exec(queryable).then(res => {
if (res.length < 1) throw new Error('No result found');
if (res.length > 1) throw new Error('More than one result found');
return res[0];
});
};

Node.prototype.toNamedQuery = function(name, dialect) {
if (!name || typeof name !== 'string' || name === '') {
throw new Error('A query name has to be a non-empty String.');
Expand Down
Loading