Skip to content

Instantly share code, notes, and snippets.

@youzysu
Last active May 13, 2023 12:21
Show Gist options
  • Save youzysu/b3cab9dddc3c3475fd8f4a3598fe728e to your computer and use it in GitHub Desktop.
Save youzysu/b3cab9dddc3c3475fd8f4a3598fe728e to your computer and use it in GitHub Desktop.
CS14. 데이터베이스 설치

CS14. 데이터베이스 설치

image

image

const createCsvWriter = require('csv-writer').createObjectCsvWriter;
class CsvFileMaker {
constructor(data) {
this.data = data;
this.csvWriter = createCsvWriter({
path: 'file.csv',
header: [
{ id: 'id', title: 'ID' },
{ id: 'nickname', title: 'TITLE' },
{ id: 'lastVisit', title: 'LAST_VISIT' },
{ id: 'money', title: 'MONEY' },
],
});
this.run(this.data);
}
run(data) {
this.csvWriter
.writeRecords(data)
.then(() => console.log('CSV file has been created!'));
}
}
module.exports = CsvFileMaker;
const mysql = require('mysql');
const RandomDataMaker = require('./RandomDataMaker');
const CsvFileMaker = require('./CsvFileMaker');
const DATA_COUNT = 1000000;
const data = new RandomDataMaker(DATA_COUNT).data;
new CsvFileMaker(data);
const connection = mysql.createConnection({
host: 'localhost',
user: 'zoey',
port: '3306',
password: 'zoey!',
database: 'zoeydb',
});
connection.query(
`LOAD DATA LOCAL INFILE '/Users/youzysu/Codesquad/CS16/CS14/file.csv'
INTO TABLE user_log
FIELDS TERMINATED BY ','
IGNORE 1 ROWS`,
(error, results, fields) => {
if (error) throw error;
console.log('Data loaded from CSV file');
}
);
connection.end();
{
"name": "cs14",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"array-uniq": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/array-uniq/-/array-uniq-1.0.2.tgz",
"integrity": "sha512-GVYjmpL05al4dNlKJm53mKE4w9OOLiuVHWorsIA3YVz+Hu0hcn6PtE3Ydl0EqU7v+7ABC4mjjWsnLUxbpno+CA=="
},
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"core-util-is": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"csv-writer": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/csv-writer/-/csv-writer-1.6.0.tgz",
"integrity": "sha512-NOx7YDFWEsM/fTRAJjRpPp8t+MKRVvniAg9wQlUKx20MFrPs73WLJhFf5iteqrxNYnsy924K3Iroh3yNHeYd2g=="
},
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ=="
},
"mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"requires": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
}
},
"object-assign": {
"version": "4.1.1",
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
"integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg=="
},
"process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
},
"random-normal": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/random-normal/-/random-normal-1.0.0.tgz",
"integrity": "sha512-aursKdfpfdHPpnabQVx6OUiuFpaBLl+6A/8k9u+ApMspjW2EIhZFjX8hNH9LZjpDyo0aTDfBCIgLWQXrsRhd2Q==",
"requires": {
"object-assign": "^4.0.1"
}
},
"random-words": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/random-words/-/random-words-1.3.0.tgz",
"integrity": "sha512-brwCGe+DN9DqZrAQVNj1Tct1Lody6GrYL/7uei5wfjeQdacFyFd2h/51LNlOoBMzIKMS9xohuL4+wlF/z1g/xg==",
"requires": {
"seedrandom": "^3.0.5"
}
},
"randombytes": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.0.3.tgz",
"integrity": "sha512-lDVjxQQFoCG1jcrP06LNo2lbWp4QTShEXnhActFBwYuHprllQV6VUpwreApsYqCgD+N1mHoqJ/BI/4eV4R2GYg=="
},
"randomstring": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/randomstring/-/randomstring-1.2.3.tgz",
"integrity": "sha512-3dEFySepTzp2CvH6W/ASYGguPPveBuz5MpZ7MuoUkoVehmyNl9+F9c9GFVrz2QPbM9NXTIHGcmJDY/3j4677kQ==",
"requires": {
"array-uniq": "1.0.2",
"randombytes": "2.0.3"
}
},
"readable-stream": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"seedrandom": {
"version": "3.0.5",
"resolved": "https://registry.npmjs.org/seedrandom/-/seedrandom-3.0.5.tgz",
"integrity": "sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg=="
},
"sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha512-ooAzh/7dxIG5+uDik1z/Rd1vli0+38izZhGzSa34FwR7IbelPWCCKSNIl8jlL/F7ERvy8CB2jNeM1E9i9mXMAQ=="
},
"string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"requires": {
"safe-buffer": "~5.1.0"
}
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw=="
}
}
}
{
"name": "cs14",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/b3cab9dddc3c3475fd8f4a3598fe728e.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/b3cab9dddc3c3475fd8f4a3598fe728e"
},
"homepage": "https://gist.github.com/b3cab9dddc3c3475fd8f4a3598fe728e",
"dependencies": {
"csv-writer": "^1.6.0",
"mysql": "^2.18.1",
"random-normal": "^1.0.0",
"random-words": "^1.3.0",
"randomstring": "^1.2.3"
}
}
class RandomDataMaker {
#data;
constructor(dataCount) {
this.dataCount = dataCount;
this.randomstringMaker = require('randomstring');
this.random100Words = require('random-words')(100);
this.#data = Array(dataCount)
.fill([])
.map((v, i) => {
const id = i + 1;
const nickname = this.generateNickname(
this.random100Words,
this.randomstringMaker
);
const lastVisit = this.generateLastVisit();
const money = this.generateRandomValue(1, 100000);
return { id, nickname, lastVisit, money };
});
}
generateNickname(words, randomstringMaker) {
const randomWord = words[Math.floor(Math.random() * words.length)];
const randomString = randomstringMaker.generate(3);
const randomNumber = Math.floor(Math.random() * 9000 + 1000);
return `${randomWord}_${randomString}_${randomNumber}`;
}
generateRandomValue(min, max) {
const randomNormal = require('random-normal');
const mean = (min + max) / 2;
const standardDeviation = (max - mean) / 3;
return Math.floor(randomNormal({ mean: mean, dev: standardDeviation }));
}
generateLastVisit() {
const start = new Date();
start.setMonth(start.getMonth() - 1);
const startTime = start.getTime();
const end = Date.now();
const lastVisit = new Date(startTime + Math.random() * (end - startTime));
return lastVisit.toISOString().slice(0, 19).replace('T', ' ');
}
get data() {
return this.#data;
}
}
module.exports = RandomDataMaker;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment