1.0.0 • Published 5 years ago

templated-query v1.0.0

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

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.