本文整理汇总了TypeScript中slonik.sql类的典型用法代码示例。如果您正苦于以下问题:TypeScript sql类的具体用法?TypeScript sql怎么用?TypeScript sql使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了sql类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: async
const sample1 = async () => {
connection.query(sql`
SELECT ${sql.identifier(['foo', 'a'])}
FROM (
VALUES ${sql.tupleList([['a1', 'b1', 'c1'], ['a2', 'b2', 'c2']])}
) foo(a, b, c)
WHERE foo.b IN (${sql.valueList(['c1', 'a2'])})
`);
};
示例2:
(async () => {
const uniquePairs = [
['a', 1],
['b', 2]
];
let placeholderIndex = 1;
const whereConditionSql = uniquePairs
.map(needleColumns => {
return needleColumns
.map((column) => {
return `${column} = $${placeholderIndex++}`;
})
.join(' AND ');
})
.join(' OR ');
const values = [];
for (const pairValues of uniquePairs) {
values.push(...pairValues);
}
const query = sql`
SELECT
id
FROM foo
WHERE
${sql.raw(whereConditionSql, values)}
`;
await connection.any(query);
})();
示例3: INTO
(async () => {
await connection.query(sql`
INSERT INTO (foo, bar, baz)
VALUES ${sql.tupleList([
[1, 2, 3],
[4, 5, 6]
])}
`);
})();
示例4: FROM
(async () => {
// ExpectType SqlSqlTokenType
const query0 = sql`SELECT ${'foo'} FROM bar`;
// ExpectType SqlSqlTokenType
const query1 = sql`SELECT ${'baz'} FROM (${query0})`;
await connection.query(sql`
SELECT (${sql.valueList([1, 2, 3])})
`);
await connection.query(sql`
INSERT INTO (foo, bar, baz)
VALUES ${sql.tuple([1, 2, 3])}
`);
await connection.query(sql`
INSERT INTO (foo, bar, baz)
VALUES ${sql.tupleList([
[1, 2, 3],
[4, 5, 6]
])}
`);
await connection.query(sql`
SELECT bar, baz
FROM ${sql.unnest(
[
[1, 'foo'],
[2, 'bar']
],
[
'int4',
'text'
]
)} AS foo(bar, baz)
`);
sql`
SELECT 1
FROM ${sql.identifier(['bar', 'baz'])}
`;
sql`
SELECT 1
FROM ${sql.raw('"bar"')}
`;
sql`
SELECT ${sql.raw('$1', [1])}
`;
})();