├── .gitignore ├── .jshintrc ├── .travis.yml ├── CHANGELOG ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .tmp 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "undef": true, 3 | "unused": true, 4 | "node": true, 5 | "esnext": true, 6 | "expr": true, 7 | "globals": { 8 | "describe": true, 9 | "it": true, 10 | "before": true 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.11" 5 | matrix: 6 | fast_finish: true 7 | allow_failures: 8 | - node_js: 0.11 9 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | v0.1.3: 2 | date: 2017-03-14 3 | changes: 4 | - #all will return entire database as an object. 5 | - Updated dependencies. 6 | v0.1.2: 7 | date: 2015-02-25 8 | changes: 9 | - Throw if destination or callback are not specified. 10 | v0.1.1: 11 | date: 2014-07-16 12 | changes: 13 | - Initial release 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Tyler Kellen 2 | 3 | Permission is hereby granted, free of charge, to any person 4 | obtaining a copy of this software and associated documentation 5 | files (the "Software"), to deal in the Software without 6 | restriction, including without limitation the rights to use, 7 | copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following 10 | conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sqlite-to-json [![Build Status](https://secure.travis-ci.org/tkellen/js-sqlite-to-json.png)](http://travis-ci.org/tkellen/js-sqlite-to-json) 2 | > Dump data from sqlite databases to JSON files easily. 3 | 4 | [![NPM](https://nodei.co/npm/sqlite-to-json.png)](https://nodei.co/npm/sqlite-to-json/) 5 | 6 | ## API 7 | 8 | ### constructor(opts) 9 | 10 | Create an instance of SqliteToJson. 11 | 12 | Example: 13 | ```js 14 | const SqliteToJson = require('sqlite-to-json'); 15 | const sqlite3 = require('sqlite3'); 16 | const exporter = new SqliteToJson({ 17 | client: new sqlite3.Database('./mydb.sqlite3') 18 | }); 19 | ``` 20 | 21 | #### opts.client 22 | 23 | A [sqlite3](https://github.com/mapbox/node-sqlite3) client instance. 24 | 25 | Type: `sqlite3.Database` 26 | Default: `null` 27 | 28 | 29 | ### tables(cb) 30 | 31 | List all tables in the current database. 32 | 33 | Example: 34 | ```js 35 | const SqliteToJson = require('sqlite-to-json'); 36 | const sqlite3 = require('sqlite3'); 37 | const exporter = new SqliteToJson({ 38 | client: new sqlite3.Database('./mydb.sqlite3') 39 | }); 40 | exporter.tables(function (err, tables) { 41 | // all your table names here 42 | }); 43 | ``` 44 | 45 | ### save(table, dest, cb) 46 | 47 | Save the contents of a table to the specified output directory. 48 | 49 | Example: 50 | ```js 51 | const SqliteToJson = require('sqlite-to-json'); 52 | const sqlite3 = require('sqlite3'); 53 | const exporter = new SqliteToJson({ 54 | client: new sqlite3.Database('./mydb.sqlite3') 55 | }); 56 | exporter.save('table_name', './data/table_name.json', function (err) { 57 | // no error and you're good. 58 | }); 59 | ``` 60 | 61 | ### all(cb) 62 | 63 | Returns the entire database and all tables as a single object. 64 | 65 | Example: 66 | ```js 67 | const SqliteToJson = require('sqlite-to-json'); 68 | const sqlite3 = require('sqlite3'); 69 | const exporter = new SqliteToJson({ 70 | client: new sqlite3.Database('./mydb.sqlite3') 71 | }); 72 | exporter.all(function (err, all) { 73 | // all your data here 74 | }); 75 | ``` 76 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const path = require('path'); 3 | const mkdirp = require('mkdirp'); 4 | 5 | const SqliteToJson = module.exports = function SqliteToJson(opts) { 6 | opts = opts || {}; 7 | if (!opts.client) { 8 | throw new Error('No sqlite3 client provided.'); 9 | } 10 | this.client = opts.client; 11 | }; 12 | 13 | SqliteToJson.prototype.tables = function (cb) { 14 | var query = "SELECT name FROM sqlite_master WHERE type='table'"; 15 | this.client.all(query, function (err, tables) { 16 | if (err) { 17 | return cb(err); 18 | } 19 | cb(null, tables.map(function (result) { 20 | return result.name; 21 | })); 22 | }); 23 | }; 24 | 25 | SqliteToJson.prototype.save = function (table, dest, cb) { 26 | if (typeof cb !== 'function') { 27 | throw new Error('No callback specified.'); 28 | } 29 | if (!dest) { 30 | return cb(new Error('No destination file specified.')); 31 | } 32 | this._dataFor(table, function (dataErr, tableData) { 33 | if (dataErr) { 34 | cb(dataErr); 35 | } else { 36 | mkdirp(path.dirname(dest), function (mkdirErr) { 37 | if (mkdirErr) { 38 | cb(mkdirErr); 39 | } else { 40 | fs.writeFile(dest, JSON.stringify(tableData), function (writeErr) { 41 | if (writeErr) { 42 | cb(writeErr); 43 | } else { 44 | cb(null); 45 | } 46 | }); 47 | } 48 | }); 49 | } 50 | }); 51 | }; 52 | 53 | SqliteToJson.prototype.all = function(cb) { 54 | var self = this; 55 | this.tables(function (err, tables) { 56 | if (err) return cb(err); 57 | var ret = {}; 58 | function loop (i) { 59 | if (i === tables.length) cb(null, ret); 60 | else self._dataFor(tables[i], function (dataErr, tableData) { 61 | if (dataErr) cb(dataErr); 62 | else loop(i + 1, ret[tables[i]] = tableData); 63 | }) 64 | } 65 | loop(0); 66 | }) 67 | }; 68 | 69 | SqliteToJson.prototype._dataFor = function (table, cb) { 70 | // apparently you can't used named params for table names? 71 | this.client.all('SELECT * FROM '+table, cb); 72 | }; 73 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sqlite-to-json", 3 | "description": "Dump data from sqlite databases to JSON files easily.", 4 | "version": "0.1.3", 5 | "homepage": "https://github.com/tkellen/node-sqlite-to-json", 6 | "author": { 7 | "name": "Tyler Kellen", 8 | "url": "http://goingslowly.com/" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/tkellen/node-sqlite-to-json.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/tkellen/node-sqlite-to-json/issues" 16 | }, 17 | "licenses": [ 18 | { 19 | "type": "MIT", 20 | "url": "https://github.com/tkellen/node-sqlite-to-json/blob/master/LICENSE" 21 | } 22 | ], 23 | "main": "index.js", 24 | "engines": { 25 | "node": ">= 0.8" 26 | }, 27 | "scripts": { 28 | "test": "mocha -R spec test.js" 29 | }, 30 | "devDependencies": { 31 | "chai": "^1.9.1", 32 | "mocha": "^3.2.0", 33 | "rimraf": "^2.2.8", 34 | "sqlite3": "^3.1.8" 35 | }, 36 | "keywords": [ 37 | "sqlite to json" 38 | ], 39 | "dependencies": { 40 | "mkdirp": "^0.5.1" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | const expect = require('chai').expect; 4 | const sqlite3 = require('sqlite3'); 5 | const rimraf = require('rimraf').sync; 6 | 7 | const SqliteToJson = require('./'); 8 | 9 | const db = new sqlite3.Database(':memory:'); 10 | const exporter = new SqliteToJson({ 11 | client: db 12 | }); 13 | 14 | const data = [ 15 | { name: 'one' }, 16 | { name: 'two' }, 17 | { name: 'three' }, 18 | { name: 'four' }, 19 | { name: 'five' }, 20 | { name: 'six' }, 21 | { name: 'seven' }, 22 | { name: 'eight' }, 23 | { name: 'nine' }, 24 | { name: 'ten' } 25 | ]; 26 | 27 | describe('sqliteToJson', function () { 28 | 29 | before(function (done) { 30 | rimraf('./.tmp'); 31 | db.serialize(function() { 32 | db.run("CREATE TABLE numbers (name TEXT)"); 33 | var stmt = db.prepare("INSERT INTO numbers VALUES (?)"); 34 | data.forEach(function(row) { 35 | stmt.run(row.name); 36 | }); 37 | stmt.finalize(); 38 | done(); 39 | }); 40 | }); 41 | 42 | describe('#tables', function () { 43 | 44 | it('should callback with all tables in the specified database', function (done) { 45 | exporter.tables(function (err, tables) { 46 | expect(tables).to.deep.equal(['numbers']); 47 | done(); 48 | }); 49 | }); 50 | 51 | }); 52 | 53 | describe('#save', function () { 54 | 55 | it('should callback with error if no destination is specified', function (done) { 56 | var dest = './.tmp/numbers.json'; 57 | exporter.save('numbers', null, function(err) { 58 | expect(err).to.not.be.null; 59 | done(); 60 | }) 61 | }); 62 | 63 | it('should throw if no callback is specified', function () { 64 | expect(function () { 65 | exporter.save('numbers', 'file'); 66 | }).to.throw(/No callback/); 67 | }); 68 | 69 | it('should export a table in a database to a file', function (done) { 70 | var dest = './.tmp/numbers.json'; 71 | exporter.save('numbers', dest, function (err) { 72 | expect(JSON.parse(fs.readFileSync(dest))).to.deep.equal(data); 73 | done(err); 74 | }); 75 | }); 76 | 77 | }); 78 | 79 | describe('#all', function() { 80 | it('should export all data into a single object', function(done) { 81 | exporter.all(function (err, all) { 82 | expect(all && all.numbers).to.deep.equal(data); 83 | done(); 84 | }) 85 | }) 86 | }); 87 | 88 | }); 89 | --------------------------------------------------------------------------------