-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): add support for BunSQLite (#2944)
--------- Co-authored-by: Farnabaz <[email protected]>
- Loading branch information
1 parent
71036e2
commit db77463
Showing
15 changed files
with
291 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
import Database from 'better-sqlite3' | ||
import { isAbsolute } from 'pathe' | ||
import { createDatabaseAdapter } from '../internal/database-adapter' | ||
import { getBetter3DatabaseAdapter } from '../internal/sqlite' | ||
import { getBunSqliteDatabaseAdapter } from '../internal/bunsqlite' | ||
|
||
let db: Database.Database | ||
export default createDatabaseAdapter<{ filename: string }>((opts) => { | ||
if (!db) { | ||
const filename = !opts || isAbsolute(opts?.filename || '') | ||
? opts?.filename | ||
: new URL(opts.filename, (globalThis as unknown as { _importMeta_: { url: string } })._importMeta_.url).pathname | ||
db = new Database(process.platform === 'win32' ? filename.slice(1) : filename) | ||
} | ||
|
||
return { | ||
async all<T>(sql: string, params?: Array<number | string | boolean>): Promise<T[]> { | ||
return params ? db.prepare<unknown[], T>(sql).all(params) : db.prepare<unknown[], T>(sql).all() | ||
}, | ||
async first<T>(sql: string, params?: Array<number | string | boolean>) { | ||
return params ? db.prepare<unknown[], T>(sql).get(params) : db.prepare<unknown[], T>(sql).get() | ||
}, | ||
async exec(sql: string): Promise<void> { | ||
await db.exec(sql) | ||
}, | ||
// NOTE: Not using the getDefaultSqliteAdapter function here because its not in the runtime directory. | ||
if (process.versions.bun) { | ||
return getBunSqliteDatabaseAdapter(opts) | ||
} | ||
return getBetter3DatabaseAdapter(opts) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as zlib from 'node:zlib' | ||
import { isAbsolute } from 'pathe' | ||
import type { Database as BunDatabaseType } from 'bun:sqlite' | ||
|
||
/** | ||
* CompressionStream and DecompressionStream polyfill for Bun | ||
*/ | ||
if (!globalThis.CompressionStream) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const make = (ctx: unknown, handle: any) => Object.assign(ctx, { | ||
writable: new WritableStream({ | ||
write: (chunk: ArrayBufferView) => handle.write(chunk), | ||
close: () => handle.end(), | ||
}), | ||
readable: new ReadableStream({ | ||
type: 'bytes', | ||
start(ctrl) { | ||
handle.on('data', (chunk: ArrayBufferView) => ctrl.enqueue(chunk)) | ||
handle.once('end', () => ctrl.close()) | ||
}, | ||
}), | ||
}) | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
globalThis.CompressionStream = class CompressionStream { | ||
constructor(format: string) { | ||
make(this, format === 'deflate' | ||
? zlib.createDeflate() | ||
: format === 'gzip' ? zlib.createGzip() : zlib.createDeflateRaw()) | ||
} | ||
} as unknown as typeof CompressionStream | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
globalThis.DecompressionStream = class DecompressionStream { | ||
constructor(format: string) { | ||
make(this, format === 'deflate' | ||
? zlib.createInflate() | ||
: format === 'gzip' | ||
? zlib.createGunzip() | ||
: zlib.createInflateRaw()) | ||
} | ||
} as unknown as typeof DecompressionStream | ||
} | ||
|
||
function getBunDatabaseSync() { | ||
// A top level import will make Nuxt complain about a missing module | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
return require('bun:sqlite').Database | ||
} | ||
|
||
let db: BunDatabaseType | ||
export const getBunSqliteDatabaseAdapter = (opts: { filename: string }) => { | ||
const Database = getBunDatabaseSync() | ||
if (!db) { | ||
const filename = !opts || isAbsolute(opts?.filename || '') || opts?.filename === ':memory:' | ||
? opts?.filename | ||
: new URL(opts.filename, (globalThis as unknown as { _importMeta_: { url: string } })._importMeta_.url).pathname | ||
db = new Database(filename, { create: true }) | ||
} | ||
|
||
return { | ||
async all<T>(sql: string, params?: Array<number | string | boolean>): Promise<T[]> { | ||
return params ? db.prepare(sql).all(...params) as T[] : db.prepare(sql).all() as T[] | ||
}, | ||
async first<T>(sql: string, params?: Array<number | string | boolean>): Promise<T | null> { | ||
return params ? db.prepare(sql).get(...params) as T : db.prepare(sql).get() as T | ||
}, | ||
async exec(sql: string): Promise<unknown> { | ||
return db.prepare(sql).run() | ||
}, | ||
} | ||
} |
Oops, something went wrong.