{"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/bad083fafbefdeae9a42ec19bdaaad8918dcf411/bindings/react-native/src/Database.ts#L465-L487","documentation":"checkOpen() throws 'Database not connected. Call connect() first.' when the database is not closed but _connected or _connection is false. The constructor deliberately does no I/O; connect() is what opens the local database or bootstraps the sync one, and it sets _connected = true only after init succeeds. So this error means either connect() was never awaited, or a previous connect() failed and left the instance half-initialized.","triggerScenarios":"Constructing `new Database(opts)` and immediately calling `db.query(...)` without `await db.connect()`; fire-and-forget connect() racing the first query; connect() throwing (e.g., native module missing, bad path) and later calls proceeding anyway.","commonSituations":"Forgetting the connect step when porting from a binding that opens eagerly; startup races in useEffect or init code; swallowing the connect() error with an empty catch and continuing; hot reload creating a new instance whose connect promise was lost.","solutions":["Call and await `db.connect()` once before any query/exec — keep the promise and reuse it everywhere (`dbReady` pattern)","If connect() failed, handle that error; do not continue issuing queries on the same instance without reconnecting","Wrap the instance in a small manager that lazily connects and gates every operation on readiness"],"exampleFix":"// before\nconst db = new Database({ path: 'app.db' });\nawait db.query('SELECT * FROM users'); // throws: not connected\n\n// after\nconst db = new Database({ path: 'app.db' });\nawait db.connect();\nawait db.query('SELECT * FROM users');","handlingStrategy":"validation","validationCode":"// Gate every operation on one shared connect promise\nlet db: Database;\nlet ready: Promise<void>;\n\nfunction openDb(opts: DatabaseOpts) {\n  db = new Database(opts);\n  ready = db.connect().catch((e) => { ready = Promise.reject(e); throw e; });\n  return ready;\n}\n\nawait ready; // before ANY query/exec\nawait db.query('SELECT 1');","typeGuard":null,"tryCatchPattern":"try { await db.query(sql); } catch (e) { if (e instanceof Error && /Call connect\\(\\) first/.test(e.message)) { await db.connect(); return db.query(sql); } throw e; }","preventionTips":["The constructor does not open anything — connect() is mandatory, await it before first use","Reuse a single connect promise app-wide instead of calling connect() from many places","Never swallow connect() errors; a failed connect leaves the instance unusable"],"tags":["react-native","lifecycle","connect","initialization"],"backgroundTag":"connection-not-initialized","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}