@floogulinc/cypress-mongo-seeder
Version:
A cypress plugin to populate a mongo db from json files
60 lines (59 loc) • 2.15 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var mongodb_1 = __importDefault(require("mongodb"));
exports.json2mongo = function (obj) {
var key;
var val;
for (key in obj) {
if (!obj.hasOwnProperty(key)) {
continue;
}
val = obj[key];
switch (key) {
case '$binary':
case '$type':
return new mongodb_1.default.Binary(obj.$binary, obj.$type);
case '$date':
if (!val) {
return new Date();
}
return new Date(val);
case '$decimal128':
return new mongodb_1.default.Decimal128(Buffer.from(val));
case '$timestamp':
return new mongodb_1.default.Timestamp(val.t, val.i);
case '$regex':
case '$options':
return new RegExp(obj.$regex, obj.$options);
case '$oid':
return new mongodb_1.default.ObjectID(val);
case '$ref':
case '$id':
case '$db':
var id = obj.$id._bsontype
? obj.$id
: new mongodb_1.default.ObjectID(obj.$id.$oid);
return new mongodb_1.default.DBRef(obj.$ref, id, obj.$db);
case '$undefined':
return undefined;
case '$minKey':
return new mongodb_1.default.MinKey();
case '$maxKey':
return new mongodb_1.default.MaxKey();
case '$numberLong':
if (typeof val === 'string') {
return mongodb_1.default.Long.fromString(val);
}
else {
return mongodb_1.default.Long.fromNumber(val);
}
}
if (typeof val === 'object') {
obj[key] = exports.json2mongo(val);
}
}
return obj;
};