本文整理匯總了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])}
`;
})();