Add seeding to users
This commit is contained in:
2
backend_splatournament_manager/.gitignore
vendored
2
backend_splatournament_manager/.gitignore
vendored
@@ -2,7 +2,7 @@ node_modules
|
|||||||
dist/*
|
dist/*
|
||||||
!dist/csv/
|
!dist/csv/
|
||||||
dist/csv/*
|
dist/csv/*
|
||||||
!dist/csv/tournaments.csv
|
!dist/csv/*.csv
|
||||||
*.sqlite
|
*.sqlite
|
||||||
_requests.http
|
_requests.http
|
||||||
request_logs.txt
|
request_logs.txt
|
||||||
2
backend_splatournament_manager/dist/csv/users.csv
vendored
Normal file
2
backend_splatournament_manager/dist/csv/users.csv
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
username, password
|
||||||
|
tikaiz, htlgkr
|
||||||
|
@@ -1,18 +1,43 @@
|
|||||||
import { User } from '../models/user';
|
import { User } from '../models/user';
|
||||||
import { Database } from 'sqlite3';
|
import { Database } from 'sqlite3';
|
||||||
import * as argon2 from 'argon2';
|
import * as argon2 from 'argon2';
|
||||||
|
import fs from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export class UserService {
|
export class UserService {
|
||||||
|
private csvFilename = 'csv/users.csv';
|
||||||
private db: Database;
|
private db: Database;
|
||||||
|
|
||||||
constructor(db: Database) {
|
constructor(db: Database) {
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.db.run(`CREATE TABLE IF NOT EXISTS Users
|
this.db.serialize(() => {
|
||||||
|
this.db.run(`CREATE TABLE IF NOT EXISTS Users
|
||||||
(
|
(
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
username TEXT UNIQUE NOT NULL,
|
username TEXT UNIQUE NOT NULL,
|
||||||
password TEXT NOT NULL
|
password TEXT NOT NULL
|
||||||
)`);
|
)`);
|
||||||
|
this.seedDb();
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
seedDb() {
|
||||||
|
fs.readFile(path.join(process.cwd(), "dist", this.csvFilename), 'utf-8', async (_, data) => {
|
||||||
|
const entries = data.split('\n');
|
||||||
|
entries.shift();
|
||||||
|
const lines = entries.filter(line => line.trim());
|
||||||
|
const statement = this.db.prepare(`INSERT INTO Users
|
||||||
|
(username, password)
|
||||||
|
VALUES (?, ?)`);
|
||||||
|
for (const line of lines) {
|
||||||
|
const parts = line.split(',');
|
||||||
|
const username = parts[0].trim();
|
||||||
|
const hash = await argon2.hash(parts[1].trim());
|
||||||
|
statement.run(username, hash);
|
||||||
|
console.log({ username, password: hash });
|
||||||
|
}
|
||||||
|
statement.finalize();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
register(username: string, password: string): Promise<{ id: number; username: string }> {
|
register(username: string, password: string): Promise<{ id: number; username: string }> {
|
||||||
|
|||||||
Reference in New Issue
Block a user