Skip to content

Commit

Permalink
add adapter loopback (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkie committed May 10, 2016
1 parent 49cca52 commit 13f4770
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- '4.0.0'
- '5.0.0'
- '6.0.0'
services:
- mongodb
20 changes: 20 additions & 0 deletions lib/adapters/loopback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const collections = [];
let context = null;

exports.setContext = function (val) {
context = val;
};

exports.put = function (coll, docs, cb) {
return context.models[coll].create(docs, cb);
};

exports.del = function (coll, cb) {
return context.models[coll].destroyAll(cb);
};

exports.close = function (cb) {
// TODO
};
4 changes: 2 additions & 2 deletions lib/adapters/mongo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict"
'use strict'

const typifyJSON = require('typify-json');
const exec = require('child_process').execSync;
Expand Down Expand Up @@ -50,7 +50,7 @@ exports.put = function (coll, docs, cb) {
}
};

exports.del = function (cb) {
exports.del = function (coll, cb) {
if (typeof cb !== 'function') {
exec(`mongo ${dburl} --eval "db.dropDatabase()"`);
} else {
Expand Down
22 changes: 18 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"use strict"
'use strict'

const path = require('path');
const once = require('once');
Expand All @@ -11,7 +11,13 @@ const fixturesDir = process.cwd() + '/test/fixtures';
const dirs = new Map();
var currentDir;

exports.use = function (name, cb) {
exports.use = function (name, context, cb) {
// normalize the arguments
if (typeof arguments[1] === 'function' &&
arguments.length === 2) {
cb = arguments[1];
context = false;
}
var files;
if (!dirs.has(name)) {
debug(`${name} does not have caches, parsing a new one`);
Expand All @@ -24,6 +30,9 @@ exports.use = function (name, cb) {
meta.name = path.basename(p, pmatch[1]);
if (typeof meta.adapter === 'string') {
meta.adapter = require('./adapters/' + meta.adapter);
if (typeof meta.adapter.setContext === 'function' && context) {
meta.adapter.setContext(context);
}
adapters.push(meta.adapter);
}
return meta;
Expand Down Expand Up @@ -51,9 +60,13 @@ exports.use = function (name, cb) {
debug('async mode');
// delete documents tasks from adapters
let deleteTasks = function () {
return Promise.race(
return Promise.all(
files.adapters.map(function (adapter) {
return promisify(adapter.del)();
return Promise.all(
files.map(function (meta) {
return promisify(adapter.del)(meta.name);
})
);
})
);
};
Expand Down Expand Up @@ -87,6 +100,7 @@ exports.use = function (name, cb) {
debug('closing connections');
return closeTasks();
}).catch(function (err) {
console.error(err);
cb(err);
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fibula",
"version": "1.3.4",
"version": "1.4.0",
"description": "A **fixtures** framework for Node.js",
"main": "lib/index.js",
"scripts": {
Expand Down

0 comments on commit 13f4770

Please sign in to comment.