Skip to content

Instantly share code, notes, and snippets.

@neogeek
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neogeek/70c80c7ddaf998bee4bd to your computer and use it in GitHub Desktop.
Save neogeek/70c80c7ddaf998bee4bd to your computer and use it in GitHub Desktop.
SQLite3 + ADM-ZIP Test
node_modules/
test.sqlite
test.zip

#SQLite3 + ADM-ZIP Test

I'm trying to use the sqlite3 module to create an in memory SQLite database and the adm-zip module to save it into a zip file. So far I have been able to create an in memory database and add data to it, but I have been unsuccessful in finding a way to store it in a zip made through adm-zip as it requires either a file, buffer or string.

My question is this: does the sqlite3 module even support storing or saving as a buffer? If it doesn't then what would be an advisable solution for storing temporary files in Node.js when the script is used as both a requirable module and a command line script?

I've included the code I've been using to test with below and a cloneable gist.

var fs = require('fs'),
    admzip = require('adm-zip'),
    sqlite3 = require('sqlite3').verbose(),
    zip = new admzip(),
    db = new sqlite3.Database('test.sqlite');
    // db = new sqlite3.Database(':memory:');

db.serialize(function() {

    db.run('CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT);');
    db.run('INSERT OR IGNORE INTO test(name) VALUES ("neogeek");');

});

zip.addFile('README.md', '#SQLite3 + ADM-ZIP Test');

// zip.addFile('db.sqlite', db);

db.close();

fs.writeFileSync('test.zip', zip.toBuffer(), 'utf8');
{
    "private": true,
    "dependencies": {
        "sqlite3": "3.0.2",
        "adm-zip": "0.4.4"
    }
}

https://gist.github.com/neogeek/70c80c7ddaf998bee4bd

var fs = require('fs'),
temp = require('temp').track(),
admzip = require('adm-zip'),
sqlite3 = require('sqlite3').verbose(),
zip = new admzip(),
tempdb = temp.openSync('db.sqlite'),
db = new sqlite3.Database(tempdb.path);
db.serialize(function() {
db.run('CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT);');
db.run('INSERT OR IGNORE INTO test(name) VALUES ("neogeek");');
});
zip.addFile('README.md', '#SQLite3 + ADM-ZIP Test');
db.close(function () {
zip.addFile('test.sqlite', fs.readFileSync(tempdb.path));
fs.writeFileSync('test.zip', zip.toBuffer(), 'utf8');
});
{
"private": true,
"dependencies": {
"adm-zip": "0.4.4",
"sqlite3": "3.0.2",
"temp": "0.8.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment