diff options
author | 2023-04-30 23:36:32 -0700 | |
---|---|---|
committer | 2023-04-30 23:36:32 -0700 | |
commit | fe57932dfb850f0feb509cd8ab8d7a32a3e7dadd (patch) | |
tree | 42db3cd22bee328fc33a859d9a62ce76b92b87e8 | |
parent | 59b3556faba7e8dd1b0271c4815238fe9ea3f496 (diff) | |
download | bun-fe57932dfb850f0feb509cd8ab8d7a32a3e7dadd.tar.gz bun-fe57932dfb850f0feb509cd8ab8d7a32a3e7dadd.tar.zst bun-fe57932dfb850f0feb509cd8ab8d7a32a3e7dadd.zip |
Add missing type for sqlite (#2764)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | packages/bun-types/sqlite.d.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/packages/bun-types/sqlite.d.ts b/packages/bun-types/sqlite.d.ts index 92acce170..8066aa475 100644 --- a/packages/bun-types/sqlite.d.ts +++ b/packages/bun-types/sqlite.d.ts @@ -363,6 +363,90 @@ declare module "bun:sqlite" { */ exclusive: (...args: any) => void; }; + + /** + * + * Save the database to an in-memory {@link Buffer} object. + * + * Internally, this calls `sqlite3_serialize`. + * + * @param name Name to save the database as @default "main" + * @returns Buffer containing the serialized database + */ + serialize(name?: string): Buffer; + + /** + * + * Load a serialized SQLite3 database + * + * Internally, this calls `sqlite3_deserialize`. + * + * @param serialized Data to load + * @returns `Database` instance + * + * @example + * ```ts + * test("supports serialize/deserialize", () => { + * const db = Database.open(":memory:"); + * db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)"); + * db.exec('INSERT INTO test (name) VALUES ("Hello")'); + * db.exec('INSERT INTO test (name) VALUES ("World")'); + * + * const input = db.serialize(); + * const db2 = new Database(input); + * + * const stmt = db2.prepare("SELECT * FROM test"); + * expect(JSON.stringify(stmt.get())).toBe( + * JSON.stringify({ + * id: 1, + * name: "Hello", + * }), + * ); + * + * expect(JSON.stringify(stmt.all())).toBe( + * JSON.stringify([ + * { + * id: 1, + * name: "Hello", + * }, + * { + * id: 2, + * name: "World", + * }, + * ]), + * ); + * db2.exec("insert into test (name) values ('foo')"); + * expect(JSON.stringify(stmt.all())).toBe( + * JSON.stringify([ + * { + * id: 1, + * name: "Hello", + * }, + * { + * id: 2, + * name: "World", + * }, + * { + * id: 3, + * name: "foo", + * }, + * ]), + * ); + * + * const db3 = Database.deserialize(input, true); + * try { + * db3.exec("insert into test (name) values ('foo')"); + * throw new Error("Expected error"); + * } catch (e) { + * expect(e.message).toBe("attempt to write a readonly database"); + * } + * }); + * ``` + */ + static deserialize( + serialized: TypedArray | ArrayBufferLike, + isReadOnly?: boolean, + ): Database; } /** |