BY Simmi Kava26 Jul 2021 Edit
Templated Query for TypeScript

Tagged Templates and Template Query


What are Tagged Templates?

✓ Advanced form of Template Literals

✓ Parse Template literal with a function

✓ First argument is array of strings. Remaining arguments are related to the expressions

✓ Tag function need not return a string

raw property allows us to access the raw strings as it is i.e. as they were entered without the escape sequence.

✓ Use String.raw() method to create the raw strings

Example:

//Templated Query for TypeScript

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();

Here, If you look in the above example TemplatedQuery.create and where.add are Tagged Templates.

What will happen if id was empty?

If id was empty, corresponding condition will be skipped and first AND will disappear.

Can you give me the advantage of using the Templated Query?
Yes, ofcourse

✓ It can prevent SQL Injection attack
✓ It can combine multiple Template Literals (String literals allowing embedded expressions)

BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2024
2023
2022
2021
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository