templated-query

1.0.0 • Public • Published

ts-templated-query

Templated Query for TypeScript

Usage

const where = TemplatedQuery.fragments(" where ", " and ");
if (id) {
    where = where.add `CustomerID = ${id}`;
}
if (name) {
    where = where.add `Name like ${name + '%'}`;
}
const q = TemplatedQuery.create `SELECT * FROM Customers ${where}`;

const tq = q.toQuery();

// tq.command = "SELECT * FROM Customers WHERE CustomerID = @p0 AND Name like @p1";
// tq.arguments["@p0"] = id
// tq.arguments["@p1"] = name + %

// if id was empty, condition will be skipped and first `AND` will disappear

Benefits

It is easy to compose larger queries without having to worry about formatting.

Readme

Keywords

none

Package Sidebar

Install

npm i templated-query

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

18.5 kB

Total Files

10

Last publish

Collaborators

  • ackava