Skip to content

Commit 837d25d

Browse files
committed
Database constructor with custom FS
1 parent f5d9f54 commit 837d25d

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ CFLAGS = \
2323
-DSQLITE_DISABLE_LFS \
2424
-DSQLITE_ENABLE_FTS3 \
2525
-DSQLITE_ENABLE_FTS3_PARENTHESIS \
26+
-DSQLITE_ENABLE_FTS5 \
2627
-DSQLITE_ENABLE_JSON1 \
2728
-DSQLITE_THREADSAFE=0 \
2829
-DSQLITE_ENABLE_NORMALIZE
@@ -54,8 +55,7 @@ EMFLAGS_WASM = \
5455
EMFLAGS_OPTIMIZED= \
5556
-s INLINING_LIMIT=50 \
5657
-O3 \
57-
-flto \
58-
--closure 1
58+
-flto
5959

6060
EMFLAGS_DEBUG = \
6161
-s INLINING_LIMIT=10 \

dist/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
*
33
# Except this file
44
!.gitignore
5-
!.npmignore
5+
!.npmignore

src/api.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
7373
var SQLITE_UTF8 = 1;
7474
// var - cwrap function
7575
var sqlite3_open = cwrap("sqlite3_open", "number", ["string", "number"]);
76+
var sqlite3_open_v2 = cwrap("sqlite3_open_v2", "number", ["string", "number", "number", "string"]);
7677
var sqlite3_close_v2 = cwrap("sqlite3_close_v2", "number", ["number"]);
7778
var sqlite3_exec = cwrap(
7879
"sqlite3_exec",
@@ -323,6 +324,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
323324
}
324325
return true;
325326
};
327+
Statement.prototype["bind_"] = Statement.prototype.bind; // work around https://github.com/GoogleChromeLabs/comlink/blob/4ba8162f6c28fb1bf53b491565ef9a3ae42b72d3/src/comlink.ts#L432
326328

327329
/** Execute the statement, fetching the the next line of result,
328330
that can be retrieved with {@link Statement.get}.
@@ -790,8 +792,22 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
790792
if (data != null) {
791793
FS.createDataFile("/", this.filename, data, true, true);
792794
}
793-
this.handleError(sqlite3_open(this.filename, apiTemp));
795+
const ret = sqlite3_open(this.filename, apiTemp);
796+
this.db = getValue(apiTemp, "i32");
797+
this.handleError(ret);
798+
registerExtensionFunctions(this.db);
799+
// A list of all prepared statements of the database
800+
this.statements = {};
801+
// A list of all user function of the database
802+
// (created by create_function call)
803+
this.functions = {};
804+
}
805+
806+
function CustomDatabase(filename) {
807+
this.filename = filename;
808+
const ret = sqlite3_open(this.filename, apiTemp);
794809
this.db = getValue(apiTemp, "i32");
810+
this.handleError(ret);
795811
registerExtensionFunctions(this.db);
796812
// A list of all prepared statements of the database
797813
this.statements = {};
@@ -1200,4 +1216,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
12001216

12011217
// export Database to Module
12021218
Module.Database = Database;
1219+
Module["CustomDatabase"] = CustomDatabase;
1220+
Module["FS"] = FS;
1221+
CustomDatabase.prototype = Object.create(Database.prototype);
12031222
};

0 commit comments

Comments
 (0)