-
Notifications
You must be signed in to change notification settings - Fork 0
/
seader.ts
82 lines (70 loc) · 1.69 KB
/
seader.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
78
79
80
81
82
/* eslint-disable @typescript-eslint/no-var-requires */
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const admins = require('./data.ts');
// Load environment variables from .env file
dotenv.config();
// Database connection setup
const MONGO_URI = process.env.DATABASE_URL;
mongoose.Promise = global.Promise;
const connectToDB = async () => {
try {
if (process.env.NODE_ENV === 'development') {
mongoose.set('debug', true);
}
await mongoose.connect(MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
autoIndex: true,
});
// eslint-disable-next-line no-console
console.log('Connected to database');
} catch (err) {
// eslint-disable-next-line no-console
console.error('Failed to connect to database', err);
process.exit(1);
}
};
// Define the model directly in the script
const { Schema, model } = mongoose;
const userSchema = new Schema({
fullName: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
email: {
required: true,
type: String,
unique: true,
},
role: {
type: String,
enum: ['ADMIN', 'USER'],
default: 'USER',
},
});
const User = model('User', userSchema, 'User');
// Function to import data
const importData = async () => {
try {
await User.insertMany(admins);
// eslint-disable-next-line no-console
console.log('Data Imported!');
process.exit();
} catch (error) {
// eslint-disable-next-line no-console
console.error(`${error}`);
process.exit(1);
}
};
// Connect to the database and import data
const run = async () => {
await connectToDB();
await importData();
};
run();
export {};