-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.ts
77 lines (72 loc) · 2.57 KB
/
setup.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Client } from "pg";
import { mongoDB } from "./src/config/mongo.config";
import { sequelizeConnection } from "./src/config/postgres.config";
import { ParkingPlaceInput } from "./src/interfaces/parkingPlace.interface";
import { Roles, UserInput } from "./src/interfaces/user.interface";
import { ParkingPlace } from "./src/models/parkingPlace";
import { User } from "./src/models/user";
import { encrypt } from "./src/utils/bcrypt.handle";
import 'dotenv/config'
import { BookingInput } from "./src/interfaces/booking.interface";
import { Booking } from "./src/models/booking";
const main = async() => {
const NAME_DB_PG = process.env.NAME_DB_PG
const clientPG = new Client({
host: process.env.HOST_DB_PG,
user: process.env.USER_DB_PG,
password: process.env.PASS_DB_PG,
port: 5432,
})
const passHass = await encrypt("1234567")
const newClient: UserInput = {
name: "Client",
email: "[email protected]",
password: `${passHass}`,
rol: Roles.client
}
const newAdmin: UserInput = {
name: "Admin",
email: "[email protected]",
password: `${passHass}`,
rol: Roles.admin
}
const newEmployee: UserInput = {
name: "Employee",
email: "[email protected]",
password: `${passHass}`,
rol: Roles.employee
}
const newBooking: BookingInput = {
dateStart: "2024-07-20T10:00",
dateEnd: "2024-07-20T10:30"
}
const newPlace: ParkingPlaceInput = {
name: "Place 001"
}
const newPlace2: ParkingPlaceInput = {
name: "Place 002"
}
try {
await clientPG.connect()
const res = await clientPG.query(`SELECT datname FROM pg_catalog.pg_database WHERE datname = '${NAME_DB_PG}'`)
if (res.rowCount === 0) {
await clientPG.query(`CREATE DATABASE "${NAME_DB_PG}";`)
await clientPG.end()
await sequelizeConnection.sync({force: false, alter:true})
await User.create(newAdmin)
await User.create(newEmployee)
const client = await User.create(newClient)
const place = await ParkingPlace.create(newPlace)
await ParkingPlace.create(newPlace2)
await Booking.create({userId: client.id, placeId: place.id, ...newBooking})
await sequelizeConnection.close()
console.log("Preset Database Success")
} else {
await clientPG.end()
}
} catch (error) {
console.log("Error to preset data in database")
await clientPG.end()
}
}
main()