{"record":{"id":"4d725f8bb45f05c4","repo":"tursodatabase/turso","slug":"database-not-connected-call-connect-first","errorCode":null,"errorMessage":"Database not connected. Call connect() first.","messagePattern":"Database not connected\\. Call connect\\(\\) first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"bindings/react-native/src/Database.ts","lineNumber":483,"sourceCode":"  /**\n   * Get last insert rowid\n   */\n  get lastInsertRowid(): number {\n    if (!this._connection) {\n      return 0;\n    }\n    return this._connection.lastInsertRowid();\n  }\n\n  /**\n   * Check if open and throw if not\n   */\n  private checkOpen(): void {\n    if (this._closed) {\n      throw new Error('Database is closed');\n    }\n    if (!this._connected || !this._connection) {\n      throw new Error('Database not connected. Call connect() first.');\n    }\n  }\n}\n","sourceCodeStart":465,"sourceCodeEnd":487,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/react-native/src/Database.ts#L465-L487","documentation":"Second branch of checkOpen(): _closed is false but _connected or _connection is unset. The constructor only stores options; the native database, connection, and sync bootstrap all happen inside connect(). Until `await db.connect()` resolves, the Database is a shell and any statement-level call throws this.","triggerScenarios":"Calling db.prepare/exec/all/run/get immediately after `new Database(...)` without awaiting connect(); calling them from code that runs before an async init routine finishes (e.g. render firing before the connect() promise resolves); connect() rejected and the error swallowed, leaving callers to hit this on the next use.","commonSituations":"Forgetting the await on connect() in app bootstrap; React component effects querying before a `ready` flag is set; connect() failing (bad path, native error) inside a try that logs but continues, after which every query fails with this message instead of the original error.","solutions":["Always `await db.connect()` before any query, ideally once in a dedicated init module that exports a ready promise","If connect() can fail, surface and fix that error first — this message is just the downstream symptom","Guard entry points with `db.open` (true only when connected and not closed)","Initialize the Database lazily: export `getDb()` that connects on first call and caches the promise"],"exampleFix":"// before\nconst db = new Database({ path: getDbPath('app.db') });\ndb.connect(); // missing await\nconst row = await db.get('SELECT 1'); // throws 'Database not connected...'\n\n// after\nconst db = new Database({ path: getDbPath('app.db') });\nconst ready = db.connect();\nawait ready;\nconst row = await db.get('SELECT 1');","handlingStrategy":"validation","validationCode":"const ready = db.connect(); // once, at startup\n// ...\nawait ready; // every consumer awaits this before querying\nawait db.get('SELECT 1');","typeGuard":"function isConnected(db: Database): boolean {\n  return db.open; // !_closed && _connection !== null\n}","tryCatchPattern":"try {\n  await db.get(sql, params);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Call connect() first')) {\n    await db.connect();\n    return db.get(sql, params);\n  }\n  throw e;\n}","preventionTips":["Export a single ready promise (or lazily-connecting getDb()) from your db module and await it everywhere","Always await connect(); treat a failed connect() as a hard startup error instead of swallowing it","Gate early render/effects on a connected flag so queries never fire first"],"tags":["react-native","lifecycle","initialization","async"],"backgroundTag":"api-called-before-initialization","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}