krauter allows you to quickly create data-backed web services by configuring an Express router with a database connection and automatically producing parameterized query middleware from strings and objects. Middleware can also be produced from integers (sets the HTTP response status code), unary functions (sets the value of
null (clears the value of
npm install --save krauter
And depending on which DBMS is being used:
npm install --save pgnpm install --save mysqlnpm install --save mssqlnpm install --save sqlite3
Krauter must be created with a supplied executor function that simply takes a query along with an array of parameter values (and optionally an array of corresponding datatypes) and returns a promise for the results. krauter has predefined executors available for supported DBMSs. These can be used by calling
krauter.DBMS(connection[, options]) (where
DBMS is the name of a supported DBMS's npm package) with a corresponding connection/pool and an options object (used for the Express router), which will return a
Krauter configured to run queries on that specific connection/pool.
const krauter = ;const mysql = ;// Create database connection poolconst pool = mysql;// Create a Krauterconst api = krauter;
Krauter can also be initialized with a custom executor function:
const api = ;
Krauter works the same as a normal Express router, but with the added capability of its HTTP methods (including
all) taking various argument types and internally replacing them with middleware.
When a string is encountered, it is replaced with a middleware function that will execute the string as a query to the configured database and then store the result to
When an object is encountered, each of its properties' values will be interpreted as a query (to be ran in parallel) with each result being stored as a property of
req.data (with the same key).
req object and are denoted in dot notation within surrounding colons.
resobject can be accessed with
For DBMSs that typically have datatypes specified for parameters (such as mssql), the datatype can be denoted within surrounding braces preceding the specified property.
When a unary function is encountered, it is replaced with a middleware function that will call it with the supplied argument being a single object containing properties
data, where the value of
data is taken (and removed) from
req.data. The value returned from the unary function is then subsequently set to
req.data (unless a
Query object is returned... see below).
A unary function can be defined in a syntactically similar manner as a typical middleware function by using destructuring assignments and unpacking them from the single object argument.
krauter exposes a
Query global constructor that can be used to return a value from within a unary function which will be processed just like a normal
Krauter HTTP method argument. You can think of it as like returning a
Promise from within a
Promise.prototype.then() handler... a unary function that returns a
Query will instead return the result from executing the
Query. This allows for the building of more dynamic queries which would otherwise be very difficult to create using only variable expansion.
null is encountered, it is replaced with a middleware function that removes the value of
HTTP Response Status Codes
When a number is encountered, it is replaced with a middleware function that will set it as the response's status code.
Krauter automatically sends a response with
req.data as the body if the request previously matched a route but has not been answered. This can be bypassed singularly by calling
next('router') within a middleware function, or completely by setting the
automatic option to
false when constructing your
const api = ;
Clone the repo locally, and then:
npm installnpm test