tursodatabase/turso · error · TypeError

Expected second argument to be an options object

Error message

Expected second argument to be an options object

What it means

Thrown by Database.pragma() when the second argument is present, non-null, and not an object. The method first normalizes null/undefined to {}, so this TypeError only fires for real non-object values such as strings, numbers, booleans, or functions passed as the options parameter.

Source

Thrown at bindings/javascript/packages/common/compat.ts:216

      immediate: { value: wrapTxn("IMMEDIATE") },
      exclusive: { value: wrapTxn("EXCLUSIVE") },
      database: { value: this, enumerable: true },
    };
    Object.defineProperties(properties.default.value, properties);
    Object.defineProperties(properties.deferred.value, properties);
    Object.defineProperties(properties.immediate.value, properties);
    Object.defineProperties(properties.exclusive.value, properties);
    return properties.default.value;
  }

  pragma(source, options) {
    if (options == null) options = {};

    if (typeof source !== "string")
      throw new TypeError("Expected first argument to be a string");

    if (typeof options !== "object")
      throw new TypeError("Expected second argument to be an options object");

    const pragma = `PRAGMA ${source}`;

    const stmt = this.prepare(pragma);
    try {
      const results = stmt.all();
      return results;
    } finally {
      stmt.close();
    }
  }

  backup(filename, options) {
    throw new Error("not implemented");
  }

  serialize(options) {
    throw new Error("not implemented");

View on GitHub (pinned to bad083fafb)

Solutions

  1. Embed the value in the pragma string: db.pragma('user_version = 2') instead of db.pragma('user_version', 2)
  2. If you genuinely need options, pass an object: db.pragma('journal_mode', { simple: true })
  3. Omit the second argument entirely when no options are needed

Example fix

// before
db.pragma('user_version', 2); // number as 2nd arg -> TypeError

// after
db.pragma('user_version = 2'); // assignment inside the pragma string
Defensive patterns

Strategy: validation

Validate before calling

if (options !== undefined && options !== null && typeof options !== 'object') {
  throw new TypeError(`pragma options must be an object, got ${typeof options}`);
}
return db.pragma(source, options);

Type guard

function isPragmaOptions(value: unknown): value is Record<string, unknown> {
  return value == null || (typeof value === 'object' && !Array.isArray(value));
}

Prevention

When it happens

Trigger: Calling db.pragma('journal_mode', 'WAL') (passing the value as the second argument instead of embedding it in the pragma string); passing a number or boolean; passing a function. Arrays technically pass since typeof [] === 'object', but strings and primitives do not.

Common situations: Misreading the API and trying to set a pragma value positionally (db.pragma('user_version', 2)); copy-pasting examples from a different SQLite wrapper whose pragma signature is (name, value); refactoring where an options object got replaced by one of its fields.

Related errors


AI-assisted analysis of tursodatabase/turso@bad083fafb (2026-08-16). Data as JSON: /api/errors/fc543e95c2ce1f80. Report an issue: GitHub.