altcodepro-polydb-python 2.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.
Files changed (65) hide show
  1. altcodepro_polydb_python-2.1.0/LICENSE +21 -0
  2. altcodepro_polydb_python-2.1.0/MANIFEST.in +17 -0
  3. altcodepro_polydb_python-2.1.0/PKG-INFO +378 -0
  4. altcodepro_polydb_python-2.1.0/README.md +298 -0
  5. altcodepro_polydb_python-2.1.0/example_usage.py +189 -0
  6. altcodepro_polydb_python-2.1.0/pyproject.toml +170 -0
  7. altcodepro_polydb_python-2.1.0/requirements-aws.txt +6 -0
  8. altcodepro_polydb_python-2.1.0/requirements-azure.txt +9 -0
  9. altcodepro_polydb_python-2.1.0/requirements-dev.txt +22 -0
  10. altcodepro_polydb_python-2.1.0/requirements-gcp.txt +16 -0
  11. altcodepro_polydb_python-2.1.0/requirements-generic.txt +7 -0
  12. altcodepro_polydb_python-2.1.0/requirements.txt +268 -0
  13. altcodepro_polydb_python-2.1.0/setup.cfg +4 -0
  14. altcodepro_polydb_python-2.1.0/setup.py +10 -0
  15. altcodepro_polydb_python-2.1.0/src/altcodepro_polydb_python.egg-info/PKG-INFO +378 -0
  16. altcodepro_polydb_python-2.1.0/src/altcodepro_polydb_python.egg-info/SOURCES.txt +63 -0
  17. altcodepro_polydb_python-2.1.0/src/altcodepro_polydb_python.egg-info/dependency_links.txt +1 -0
  18. altcodepro_polydb_python-2.1.0/src/altcodepro_polydb_python.egg-info/requires.txt +63 -0
  19. altcodepro_polydb_python-2.1.0/src/altcodepro_polydb_python.egg-info/top_level.txt +1 -0
  20. altcodepro_polydb_python-2.1.0/src/polydb/__init__.py +64 -0
  21. altcodepro_polydb_python-2.1.0/src/polydb/adapters/AzureBlobStorageAdapter.py +77 -0
  22. altcodepro_polydb_python-2.1.0/src/polydb/adapters/AzureFileStorageAdapter.py +79 -0
  23. altcodepro_polydb_python-2.1.0/src/polydb/adapters/AzureQueueAdapter.py +61 -0
  24. altcodepro_polydb_python-2.1.0/src/polydb/adapters/AzureTableStorageAdapter.py +182 -0
  25. altcodepro_polydb_python-2.1.0/src/polydb/adapters/DynamoDBAdapter.py +216 -0
  26. altcodepro_polydb_python-2.1.0/src/polydb/adapters/EFSAdapter.py +50 -0
  27. altcodepro_polydb_python-2.1.0/src/polydb/adapters/FirestoreAdapter.py +193 -0
  28. altcodepro_polydb_python-2.1.0/src/polydb/adapters/GCPStorageAdapter.py +81 -0
  29. altcodepro_polydb_python-2.1.0/src/polydb/adapters/MongoDBAdapter.py +136 -0
  30. altcodepro_polydb_python-2.1.0/src/polydb/adapters/PostgreSQLAdapter.py +453 -0
  31. altcodepro_polydb_python-2.1.0/src/polydb/adapters/PubSubAdapter.py +83 -0
  32. altcodepro_polydb_python-2.1.0/src/polydb/adapters/S3Adapter.py +86 -0
  33. altcodepro_polydb_python-2.1.0/src/polydb/adapters/S3CompatibleAdapter.py +90 -0
  34. altcodepro_polydb_python-2.1.0/src/polydb/adapters/SQSAdapter.py +84 -0
  35. altcodepro_polydb_python-2.1.0/src/polydb/adapters/VercelKVAdapter.py +327 -0
  36. altcodepro_polydb_python-2.1.0/src/polydb/adapters/__init__.py +0 -0
  37. altcodepro_polydb_python-2.1.0/src/polydb/advanced_query.py +147 -0
  38. altcodepro_polydb_python-2.1.0/src/polydb/audit/AuditStorage.py +136 -0
  39. altcodepro_polydb_python-2.1.0/src/polydb/audit/__init__.py +7 -0
  40. altcodepro_polydb_python-2.1.0/src/polydb/audit/context.py +53 -0
  41. altcodepro_polydb_python-2.1.0/src/polydb/audit/manager.py +47 -0
  42. altcodepro_polydb_python-2.1.0/src/polydb/audit/models.py +86 -0
  43. altcodepro_polydb_python-2.1.0/src/polydb/base/NoSQLKVAdapter.py +301 -0
  44. altcodepro_polydb_python-2.1.0/src/polydb/base/ObjectStorageAdapter.py +42 -0
  45. altcodepro_polydb_python-2.1.0/src/polydb/base/QueueAdapter.py +27 -0
  46. altcodepro_polydb_python-2.1.0/src/polydb/base/SharedFilesAdapter.py +32 -0
  47. altcodepro_polydb_python-2.1.0/src/polydb/base/__init__.py +0 -0
  48. altcodepro_polydb_python-2.1.0/src/polydb/batch.py +163 -0
  49. altcodepro_polydb_python-2.1.0/src/polydb/cache.py +204 -0
  50. altcodepro_polydb_python-2.1.0/src/polydb/databaseFactory.py +748 -0
  51. altcodepro_polydb_python-2.1.0/src/polydb/decorators.py +21 -0
  52. altcodepro_polydb_python-2.1.0/src/polydb/errors.py +82 -0
  53. altcodepro_polydb_python-2.1.0/src/polydb/factory.py +107 -0
  54. altcodepro_polydb_python-2.1.0/src/polydb/models.py +39 -0
  55. altcodepro_polydb_python-2.1.0/src/polydb/monitoring.py +313 -0
  56. altcodepro_polydb_python-2.1.0/src/polydb/multitenancy.py +197 -0
  57. altcodepro_polydb_python-2.1.0/src/polydb/py.typed +0 -0
  58. altcodepro_polydb_python-2.1.0/src/polydb/query.py +150 -0
  59. altcodepro_polydb_python-2.1.0/src/polydb/registry.py +71 -0
  60. altcodepro_polydb_python-2.1.0/src/polydb/retry.py +76 -0
  61. altcodepro_polydb_python-2.1.0/src/polydb/schema.py +205 -0
  62. altcodepro_polydb_python-2.1.0/src/polydb/security.py +458 -0
  63. altcodepro_polydb_python-2.1.0/src/polydb/types.py +127 -0
  64. altcodepro_polydb_python-2.1.0/src/polydb/utils.py +61 -0
  65. altcodepro_polydb_python-2.1.0/src/polydb/validation.py +131 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 AltCodePro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,17 @@
1
+ # Include documentation
2
+ include README.md
3
+ include LICENSE
4
+ include pyproject.toml
5
+
6
+ # Include requirements files
7
+ include requirements*.txt
8
+
9
+ # Include examples
10
+ include example_usage.py
11
+
12
+ # Exclude unnecessary files
13
+ exclude .gitignore
14
+ exclude .env*
15
+ recursive-exclude * __pycache__
16
+ recursive-exclude * *.py[co]
17
+ recursive-exclude * .DS_Store
@@ -0,0 +1,378 @@
1
+ Metadata-Version: 2.4
2
+ Name: altcodepro-polydb-python
3
+ Version: 2.1.0
4
+ Summary: Production-ready multi-cloud database abstraction layer with connection pooling, retry logic, and thread safety
5
+ Author: AltCodePro
6
+ Project-URL: Homepage, https://github.com/altcodepro/polydb-python
7
+ Project-URL: Documentation, https://github.com/altcodepro/polydb-python#readme
8
+ Project-URL: Repository, https://github.com/altcodepro/polydb-python
9
+ Project-URL: Bug Tracker, https://github.com/altcodepro/polydb-python/issues
10
+ Keywords: database,cloud,multi-cloud,aws,azure,gcp,abstraction,nosql,sql,postgres,mongodb,dynamodb,s3
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Topic :: Database
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.8
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Operating System :: OS Independent
23
+ Requires-Python: >=3.8
24
+ Description-Content-Type: text/markdown
25
+ License-File: LICENSE
26
+ Requires-Dist: psycopg2-binary>=2.9.11
27
+ Requires-Dist: tenacity>=9.1.4
28
+ Provides-Extra: aws
29
+ Requires-Dist: boto3>=1.42.47; extra == "aws"
30
+ Requires-Dist: botocore>=1.42.47; extra == "aws"
31
+ Provides-Extra: azure
32
+ Requires-Dist: azure-core>=1.38.1; extra == "azure"
33
+ Requires-Dist: azure-data-tables>=12.7.0; extra == "azure"
34
+ Requires-Dist: azure-storage-blob>=12.28.0; extra == "azure"
35
+ Requires-Dist: azure-storage-file-share>=12.24.0; extra == "azure"
36
+ Requires-Dist: azure-storage-queue>=12.15.0; extra == "azure"
37
+ Provides-Extra: gcp
38
+ Requires-Dist: google-api-core>=2.29.0; extra == "gcp"
39
+ Requires-Dist: google-auth>=2.48.0; extra == "gcp"
40
+ Requires-Dist: google-cloud-core>=2.5.0; extra == "gcp"
41
+ Requires-Dist: google-cloud-firestore>=2.23.0; extra == "gcp"
42
+ Requires-Dist: google-cloud-pubsub>=2.35.0; extra == "gcp"
43
+ Requires-Dist: google-cloud-storage>=3.9.0; extra == "gcp"
44
+ Provides-Extra: mongodb
45
+ Requires-Dist: pymongo>=4.16.0; extra == "mongodb"
46
+ Provides-Extra: rabbitmq
47
+ Requires-Dist: pika>=1.3.2; extra == "rabbitmq"
48
+ Provides-Extra: vercel
49
+ Requires-Dist: requests>=2.32.5; extra == "vercel"
50
+ Provides-Extra: generic
51
+ Requires-Dist: pymongo>=4.16.0; extra == "generic"
52
+ Requires-Dist: pika>=1.3.2; extra == "generic"
53
+ Requires-Dist: boto3>=1.42.47; extra == "generic"
54
+ Provides-Extra: all
55
+ Requires-Dist: boto3>=1.42.47; extra == "all"
56
+ Requires-Dist: botocore>=1.42.47; extra == "all"
57
+ Requires-Dist: azure-core>=1.38.1; extra == "all"
58
+ Requires-Dist: azure-data-tables>=12.7.0; extra == "all"
59
+ Requires-Dist: azure-storage-blob>=12.28.0; extra == "all"
60
+ Requires-Dist: azure-storage-file-share>=12.24.0; extra == "all"
61
+ Requires-Dist: azure-storage-queue>=12.15.0; extra == "all"
62
+ Requires-Dist: google-cloud-firestore>=2.23.0; extra == "all"
63
+ Requires-Dist: google-cloud-pubsub>=2.35.0; extra == "all"
64
+ Requires-Dist: google-cloud-storage>=3.9.0; extra == "all"
65
+ Requires-Dist: pymongo>=4.16.0; extra == "all"
66
+ Requires-Dist: pika>=1.3.2; extra == "all"
67
+ Requires-Dist: requests>=2.32.5; extra == "all"
68
+ Requires-Dist: redis>=6.4.0; extra == "all"
69
+ Provides-Extra: dev
70
+ Requires-Dist: black>=26.1.0; extra == "dev"
71
+ Requires-Dist: flake8>=7.3.0; extra == "dev"
72
+ Requires-Dist: isort>=7.0.0; extra == "dev"
73
+ Requires-Dist: mypy>=1.19.1; extra == "dev"
74
+ Provides-Extra: test
75
+ Requires-Dist: pytest>=9.0.2; extra == "test"
76
+ Requires-Dist: pytest-cov>=7.0.0; extra == "test"
77
+ Requires-Dist: pytest-mock>=3.15.1; extra == "test"
78
+ Requires-Dist: moto>=5.1.21; extra == "test"
79
+ Dynamic: license-file
80
+
81
+ # PolyDB v3.0 - Enterprise Database Abstraction Layer
82
+
83
+ **Production-ready, cloud-independent database abstraction with full LINQ support, field-level audit, cache, and overflow storage**
84
+
85
+ ## Features
86
+
87
+ ✅ **LINQ-Style Queries** - All SQL clauses: WHERE, ORDER BY, SELECT, GROUP BY, DISTINCT, COUNT
88
+ ✅ **Multi-Cloud** - Azure, AWS, GCP, Vercel, MongoDB, PostgreSQL
89
+ ✅ **Automatic Overflow** - Large NoSQL records → Object Storage (transparent)
90
+ ✅ **Enterprise Audit** - Cryptographic hash chain, field-level changes, strict ordering
91
+ ✅ **Cache Engine** - In-memory with TTL, automatic invalidation
92
+ ✅ **Soft Delete** - Optional logical deletion with audit trail
93
+ ✅ **Auto-Inject** - Tenant ID, audit fields (created_at, updated_by, etc.)
94
+ ✅ **Thread-Safe** - Connection pooling, distributed-safe hash chaining
95
+ ✅ **Retry Logic** - Exponential backoff, configurable
96
+ ✅ **Type-Safe** - Protocol-based adapters, full type hints
97
+
98
+ ## Quick Start
99
+
100
+ ```python
101
+ from polydb import DatabaseFactory, polydb_model, QueryBuilder, Operator, AuditContext
102
+
103
+ # 1. Define model
104
+ @polydb_model
105
+ class User:
106
+ __polydb__ = {
107
+ "storage": "sql",
108
+ "table": "users",
109
+ "cache": True,
110
+ "cache_ttl": 600,
111
+ }
112
+
113
+ # 2. Initialize
114
+ db = DatabaseFactory(
115
+ enable_audit=True,
116
+ enable_cache=True,
117
+ soft_delete=True,
118
+ )
119
+
120
+ # 3. Set context (per-request)
121
+ AuditContext.set(
122
+ actor_id="user_123",
123
+ roles=["admin"],
124
+ tenant_id="tenant_abc",
125
+ )
126
+
127
+ # 4. CRUD with auto-audit
128
+ user = db.create(User, {"name": "John", "email": "john@example.com"})
129
+ # Auto-injects: tenant_id, created_at, created_by, updated_at, updated_by
130
+
131
+ # 5. LINQ queries
132
+ query = (
133
+ QueryBuilder()
134
+ .where("role", Operator.EQ, "admin")
135
+ .where("age", Operator.GTE, 18)
136
+ .order_by("created_at", descending=True)
137
+ .skip(10)
138
+ .take(20)
139
+ .select("id", "name", "email")
140
+ )
141
+
142
+ admins = db.query_linq(User, query)
143
+ ```
144
+
145
+ ## LINQ Operations
146
+
147
+ ### Filters
148
+ ```python
149
+ builder.where("field", Operator.EQ, value) # ==
150
+ builder.where("field", Operator.NE, value) # !=
151
+ builder.where("field", Operator.GT, value) # >
152
+ builder.where("field", Operator.GTE, value) # >=
153
+ builder.where("field", Operator.LT, value) # <
154
+ builder.where("field", Operator.LTE, value) # <=
155
+ builder.where("field", Operator.IN, [1,2,3]) # IN
156
+ builder.where("field", Operator.CONTAINS, "text") # LIKE
157
+ builder.where("field", Operator.STARTS_WITH, "prefix")
158
+ builder.where("field", Operator.ENDS_WITH, "suffix")
159
+ ```
160
+
161
+ ### Ordering & Pagination
162
+ ```python
163
+ builder.order_by("field", descending=True)
164
+ builder.skip(10)
165
+ builder.take(20)
166
+ ```
167
+
168
+ ### Projection
169
+ ```python
170
+ builder.select("id", "name", "email")
171
+ ```
172
+
173
+ ### Aggregation
174
+ ```python
175
+ builder.count()
176
+ builder.distinct_on()
177
+ builder.group_by("role", "department")
178
+ ```
179
+
180
+ ## NoSQL Overflow Storage
181
+
182
+ Records >1MB automatically stored in object storage:
183
+
184
+ ```python
185
+ @polydb_model
186
+ class Product:
187
+ __polydb__ = {
188
+ "storage": "nosql",
189
+ "collection": "products",
190
+ }
191
+
192
+ # Large data automatically overflows
193
+ product = db.create(Product, {
194
+ "data": {"huge": "payload"} * 100000
195
+ })
196
+
197
+ # Transparent retrieval
198
+ retrieved = db.read_one(Product, {"id": product["id"]})
199
+ # User never knows it came from blob storage
200
+ ```
201
+
202
+ ## Enterprise Audit Trail
203
+
204
+ ### Automatic Tracking
205
+ - **Who**: actor_id, roles
206
+ - **What**: action, model, entity_id, changed_fields
207
+ - **When**: timestamp (microsecond precision)
208
+ - **Where**: tenant_id, ip_address, user_agent
209
+ - **Context**: trace_id, request_id
210
+ - **Integrity**: cryptographic hash chain
211
+
212
+ ### Field-Level Changes
213
+ ```python
214
+ db.update(User, user_id, {"email": "new@example.com"})
215
+ # Audit log shows: changed_fields = ["email", "updated_at", "updated_by"]
216
+ ```
217
+
218
+ ### Verify Chain
219
+ ```python
220
+ from polydb.audit.storage import AuditStorage
221
+
222
+ audit = AuditStorage()
223
+ is_valid = audit.verify_chain(tenant_id="tenant_abc")
224
+ ```
225
+
226
+ ## Cache Engine
227
+
228
+ ```python
229
+ # Auto-cache from model metadata
230
+ @polydb_model
231
+ class User:
232
+ __polydb__ = {
233
+ "storage": "sql",
234
+ "table": "users",
235
+ "cache": True,
236
+ "cache_ttl": 600, # 10 minutes
237
+ }
238
+
239
+ # Cached read
240
+ users = db.read(User, {"role": "admin"})
241
+
242
+ # Bypass cache
243
+ users = db.read(User, {"role": "admin"}, no_cache=True)
244
+
245
+ # Manual invalidation
246
+ from polydb.cache import CacheEngine
247
+ cache = CacheEngine()
248
+ cache.invalidate("User")
249
+ cache.clear()
250
+ ```
251
+
252
+ ## Soft Delete
253
+
254
+ ```python
255
+ db = DatabaseFactory(soft_delete=True)
256
+
257
+ # Soft delete (sets deleted_at, deleted_by)
258
+ db.delete(User, user_id)
259
+
260
+ # Hard delete
261
+ db.delete(User, user_id, hard=True)
262
+
263
+ # Include deleted in queries
264
+ all_users = db.read(User, {}, include_deleted=True)
265
+ ```
266
+
267
+ ## Pagination
268
+
269
+ ```python
270
+ page1, next_token = db.read_page(User, {"role": "admin"}, page_size=50)
271
+ page2, token2 = db.read_page(User, {"role": "admin"}, page_size=50, continuation_token=next_token)
272
+ ```
273
+
274
+ ## Multi-Tenant
275
+
276
+ ```python
277
+ # Auto-inject tenant_id from context
278
+ AuditContext.set(tenant_id="tenant_123", actor_id="user_456", roles=["admin"])
279
+
280
+ user = db.create(User, {"name": "John"})
281
+ # Result: {"name": "John", "tenant_id": "tenant_123", "created_by": "user_456", ...}
282
+
283
+ # Filter by tenant (automatic)
284
+ users = db.read(User, {}) # Only returns tenant_123 records
285
+ ```
286
+
287
+ ## Environment Variables
288
+
289
+ ```bash
290
+ # Provider selection (optional, auto-detected)
291
+ CLOUD_PROVIDER=aws|azure|gcp|vercel|mongodb|postgresql
292
+
293
+ # PostgreSQL
294
+ POSTGRES_CONNECTION_STRING=postgresql://user:pass@host:5432/db
295
+ POSTGRES_MIN_CONNECTIONS=2
296
+ POSTGRES_MAX_CONNECTIONS=20
297
+
298
+ # AWS
299
+ AWS_ACCESS_KEY_ID=...
300
+ AWS_SECRET_ACCESS_KEY=...
301
+ DYNAMODB_TABLE_NAME=...
302
+ S3_BUCKET_NAME=...
303
+
304
+ # Azure
305
+ AZURE_STORAGE_CONNECTION_STRING=...
306
+ AZURE_TABLE_NAME=...
307
+ AZURE_CONTAINER_NAME=...
308
+
309
+ # GCP
310
+ GOOGLE_CLOUD_PROJECT=...
311
+ FIRESTORE_COLLECTION=...
312
+ GCS_BUCKET_NAME=...
313
+
314
+ # MongoDB
315
+ MONGODB_URI=mongodb://...
316
+ MONGODB_DATABASE=...
317
+ MONGODB_COLLECTION=...
318
+
319
+ # Vercel
320
+ KV_URL=...
321
+ KV_REST_API_TOKEN=...
322
+ BLOB_READ_WRITE_TOKEN=...
323
+ ```
324
+
325
+ ## Model Metadata
326
+
327
+ ```python
328
+ @polydb_model
329
+ class Entity:
330
+ __polydb__ = {
331
+ # Required
332
+ "storage": "sql" | "nosql",
333
+
334
+ # SQL
335
+ "table": "table_name",
336
+
337
+ # NoSQL
338
+ "collection": "collection_name",
339
+ "pk_field": "partition_key_field",
340
+ "rk_field": "row_key_field",
341
+
342
+ # Cache
343
+ "cache": True,
344
+ "cache_ttl": 300, # seconds
345
+
346
+ # Optional
347
+ "provider": "aws", # Override auto-detection
348
+ }
349
+ ```
350
+
351
+ ## Thread Safety
352
+
353
+ - Connection pooling with locks
354
+ - Distributed-safe audit hash chaining
355
+ - Cache with thread-safe operations
356
+ - Retry logic with exponential backoff
357
+
358
+ ## Performance
359
+
360
+ - **SQL**: Connection pooling (min=2, max=20)
361
+ - **NoSQL**: Client reuse, overflow storage
362
+ - **Cache**: In-memory with automatic invalidation
363
+ - **Retry**: Configurable backoff (0.5s-6s)
364
+
365
+ ## Production Checklist
366
+
367
+ ✅ Set `CLOUD_PROVIDER` explicitly
368
+ ✅ Configure connection pool sizes
369
+ ✅ Enable audit (`enable_audit=True`)
370
+ ✅ Set cache TTL per model
371
+ ✅ Use soft delete for compliance
372
+ ✅ Set audit context per request
373
+ ✅ Monitor audit chain integrity
374
+ ✅ Configure retry attempts
375
+
376
+ ## License
377
+
378
+ MIT
@@ -0,0 +1,298 @@
1
+ # PolyDB v3.0 - Enterprise Database Abstraction Layer
2
+
3
+ **Production-ready, cloud-independent database abstraction with full LINQ support, field-level audit, cache, and overflow storage**
4
+
5
+ ## Features
6
+
7
+ ✅ **LINQ-Style Queries** - All SQL clauses: WHERE, ORDER BY, SELECT, GROUP BY, DISTINCT, COUNT
8
+ ✅ **Multi-Cloud** - Azure, AWS, GCP, Vercel, MongoDB, PostgreSQL
9
+ ✅ **Automatic Overflow** - Large NoSQL records → Object Storage (transparent)
10
+ ✅ **Enterprise Audit** - Cryptographic hash chain, field-level changes, strict ordering
11
+ ✅ **Cache Engine** - In-memory with TTL, automatic invalidation
12
+ ✅ **Soft Delete** - Optional logical deletion with audit trail
13
+ ✅ **Auto-Inject** - Tenant ID, audit fields (created_at, updated_by, etc.)
14
+ ✅ **Thread-Safe** - Connection pooling, distributed-safe hash chaining
15
+ ✅ **Retry Logic** - Exponential backoff, configurable
16
+ ✅ **Type-Safe** - Protocol-based adapters, full type hints
17
+
18
+ ## Quick Start
19
+
20
+ ```python
21
+ from polydb import DatabaseFactory, polydb_model, QueryBuilder, Operator, AuditContext
22
+
23
+ # 1. Define model
24
+ @polydb_model
25
+ class User:
26
+ __polydb__ = {
27
+ "storage": "sql",
28
+ "table": "users",
29
+ "cache": True,
30
+ "cache_ttl": 600,
31
+ }
32
+
33
+ # 2. Initialize
34
+ db = DatabaseFactory(
35
+ enable_audit=True,
36
+ enable_cache=True,
37
+ soft_delete=True,
38
+ )
39
+
40
+ # 3. Set context (per-request)
41
+ AuditContext.set(
42
+ actor_id="user_123",
43
+ roles=["admin"],
44
+ tenant_id="tenant_abc",
45
+ )
46
+
47
+ # 4. CRUD with auto-audit
48
+ user = db.create(User, {"name": "John", "email": "john@example.com"})
49
+ # Auto-injects: tenant_id, created_at, created_by, updated_at, updated_by
50
+
51
+ # 5. LINQ queries
52
+ query = (
53
+ QueryBuilder()
54
+ .where("role", Operator.EQ, "admin")
55
+ .where("age", Operator.GTE, 18)
56
+ .order_by("created_at", descending=True)
57
+ .skip(10)
58
+ .take(20)
59
+ .select("id", "name", "email")
60
+ )
61
+
62
+ admins = db.query_linq(User, query)
63
+ ```
64
+
65
+ ## LINQ Operations
66
+
67
+ ### Filters
68
+ ```python
69
+ builder.where("field", Operator.EQ, value) # ==
70
+ builder.where("field", Operator.NE, value) # !=
71
+ builder.where("field", Operator.GT, value) # >
72
+ builder.where("field", Operator.GTE, value) # >=
73
+ builder.where("field", Operator.LT, value) # <
74
+ builder.where("field", Operator.LTE, value) # <=
75
+ builder.where("field", Operator.IN, [1,2,3]) # IN
76
+ builder.where("field", Operator.CONTAINS, "text") # LIKE
77
+ builder.where("field", Operator.STARTS_WITH, "prefix")
78
+ builder.where("field", Operator.ENDS_WITH, "suffix")
79
+ ```
80
+
81
+ ### Ordering & Pagination
82
+ ```python
83
+ builder.order_by("field", descending=True)
84
+ builder.skip(10)
85
+ builder.take(20)
86
+ ```
87
+
88
+ ### Projection
89
+ ```python
90
+ builder.select("id", "name", "email")
91
+ ```
92
+
93
+ ### Aggregation
94
+ ```python
95
+ builder.count()
96
+ builder.distinct_on()
97
+ builder.group_by("role", "department")
98
+ ```
99
+
100
+ ## NoSQL Overflow Storage
101
+
102
+ Records >1MB automatically stored in object storage:
103
+
104
+ ```python
105
+ @polydb_model
106
+ class Product:
107
+ __polydb__ = {
108
+ "storage": "nosql",
109
+ "collection": "products",
110
+ }
111
+
112
+ # Large data automatically overflows
113
+ product = db.create(Product, {
114
+ "data": {"huge": "payload"} * 100000
115
+ })
116
+
117
+ # Transparent retrieval
118
+ retrieved = db.read_one(Product, {"id": product["id"]})
119
+ # User never knows it came from blob storage
120
+ ```
121
+
122
+ ## Enterprise Audit Trail
123
+
124
+ ### Automatic Tracking
125
+ - **Who**: actor_id, roles
126
+ - **What**: action, model, entity_id, changed_fields
127
+ - **When**: timestamp (microsecond precision)
128
+ - **Where**: tenant_id, ip_address, user_agent
129
+ - **Context**: trace_id, request_id
130
+ - **Integrity**: cryptographic hash chain
131
+
132
+ ### Field-Level Changes
133
+ ```python
134
+ db.update(User, user_id, {"email": "new@example.com"})
135
+ # Audit log shows: changed_fields = ["email", "updated_at", "updated_by"]
136
+ ```
137
+
138
+ ### Verify Chain
139
+ ```python
140
+ from polydb.audit.storage import AuditStorage
141
+
142
+ audit = AuditStorage()
143
+ is_valid = audit.verify_chain(tenant_id="tenant_abc")
144
+ ```
145
+
146
+ ## Cache Engine
147
+
148
+ ```python
149
+ # Auto-cache from model metadata
150
+ @polydb_model
151
+ class User:
152
+ __polydb__ = {
153
+ "storage": "sql",
154
+ "table": "users",
155
+ "cache": True,
156
+ "cache_ttl": 600, # 10 minutes
157
+ }
158
+
159
+ # Cached read
160
+ users = db.read(User, {"role": "admin"})
161
+
162
+ # Bypass cache
163
+ users = db.read(User, {"role": "admin"}, no_cache=True)
164
+
165
+ # Manual invalidation
166
+ from polydb.cache import CacheEngine
167
+ cache = CacheEngine()
168
+ cache.invalidate("User")
169
+ cache.clear()
170
+ ```
171
+
172
+ ## Soft Delete
173
+
174
+ ```python
175
+ db = DatabaseFactory(soft_delete=True)
176
+
177
+ # Soft delete (sets deleted_at, deleted_by)
178
+ db.delete(User, user_id)
179
+
180
+ # Hard delete
181
+ db.delete(User, user_id, hard=True)
182
+
183
+ # Include deleted in queries
184
+ all_users = db.read(User, {}, include_deleted=True)
185
+ ```
186
+
187
+ ## Pagination
188
+
189
+ ```python
190
+ page1, next_token = db.read_page(User, {"role": "admin"}, page_size=50)
191
+ page2, token2 = db.read_page(User, {"role": "admin"}, page_size=50, continuation_token=next_token)
192
+ ```
193
+
194
+ ## Multi-Tenant
195
+
196
+ ```python
197
+ # Auto-inject tenant_id from context
198
+ AuditContext.set(tenant_id="tenant_123", actor_id="user_456", roles=["admin"])
199
+
200
+ user = db.create(User, {"name": "John"})
201
+ # Result: {"name": "John", "tenant_id": "tenant_123", "created_by": "user_456", ...}
202
+
203
+ # Filter by tenant (automatic)
204
+ users = db.read(User, {}) # Only returns tenant_123 records
205
+ ```
206
+
207
+ ## Environment Variables
208
+
209
+ ```bash
210
+ # Provider selection (optional, auto-detected)
211
+ CLOUD_PROVIDER=aws|azure|gcp|vercel|mongodb|postgresql
212
+
213
+ # PostgreSQL
214
+ POSTGRES_CONNECTION_STRING=postgresql://user:pass@host:5432/db
215
+ POSTGRES_MIN_CONNECTIONS=2
216
+ POSTGRES_MAX_CONNECTIONS=20
217
+
218
+ # AWS
219
+ AWS_ACCESS_KEY_ID=...
220
+ AWS_SECRET_ACCESS_KEY=...
221
+ DYNAMODB_TABLE_NAME=...
222
+ S3_BUCKET_NAME=...
223
+
224
+ # Azure
225
+ AZURE_STORAGE_CONNECTION_STRING=...
226
+ AZURE_TABLE_NAME=...
227
+ AZURE_CONTAINER_NAME=...
228
+
229
+ # GCP
230
+ GOOGLE_CLOUD_PROJECT=...
231
+ FIRESTORE_COLLECTION=...
232
+ GCS_BUCKET_NAME=...
233
+
234
+ # MongoDB
235
+ MONGODB_URI=mongodb://...
236
+ MONGODB_DATABASE=...
237
+ MONGODB_COLLECTION=...
238
+
239
+ # Vercel
240
+ KV_URL=...
241
+ KV_REST_API_TOKEN=...
242
+ BLOB_READ_WRITE_TOKEN=...
243
+ ```
244
+
245
+ ## Model Metadata
246
+
247
+ ```python
248
+ @polydb_model
249
+ class Entity:
250
+ __polydb__ = {
251
+ # Required
252
+ "storage": "sql" | "nosql",
253
+
254
+ # SQL
255
+ "table": "table_name",
256
+
257
+ # NoSQL
258
+ "collection": "collection_name",
259
+ "pk_field": "partition_key_field",
260
+ "rk_field": "row_key_field",
261
+
262
+ # Cache
263
+ "cache": True,
264
+ "cache_ttl": 300, # seconds
265
+
266
+ # Optional
267
+ "provider": "aws", # Override auto-detection
268
+ }
269
+ ```
270
+
271
+ ## Thread Safety
272
+
273
+ - Connection pooling with locks
274
+ - Distributed-safe audit hash chaining
275
+ - Cache with thread-safe operations
276
+ - Retry logic with exponential backoff
277
+
278
+ ## Performance
279
+
280
+ - **SQL**: Connection pooling (min=2, max=20)
281
+ - **NoSQL**: Client reuse, overflow storage
282
+ - **Cache**: In-memory with automatic invalidation
283
+ - **Retry**: Configurable backoff (0.5s-6s)
284
+
285
+ ## Production Checklist
286
+
287
+ ✅ Set `CLOUD_PROVIDER` explicitly
288
+ ✅ Configure connection pool sizes
289
+ ✅ Enable audit (`enable_audit=True`)
290
+ ✅ Set cache TTL per model
291
+ ✅ Use soft delete for compliance
292
+ ✅ Set audit context per request
293
+ ✅ Monitor audit chain integrity
294
+ ✅ Configure retry attempts
295
+
296
+ ## License
297
+
298
+ MIT