1.0.4 • Published 10 months ago

@pforth/dbfuncs v1.0.4

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
10 months ago

This module lets you get async mysql database pool connections. Also has lots of cool helper query funcs.

Veradaccio

This project should be published in a locally running npm repo. You can do the following:

npm install -g verdaccio

nohup verdaccio --listen 4873

npm set registry http://localhost:4873/

You can also refer to here

Usage

const db=require("dbfuncs");

db.createPoolsFromFile(); // defaults to config.json
OR
db.createPoolsFromFile("config.json", {local: {password:"fred"}});  // Provide a default password of fred for the db "local"
OR
let pool=db.createPool(conName, host, user, password, database)

// If you create a pool called "local" then you can use
let conn=db.getConnLocal();
OR
let conn=db.getConn("local");
OR
let conn=db.getConn(localPool); // If you already got the local pool object from somewhere.
OR
let conn=db.getConn(); // To get the default pool (first one created or explicitly set)

await conn.query("DELETE FROM atable"); // Any query.  Returns a list of rows (dictionaries) if a select
await conn.getRow("SELECT field, field2 FROM hi WHERE id=1"); // Returns a single (or first) row as a dictionary
await conn.get("SELECT field FROM hi WHERE id=1;"); // Get the first field in the first row returned (synonym for getFirstField())
await conn.getField("one", "SELECT one, two FROM three"); // Returns a single field from the first row
await conn.getDictForField("id", "field", "SELECT id, field FROM hi"); // Returns a dictionary of key -> value
await conn.getDict("id", "SELECT id, field, another FROM hi"); // Returns a dictionary of keys -> each row of results.
await conn.getList("name", "SELECT name FROM users"); // Returns a list by taking the value of a single field from each row

await conn.getDictOfLists("nonuniqueId", "saleId", "SELECT userId, saleId FROM sales")
// Returns a dictionary of lists (in this case a list of all the sales for each user)
// If the second arg = null then each list item contains the entire row for that key

await conn.insert("users SET name=?", ["fred"]); // Does a sql insert

db.setDefPool(name); // Explicitly set the default pool to a pool with a different name
1.0.4

10 months ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago