mongorai
Version:
Light MongoDB client for the web. Minimalistic UI used React with minimum dependencies.
60 lines (59 loc) • 1.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const mongodb_1 = require("mongodb");
class JsonEncoder {
static encode(obj) {
if (obj instanceof mongodb_1.ObjectId) {
return {
$type: 'ObjectId',
$value: obj.toHexString(),
$date: obj.getTimestamp().getTime()
};
}
if (obj instanceof Date) {
return {
$type: 'Date',
$value: obj.toISOString()
};
}
if (obj instanceof RegExp) {
return {
$type: 'RegExp',
$value: {
$pattern: obj.source,
$flags: obj.flags
}
};
}
if (Array.isArray(obj)) {
return [...obj.map(JsonEncoder.encode)];
}
if (obj && typeof obj === 'object') {
for (const [key, value] of Object.entries(obj)) {
obj[key] = JsonEncoder.encode(value);
}
}
return obj;
}
static decode(obj) {
if (obj && obj.$type === 'ObjectId') {
return new mongodb_1.ObjectId(obj.$value);
}
if (obj && obj.$type === "Date") {
return new Date(obj.$value);
}
if (obj && obj.$type === "RegExp") {
return new RegExp(obj.$value.$pattern, obj.$value.$flags);
}
if (Array.isArray(obj)) {
return [...obj.map(JsonEncoder.decode)];
}
if (obj && typeof obj === 'object') {
for (const [key, value] of Object.entries(obj)) {
obj[key] = JsonEncoder.decode(value);
}
}
return obj;
}
}
exports.default = JsonEncoder;