{"record":{"id":"6151245046ac6214","repo":"tursodatabase/turso","slug":"expected-first-argument-to-be-a-string","errorCode":null,"errorMessage":"Expected first argument to be a string","messagePattern":"Expected first argument to be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"bindings/javascript/packages/common/compat.ts","lineNumber":213,"sourceCode":"    const properties = {\n      default: { value: wrapTxn(\"\") },\n      deferred: { value: wrapTxn(\"DEFERRED\") },\n      immediate: { value: wrapTxn(\"IMMEDIATE\") },\n      exclusive: { value: wrapTxn(\"EXCLUSIVE\") },\n      database: { value: this, enumerable: true },\n    };\n    Object.defineProperties(properties.default.value, properties);\n    Object.defineProperties(properties.deferred.value, properties);\n    Object.defineProperties(properties.immediate.value, properties);\n    Object.defineProperties(properties.exclusive.value, properties);\n    return properties.default.value;\n  }\n\n  pragma(source, options) {\n    if (options == null) options = {};\n\n    if (typeof source !== \"string\")\n      throw new TypeError(\"Expected first argument to be a string\");\n\n    if (typeof options !== \"object\")\n      throw new TypeError(\"Expected second argument to be an options object\");\n\n    const pragma = `PRAGMA ${source}`;\n\n    const stmt = this.prepare(pragma);\n    try {\n      const results = stmt.all();\n      return results;\n    } finally {\n      stmt.close();\n    }\n  }\n\n  backup(filename, options) {\n    throw new Error(\"not implemented\");\n  }","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/bindings/javascript/packages/common/compat.ts#L195-L231","documentation":"Thrown by Database.pragma() when the first argument (the pragma source) is not a string. pragma() interpolates the value directly into `PRAGMA ${source}` and executes it, so it must be a string like 'journal_mode' or 'user_version = 1'. Any other type is rejected before reaching prepare().","triggerScenarios":"Calling db.pragma(123), db.pragma(null), or db.pragma(['journal_mode']); passing a computed value that resolves to a non-string (e.g. a pragma name read from JSON config typed as number); template-literal mistakes that produce an array or object.","commonSituations":"Reading pragma names from configuration files where a value like user_version is parsed as a number; passing a Pragma name variable that was shadowed or never defined; porting code that used a different SQLite API taking an options object as the first argument.","solutions":["Pass the pragma as a string: db.pragma('journal_mode')","Coerce known inputs explicitly: db.pragma(String(name))","Validate config-provided pragma names against a whitelist of strings before use","For assignments, include the value in the string: db.pragma('user_version = 2')"],"exampleFix":"// before\ndb.pragma(pragmaNameFromConfig); // number or undefined from JSON\n\n// after\nif (typeof pragmaNameFromConfig !== 'string') throw new TypeError('pragma name must be a string');\ndb.pragma(pragmaNameFromConfig);","handlingStrategy":"validation","validationCode":"function pragma(db: Database, source: string) {\n  if (typeof source !== 'string' || source.length === 0) {\n    throw new TypeError(`pragma source must be a non-empty string, got ${typeof source}`);\n  }\n  return db.pragma(source);\n}","typeGuard":"function isPragmaName(source: unknown): source is string {\n  return typeof source === 'string' && /^[a-z_][a-z0-9_]*(\\s*=\\s*.+)?$/i.test(source.trim());\n}","tryCatchPattern":null,"preventionTips":["Whitelist pragma names from a constant map instead of accepting free-form config values","Coerce with String() only after confirming the input shape","Include assignments inline: 'user_version = 2'"],"tags":["turso","javascript","pragma","input-validation","better-sqlite3-compat"],"backgroundTag":"invalid-argument-type","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}