Skip to content

Commit

Permalink
use required instead of import
Browse files Browse the repository at this point in the history
  • Loading branch information
cayasso committed Nov 22, 2017
1 parent 45ad353 commit a9e38e7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
exports.default = require('./lib').default

module.exports = exports.default
module.exports = require('./lib')
12 changes: 7 additions & 5 deletions src/filter.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict'

import Emitter from 'eventemitter3'
import dbg from 'debug'
import { events } from './'
const Emitter = require('eventemitter3')
const dbg = require('debug')
const { events } = require('./')

export function regex(pattern) {
function regex(pattern) {
pattern = pattern || '*'
pattern = pattern.replace(/[*]/g, '(.*?)')
return new RegExp(`^${pattern}$`, 'i')
}

export default (ns, oplog) => {
module.exports = (ns, oplog) => {
const debug = dbg('mongo-oplog:filter')
const filter = new Emitter()
const re = regex(ns)
Expand All @@ -34,3 +34,5 @@ export default (ns, oplog) => {

return Object.assign(filter, { destroy })
}

module.exports.regex = regex
19 changes: 11 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict'

import 'babel-polyfill'
import Emitter from 'eventemitter3'
import { MongoClient } from 'mongodb'
import createDebug from 'debug'
import createFilter from './filter'
import createStream from './stream'
require('babel-polyfill')
const Emitter = require('eventemitter3')
const { MongoClient } = require('mongodb')
const createDebug = require('debug')
const createFilter = require('./filter')
const createStream = require('./stream')

const MONGO_URI = 'mongodb://127.0.0.1:27017/local'
const debug = createDebug('mongo-oplog')
export const events = {

const events = {
i: 'insert',
u: 'update',
d: 'delete'
Expand All @@ -29,7 +30,7 @@ const toCb = fn => cb => {
}
}

export default (uri, options = {}) => {
module.exports = (uri, options = {}) => {
let db
let stream
let connected = false
Expand Down Expand Up @@ -119,3 +120,5 @@ export default (uri, options = {}) => {
destroy: toCb(destroy)
})
}

module.exports.events = events
6 changes: 3 additions & 3 deletions src/stream.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

import { Timestamp } from 'mongodb'
import { regex } from './filter'
const { Timestamp } = require('mongodb')
const { regex } = require('./filter')

export default async ({ db, ns, ts, coll }) => {
module.exports = async ({ db, ns, ts, coll }) => {
if (!db) throw new Error('Mongo db is missing.')

const query = {}
Expand Down
66 changes: 32 additions & 34 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict';
'use strict'

/**
* Module dependencies.
*/
const should = require('should')
const { MongoClient } = require('mongodb')
const MongoOplog = require('../src/index')

var should = require('should');
var MongoClient = require('mongodb').MongoClient;
var MongoOplog = require('../src/index').default;
var oplog, db, opdb;
var conn = {
const conn = {
mongo: 'mongodb://127.0.0.1:27017/optest',
oplog: 'mongodb://127.0.0.1:27017/local',
error: 'mongodb://127.0.0.1:8888/error'
};
}

describe('mongo-oplog', function () {
let db
let opdb

describe('mongo-oplog', function () {
before(function (done) {
MongoClient.connect(conn.mongo, function (err, database) {
if (err) return done(err);
Expand All @@ -25,33 +23,33 @@ describe('mongo-oplog', function () {
});

it('should be a function', function () {
MongoOplog.should.be.a.Function;
should(MongoOplog).be.a.Function;
});

it('should have required methods', function (done) {
var oplog = MongoOplog(opdb);
oplog.tail.should.be.a.Function;
oplog.stop.should.be.a.Function;
oplog.filter.should.be.a.Function;
oplog.destroy.should.be.a.Function;
var oplog = MongoOplog(opdb)
should(oplog.tail).be.a.Function;
should(oplog.stop).be.a.Function;
should(oplog.filter).be.a.Function;
should(oplog.destroy).be.a.Function;
done();
});

it('should accept mongodb object as connection', function () {
MongoClient.connect(conn.oplog, function (err, db) {
if (err) return done(err);
var oplog = MongoOplog(db)
oplog.db.should.eql(db);
should(oplog.db).eql(db);
});
});

it('should emit `op` event', function (done) {
var coll = db.collection('a');
var oplog = MongoOplog(conn.oplog, { ns: 'optest.a' });
oplog.on('op', function (doc) {
doc.op.should.be.eql('i');
doc.o.n.should.be.eql('JB');
doc.o.c.should.be.eql(1);
should(doc.op).be.eql('i');
should(doc.o.n).be.eql('JB');
should(doc.o.c).be.eql(1);
done();
});
oplog.tail(function (err) {
Expand All @@ -66,9 +64,9 @@ describe('mongo-oplog', function () {
var coll = db.collection('b');
var oplog = MongoOplog(conn.oplog, { ns: 'optest.b' });
oplog.on('insert', function (doc) {
doc.op.should.be.eql('i');
doc.o.n.should.be.eql('JBL');
doc.o.c.should.be.eql(1);
should(doc.op).be.eql('i');
should(doc.o.n).be.eql('JBL');
should(doc.o.c).be.eql(1);
done();
});
oplog.tail(function (err) {
Expand All @@ -83,9 +81,9 @@ describe('mongo-oplog', function () {
var coll = db.collection('c');
var oplog = MongoOplog(conn.oplog, { ns: 'optest.c' });
oplog.on('update', function (doc) {
doc.op.should.be.eql('u');
doc.o.$set.n.should.be.eql('US');
doc.o.$set.c.should.be.eql(7);
should(doc.op).be.eql('u');
should(doc.o.$set.n).be.eql('US');
should(doc.o.$set.c).be.eql(7);
done();
});
oplog.tail(function (err) {
Expand All @@ -109,8 +107,8 @@ describe('mongo-oplog', function () {
if (err) return done(err);
var id = (doc.ops || doc)[0]._id;
oplog.on('delete', function (doc) {
doc.op.should.be.eql('d');
doc.o._id.should.be.eql(id);
should(doc.op).be.eql('d');
should(doc.o._id).be.eql(id);
done();
});
coll.remove({ n: 'PM', c: 4 }, function (err) {
Expand All @@ -133,7 +131,7 @@ describe('mongo-oplog', function () {
var oplog = MongoOplog(conn.error)
oplog.tail()
oplog.on('error', function (err) {
err.should.be.an.Error;
should(err).be.an.Error;
done();
});
});
Expand All @@ -146,7 +144,7 @@ describe('mongo-oplog', function () {
var filter = oplog.filter('*.e1');

filter.on('op', function(doc) {
doc.o.n.should.be.eql('L1');
should(doc.o.n).be.eql('L1');
done();
});

Expand Down Expand Up @@ -189,7 +187,7 @@ describe('mongo-oplog', function () {
var f2 = db.collection('f2');
var oplog = MongoOplog(conn.oplog, { ns: '*.f1' });
oplog.on('op', function (doc) {
doc.o.n.should.be.eql('L2');
should(doc.o.n).be.eql('L2');
done();
});
oplog.tail(function (err) {
Expand Down Expand Up @@ -322,7 +320,7 @@ describe('mongo-oplog', function () {
var oplog = MongoOplog(conn.oplog, { ns: 'optest.i' });
oplog.on('op', function (doc) {
v[doc.o.c] = 1;
Object.keys(v).length.should.be.equal(++c);
should(Object.keys(v).length).be.equal(++c);
if (6 === c) done();
else if (c > 6) done('Not valid')
});
Expand Down Expand Up @@ -358,7 +356,7 @@ describe('mongo-oplog', function () {
var valueSize = 0
oplog.on('op', function (doc) {
v[doc.o.c] = 1;
Object.keys(v).length.should.be.equal(++c);
should(Object.keys(v).length).be.equal(++c);
if (6 === c) done();
else if (c > 6) done('Not valid')
});
Expand Down

0 comments on commit a9e38e7

Please sign in to comment.