-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypeorm-db.js
More file actions
46 lines (39 loc) · 1.1 KB
/
typeorm-db.js
File metadata and controls
46 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const { DataSource, EntitySchema } = require("typeorm");
const Users = require("./entity/Users");
// Define a new data source
const AppDataSource = new DataSource({
type: "mysql",
host: process.env.MYSQL_URI,
port: 3306,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
synchronize: true,
logging: true,
entities: [
new EntitySchema(Users)
],
});
AppDataSource.initialize()
.then(async (dataSource) => {
console.log("Data Source has been initialized!");
const repo = dataSource.getRepository("Users");
console.log("Seeding 2 users to MySQL users table: Liran (role: user), Simon (role: admin)");
const inserts = [
repo.insert({
name: "Liran",
address: "IL",
role: "user",
}),
repo.insert({
name: "Simon",
address: "UK",
role: "admin",
}),
await Promise.all(inserts);
console.log("Users have been successfully seeded!");
})
.catch((err) => {
console.error("Failed connecting and seeding users to the MySQL database");
console.error(err);
});