Skip to content

Instantly share code, notes, and snippets.

@NetOperatorWibby

NetOperatorWibby/index.js Secret

Last active Apr 9, 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 { existsSync, mkdirSync, readFileSync, writeFile } = require("fs");
const { join } = require("path");
// U T I L S
const exportDirectory = join(process.cwd(), "migrations");
// 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
if (!existsSync(exportDirectory))
mkdirSync("./migrations");
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, null, 2), 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, null, 2), 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, null, 2), err => {
if (err)
throw err;
console.log("✅ Visit migration complete!");
});
}
@NetOperatorWibby

This comment has been minimized.

Copy link
Owner Author

@NetOperatorWibby NetOperatorWibby commented Jan 28, 2020

Make sure the paths to your raw exports are correct. The export folder will be created 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.