-
Notifications
You must be signed in to change notification settings - Fork 37
/
mod.ts
51 lines (48 loc) · 1.38 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { DB } from "./src/db.js";
import { Empty } from "./src/rows.js";
import { Status } from "./src/constants.ts";
import SqliteError from "./src/error.ts";
/**
* open
*
* Open a new SQLite3 database. The file at
* the path is read and preloaded into the database.
*
* ?> Unlike the SQLite3 C library, this will not
* automatically write any changes to disk. Use
* `db.data()` or `save(db)` to persist any changes
* you make.
*/
async function open(path: string, ignoreNotFound = true): Promise<DB> {
let bytes = undefined;
try {
bytes = await Deno.readFile(path);
} catch (err) {
if (!ignoreNotFound || !(err instanceof Deno.errors.NotFound)) {
throw err;
}
}
const db: any = new DB(bytes);
db._save_path = path;
return db;
}
/**
* save
*
* Save database to file. If the database was opened
* from a file using `open()`, the second parameter
* is optional.
*/
async function save(db: DB, path?: string): Promise<void> {
path = path || (db as any)._save_path as string;
if (!db._open) {
throw new SqliteError("Database was closed.");
}
// We obtain the data array ourselves to avoid
// .data() making a copy
const ptr = db._wasm.get_db_file(db._id);
const len = db._wasm.get_db_file_size(db._id);
const data = new Uint8Array(db._wasm.memory.buffer, ptr, len);
return Deno.writeFile(path, data);
}
export { open, save, DB, Empty, Status };