-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_db.py
75 lines (61 loc) · 1.69 KB
/
init_db.py
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
from pymongo import MongoClient
from passlib.hash import scrypt
from bson.objectid import ObjectId
from config import Config
from urllib.parse import urlparse
# Initialize Config
config = Config()
# Parse the MongoDB URI
parsed_uri = urlparse(config.MONGO_URI)
# Extract the database name
db_name = parsed_uri.path.lstrip('/')
# Remove any query parameters from the database name
db_name = db_name.split('?')[0]
# Connect to MongoDB using the URI from Config
client = MongoClient(config.MONGO_URI)
# Use the extracted database name
db = client[db_name]
print(f"Connected to database: {db_name}")
# Clear existing data
db.customers.delete_many({})
db.accounts.delete_many({})
db.transactions.delete_many({})
# Create a sample customer
customer = {
'username': 'johndoe',
'password': scrypt.hash('password123')
}
customer_id = db.customers.insert_one(customer).inserted_id
# ... rest of your init_db.py code ...
print("Sample data initialized successfully!")
# Create sample accounts
accounts = [
{
'customer_id': customer_id,
'account_type': 'Checking',
'balance': 1000.00
},
{
'customer_id': customer_id,
'account_type': 'Savings',
'balance': 5000.00
}
]
account_ids = db.accounts.insert_many(accounts).inserted_ids
# Create sample transactions
transactions = [
{
'account_id': account_ids[0],
'amount': 500.00,
'type': 'deposit',
'timestamp': '2023-01-01T12:00:00'
},
{
'account_id': account_ids[1],
'amount': 1000.00,
'type': 'deposit',
'timestamp': '2023-01-02T14:30:00'
}
]
db.transactions.insert_many(transactions)
print("Sample data initialized successfully!")