()
| 171 | let _dialect: Dialect | undefined; |
| 172 | |
| 173 | export function getDialect(): Dialect { |
| 174 | if (_dialect !== undefined) return _dialect; |
| 175 | |
| 176 | // DATABASE_URL takes priority over D1 when set. |
| 177 | const url = getDatabaseUrl(); |
| 178 | if (url.startsWith("postgres://") || url.startsWith("postgresql://")) { |
| 179 | _dialect = "postgres"; |
| 180 | return _dialect; |
| 181 | } |
| 182 | if (url && !url.startsWith("file:")) { |
| 183 | // Remote libsql (e.g. Turso) |
| 184 | _dialect = "sqlite"; |
| 185 | return _dialect; |
| 186 | } |
| 187 | |
| 188 | const d1 = globalThis.__cf_env?.DB; |
| 189 | if (d1) { |
| 190 | _dialect = "d1"; |
| 191 | return _dialect; |
| 192 | } |
| 193 | |
| 194 | // Don't cache the fallthrough — on CF Workers, env bindings (__cf_env) aren't |
| 195 | // available at import time. If we cache "sqlite" here, D1 will never be |
| 196 | // detected once the bindings are set in the fetch handler. |
| 197 | return "sqlite"; |
| 198 | } |
| 199 | |
| 200 | export function isPostgres(): boolean { |
| 201 | return getDialect() === "postgres"; |
no test coverage detected