├── config.json ├── example.js ├── index.js ├── package.json └── readme.md /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "couch" : { 3 | "host" : "127.0.0.1", 4 | "port" : "5984", 5 | "database" : "offline" 6 | }, 7 | 8 | "mySQL" : { 9 | "host" : "127.0.0.1", 10 | "port" : "", 11 | "user" : "root", 12 | "password" : "", 13 | "database" : "offline" 14 | }, 15 | 16 | "queries" : { 17 | "insert" : "insert into post set ?", 18 | "update" : "", 19 | "delete" : "delete from post where id = ?" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var converter = require('./'); 2 | var cvr = converter(); 3 | 4 | cvr.connect(); 5 | 6 | cvr.on('created', function (change) { 7 | var self = this; 8 | var query = this.config.queries.insert; 9 | this.database.get(change.id, function (err, res) { 10 | if (err) throw err; 11 | var doc = { id : res._id, title : res.title }; 12 | self.mysql.query(query, doc, function (err) { 13 | // prevents dups error. 14 | }); 15 | }); 16 | }); 17 | 18 | cvr.on('deleted', function (change) { 19 | var query = this.config.queries.delete; 20 | this.mysql.query(query, change.id); 21 | }); 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var follow = require('follow'); 2 | var nano = require('nano'); 3 | var mysql = require('mysql'); 4 | var util = require('util'); 5 | var EventEmitter = require('events').EventEmitter; 6 | var CONFIG = require('./config.json'); 7 | 8 | function Converter (config) { 9 | if (!(this instanceof Converter)) return new Converter(config); 10 | this.config = config || CONFIG; 11 | this.couch = this.parseCouchDB(); 12 | this.mysql = this.parseMySQL(); 13 | this.database = require('nano')(this.couch); 14 | } 15 | 16 | util.inherits(Converter, EventEmitter); 17 | 18 | Converter.prototype.parseCouchDB = function () { 19 | return 'http://' 20 | + this.config.couch.host + ':' 21 | + this.config.couch.port + '/' 22 | + this.config.couch.database; 23 | }; 24 | 25 | Converter.prototype.parseMySQL = function () { 26 | return mysql.createConnection({ 27 | host : this.config.mySQL.host, 28 | user : this.config.mySQL.user, 29 | password : this.config.mySQL.password, 30 | database : this.config.mySQL.database 31 | }); 32 | }; 33 | 34 | Converter.prototype.connect = function () { 35 | var self = this; 36 | this.mysql.connect(function (err) { 37 | if (err) throw err; 38 | self.listen(); 39 | }); 40 | }; 41 | 42 | Converter.prototype.listen = function () { 43 | var self = this; 44 | follow(this.couch, function (err, change) { 45 | if (err) throw err; 46 | self.handle(change); 47 | }); 48 | }; 49 | 50 | Converter.prototype.handle = function (change) { 51 | if (change.deleted) { 52 | this.sync(change, 'deleted'); 53 | } else if (change.changes[0].rev[0] != '1') { 54 | this.sync(change, 'updated'); 55 | } else { 56 | this.sync(change, 'created'); 57 | } 58 | }; 59 | 60 | Converter.prototype.sync = function (change, status) { 61 | this.emit(status, change); 62 | }; 63 | 64 | module.exports = Converter; 65 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "couchdb-to-mysql", 3 | "version": "0.0.2", 4 | "description": "sync between couchdb and mysql", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/ordepdev/converter.git" 8 | }, 9 | "main": "index.js", 10 | "keywords": [ 11 | "sync", 12 | "couchdb", 13 | "mysql" 14 | ], 15 | "author": "ordepdev", 16 | "license": "MIT", 17 | "dependencies": { 18 | "follow": "^0.11.4", 19 | "nano": "^6.0.2", 20 | "mysql": "^2.5.4" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | #### converter 2 | > tiny tool to sync data between CouchDB and MySQL 3 | 4 | ##### why? 5 | well, i presented CouchDB to a person and the first question was... "can you sync with MySQL?" 6 | 7 | > "Can PouchDB sync with MongoDB/MySQL/my current non-CouchDB database? No." - CouchDB FAQ. 8 | 9 | Whith this tool... Yes. 10 | 11 | ##### how? 12 | converter listen for CouchDB changes and reflects them on MySQL. 13 | 14 | ##### example 15 | 16 | ```js 17 | var converter = require('couchdb-to-mysql'); 18 | var cvr = converter(); 19 | cvr.connect(); 20 | cvr.on('created', function (change) { 21 | // replicate changes on mysql 22 | }); 23 | ``` 24 | 25 | ##### methods 26 | 27 | ```js 28 | var converter = require('couchdb-to-mysql') 29 | ``` 30 | 31 | ###### var cvr = converter(config={}) 32 | 33 | Optionaly pass in a `config`: 34 | * `config.couch.host` 35 | * `config.couch.port` 36 | * `config.couch.database` 37 | * `config.mySQL.host` 38 | * `config.mySQL.port` 39 | * `config.mySQL.user` 40 | * `config.mySQL.password` 41 | * `config.mySQL.database` 42 | 43 | ##### events 44 | 45 | ###### cvr.on('created', function (change) {}) 46 | Every time a document is created, a `created` event fires. 47 | 48 | ###### cvr.on('updated', function (change) {}) 49 | Every time a document is updated, a `updated` event fires. 50 | 51 | ###### cvr.on('deleted', function (change) {}) 52 | Every time a document is deleted, a `deleted` event fires. 53 | --------------------------------------------------------------------------------