Last active
May 25, 2022 08:45
-
-
Save Tymek/3db1f7e849c0c62c712317dc6a4a63ad to your computer and use it in GitHub Desktop.
Unleash features - seed db
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require("readline"); | |
const { default: faker } = require("@faker-js/faker"); | |
require("isomorphic-fetch"); | |
const API_URL = "http://localhost:4242/api/admin/projects/default/features"; | |
const API_KEY = "<YOUR_ADMIN_TOKEN>"; | |
const pastDate = (min = 0) => new Date(Date.now() - 1000 * 60 * min); | |
const FgGreen = "\x1b[32m"; | |
const FgBlack = "\x1b[30m"; | |
const BgCyan = "\x1b[46m"; | |
const Reset = "\x1b[0m"; | |
const insertFeature = async () => { | |
const name = `seed-${faker.random.alpha(6)}`; | |
const created_at = faker.date.past(3 /* years */).toISOString(); | |
const type = faker.helpers.arrayElement([ | |
"experiment", | |
"release", | |
"operational", | |
"kill-switch", | |
"permission", | |
]); | |
const description = faker.helpers.arrayElement([ | |
"", | |
faker.random.words(), | |
faker.hacker.phrase(), | |
faker.lorem.sentence(), | |
]); | |
const stale = | |
faker.helpers.maybe(() => "true", { probability: 0.05 }) || "false"; | |
const seen = faker.helpers.maybe( | |
() => | |
faker.helpers.arrayElement([ | |
faker.date.between(created_at, new Date()), // probably long ago | |
faker.date.recent(1), // today | |
faker.date.recent(31), // this month | |
faker.date.between(pastDate(0.5), pastDate()), // almost now | |
faker.date.between(pastDate(16 * 60), pastDate(1)), // within 16h | |
]), | |
{ probability: 0.95 } | |
); | |
const last_seen_at = seen ? seen.toISOString() : null; | |
const data = { | |
created_at, | |
last_seen_at, | |
type, | |
name, | |
description, | |
impressionData: false, | |
stale, | |
}; | |
// console.log(data); | |
await fetch(API_URL, { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: API_KEY, | |
}, | |
body: JSON.stringify(data), | |
}); | |
console.log(`${FgGreen}Inserted feature:${Reset} ${name}`); | |
}; | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
}); | |
console.log(`\n${FgBlack}${BgCyan} 🌱 SEEDING FEATURES 🌱 ${Reset}\n`); | |
rl.question("How many features do you need? ", async (length) => { | |
const num = Number(length); | |
if (!Number.isInteger(num) || num <= 0) { | |
throw new Error("Please enter a valid number"); | |
} | |
await Promise.allSettled(Array.from({ length }, () => insertFeature())); | |
rl.close(); | |
}); | |
rl.on("close", function () { | |
console.log("\n 🌱 DONE"); | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment