當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript slonik.sql類代碼示例

本文整理匯總了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'])})
   `);
 };
開發者ID:TeamworkGuy2,項目名稱:DefinitelyTyped,代碼行數:9,代碼來源:slonik-tests.ts

示例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);
})();
開發者ID:benleilei,項目名稱:DefinitelyTyped,代碼行數:34,代碼來源:slonik-tests.ts

示例3: INTO

(async () => {
    await connection.query(sql`
        INSERT INTO (foo, bar, baz)
        VALUES ${sql.tupleList([
            [1, 2, 3],
            [4, 5, 6]
            ])}
    `);
})();
開發者ID:benleilei,項目名稱:DefinitelyTyped,代碼行數:9,代碼來源:slonik-tests.ts

示例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])}
  `;
})();
開發者ID:benleilei,項目名稱:DefinitelyTyped,代碼行數:51,代碼來源:slonik-tests.ts


注:本文中的slonik.sql類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。