@earth-app/collegedb
    Preparing search index...

    Function buildUpsert

    • Builds an "upsert". SQLite and PostgreSQL get ON CONFLICT ... DO UPDATE: on conflict for conflictColumns, the listed update columns (or all non-conflict columns by default) are overwritten with the incoming values via the excluded pseudo-row. MySQL and MariaDB get ON DUPLICATE KEY UPDATE col = VALUES(col), which reacts to a conflict on any unique key of the table, so conflictColumns should name the primary key or the unique key you mean. MySQL and MariaDB report 2 affected rows when the update changed the row, 1 for an insert, and 0 when nothing changed.

      Parameters

      • table: string

        Target table name

      • values: ColumnValues

        Column to value map for the row to insert

      • conflictColumns: string | string[]

        Column(s) that trigger the conflict resolution

      • options: BuildUpsertOptions = {}

        Upsert modifiers (update subset, RETURNING)

      Returns BuiltQuery

      The parameterized SQL and its bindings

      If no columns, no conflict columns, or an identifier is invalid

      const { sql, bindings } = buildUpsert('kv', { k: 'a', v: '1' }, 'k');
      // sql: INSERT INTO "kv" ("k", "v") VALUES (?, ?) ON CONFLICT ("k") DO UPDATE SET "v" = excluded."v"
      // bindings: ['a', '1']