Skip to content

[WIP]Add support for NODEFS #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
nodefs db should not deleted; add test file
  • Loading branch information
Boyi committed Mar 1, 2021
commit df29c051a58181a05dc55133204c76d61859209c
21 changes: 12 additions & 9 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
@param {string} fn filename for database file
@return {Database} the created database instance
*/
Database["open"] = function(fn) {
Database["open"] = function open(fn) {
var obj = Object.create(Database.prototype);
obj.filetype = "FS";
obj.filename = fn;
obj.handleError(sqlite3_open(obj.filename, apiTemp));
obj.db = getValue(apiTemp, "i32");
Expand All @@ -816,7 +817,7 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
// (created by create_function call)
obj.functions = {};
return obj;
}
};

/** Execute an SQL query, ignoring the rows it returns.
@param {string} sql a string containing some SQL text to execute
Expand Down Expand Up @@ -1090,7 +1091,9 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
Object.values(this.functions).forEach(removeFunction);
this.functions = {};
this.handleError(sqlite3_close_v2(this.db));
FS.unlink("/" + this.filename);
if (this.filetype != "FS") {
FS.unlink("/" + this.filename);
}
this.db = null;
};

Expand Down Expand Up @@ -1219,15 +1222,15 @@ Module["onRuntimeInitialized"] = function onRuntimeInitialized() {
var vfs = Module["FS"];

/** Create database instance
@param {string} fn filename for database file
@return {Database} the created database instance
@param {string} vdir directory name to mount for virtual fs
@param {string} osdir system directory to mount
*/
Module["mount"] = function(vdir, osdir) {
Module["mount"] = function mount(vdir, osdir) {
if (vfs.mayCreate(vdir)) {
vfs.mkdir(vdir)
vfs.mkdir(vdir);
}
return vfs.mount(vfs.filesystems["NODEFS"], {root: osdir}, vdir);
}
vfs.mount(vfs.filesystems["NODEFS"], { root: osdir }, vdir);
};

// export Database to Module
Module.Database = Database;
Expand Down
35 changes: 35 additions & 0 deletions test/test_nodefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
exports.test = function(SQL, assert) {
//Node filesystem module - You know that.
var fs = require('fs');

//Ditto, path module
var path = require('path');

SQL.mount('/nfs', __dirname);

//Works
var db = SQL.Database.open('/nfs/test.sqlite');

//[{"columns":["id","content"],"values":[["0","hello"],["1","world"]]}]
var res = db.exec("SELECT * FROM test WHERE id = 0");
assert.deepEqual(res,
[{"columns":["id","content"],"values":[[0,"hello"]]}],
"One should be able to read the mounted file from disk");
db.close();
}

if (module == require.main) {
const target_file = process.argv[2];
const sql_loader = require('./load_sql_lib');
sql_loader(target_file).then((sql)=>{
require('test').run({
'test node file': function(assert){
exports.test(sql, assert);
}
});
})
.catch((e)=>{
console.error(e);
assert.fail(e);
});
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy