forked from snyk-labs/nodejs-goof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeorm-db.js
45 lines (38 loc) · 966 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
var typeorm = require("typeorm");
var EntitySchema = typeorm.EntitySchema;
const Users = require("./entity/Users")
typeorm.createConnection({
type: "mysql",
host: env.DB_HOST,
port: env.DB_PORT,
username: env.DB_USER,
password: env.DB_PASSWORD,
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)
})