-
Notifications
You must be signed in to change notification settings - Fork 9
/
typeorm-db.js
46 lines (39 loc) · 962 Bytes
/
typeorm-db.js
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
var typeorm = require("typeorm");
var EntitySchema = typeorm.EntitySchema;
const Users = require("./entity/Users")
typeorm.createConnection({
name: "mysql",
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "root",
database: "acme",
synchronize: true,
"logging": true,
entities: [
new EntitySchema(Users)
]
}).then(() => {
const dbConnection = typeorm.getConnection('mysql')
const repo = dbConnection.getRepository("Users")
return repo
}).then((repo) => {
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"
})
];
return Promise.all(inserts)
}).catch((err) => {
console.error('failed connecting and seeding users to the MySQL database')
console.error(err)
})