A quick class for connecting to a mysql db in node and doing common functions against it.
There's no npm package for this. You'll have to install differently:
npm install --save github.com/srgraham/db2
Or:
npm install --save github.com/srgraham/db2#deadbeef
Swap out deadbeef
for the commit id you like.
const DB2 = require('db2');
const config = {
host: 'localhost',
database: 'database',
user: 'user',
pass: 'pass',
options: {
charset : 'utf8mb4',
},
};
const db = new DB2(config);
await db.execute('SELECT 1;');
There are a handful of common functions defined:
query
= query string to runbinds
= array of binds to fill in the?
for in the query
await db.execute('SELECT 7');
await db.execute('SELECT ?', [7]);
await db.execute('SELECT ?, ?, ?', [7, 8, 9]);
table_name
- table namerow_obj
- object to insert
await db.insert('test', {id: 1});
await db.insert('test', {id: 1, foo: 222, bar: 'asdf'});
table_name
- table namerow_obj
- object to insert
await db.insertIgnore('test', {id: 1});
await db.insertIgnore('test', {id: 1, foo: 222, bar: 'asdf'});
table_name
- table namerow_objs
- array of objects to insert
await db.insertIgnoreMulti('test', [{id: 1}]);
await db.insertIgnoreMulti('test', [
{id: 1, foo: 222, bar: 'asdf'},
{id: 2, foo: 333, bar: 'jkl'},
{id: 3, foo: 444, bar: 'qwerty'},
]);
table_name
- table namerow_obj
- object to insertignored_update_columns
- columns to not update when updating the row; all other columns get updated
await db.insertOnDuplicateKeyUpdate('test', {foo: 222, bar: 'asdf'}, ['id']);
await db.insertOnDuplicateKeyUpdate('test', {foo: 333, bar: 'jkl'}, ['id']);
table_name
- table namerow_obj_arr
- array of objects to updateignore_update_columns
- columns to not update when updating the row; all other columns get updated
await db.insertOnDuplicateKeyUpdate('test', [{foo: 222, bar: 'asdf'}], ['id']);
await db.insertOnDuplicateKeyUpdate(
'test',
[
{foo: 333, bar: 'asdf'},
{foo: 444, bar: 'jkl'},
{foo: 555, bar: 'qwerty'},
],
['id'],
);
table_name
- table nameid_where_obj
- object column/values to match which rows to updaterow_obj
- object to update
await db.update('test', {id: 1}, {foo: 123});
// update all rows with id = 2, setting foo = 444 and bar = 'jkl'
await db.update('test', {id: 2}, {foo: 444, bar: 'jkl'});
// update all rows with foo = 123, setting foo = 444 and bar = 'jkl'
await db.update('test', {foo: 123}, {foo: 444, bar: 'jkl'});
query
= query string to runbinds
= array of binds to fill in the?
for in the query returns the string with each?
filled in with each bind This function is called internally when runningexecute(query, binds)
You probably won't need to use it yourself.
DB2.format('SELECT 7;'); // SELECT 7;
DB2.format('SELECT ?;', [7]); // SELECT 7;
DB2.format('SELECT ?, ?, ?;', [7, 8, 9]); // SELECT 7, 8, 9;
in_list
When creating more detailed queries, you might want to look for a columnIN(?, ?, ?)
. This is convenience static method to be used to create those.
const binds = [1, 2, 3, 4, 5];
const query = `
SELECT *
FROM table
WHERE id IN(${DB2.createIn(binds)});
`;
// query becomes:
// SELECT *
// FROM table
// WHERE id IN(?, ?, ?, ?, ?);
await db.execute(query, binds)
column_name
- the column to run LIKE checks againstin_list
- list of values to LIKE againstjoiner
- the text to join on.OR
by default.AND
is another common option.
When creating more detailed queries, you might want to look for column LIKE ?
. This is convenience static method to be used to create a bunch of those.
const binds = ['a', 'e', 'i', 'o', 'u'];
const query = `
SELECT *
FROM table
WHERE ${DB2.createLikeIn('id', binds)};
`;
// query becomes:
// SELECT *
// FROM table
// WHERE ( id LIKE ? OR id LIKE ? OR id LIKE ? OR id LIKE ? OR id LIKE ? )
await db.execute(query, binds)
You can swap out OR
to AND
with the last argument.
column_name
- the column to run LIKE checks againstin_list
- list of values to LIKE againstjoiner
- the text to join on.OR
by default.AND
is another common option.
When creating more detailed queries, you might want to look for column REGEXP ?
. This is convenience static method to be used to create a bunch of those.
const binds = ['a', 'e', 'i', 'o', 'u'];
const query = `
SELECT *
FROM table
WHERE ${DB2.createRegexpIn('id', binds)};
`;
// query becomes:
// SELECT *
// FROM table
// WHERE ( id REGEXP ? OR id REGEXP ? OR id REGEXP ? OR id REGEXP ? OR id REGEXP ? )
await db.execute(query, binds)
You can swap out OR
to AND
with the last argument.
An npm package implies maintenance. I don't want to use up npm names for a repo that will likely not receive much maintenance.