templated-query

TypeScript templated query

Usage no npm install needed!

<script type="module">
  import templatedQuery from 'https://cdn.skypack.dev/templated-query';
</script>

README

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.