
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
@apigrate/dao
Advanced tools
A Promise-based DAO implementation, designed for mysql databases (formerly @apigrate/mysqlutils).
A library that simplifies working with relational database databases, using a Data Access Object (DAO) pattern. It provides promise-based functions making it easy to get objects out of database table rows with intuitive language.
lodash, @fast-csv/format, moment dependencies.db-api.js helper library.query method for better clarity.filter method. Use query instead.log_category Dao option.Create a DAO for each table in your database. Once instantiated, you can use any of the available methods outlined below to query, create, update, and delete rows from that table.
Note, this library is currently designed to work with mysql databases (note the peer dependency). Support for additional databases may become available in the future.
Important Prerequsite: your app should configure a mysql connection pool that it can pass to this library. This library is not opinionated about connection management. It does not close or otherwise manage pool connections directly.
//var pool = (assumed to be provided by your app)
const {Dao} = require('@apigrate/dao');
//An optional configuration object containing some options that you might want to use on a table.
var opts = {
created_timestamp_column: 'created',
updated_timestamp_column: 'updated',
version_number_column: 'version'
};
var Customer = new Dao('t_customer', 'customer', opts, pool);
//Note, in addition to tables, you use this on views as well...
Get a single table row by id and return it as an object. Returns null when not found.
//Get a customer by id = 27
let result = await Customer.get(27);
//result --> {id: 27, name: 'John Smith', city: 'Chicago', active: true ... }
Simplest form of query. Retrieves a count rows from DB matching the query object.
//Search for customers where status='active' and city='Chicago'
let result = await Customer.count({status: 'active', city: 'Chicago'})
//result --> 2
Simple matches-all query. Retrieves all rows from DB matching the query object as an array. Returns an empty array when not found.
//Search for customers where status='active' and city='Chicago'
let result = await Customer.query({status: 'active', city: 'Chicago'})
//result --> [ {id: 27, name: 'John Smith', city: 'Chicago' active: true ... }, {id: 28, name: 'Sally Woo', city: 'Chicago', active: true ... }, ...]
Identical to query, except only the first entity from results is returned as an object. Returns null when not found.
//Search for customers where status='active' and city='Chicago'
let result = await Customer.one({status: 'active', city: 'Chicago'})
//result --> {id: 27, name: 'John Smith', city: 'Chicago' active: true ... }
Select multiple entities matching a where clause and parameters.
//Retrieve active customers in Chicago, Indianpolis.
let result = await Customer.selectWhere("active=? AND (city=? or city=?)" [true, "Chicago", "Indianapolis"]);
//result --> [ {id: 27, name: 'John Smith', city: 'Chicago' active: true ... }, {id: 28, name: 'Sally Woo', city: 'Chicago', active: true ... }, {id: 28, name: 'Jake Plumber', city: 'Indianapolis', active: true ... }, ...]
Creates a new entity.
//Create a new customer
let customerToSave = { name: 'Acme, Inc.', city: 'Chicago', active: true};
let result = await Customer.create(customerToSave);
//result --> {id: 27, name: 'Acme, Inc.', city: 'Chicago', active: true}; (assuming id is auto-generated)
Updates an entity by primary key (which must be included on the payload).
//Update an existing customer by id.
let customerToSave = {id: 27, name: 'Acme, Inc.', city: 'Chicago', active: true};
customerToSave.active = false;
let result = await Customer.update(customerToSave);
//result --> {id: 27, name: 'Acme, Inc.', city: 'Chicago', active: false, _affectedRows: 1};
Deletes an entity by primary key.
//Delete customer id = 27
let result = await Customer.delete(27);
//result --> {_affectedRows: 1, ...}
Deletes multiple entities matching the query object.
//Delete inactive customers in Chicago
let result = await Customer.deleteMatching({active: false, city: "Chicago"});
//result --> {_affectedRows: 3, active: false, city: "Chicago"}
Deletes multiple entities matching a where clause and parameters.
//Delete inactive customers in Chicago, Indianpolis.
let result = await Customer.deleteWhere("active=? AND (city=? or city=?)" [false, "Chicago", "Indianapolis"]);
//result --> {_affectedRows: 4}
Use the sqlCommand method to issue any kind of parameterized SQL command (SELECT, INSERT, UPDATE, DELETE, etc.). The result
returned is simply the result returned from the underlying mysql library callback function.
//Custom query example
let result = await Customer.sqlCommand("SELECT id, name from my_customer_view where active=? AND (city=? or city=?)" [false, "Chicago", "Indianapolis"]);
//result --> [{id: 27, name: "Acme, Inc."}, {id: 33, name: "American Finance Corporation"}, {id: 35, name: "Integrity Engineering"}]
The debug library is used. Use process.env.NODE_ENV='gr8:db' for general debugging. For verbose logging (outputs raw responses on create, update, delete operations) use gr8:db:verbose.
Note: as of version 3.x logger injection is no longer supported and will be ignored.
console.errorDEBUG='gr8:db', the following is logged:
DEBUG='gr8:db:verbose', the following is logged:
There are two ways to suppress log output:
DB_SUPPRESS_LOGGING_TABLES environment to provide a comma-separated list of tables/views for which SQL/parameter logging output will be suppressed entirely.{suppressLogging: true} on the sqlCommand opts parameterFAQs
A Promise-based DAO implementation, designed for mysql databases (formerly @apigrate/mysqlutils).
We found that @apigrate/dao demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.