it('should be able to specify custom list of stopwords', function(done) { db = levelQuery(db); db.query.use(fulltextEngine({ stopwords: ['about', 'after', 'all', 'also'] })); db.ensureIndex('doc', 'fulltext', fulltextEngine.index()); db.batch(testData(), doQuery); function doQuery(err) { if (err) return done(err); var hits = 0; db.query('doc', '2') .on('data', function (data) { hits++; }) .on('stats', function (stats) { expect(stats).to.deep.equals( { indexHits: 2, dataHits: 2, matchHits: 2 }); }) .on('end', function () { expect(hits).to.deep.equals(2); done(); }); } });
it('should be able to query an irregular json database', function(done) { var server; var users = levelQuery(db.sublevel('packages')); users.query.use(jsonqueryEngine()); users.batch(jsonTestData.leveldata, launchServer); function launchServer() { server = sqlDriver(db, 3307, undefined, undefined, doQuery); } function doQuery() { var clientConn = mysql.createConnection({ user: 'root', database: 'triggertest', host:'localhost', password: 'iluvhslim2', port: 3307 }); clientConn.query('select * from packages where author = "TJ Holowaychuk <tj@vision-media.ca>"', function (err, rows, fields) { if (err) return done(err); console.log(rows); server._server.close(); done(); }); } });
it('should be able to specify columns', function(done) { var server; var users = levelQuery(db.sublevel('users')); users.query.use(jsonqueryEngine()); users.batch(batchData(), launchServer); function launchServer() { server = sqlDriver(db, 3307, undefined, undefined, doQuery); } function doQuery() { var clientConn = mysql.createConnection({ user: 'root', database: 'triggertest', host:'localhost', password: 'iluvhslim2', port: 3307 }); clientConn.query('select name, num from users', function (err, rows, fields) { if (err) return done(err); console.log(rows); server._server.close(); done(); }); } });
beforeEach(function(done) { rimraf.sync(dbPath); db = levelup(dbPath, { valueEncoding: 'json' }); db = levelQuery(db); testData = [ { name: 'Eugene', num: 42, awesome: 'goodbye', x: 99 }, { name: 'Susan', num: 43, awesome: 'blah' }, { name: 'Edmund', num: 88, awesome: true, car: { make: 'Toyota', model: 'Corolla' } } ]; done(); });
beforeEach(function(done) { rimraf.sync(dbPath); db = levelup(dbPath, { valueEncoding: 'json' }); db = levelQuery(db); testData = [ { name: 'Eugene', num: 42 }, { name: 'Susan', num: 43 }, { name: 'Edmund', num: 88 } ]; done(); });
test('open', function(t) { t.timeoutAfter(5000) db = queryEngine(level(dbPath, { valueEncoding: 'json' })) db.query.use(jsonQueryEngine()) promisify(db) promisify(db, 'query', { type: 'readable' }) Q.all(Object.keys(SAMPLE_DATA).map(function(key) { return db.put(key, SAMPLE_DATA[key]) })) .done(function() { t.end() }) })
constructor(path, cols, back) { const opts = {valueEncoding: 'json'} if (back) { opts.db = back } else { opts.db = require('leveldown') } if (path) { this._db = sublevel(levelup(path, opts)) } else { this._db = sublevel(levelup(opts)) } this.cols = {} for (let name in cols) { let db = this._db.sublevel(name) db = levelQuery(db) db.query.use(jsonQueryEngine()) cols[name].forEach(attr => db.ensureIndex(attr)) this.cols[name] = db } }
it('should be able to query against sublevels', function(done) { var users = levelQuery(db.sublevel('users')); users.query.use(jsonqueryEngine()); var count = 0; users.query({ $or: [ { name: 'Susan' }, { name: 'Edmund' } ] }) .pipe(through( function (data) { count++; }, check)); function check() { expect(count).to.equal(0); addSomeData(); } function addSomeData() { var differentData = batchData() .map(function (data) { data.value.num *= 2; return data; }); users.batch(differentData, checkDiffData); } function checkDiffData() { var count = 0; users.query({ $or: [ { name: 'Susan' }, { name: 'Edmund' } ] }) .pipe(through( function (data) { count++; }, function () { expect(count).to.equal(2); done(); })); } });
it('should be able to search a document with no indexes', function(done) { db = levelQuery(db); db.query.use(fulltextEngine(true)); db.batch(testData(), doQuery); function doQuery(err) { if (err) return done(err); var hits = 0; db.query('doc', 'fear word') .on('data', function (data) { hits++; }) .on('stats', function (stats) { expect(stats).to.deep.equals( { indexHits: 0, dataHits: 100, matchHits: 3 }); }) .on('end', function () { expect(hits).to.deep.equals(3); done(); }); } });
it('should be able to search with ors', function(done) { db = levelQuery(db); db.query.use(fulltextEngine()); db.ensureIndex('doc', 'fulltext', fulltextEngine.index()); db.batch(testData(), doQuery); function doQuery(err) { if (err) return done(err); var hits = 0; db.query('doc', 'fear word', 'or') .on('data', function (data) { hits++; }) .on('stats', function (stats) { expect(stats).to.deep.equals( { indexHits: 26, dataHits: 24, matchHits: 24 }); }) .on('end', function () { expect(hits).to.deep.equals(24); done(); }); } });
it('should be able to search for a stopword and not hang', function(done) { db = levelQuery(db); db.query.use(fulltextEngine()); db.ensureIndex('doc', 'fulltext', fulltextEngine.index()); db.batch(testData(), doQuery); function doQuery(err) { if (err) return done(err); var hits = 0; db.query('doc', 'while') .on('data', function (data) { hits++; }) .on('stats', function (stats) { expect(stats).to.deep.equals( { indexHits: 0, dataHits: 0, matchHits: 0 }); }) .on('end', function () { expect(hits).to.deep.equals(0); done(); }); } });
respond(res); }) }); // ======================================================== function createFakeData(q) { q.createNode({ type: 'node' }, function(err, node) { console.log('Got: ' + util.inspect(node)); }); }; var db = levelQuery(levelup('meshnode.db', {encoding: 'json'})); db.query.use(jsonqueryEngine()); var q = new query.Query(db); q.init(true, function(err) { if(err) { console.log("Query error: " + err); return; } // createFakeData(q); var port = 3000; console.log("Starting on port " + port); app.listen(port);