<!-- Generated by SpecStory -->

# 改成 neondatabase (2025-06-27 00:50:50)

_**User**_

改成 neondatabase 

---

_**Assistant**_



---

Okay, I see that the current database connection logic in `apps/ssr/src/lib/db.ts` is not quite right and has some errors. I'll fix it to correctly use `neondatabase`.

Here's what I'll do:
1.  I'll correct the import to use `neon` from `@neondatabase/serverless` and also import the `NeonQueryFunction` type for type safety.
2.  I will fix the `createDrizzle` helper function to accept the database client as an argument.
3.  I'll update the `DbManager` class to properly manage the database connection state, including the client and drizzle instances.
4.  The `connect` method will be adjusted to correctly establish a connection using `neon` and initialize the drizzle instance.
5.  Finally, `getDb` and `getPool` methods will be updated to ensure the database is connected and ready before returning the instance or pool.

I will now apply these changes.

---

```diff
  import { env } from '@env'
- import { drizzle } from 'drizzle-orm/neon-http';
- import { neon } from '@neondatabase/serverless';
+ import { drizzle } from 'drizzle-orm/neon-http'
+ import { neon, type NeonQueryFunction } from '@neondatabase/serverless'
  
  import * as schema from '../schemas'
  
- const createDrizzle = () => drizzle({ client: sql, schema, logger: true })
+ const createDrizzle = (client: NeonQueryFunction<false, false>) =>
+   drizzle({ client, schema, logger: true })
  
  export class DbManager {
-   private drizzle: ReturnType<typeof createDrizzle>
+   private drizzle: ReturnType<typeof createDrizzle> | undefined
    private ready = false
+   private client: NeonQueryFunction<false, false> | undefined
  
    static shared = new DbManager()
```

```diff
      }
  
-     const sql = postgres(env.PG_CONNECTION_STRING, {
+     const sql = neon(env.PG_CONNECTION_STRING!, {
        prepare: false,
      })
-     this.ready = true
      this.client = sql
  
      await sql`select 1`
  
      this.drizzle = createDrizzle(sql)
+     this.ready = true
    }
  
```

```diff
      }
  
-     if (!this.isReady()) {
+     if (!this.isReady() || !this.drizzle) {
        throw new Error('Database is not ready')
      }
```

```diff
        throw new Error('Database is not enabled')
      }
+ 
+     if (!this.isReady() || !this.client) {
+       throw new Error('Database is not ready')
+     }
  
      return this.client
```

---



---

I see there's a linter error. It seems the `prepare` option is not expected in the `neon` function call within this context. The most common usage of the `neon` function from `@neondatabase/serverless` doesn't require a second argument for basic setup, and the default behavior is what's needed here.

I will correct this by removing the options object from the `neon()` call in `apps/ssr/src/lib/db.ts`.

---

```diff
      }
  
-     const sql = neon(env.PG_CONNECTION_STRING!, {
-       prepare: false,
-     })
+     const sql = neon(env.PG_CONNECTION_STRING!)
      this.client = sql
  
```

---



---

I've updated the database connection logic to use `neondatabase` and resolved the linter errors. Everything should be working correctly now. Let me know if there is anything else I can help with.

---

