Skip to content

Commit 031aed8

Browse files
chore: make seed more dynamic
1 parent ed79979 commit 031aed8

2 files changed

Lines changed: 57 additions & 20 deletions

File tree

scripts/mysql/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const connection = await mysql.createConnection({
1010

1111
connection.connect();
1212

13-
const service = createService(connection);
13+
const service = createService(connection, "accounts");
1414
await service.migrate();
15-
console.log(await service.seed(2000));
15+
console.log(await service.seed(1_000_000));
1616

1717
connection.end();

scripts/mysql/service.ts

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,57 @@
11
import { faker } from "@faker-js/faker";
22

3-
class Service {
4-
constructor(db) {
5-
this.db = db;
6-
}
3+
const USERS = "users";
4+
const ACCOUNTS = "accounts";
75

8-
async count() {
9-
const [rows] = await this.db.query("select 1 + 1 as solution");
10-
return rows;
11-
}
6+
class Service {
7+
MAX_BATCH_SIZE = 1000;
128

13-
async migrate() {
14-
return this.db.execute(
15-
`create table if not exists users (
9+
sql = {
10+
[USERS]: `
11+
create table if not exists users (
1612
id int auto_increment,
1713
name varchar(80) not null,
1814
email varchar(80) not null,
1915
primary key (id),
2016
unique(email)
21-
)`
22-
);
17+
)
18+
`,
19+
[ACCOUNTS]: `
20+
create table if not exists accounts (
21+
id int auto_increment,
22+
name varchar(80) not null,
23+
email varchar(80) not null,
24+
created_at timestamp not null default now(),
25+
updated_at timestamp not null default now(),
26+
primary key (id)
27+
)
28+
`,
29+
};
30+
31+
constructor(db, ns) {
32+
if (!this.sql[ns]) {
33+
throw new Error(`unknown namespace: ${ns}`);
34+
}
35+
this.db = db;
36+
this.ns = ns;
37+
this.seeder = {
38+
[USERS]: (n) => this.seedUsers(n),
39+
[ACCOUNTS]: (n) => this.seedAccounts(n),
40+
};
41+
}
42+
43+
async migrate() {
44+
const sql = this.sql[this.ns];
45+
return this.db.execute(sql);
2346
}
2447

2548
async seed(n = 1_000) {
26-
const sizes = batch(n, 1000);
27-
return Promise.all(sizes.flatMap((n) => this._seed(n)));
49+
const fn = this.seeder[this.ns];
50+
const sizes = batch(n, this.MAX_BATCH_SIZE);
51+
return Promise.all(sizes.flatMap((n) => fn(n)));
2852
}
2953

30-
async _seed(n) {
54+
async seedUsers(n) {
3155
const names = Array.from({ length: n }).map(() => [
3256
faker.person.fullName(),
3357
faker.internet.email(),
@@ -39,6 +63,19 @@ class Service {
3963
[names]
4064
);
4165
}
66+
67+
async seedAccounts(n) {
68+
const names = Array.from({ length: n }).map(() => [
69+
faker.person.fullName(),
70+
faker.internet.email(),
71+
]);
72+
return this.db.query(
73+
`
74+
insert into accounts(name, email) values ?
75+
`,
76+
[names]
77+
);
78+
}
4279
}
4380
function batch(n, size) {
4481
const result = [];
@@ -49,6 +86,6 @@ function batch(n, size) {
4986
return result;
5087
}
5188

52-
export default function createService(conn) {
53-
return new Service(conn);
89+
export default function createService(conn, ns) {
90+
return new Service(conn, ns);
5491
}

0 commit comments

Comments
 (0)