Skip to content

Instantly share code, notes, and snippets.

@ndugger
Created July 22, 2017 23:05
Show Gist options
  • Save ndugger/7fed5a999b29678d256dfe15c1481a6d to your computer and use it in GitHub Desktop.
Save ndugger/7fed5a999b29678d256dfe15c1481a6d to your computer and use it in GitHub Desktop.
export default function query () {
let cql = '';
return {
insert (table, data) {
const keys = Object.keys(data).join(', ');
const values = Object.values(data).join(', ');
cql += `INSERT INTO ${ table } (${ keys }) VALUES (${ values }) `;
return this;
},
update (table, data) {
const set = Object.keys(data).map(key => `${ key } = ${ data[ key ] }`).join(', ');
cql += `UPDATE ${ table } SET ${ set } `;
return this;
},
delete (column, table) {
cql += `DELETE ${ column } FROM ${ table } `;
return this;
},
select (...columns) {
cql += `SELECT ${ columns.join(', ') } `;
return this;
},
from (table) {
cql += `FROM ${ table } `;
return this;
},
where (condition) {
cql += `WHERE ${ condition } `;
return this;
},
and (condition) {
cql += `AND ${ condition } `;
return this;
},
group (column) {
cql += `GROUP BY ${ column } `;
return this;
},
order (column, direction = `DESC`) {
cql += `ORDER BY (${ column } ${ direction }) `;
return this;
},
limit (amount) {
cql += `LIMIT ${ amount } `;
return this;
},
raw (query) {
cql += `${ query } `;
return this;
},
toString () {
return cql.trim() + `;`;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment