postgresdb3 2.0.2__tar.gz → 2.0.3__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.
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/PKG-INFO +254 -223
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/README.md +31 -0
- postgresdb3-2.0.3/postgresdb3/core/__init__.py +2 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/core/async_db.py +518 -518
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/core/sync_db.py +558 -558
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/base.py +109 -109
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/__init__.py +5 -5
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/base.py +43 -43
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/datetime.py +32 -32
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/foreign.py +61 -61
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/misc.py +27 -27
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/numeric.py +37 -37
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/fields/text.py +22 -22
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/meta.py +127 -127
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/models.py +521 -521
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/query.py +973 -973
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3.egg-info/PKG-INFO +254 -223
- postgresdb3-2.0.3/postgresdb3.egg-info/requires.txt +2 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/pyproject.toml +2 -2
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/setup.cfg +4 -4
- postgresdb3-2.0.3/setup.py +54 -0
- postgresdb3-2.0.2/postgresdb3/core/__init__.py +0 -2
- postgresdb3-2.0.2/postgresdb3.egg-info/requires.txt +0 -2
- postgresdb3-2.0.2/setup.py +0 -27
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/__init__.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/cli.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/migrations/__init__.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/migrations/engine.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/__init__.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/expressions.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3/orm/relations.py +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3.egg-info/SOURCES.txt +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3.egg-info/dependency_links.txt +0 -0
- {postgresdb3-2.0.2 → postgresdb3-2.0.3}/postgresdb3.egg-info/top_level.txt +0 -0
|
@@ -1,223 +1,254 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: postgresdb3
|
|
3
|
-
Version: 2.0.
|
|
4
|
-
Summary: High-Performance Sync and Async PostgreSQL ORM for Python
|
|
5
|
-
Author: Abdulbosit Alijonov
|
|
6
|
-
Project-URL: Source Code, https://github.com/AlijonovUz/PostgresDB
|
|
7
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
8
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
-
Classifier: Operating System :: OS Independent
|
|
10
|
-
Requires-Python: >=3.9
|
|
11
|
-
Description-Content-Type: text/markdown
|
|
12
|
-
Requires-Dist: psycopg2>=2.9
|
|
13
|
-
Requires-Dist: asyncpg>=0.31.0
|
|
14
|
-
Dynamic: author
|
|
15
|
-
Dynamic: classifier
|
|
16
|
-
Dynamic: description
|
|
17
|
-
Dynamic: description-content-type
|
|
18
|
-
Dynamic: project-url
|
|
19
|
-
Dynamic: requires-dist
|
|
20
|
-
Dynamic: requires-python
|
|
21
|
-
Dynamic: summary
|
|
22
|
-
|
|
23
|
-
# PostgresDB3 - High-Performance Python ORM
|
|
24
|
-
|
|
25
|
-
**PostgresDB3** — bu PostgreSQL uchun mo'ljallangan zamonaviy, tezkor va ham **sinxron**, ham **asinxron** ishlash imkoniyatiga ega bo'lgan ORM (Object-Relational Mapping) kutubxonasi.
|
|
26
|
-
|
|
27
|
-
U Django ORM kabi qulay va oson tushuniladi, lekin o'zida asinxronlik (`asyncpg`) va ko'plab yuqori texnologik xususiyatlarni mujassamlashtirgan. Kichik botlardan tortib, o'ta katta masshtabdagi (Highload) loyihalargacha bemalol ishlata olasiz!
|
|
28
|
-
|
|
29
|
-
---
|
|
30
|
-
|
|
31
|
-
## 📦 O'rnatish
|
|
32
|
-
|
|
33
|
-
Kutubxonani o'rnatish uchun terminalda quyidagi buyruqni kiriting:
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
pip install postgresdb3
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: postgresdb3
|
|
3
|
+
Version: 2.0.3
|
|
4
|
+
Summary: High-Performance Sync and Async PostgreSQL ORM for Python
|
|
5
|
+
Author: Abdulbosit Alijonov
|
|
6
|
+
Project-URL: Source Code, https://github.com/AlijonovUz/PostgresDB
|
|
7
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
Requires-Dist: psycopg2-binary>=2.9
|
|
13
|
+
Requires-Dist: asyncpg>=0.31.0
|
|
14
|
+
Dynamic: author
|
|
15
|
+
Dynamic: classifier
|
|
16
|
+
Dynamic: description
|
|
17
|
+
Dynamic: description-content-type
|
|
18
|
+
Dynamic: project-url
|
|
19
|
+
Dynamic: requires-dist
|
|
20
|
+
Dynamic: requires-python
|
|
21
|
+
Dynamic: summary
|
|
22
|
+
|
|
23
|
+
# PostgresDB3 - High-Performance Python ORM
|
|
24
|
+
|
|
25
|
+
**PostgresDB3** — bu PostgreSQL uchun mo'ljallangan zamonaviy, tezkor va ham **sinxron**, ham **asinxron** ishlash imkoniyatiga ega bo'lgan ORM (Object-Relational Mapping) kutubxonasi.
|
|
26
|
+
|
|
27
|
+
U Django ORM kabi qulay va oson tushuniladi, lekin o'zida asinxronlik (`asyncpg`) va ko'plab yuqori texnologik xususiyatlarni mujassamlashtirgan. Kichik botlardan tortib, o'ta katta masshtabdagi (Highload) loyihalargacha bemalol ishlata olasiz!
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## 📦 O'rnatish
|
|
32
|
+
|
|
33
|
+
Kutubxonani o'rnatish uchun terminalda quyidagi buyruqni kiriting:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install postgresdb3
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### ⚠️ Linux/macOS tizimlarida o'rnatish va muammolar yechimi
|
|
40
|
+
|
|
41
|
+
`postgresdb3` ishlashi uchun sinxron ulanishlarda `psycopg2` kutubxonasidan foydalanadi. O'rnatish vaqtida tizimingiz holatidan kelib chiqib, quyidagi ikki variantdan biri avtomatik tanlanadi:
|
|
42
|
+
|
|
43
|
+
1. **`psycopg2` (Kompilyatsiya qilinadigan variant)**: Agar tizimingizda PostgreSQL ning ishlab chiquvchi kutubxonalari (`pg_config` va `pg_config.h` fayllari, masalan `libpq-dev`) mavjud bo'lsa, kutubxona manbadan (source) kompilyatsiya qilinadi. Bu ishlab chiqarish (production) muhitlari uchun tavsiya etiladi.
|
|
44
|
+
2. **`psycopg2-binary` (Tayyor variant)**: Agar tizimingizda kompilyatsiya qilish uchun kerakli PostgreSQL kutubxonalari topilmasa, o'rnatish jarayoni muvaffaqiyatli yakunlanishi uchun avtomatik ravishda tayyor pre-compiled `psycopg2-binary` o'rnatiladi. Bu lokal dasturlash va test muhitlari uchun juda mos keladi.
|
|
45
|
+
|
|
46
|
+
#### Tizim kutubxonalarini o'rnatish (Tavsiya etiladi)
|
|
47
|
+
Ishlab chiqarish (production) muhitida toza `psycopg2` (manbadan yig'iladigan) ishlatish tavsiya etiladi. Buning uchun `postgresdb3` ni o'rnatishdan oldin quyidagi tizim paketlarini o'rnatib olishingiz kerak bo'ladi:
|
|
48
|
+
|
|
49
|
+
* **Debian/Ubuntu:**
|
|
50
|
+
```bash
|
|
51
|
+
sudo apt update
|
|
52
|
+
sudo apt install python3-dev libpq-dev build-essential
|
|
53
|
+
```
|
|
54
|
+
* **RedHat/CentOS/Fedora/Rocky Linux:**
|
|
55
|
+
```bash
|
|
56
|
+
sudo dnf install python3-devel postgresql-devel gcc
|
|
57
|
+
```
|
|
58
|
+
* **macOS (Homebrew):**
|
|
59
|
+
```bash
|
|
60
|
+
brew install libpq
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Majburiy kompilyatsiya rejimini yoqish
|
|
64
|
+
Agar siz pre-compiled binary versiyadan mutlaqo foydalanmasdan, toza manbali `psycopg2` ni majburiy ravishda yig'moqchi bo'lsangiz, terminalda quyidagi muhit o'zgaruvchisini (env variable) ko'rsatgan holda o'rnatishni amalga oshiring:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
POSTGRESDB_SOURCE_PSYCOPG2=1 pip install postgresdb3
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 🚀 Yangi (v2.0) Imkoniyatlari
|
|
73
|
+
|
|
74
|
+
Ushbu "Enterprise" versiyada loyihaga quyidagi gigant imkoniyatlar qo'shildi:
|
|
75
|
+
- **`values()` va `values_list()`** - Xotirani tejash uchun to'g'ridan to'g'ri `dict` yoki `list` qaytarish.
|
|
76
|
+
- **`get_or_create()` va `update_or_create()`** - Kodlarni sezilarli darajada qisqartirish.
|
|
77
|
+
- **`__` orqali Auto-Join** - Munosabatlarni avtomatik bog'lash (Masalan: `filter(user__profile__age__gt=18)`).
|
|
78
|
+
- **Mavhum Modellar (Abstract Models)** - Qayta-qayta kod yozmaslik uchun `class Meta: abstract = True`.
|
|
79
|
+
- **`annotate()` va `aggregate()`** - Murakkab matematik va guruhlash amallari.
|
|
80
|
+
- **`update()` va `delete()` (Ommaviy)** - Obyektlarni bazada bitta qatorda ommaviy tahrirlash yoki o'chirish.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## ⚡ Tezkor Boshlash
|
|
85
|
+
|
|
86
|
+
PostgresDB3 bir vaqtning o'zida ikkita dunyoni qo'llab-quvvatlaydi. Xohlasangiz **Sinxron**, xohlasangiz **Asinxron** loyihalarda bir xil sintaksis bilan ishlay olasiz.
|
|
87
|
+
|
|
88
|
+
### 1. Ulanishni sozlash
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from postgresdb3 import PostgresDB, AsyncPostgresDB
|
|
92
|
+
|
|
93
|
+
# Sinxron ulanish (psycopg2 asosida)
|
|
94
|
+
db_sync = PostgresDB("my_db", "user", "password", host="localhost", port=5432, echo=True)
|
|
95
|
+
|
|
96
|
+
# Asinxron ulanish (asyncpg asosida)
|
|
97
|
+
db_async = AsyncPostgresDB("my_db", "user", "password", host="localhost", port=5432, echo=True)
|
|
98
|
+
```
|
|
99
|
+
*(Eslatma: `echo=True` yoqilsa, barcha bajarilayotgan SQL so'rovlar terminalda ko'rinib turadi, bu debug uchun juda foydali).*
|
|
100
|
+
|
|
101
|
+
### 2. Modellarni Yaratish (Abstract Model namunasida)
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from postgresdb3.orm.models import Model, AsyncModel
|
|
105
|
+
from postgresdb3 import String, Integer, Float, Timestamp
|
|
106
|
+
|
|
107
|
+
Model.db = db_sync
|
|
108
|
+
|
|
109
|
+
class TimestampedModel(Model):
|
|
110
|
+
class Meta:
|
|
111
|
+
abstract = True # Jadval yaratilmaydi
|
|
112
|
+
|
|
113
|
+
created_at = Timestamp(auto_now_add=True)
|
|
114
|
+
|
|
115
|
+
class User(TimestampedModel):
|
|
116
|
+
name = String(length=50)
|
|
117
|
+
age = Integer(default=18)
|
|
118
|
+
score = Float(default=0.0)
|
|
119
|
+
|
|
120
|
+
class Meta:
|
|
121
|
+
unique_together = (("name", "age"),) # Ism va yosh bir xil takrorlanmasligi kerak
|
|
122
|
+
index_together = (("score",),) # Qidiruvni tezlashtirish uchun index
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 3. CRUD Operatsiyalari
|
|
126
|
+
|
|
127
|
+
**Yangi funksiyalar yordamida:**
|
|
128
|
+
```python
|
|
129
|
+
# Yaratish yoki o'qish (Bazada bo'lmasa yaratadi)
|
|
130
|
+
user, created = User.get_or_create(name="Ali", defaults={'age': 25, 'score': 10.0})
|
|
131
|
+
|
|
132
|
+
# Ommaviy yangilash (Xotirani to'ldirmasdan)
|
|
133
|
+
User.query().filter(age__lt=18).update(score=0.0)
|
|
134
|
+
|
|
135
|
+
# Ommaviy o'chirish
|
|
136
|
+
User.query().filter(score=0.0).delete()
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Tezkor O'qish (Values & Values List):**
|
|
140
|
+
Agar modellar bilan ishlash shart bo'lmasa va faqat JSON yuborish kerak bo'lsa:
|
|
141
|
+
```python
|
|
142
|
+
users_dict = User.query().values('id', 'name').all()
|
|
143
|
+
# [{'id': 1, 'name': 'Ali'}, {'id': 2, 'name': 'Vali'}]
|
|
144
|
+
|
|
145
|
+
names_list = User.query().values_list('name', flat=True).all()
|
|
146
|
+
# ['Ali', 'Vali']
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## 🔄 Migratsiya Tizimi (CLI)
|
|
152
|
+
|
|
153
|
+
Siz o'z loyihangiz papkasida bitta `manage.py` yaratib olib, jadvallarni SQL o'qib-o'tirmasdan terminal orqali avtomatik yangilab borishingiz mumkin.
|
|
154
|
+
|
|
155
|
+
**`manage.py`:**
|
|
156
|
+
```python
|
|
157
|
+
import sys
|
|
158
|
+
from postgresdb3 import execute_from_command_line
|
|
159
|
+
from myapp.models import db_sync # Sizning bazaga ulangan db obyektingiz
|
|
160
|
+
|
|
161
|
+
# Modellar dvigatelga ko'rinishi uchun barchasini import qilish SHART!
|
|
162
|
+
from myapp.models import User, Post
|
|
163
|
+
|
|
164
|
+
if __name__ == "__main__":
|
|
165
|
+
execute_from_command_line(db_sync, sys.argv)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Terminalda:**
|
|
169
|
+
```bash
|
|
170
|
+
# Modellar asosida migratsiya faylini tayyorlash
|
|
171
|
+
python manage.py makemigrations initial_setup
|
|
172
|
+
|
|
173
|
+
# Bazaga jadvallarni qo'shish
|
|
174
|
+
python manage.py migrate
|
|
175
|
+
|
|
176
|
+
# Oxirgi migratsiyani bekor qilish va qaytarish (Rollback)
|
|
177
|
+
python manage.py undo
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## 🔍 Murakkab Qidiruv va Aloqalar (Relations)
|
|
183
|
+
|
|
184
|
+
**Auto-Join (Bog'langan jadvallar bo'ylab o'tish):**
|
|
185
|
+
Qo'lda JOIN yozish o'rniga, qo'shaloq chiziqcha `__` ishlating:
|
|
186
|
+
```python
|
|
187
|
+
posts = Post.query().filter(author__name__startswith='A').all()
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
**Q va F ifodalar (Murakkab shartlar):**
|
|
191
|
+
```python
|
|
192
|
+
from postgresdb3 import Q, F
|
|
193
|
+
|
|
194
|
+
# OR (|) sharti
|
|
195
|
+
User.query().filter(Q(age__gt=20) | Q(name="Ali")).all()
|
|
196
|
+
|
|
197
|
+
# Yoshiga nisbatan score'i katta bo'lganlarni izlash
|
|
198
|
+
User.query().filter(score__gt=F("age")).all()
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**N+1 xatoligini chetlab o'tish (select_related & prefetch_related):**
|
|
202
|
+
```python
|
|
203
|
+
# ForeignKey uchun (1 ta SQL so'rov)
|
|
204
|
+
posts = Post.query().select_related("author").all()
|
|
205
|
+
|
|
206
|
+
# ManyToMany uchun (Optimallashtirilgan yig'ish)
|
|
207
|
+
posts = Post.query().prefetch_related("tags").all()
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## 📊 Katta Ma'lumotlar bilan Ishlash (Bulk & Pagination)
|
|
213
|
+
|
|
214
|
+
**Sahifalash (Pagination):**
|
|
215
|
+
Foydalanuvchiga ma'lumotlarni qismlab taqdim etish:
|
|
216
|
+
```python
|
|
217
|
+
result = User.query().paginate(page=1, per_page=20)
|
|
218
|
+
print(result['total']) # Jami elementlar soni
|
|
219
|
+
print(result['has_next']) # Keyingi sahifa bormi?
|
|
220
|
+
print(result['data']) # Modellar ro'yxati (20 ta)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
**Ommaviy Yozish (Bulk Create):**
|
|
224
|
+
10,000 ta obyekti birma-bir emas, bitta so'rov orqali kiritish:
|
|
225
|
+
```python
|
|
226
|
+
users = [User(name=f"U-{i}", age=20) for i in range(10000)]
|
|
227
|
+
User.bulk_create(users)
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## 📈 Agregatsiya va Annotatsiya (GroupBy)
|
|
233
|
+
|
|
234
|
+
Baza orqali kalkulyatsiyalarni amalga oshirish:
|
|
235
|
+
|
|
236
|
+
```python
|
|
237
|
+
from postgresdb3 import Sum, Avg, Count
|
|
238
|
+
|
|
239
|
+
# Jami obshiy statiska:
|
|
240
|
+
result = User.query().aggregate(
|
|
241
|
+
total_score=Sum("score"),
|
|
242
|
+
avg_age=Avg("age")
|
|
243
|
+
)
|
|
244
|
+
# {'total_score': 1500, 'avg_age': 25}
|
|
245
|
+
|
|
246
|
+
# Guruhlab hisoblash (Masalan: Har bir userning postlari soni):
|
|
247
|
+
users_with_count = User.query().annotate(post_count=Count("posts")).all()
|
|
248
|
+
for u in users_with_count:
|
|
249
|
+
print(u.name, u.post_count)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
*Ushbu ORM kutubxonasi Python olamidagi ilg'or texnologiyalar yordamida tezlik va barqarorlikni birinchi o'ringa qo'yuvchi mutaxassislar uchun maxsus qurilgan.*
|
|
@@ -14,6 +14,37 @@ Kutubxonani o'rnatish uchun terminalda quyidagi buyruqni kiriting:
|
|
|
14
14
|
pip install postgresdb3
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
+
### ⚠️ Linux/macOS tizimlarida o'rnatish va muammolar yechimi
|
|
18
|
+
|
|
19
|
+
`postgresdb3` ishlashi uchun sinxron ulanishlarda `psycopg2` kutubxonasidan foydalanadi. O'rnatish vaqtida tizimingiz holatidan kelib chiqib, quyidagi ikki variantdan biri avtomatik tanlanadi:
|
|
20
|
+
|
|
21
|
+
1. **`psycopg2` (Kompilyatsiya qilinadigan variant)**: Agar tizimingizda PostgreSQL ning ishlab chiquvchi kutubxonalari (`pg_config` va `pg_config.h` fayllari, masalan `libpq-dev`) mavjud bo'lsa, kutubxona manbadan (source) kompilyatsiya qilinadi. Bu ishlab chiqarish (production) muhitlari uchun tavsiya etiladi.
|
|
22
|
+
2. **`psycopg2-binary` (Tayyor variant)**: Agar tizimingizda kompilyatsiya qilish uchun kerakli PostgreSQL kutubxonalari topilmasa, o'rnatish jarayoni muvaffaqiyatli yakunlanishi uchun avtomatik ravishda tayyor pre-compiled `psycopg2-binary` o'rnatiladi. Bu lokal dasturlash va test muhitlari uchun juda mos keladi.
|
|
23
|
+
|
|
24
|
+
#### Tizim kutubxonalarini o'rnatish (Tavsiya etiladi)
|
|
25
|
+
Ishlab chiqarish (production) muhitida toza `psycopg2` (manbadan yig'iladigan) ishlatish tavsiya etiladi. Buning uchun `postgresdb3` ni o'rnatishdan oldin quyidagi tizim paketlarini o'rnatib olishingiz kerak bo'ladi:
|
|
26
|
+
|
|
27
|
+
* **Debian/Ubuntu:**
|
|
28
|
+
```bash
|
|
29
|
+
sudo apt update
|
|
30
|
+
sudo apt install python3-dev libpq-dev build-essential
|
|
31
|
+
```
|
|
32
|
+
* **RedHat/CentOS/Fedora/Rocky Linux:**
|
|
33
|
+
```bash
|
|
34
|
+
sudo dnf install python3-devel postgresql-devel gcc
|
|
35
|
+
```
|
|
36
|
+
* **macOS (Homebrew):**
|
|
37
|
+
```bash
|
|
38
|
+
brew install libpq
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### Majburiy kompilyatsiya rejimini yoqish
|
|
42
|
+
Agar siz pre-compiled binary versiyadan mutlaqo foydalanmasdan, toza manbali `psycopg2` ni majburiy ravishda yig'moqchi bo'lsangiz, terminalda quyidagi muhit o'zgaruvchisini (env variable) ko'rsatgan holda o'rnatishni amalga oshiring:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
POSTGRESDB_SOURCE_PSYCOPG2=1 pip install postgresdb3
|
|
46
|
+
```
|
|
47
|
+
|
|
17
48
|
---
|
|
18
49
|
|
|
19
50
|
## 🚀 Yangi (v2.0) Imkoniyatlari
|