npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tagged-sql

v0.9.0

Published

用 ES 标签模板书写 sql 语句 Writing sql statement with ES6 tagged template strings

Downloads

6

Readme

标签化SQL TaggedSql

用 ES 标签模板书写 sql 语句。

Writing sql statement with ES6 tagged template strings.

主要特征 Key features

  • 基于 ES 标签模板语法 ES6 tagged template strings based
  • 支持给表名添加前缀 Supports adding prefixes to table names
  • 书写简单 Simple writing

引入 Import

import TaggedSql from 'tagged-sql';
// 或者 or
const TaggedSql = require('tagged-sql');

创建 Create

const sql = TaggedSql`SELECT author FROM books WHERE name = ${book} AND author = ${author}`

链接 Link

const where = TaggedSql`WHERE name = ${book} AND author = ${author}`
const limit = TaggedSql`Limit 10`


const sql1 = TaggedSql`SELECT author FROM books ${where} ${limit}`;
// 或者 or
const sql2 = TaggedSql('SELECT author FROM books', where, limit);

独立的字段及数据表 Independent fields and tables

// 数据表 Sql table name
const bookTable = TaggedSql.Table('books');

// 字段名 Field name
const dateField = TaggedSql.Field('date');


// 字段,同时指定数据表 Field, and specify the data table
const nameField = TaggedSql.Field('name', 'books');

// 字段,同时指定数据表 Field, and specify the data table
const authorField = TaggedSql.Field('author', bookTable);


const bookSql = TaggedSql`SELECT ${dateField} FROM ${bookTable} WHERE ${nameField} = ${'Help Of TaggedSql'} AND ${authorField} = ${'NyLoong'}`;

查询 Query


// postgres: 
pg.query(bookSql.build((_, i) => `$${ i + 1 }`), bookSql.values)
// 等价于 is equivalent to
pg.query(`SELECT "date" FROM "books" WHERE "books"."name" = $1 AND "books"."author" = $2`, ['Help Of TaggedSql', 'NyLoong'])

// mysql: 
mysql.query(bookSql.build('?'), bookSql.values)
// 等价于 is equivalent to
mysql.query(`SELECT "date" FROM "books" WHERE "books"."name" = ? AND "books"."author" = ?`, ['Help Of TaggedSql', 'NyLoong'])

查询时添加表格前缀 Query with table prefix

bookSqlWithPrefix.transform((v, t, g) => t !== 'table' || g ? v : `tp_${ v }`)
// postgres: 
pg.query(bookSqlWithPrefix.build((_, i) => `$${ i + 1 }`), bookSql.values)
// 等价于 is equivalent to
pg.query(`SELECT "date" FROM "tp_books" WHERE "tp_books"."name" = $1 AND "tp_books"."author" = $2`, ['Help Of TaggedSql', 'NyLoong'])

// mysql: 
mysql.query(bookSqlWithPrefix.build('?'), bookSql.values)
// 等价于 is equivalent to
mysql.query(`SELECT "date" FROM "tp_books" WHERE "tp_books"."name" = ? AND "tp_books"."author" = ?`, ['Help Of TaggedSql', 'NyLoong'])