Skip to content

Instantly share code, notes, and snippets.

@NetOperatorWibby NetOperatorWibby/index.js Secret
Last active Jan 28, 2020

Embed
What would you like to do?
Example data migration script for MongoDB to RethinkDB. Blog post: https://blog.webb.page/2020/migrating-from-mongo-to-rethink
"use strict";
// N A T I V E
const { readFileSync, writeFile } = require("fs");
// U T I L S
// These files are fresh from your MongoDB export
const sourcesFilePath = "./raw/sources-2020-01-24.json";
const usersFilePath = "./raw/users-2020-01-24.json";
const visitsFilePath = "./raw/visits-2020-01-24.json";
// P R O G R A M
processSources();
processUsers();
processVisits();
// H E L P E R S
function processSources() {
const completedMigration = [];
const fileData = JSON.parse(readFileSync(sourcesFilePath, "utf8"));
fileData.forEach(record => {
const {
hosts,
name
} = record;
completedMigration.push({
created: new Date(),
hosts,
id: record._id["$oid"],
name,
owner: record.user["$oid"],
timezone: "BEHIND_UTC_5",
updated: new Date()
});
});
writeFile("./migrations/sources-migrated.json", JSON.stringify(completedMigration), err => {
if (err)
throw err;
console.log("✅ Source migration complete!");
});
}
function processUsers() {
const completedMigration = [];
const fileData = JSON.parse(readFileSync(usersFilePath, "utf8"));
fileData.forEach(record => {
const {
email,
language,
loginMethod
} = record;
completedMigration.push({
company: null,
created: new Date(),
email,
id: record._id["$oid"],
language,
login: record.email.split("@")[0],
loginMethod,
name: record.nameFirst + " " + record.nameLast,
role: record.admin ? "Admin" : "User",
timezone: "BEHIND_UTC_5",
type: "User",
updated: new Date()
});
});
writeFile("./migrations/users-migrated.json", JSON.stringify(completedMigration), err => {
if (err)
throw err;
console.log("✅ User migration complete!");
});
}
function processVisits() {
const completedMigration = [];
const fileData = JSON.parse(readFileSync(visitsFilePath, "utf8"));
fileData.forEach(record => {
const {
host,
hostname,
httpMethod,
ip,
language,
protocol,
referrer,
siteId,
timestamp,
ua,
url
} = record;
if (host.includes("localhost") || hostname.includes("localhost"))
return; // No false visits
completedMigration.push({
created: new Date(),
endpoint: url,
host,
hostname,
httpMethod,
id: record._id["$oid"],
ip,
language,
protocol,
referrer: referrer || null,
source: record.siteId,
tags: [],
timestamp: new Date(timestamp), // RethinkDB expects Strings, not Numbers
ua: {
browser: record.ua.browser,
os: record.ua.os,
platform: determinePlatform(record.ua.os),
source: record.ua.source
}
});
function determinePlatform(suppliedString) {
if (suppliedString) {
if (suppliedString.toLowerCase().includes("mac"))
return "macOS";
if (suppliedString.toLowerCase().includes("windows"))
return "Windows";
}
return "Linux";
}
});
writeFile("./migrations/visits-migrated.json", JSON.stringify(completedMigration), err => {
if (err)
throw err;
console.log("✅ Visit migration complete!");
});
}
@NetOperatorWibby

This comment has been minimized.

Copy link
Owner Author

NetOperatorWibby commented Jan 28, 2020

Make sure the paths to your raw exports are correct and the export folder exists. I forget it writeFile auto-creates folders for you.

Run this script with node index.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.