fastapi-mongo-setup 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_mongo_setup-0.1.0/PKG-INFO +34 -0
- fastapi_mongo_setup-0.1.0/README.md +23 -0
- fastapi_mongo_setup-0.1.0/pyproject.toml +24 -0
- fastapi_mongo_setup-0.1.0/setup.cfg +4 -0
- fastapi_mongo_setup-0.1.0/src/fastapi_mongo_setup.egg-info/PKG-INFO +34 -0
- fastapi_mongo_setup-0.1.0/src/fastapi_mongo_setup.egg-info/SOURCES.txt +10 -0
- fastapi_mongo_setup-0.1.0/src/fastapi_mongo_setup.egg-info/dependency_links.txt +1 -0
- fastapi_mongo_setup-0.1.0/src/fastapi_mongo_setup.egg-info/entry_points.txt +2 -0
- fastapi_mongo_setup-0.1.0/src/fastapi_mongo_setup.egg-info/requires.txt +3 -0
- fastapi_mongo_setup-0.1.0/src/fastapi_mongo_setup.egg-info/top_level.txt +1 -0
- fastapi_mongo_setup-0.1.0/src/mongo_setup/__init__.py +1 -0
- fastapi_mongo_setup-0.1.0/src/mongo_setup/cli.py +84 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-mongo-setup
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A CLI tool to auto-generate MongoDB connection boilerplates for FastAPI projects.
|
|
5
|
+
Author-email: Developer Name <developer@example.com>
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: motor
|
|
9
|
+
Requires-Dist: python-dotenv
|
|
10
|
+
Requires-Dist: fastapi
|
|
11
|
+
|
|
12
|
+
# fastapi-mongo-setup
|
|
13
|
+
|
|
14
|
+
A Python CLI package that automatically creates a MongoDB connection structure for FastAPI projects.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install fastapi-mongo-setup
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Navigate to the root of your FastAPI project and run:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
mongo-setup
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
This command will:
|
|
31
|
+
- Check if the `src/utils` directory exists (creates it if it doesn't).
|
|
32
|
+
- Generate a `src/utils/db.py` file containing an asynchronous `Database` class using `motor`. This includes `connect_to_mongodb` and `close_mongodb_connection` methods.
|
|
33
|
+
- Generate a `src/utils/helpers.py` file with a `serialize_doc` function to convert MongoDB `ObjectId` types to plain strings.
|
|
34
|
+
- Automatically append `MONGODB_URL` and `DATABASE_NAME` variables to your `.env` file.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# fastapi-mongo-setup
|
|
2
|
+
|
|
3
|
+
A Python CLI package that automatically creates a MongoDB connection structure for FastAPI projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install fastapi-mongo-setup
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Navigate to the root of your FastAPI project and run:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
mongo-setup
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
This command will:
|
|
20
|
+
- Check if the `src/utils` directory exists (creates it if it doesn't).
|
|
21
|
+
- Generate a `src/utils/db.py` file containing an asynchronous `Database` class using `motor`. This includes `connect_to_mongodb` and `close_mongodb_connection` methods.
|
|
22
|
+
- Generate a `src/utils/helpers.py` file with a `serialize_doc` function to convert MongoDB `ObjectId` types to plain strings.
|
|
23
|
+
- Automatically append `MONGODB_URL` and `DATABASE_NAME` variables to your `.env` file.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "fastapi-mongo-setup"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A CLI tool to auto-generate MongoDB connection boilerplates for FastAPI projects."
|
|
9
|
+
authors = [
|
|
10
|
+
{name = "Developer Name", email = "developer@example.com"},
|
|
11
|
+
]
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
requires-python = ">=3.8"
|
|
14
|
+
dependencies = [
|
|
15
|
+
"motor",
|
|
16
|
+
"python-dotenv",
|
|
17
|
+
"fastapi"
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.scripts]
|
|
21
|
+
mongo-setup = "mongo_setup.cli:init"
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.packages.find]
|
|
24
|
+
where = ["src"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastapi-mongo-setup
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A CLI tool to auto-generate MongoDB connection boilerplates for FastAPI projects.
|
|
5
|
+
Author-email: Developer Name <developer@example.com>
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
Requires-Dist: motor
|
|
9
|
+
Requires-Dist: python-dotenv
|
|
10
|
+
Requires-Dist: fastapi
|
|
11
|
+
|
|
12
|
+
# fastapi-mongo-setup
|
|
13
|
+
|
|
14
|
+
A Python CLI package that automatically creates a MongoDB connection structure for FastAPI projects.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pip install fastapi-mongo-setup
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Navigate to the root of your FastAPI project and run:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
mongo-setup
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
This command will:
|
|
31
|
+
- Check if the `src/utils` directory exists (creates it if it doesn't).
|
|
32
|
+
- Generate a `src/utils/db.py` file containing an asynchronous `Database` class using `motor`. This includes `connect_to_mongodb` and `close_mongodb_connection` methods.
|
|
33
|
+
- Generate a `src/utils/helpers.py` file with a `serialize_doc` function to convert MongoDB `ObjectId` types to plain strings.
|
|
34
|
+
- Automatically append `MONGODB_URL` and `DATABASE_NAME` variables to your `.env` file.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/fastapi_mongo_setup.egg-info/PKG-INFO
|
|
4
|
+
src/fastapi_mongo_setup.egg-info/SOURCES.txt
|
|
5
|
+
src/fastapi_mongo_setup.egg-info/dependency_links.txt
|
|
6
|
+
src/fastapi_mongo_setup.egg-info/entry_points.txt
|
|
7
|
+
src/fastapi_mongo_setup.egg-info/requires.txt
|
|
8
|
+
src/fastapi_mongo_setup.egg-info/top_level.txt
|
|
9
|
+
src/mongo_setup/__init__.py
|
|
10
|
+
src/mongo_setup/cli.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mongo_setup
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""fastapi-mongo-setup package"""
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import pathlib
|
|
3
|
+
|
|
4
|
+
def init():
|
|
5
|
+
print("Initializing MongoDB setup for FastAPI...")
|
|
6
|
+
|
|
7
|
+
# Check if src/utils exists (create if not)
|
|
8
|
+
utils_dir = pathlib.Path("src/utils")
|
|
9
|
+
utils_dir.mkdir(parents=True, exist_ok=True)
|
|
10
|
+
|
|
11
|
+
# 1. Create src/utils/db.py
|
|
12
|
+
db_file = utils_dir / "db.py"
|
|
13
|
+
if not db_file.exists():
|
|
14
|
+
with open(db_file, "w", encoding="utf-8") as f:
|
|
15
|
+
f.write("""import os
|
|
16
|
+
from motor.motor_asyncio import AsyncIOMotorClient
|
|
17
|
+
from dotenv import load_dotenv
|
|
18
|
+
|
|
19
|
+
load_dotenv()
|
|
20
|
+
|
|
21
|
+
class Database:
|
|
22
|
+
client: AsyncIOMotorClient = None
|
|
23
|
+
db = None
|
|
24
|
+
|
|
25
|
+
db = Database()
|
|
26
|
+
|
|
27
|
+
async def connect_to_mongodb():
|
|
28
|
+
mongodb_url = os.getenv("MONGODB_URL", "mongodb://localhost:27017")
|
|
29
|
+
database_name = os.getenv("DATABASE_NAME", "fastapi_db")
|
|
30
|
+
|
|
31
|
+
db.client = AsyncIOMotorClient(mongodb_url)
|
|
32
|
+
db.db = db.client[database_name]
|
|
33
|
+
|
|
34
|
+
# Ping the database to ensure a successful connection
|
|
35
|
+
await db.client.admin.command('ping')
|
|
36
|
+
print("Connected to MongoDB!")
|
|
37
|
+
|
|
38
|
+
async def close_mongodb_connection():
|
|
39
|
+
if db.client:
|
|
40
|
+
db.client.close()
|
|
41
|
+
print("Closed MongoDB connection.")
|
|
42
|
+
""")
|
|
43
|
+
print(f"Created {db_file}")
|
|
44
|
+
else:
|
|
45
|
+
print(f"{db_file} already exists, skipping.")
|
|
46
|
+
|
|
47
|
+
# 2. Create src/utils/helpers.py
|
|
48
|
+
helpers_file = utils_dir / "helpers.py"
|
|
49
|
+
if not helpers_file.exists():
|
|
50
|
+
with open(helpers_file, "w", encoding="utf-8") as f:
|
|
51
|
+
f.write("""def serialize_doc(doc):
|
|
52
|
+
\"\"\"Convert MongoDB ObjectId to string\"\"\"
|
|
53
|
+
if doc and "_id" in doc:
|
|
54
|
+
doc["id"] = str(doc["_id"])
|
|
55
|
+
del doc["_id"]
|
|
56
|
+
return doc
|
|
57
|
+
""")
|
|
58
|
+
print(f"Created {helpers_file}")
|
|
59
|
+
else:
|
|
60
|
+
print(f"{helpers_file} already exists, skipping.")
|
|
61
|
+
|
|
62
|
+
# 3. Append to .env
|
|
63
|
+
env_file = pathlib.Path(".env")
|
|
64
|
+
env_content = ""
|
|
65
|
+
|
|
66
|
+
if env_file.exists():
|
|
67
|
+
with open(env_file, "r", encoding="utf-8") as f:
|
|
68
|
+
env_content = f.read()
|
|
69
|
+
|
|
70
|
+
with open(env_file, "a", encoding="utf-8") as f:
|
|
71
|
+
# Add a newline if the file does not end with one
|
|
72
|
+
if env_content and not env_content.endswith("\\n"):
|
|
73
|
+
f.write("\\n")
|
|
74
|
+
|
|
75
|
+
if "MONGODB_URL" not in env_content:
|
|
76
|
+
f.write("MONGODB_URL=mongodb://localhost:27017\\n")
|
|
77
|
+
if "DATABASE_NAME" not in env_content:
|
|
78
|
+
f.write("DATABASE_NAME=fastapi_db\\n")
|
|
79
|
+
|
|
80
|
+
print("Appended MONGODB_URL and DATABASE_NAME placeholders to .env")
|
|
81
|
+
print("MongoDB setup complete!")
|
|
82
|
+
|
|
83
|
+
if __name__ == "__main__":
|
|
84
|
+
init()
|