-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b98e30d
commit b2241b3
Showing
8 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
username,name | ||
berviantoleo,Bervianto Leo Pratama |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import fs from 'fs'; | ||
import { parse } from 'csv-parse/sync'; | ||
import mongoose from 'mongoose'; | ||
import dotenv from 'dotenv'; | ||
import logger from '../logger'; | ||
import name from '../models/name'; | ||
|
||
dotenv.config(); | ||
|
||
async function loadData() { | ||
const readSample = fs.readFileSync('sample.csv'); | ||
const records = parse(readSample, { | ||
columns: true, | ||
}); | ||
if (Array.isArray(records)) { | ||
for (const record of records) { | ||
logger.info(`Load: ${JSON.stringify(record)}`); | ||
const result = await name.findOneAndUpdate({username: record.username}, { | ||
username: record.username, | ||
name: record.name, | ||
createdAt: Date.now() | ||
}, { | ||
upsert: true, | ||
returnNewDocument: true | ||
}); | ||
logger.info(`Saved: ${result?._id}. Result: ${JSON.stringify(result)}`); | ||
} | ||
} | ||
} | ||
|
||
async function startLoader() { | ||
await mongoose.connect(process.env.MONGO_CONNECTION_STRING || ''); | ||
logger.info("MongoDB Connected!"); | ||
await loadData(); | ||
} | ||
|
||
export default startLoader; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import nameLoader from './helper/name-loader'; | ||
nameLoader(); |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import loader from '../src/helper/name-loader'; | ||
import app from "../src/app"; | ||
import Name from "../src/models/name"; | ||
|
||
describe("Test the name path", () => { | ||
test("Should load correctly", async () => { | ||
await loader(); | ||
|
||
const successLoad = await Name.findOne({username: 'berviantoleo'}); | ||
expect(successLoad?._id).not.toBeNull(); | ||
expect(successLoad?.name).toBe('Bervianto Leo Pratama'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await Name.deleteMany({}); | ||
}) | ||
}); |
This file contains 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