-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmong_init.js.rtf
More file actions
160 lines (159 loc) · 11.4 KB
/
mong_init.js.rtf
File metadata and controls
160 lines (159 loc) · 11.4 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
{\rtf1\ansi\ansicpg1252\cocoartf2818
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 Menlo-Regular;}
{\colortbl;\red255\green255\blue255;\red68\green134\blue200;\red23\green23\blue23;\red202\green202\blue202;
\red206\green207\blue149;\red194\green126\blue101;\red64\green114\blue44;\red140\green211\blue254;\red179\green107\blue176;
\red167\green197\blue138;}
{\*\expandedcolortbl;;\cssrgb\c32941\c60000\c82353;\cssrgb\c11765\c11765\c11765;\cssrgb\c83137\c83137\c83137;
\cssrgb\c84314\c84314\c65098;\cssrgb\c80784\c56863\c47059;\cssrgb\c30980\c51373\c22745;\cssrgb\c61176\c86275\c99608;\cssrgb\c76078\c51373\c74510;
\cssrgb\c70980\c80784\c61176;}
\paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0
\deftab720
\pard\pardeftab720\partightenfactor0
\f0\fs24 \cf2 \cb3 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 const\cf4 \strokec4 \{ MongoClient \} = \cf5 \strokec5 require\cf4 \strokec4 (\cf6 \strokec6 'mongodb'\cf4 \strokec4 );\cb1 \
\
\pard\pardeftab720\partightenfactor0
\cf7 \cb3 \strokec7 // Connection URI\cf4 \cb1 \strokec4 \
\pard\pardeftab720\partightenfactor0
\cf2 \cb3 \strokec2 const\cf4 \strokec4 uri = process.env.MONGO_URI \cf8 \strokec8 ||\cf4 \strokec4 \cf6 \strokec6 'mongodb://localhost:27017'\cf4 \strokec4 ; \cf7 \strokec7 // Use your connection string\cf4 \cb1 \strokec4 \
\
\pard\pardeftab720\partightenfactor0
\cf7 \cb3 \strokec7 // Create a new MongoClient\cf4 \cb1 \strokec4 \
\pard\pardeftab720\partightenfactor0
\cf2 \cb3 \strokec2 const\cf4 \strokec4 client = new \cf5 \strokec5 MongoClient\cf4 \strokec4 (uri, \{ useNewUrlParser: \cf2 \strokec2 true\cf4 \strokec4 , useUnifiedTopology: \cf2 \strokec2 true\cf4 \strokec4 \});\cb1 \
\
\pard\pardeftab720\partightenfactor0
\cf4 \cb3 async \cf2 \strokec2 function\cf4 \strokec4 \cf5 \strokec5 initDatabase\cf4 \strokec4 () \{\cb1 \
\cb3 \cf9 \strokec9 try\cf4 \strokec4 \{\cb1 \
\cb3 \cf7 \strokec7 // Connect to the MongoDB cluster\cf4 \cb1 \strokec4 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 client.\cf5 \strokec5 connect\cf4 \strokec4 ();\cb1 \
\
\cb3 \cf7 \strokec7 // Switch to the medical database\cf4 \cb1 \strokec4 \
\cb3 \cf2 \strokec2 const\cf4 \strokec4 db = client.\cf5 \strokec5 db\cf4 \strokec4 (\cf6 \strokec6 'medical_db'\cf4 \strokec4 );\cb1 \
\
\cb3 \cf7 \strokec7 // Create the admin user\cf4 \cb1 \strokec4 \
\cb3 \cf2 \strokec2 const\cf4 \strokec4 adminDb = client.\cf5 \strokec5 db\cf4 \strokec4 (\cf6 \strokec6 'admin'\cf4 \strokec4 );\cb1 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 adminDb.\cf5 \strokec5 command\cf4 \strokec4 (\{\cb1 \
\cb3 createUser: process.env.MONGO_USER \cf8 \strokec8 ||\cf4 \strokec4 \cf6 \strokec6 'medical_user'\cf4 \strokec4 ,\cb1 \
\cb3 pwd: process.env.MONGO_PASSWORD \cf8 \strokec8 ||\cf4 \strokec4 \cf6 \strokec6 'securepassword'\cf4 \strokec4 ,\cb1 \
\cb3 roles: [\{ role: \cf6 \strokec6 'readWrite'\cf4 \strokec4 , db: \cf6 \strokec6 'medical_db'\cf4 \strokec4 \}]\cb1 \
\cb3 \});\cb1 \
\
\cb3 \cf7 \strokec7 // Create collections with schema validation\cf4 \cb1 \strokec4 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 createCollection\cf4 \strokec4 (\cf6 \strokec6 'patient_summaries'\cf4 \strokec4 , \{\cb1 \
\cb3 validator: \{\cb1 \
\cb3 $jsonSchema: \{\cb1 \
\cb3 bsonType: \cf6 \strokec6 'object'\cf4 \strokec4 ,\cb1 \
\cb3 required: [\cf6 \strokec6 'summary_id'\cf4 \strokec4 , \cf6 \strokec6 'patient_id'\cf4 \strokec4 , \cf6 \strokec6 'date'\cf4 \strokec4 , \cf6 \strokec6 'summary'\cf4 \strokec4 , \cf6 \strokec6 'flag'\cf4 \strokec4 ],\cb1 \
\cb3 properties: \{\cb1 \
\cb3 summary_id: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 patient_id: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 date: \{ bsonType: \cf6 \strokec6 'date'\cf4 \strokec4 \},\cb1 \
\cb3 summary: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 flag: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \});\cb1 \
\
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 createCollection\cf4 \strokec4 (\cf6 \strokec6 'practitioners'\cf4 \strokec4 , \{\cb1 \
\cb3 validator: \{\cb1 \
\cb3 $jsonSchema: \{\cb1 \
\cb3 bsonType: \cf6 \strokec6 'object'\cf4 \strokec4 ,\cb1 \
\cb3 required: [\cf6 \strokec6 'practitioner_id'\cf4 \strokec4 , \cf6 \strokec6 'name'\cf4 \strokec4 , \cf6 \strokec6 'address'\cf4 \strokec4 , \cf6 \strokec6 'latitude'\cf4 \strokec4 , \cf6 \strokec6 'longitude'\cf4 \strokec4 ],\cb1 \
\cb3 properties: \{\cb1 \
\cb3 practitioner_id: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 name: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 address: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 latitude: \{ bsonType: \cf6 \strokec6 'double'\cf4 \strokec4 , minimum: -\cf10 \strokec10 90\cf4 \strokec4 , maximum: \cf10 \strokec10 90\cf4 \strokec4 \},\cb1 \
\cb3 longitude: \{ bsonType: \cf6 \strokec6 'double'\cf4 \strokec4 , minimum: -\cf10 \strokec10 180\cf4 \strokec4 , maximum: \cf10 \strokec10 180\cf4 \strokec4 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \});\cb1 \
\
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 createCollection\cf4 \strokec4 (\cf6 \strokec6 'patients'\cf4 \strokec4 , \{\cb1 \
\cb3 validator: \{\cb1 \
\cb3 $jsonSchema: \{\cb1 \
\cb3 bsonType: \cf6 \strokec6 'object'\cf4 \strokec4 ,\cb1 \
\cb3 required: [\cf6 \strokec6 'patient_id'\cf4 \strokec4 , \cf6 \strokec6 'name'\cf4 \strokec4 , \cf6 \strokec6 'age'\cf4 \strokec4 ],\cb1 \
\cb3 properties: \{\cb1 \
\cb3 patient_id: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 name: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 age: \{ bsonType: \cf6 \strokec6 'int'\cf4 \strokec4 , minimum: \cf10 \strokec10 0\cf4 \strokec4 , maximum: \cf10 \strokec10 150\cf4 \strokec4 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \});\cb1 \
\
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 createCollection\cf4 \strokec4 (\cf6 \strokec6 'available_slots'\cf4 \strokec4 , \{\cb1 \
\cb3 validator: \{\cb1 \
\cb3 $jsonSchema: \{\cb1 \
\cb3 bsonType: \cf6 \strokec6 'object'\cf4 \strokec4 ,\cb1 \
\cb3 required: [\cf6 \strokec6 'practitioner_id'\cf4 \strokec4 , \cf6 \strokec6 'available_slots'\cf4 \strokec4 , \cf6 \strokec6 'flag'\cf4 \strokec4 ],\cb1 \
\cb3 properties: \{\cb1 \
\cb3 practitioner_id: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \},\cb1 \
\cb3 available_slots: \{\cb1 \
\cb3 bsonType: \cf6 \strokec6 'array'\cf4 \strokec4 ,\cb1 \
\cb3 items: \{ bsonType: \cf6 \strokec6 'date'\cf4 \strokec4 \}\cb1 \
\cb3 \},\cb1 \
\cb3 flag: \{ bsonType: \cf6 \strokec6 'string'\cf4 \strokec4 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\cb3 \});\cb1 \
\
\cb3 \cf7 \strokec7 // Insert sample data\cf4 \cb1 \strokec4 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'patient_summaries'\cf4 \strokec4 ).\cf5 \strokec5 insertOne\cf4 \strokec4 (\{\cb1 \
\cb3 summary_id: \cf6 \strokec6 '0001'\cf4 \strokec4 ,\cb1 \
\cb3 patient_id: \cf6 \strokec6 '0001'\cf4 \strokec4 ,\cb1 \
\cb3 date: new \cf5 \strokec5 Date\cf4 \strokec4 (\cf6 \strokec6 '2024-01-12'\cf4 \strokec4 ),\cb1 \
\cb3 summary: \cf6 \strokec6 'Hypertension with mild symptoms.'\cf4 \strokec4 ,\cb1 \
\cb3 flag: \cf6 \strokec6 '1/10'\cf4 \cb1 \strokec4 \
\cb3 \});\cb1 \
\
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'practitioners'\cf4 \strokec4 ).\cf5 \strokec5 insertOne\cf4 \strokec4 (\{\cb1 \
\cb3 practitioner_id: \cf6 \strokec6 '0001'\cf4 \strokec4 ,\cb1 \
\cb3 name: \cf6 \strokec6 'John Doe'\cf4 \strokec4 ,\cb1 \
\cb3 address: \cf6 \strokec6 '4 av de la Grande Arm\'e9e'\cf4 \strokec4 ,\cb1 \
\cb3 latitude: \cf10 \strokec10 48.8742\cf4 \strokec4 ,\cb1 \
\cb3 longitude: \cf10 \strokec10 2.2935\cf4 \cb1 \strokec4 \
\cb3 \});\cb1 \
\
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'patients'\cf4 \strokec4 ).\cf5 \strokec5 insertOne\cf4 \strokec4 (\{\cb1 \
\cb3 patient_id: \cf6 \strokec6 '0001'\cf4 \strokec4 ,\cb1 \
\cb3 name: \cf6 \strokec6 'John Doe'\cf4 \strokec4 ,\cb1 \
\cb3 age: \cf10 \strokec10 45\cf4 \cb1 \strokec4 \
\cb3 \});\cb1 \
\
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'available_slots'\cf4 \strokec4 ).\cf5 \strokec5 insertOne\cf4 \strokec4 (\{\cb1 \
\cb3 practitioner_id: \cf6 \strokec6 '0001'\cf4 \strokec4 ,\cb1 \
\cb3 available_slots: [\cb1 \
\cb3 new \cf5 \strokec5 Date\cf4 \strokec4 (\cf6 \strokec6 '2025-04-12T12:00:00Z'\cf4 \strokec4 ),\cb1 \
\cb3 new \cf5 \strokec5 Date\cf4 \strokec4 (\cf6 \strokec6 '2025-04-12T13:00:00Z'\cf4 \strokec4 ),\cb1 \
\cb3 new \cf5 \strokec5 Date\cf4 \strokec4 (\cf6 \strokec6 '2025-04-12T14:00:00Z'\cf4 \strokec4 )\cb1 \
\cb3 ],\cb1 \
\cb3 flag: \cf6 \strokec6 '1/10'\cf4 \cb1 \strokec4 \
\cb3 \});\cb1 \
\
\cb3 \cf7 \strokec7 // Create indexes for efficient querying\cf4 \cb1 \strokec4 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'patient_summaries'\cf4 \strokec4 ).\cf5 \strokec5 createIndex\cf4 \strokec4 (\{ patient_id: \cf10 \strokec10 1\cf4 \strokec4 \});\cb1 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'practitioners'\cf4 \strokec4 ).\cf5 \strokec5 createIndex\cf4 \strokec4 (\{ practitioner_id: \cf10 \strokec10 1\cf4 \strokec4 \});\cb1 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'patients'\cf4 \strokec4 ).\cf5 \strokec5 createIndex\cf4 \strokec4 (\{ patient_id: \cf10 \strokec10 1\cf4 \strokec4 \});\cb1 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 db.\cf5 \strokec5 collection\cf4 \strokec4 (\cf6 \strokec6 'available_slots'\cf4 \strokec4 ).\cf5 \strokec5 createIndex\cf4 \strokec4 (\{ practitioner_id: \cf10 \strokec10 1\cf4 \strokec4 \});\cb1 \
\
\cb3 console.\cf5 \strokec5 log\cf4 \strokec4 (\cf6 \strokec6 'Database initialization completed successfully.'\cf4 \strokec4 );\cb1 \
\cb3 \} \cf9 \strokec9 catch\cf4 \strokec4 (error) \{\cb1 \
\cb3 console.\cf5 \strokec5 error\cf4 \strokec4 (\cf6 \strokec6 'Error during database initialization:'\cf4 \strokec4 , error);\cb1 \
\cb3 \} \cf9 \strokec9 finally\cf4 \strokec4 \{\cb1 \
\cb3 \cf7 \strokec7 // Close the connection\cf4 \cb1 \strokec4 \
\cb3 \cf9 \strokec9 await\cf4 \strokec4 client.\cf5 \strokec5 close\cf4 \strokec4 ();\cb1 \
\cb3 \}\cb1 \
\cb3 \}\cb1 \
\
\pard\pardeftab720\partightenfactor0
\cf7 \cb3 \strokec7 // Run the initialization function\cf4 \cb1 \strokec4 \
\pard\pardeftab720\partightenfactor0
\cf5 \cb3 \strokec5 initDatabase\cf4 \strokec4 ();\cb1 \
\
}