How to use the slonik.sql.identifier function in slonik

To help you get started, we’ve selected a few slonik examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mmkal / slonik-tools / packages / migrator / src / index.ts View on Github external
async executed() {
        await createMigrationTable()
        return slonik
          .any(sql`select name, hash from ${sql.identifier([migrationTableName])}`)
          .then(migrations => {
            log('migrations in database:', migrations)
            return migrations
          })
          .then(migrations => migrations.map(r => {
            const name = r.name as string
            /* istanbul ignore if */
            if (r.hash !== hash(name)) {
              log(
                `warning:`,
                `hash in '${migrationTableName}' table didn't match content on disk.`,
                `did you try to change a migration file after it had been run?`,
                {migration: r.name, dbHash: r.hash, diskHash: hash(name)}
              )
            }
            return name
github gajus / postloader / src / routines / getByIds.js View on Github external
tableName: string,
  ids: $ReadOnlyArray,
  idName: string = 'id',
  identifiers: string,
  resultIsArray: boolean,
): Promise<$ReadOnlyArray> => {
  let rows = [];

  if (ids.length > 0) {
    const idType = typeof ids[0] === 'number' ? 'int4' : 'text';

    // @todo Do not use slonik-sql-tag-raw.

    rows = await connection.any(sql`
      SELECT ${raw(identifiers)}
      FROM ${sql.identifier([tableName])}
      WHERE ${sql.identifier([idName])} = ANY(${sql.array(ids, idType)})
    `);
  }

  const results = [];

  const targetPropertyName = camelCase(idName);

  if (resultIsArray) {
    for (const id of ids) {
      const result = filter(rows, (row) => {
        return row[targetPropertyName] === id;
      });

      results.push(result);
    }
github gajus / slonik-utilities / src / routines / update.js View on Github external
.map(([key, value]) => {
          // $FlowFixMe
          return sql`${sql.identifier([normalizeIdentifier(key)])} = ${value}`;
        }),
      sql` AND `,
github gajus / slonik-utilities / src / routines / updateDistinct.js View on Github external
sql.join(
          Object
            .entries(booleanExpressionValues)
            .map(([key, value]) => {
              // $FlowFixMe
              return sql`${sql.identifier([normalizeIdentifier(key)])} = ${value}`;
            }),
          sql` AND `,
        ),
      ],
      sql` AND `,
    );
  }

  const result = await connection.query(sql`
    UPDATE ${sql.identifier([tableName])}
    SET ${assignmentList(namedAssignmentPayload)}
    WHERE ${booleanExpression}
  `);

  return {
    rowCount: result.rowCount,
  };
};
github gajus / postloader / src / routines / getByIdsUsingJoiningTable.js View on Github external
connection: DatabaseConnectionType,
  joiningTableName: string,
  targetResourceTableName: string,
  joiningKeyName: string,
  lookupKeyName: string,
  identifiers: string,
  ids: $ReadOnlyArray,
): Promise<$ReadOnlyArray> => {
  let rows = [];

  // @todo Do not use slonik-sql-tag-raw.

  if (ids.length > 0) {
    rows = await connection.any(sql`
      SELECT
        ${sql.identifier(['r1', lookupKeyName + '_id'])} "POSTLOADER_LOOKUP_KEY",
        ${raw(identifiers)}
      FROM ${sql.identifier([joiningTableName])} r1
      INNER JOIN ${sql.identifier([targetResourceTableName])} r2 ON r2.id = ${sql.identifier(['r1', joiningKeyName + '_id'])}
      WHERE ${sql.identifier(['r1', lookupKeyName + '_id'])} = ANY(${sql.array(ids, 'int4')})
    `);
  }

  const results = [];

  for (const id of ids) {
    const result = filter(rows, (row) => {
      return row.POSTLOADER_LOOKUP_KEY === id;
    });

    results.push(result);
  }
github gajus / slonik-utilities / src / routines / upsert.js View on Github external
uniqueConstraintColumnNames.map((uniqueConstraintColumnName) => {
      return sql.identifier([uniqueConstraintColumnName]);
    }),
    sql`, `,

slonik

A Node.js PostgreSQL client with strict types, detailed logging and assertions.

BSD-3-Clause
Latest version published 7 days ago

Package Health Score

83 / 100
Full package analysis