viveka-kosha 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.
- viveka_kosha-0.1.0/LICENSE.txt +110 -0
- viveka_kosha-0.1.0/PKG-INFO +455 -0
- viveka_kosha-0.1.0/README.md +308 -0
- viveka_kosha-0.1.0/pyproject.toml +54 -0
- viveka_kosha-0.1.0/setup.cfg +4 -0
- viveka_kosha-0.1.0/viveka_db/__init__.py +33 -0
- viveka_kosha-0.1.0/viveka_db/base/__init__.py +4 -0
- viveka_kosha-0.1.0/viveka_db/base/db_base.py +23 -0
- viveka_kosha-0.1.0/viveka_db/base/db_repo.py +69 -0
- viveka_kosha-0.1.0/viveka_db/cache/__init__.py +3 -0
- viveka_kosha-0.1.0/viveka_db/cache/db_cache.py +131 -0
- viveka_kosha-0.1.0/viveka_db/context/__init__.py +3 -0
- viveka_kosha-0.1.0/viveka_db/context/db_context.py +51 -0
- viveka_kosha-0.1.0/viveka_db/decorator/__init__.py +19 -0
- viveka_kosha-0.1.0/viveka_db/decorator/db_decorator.py +159 -0
- viveka_kosha-0.1.0/viveka_db/middleware/__init__.py +3 -0
- viveka_kosha-0.1.0/viveka_db/middleware/db_middleware.py +105 -0
- viveka_kosha-0.1.0/viveka_db/py.typed +0 -0
- viveka_kosha-0.1.0/viveka_db/session/__init__.py +3 -0
- viveka_kosha-0.1.0/viveka_db/session/db_session.py +136 -0
- viveka_kosha-0.1.0/viveka_kosha.egg-info/PKG-INFO +455 -0
- viveka_kosha-0.1.0/viveka_kosha.egg-info/SOURCES.txt +23 -0
- viveka_kosha-0.1.0/viveka_kosha.egg-info/dependency_links.txt +1 -0
- viveka_kosha-0.1.0/viveka_kosha.egg-info/requires.txt +16 -0
- viveka_kosha-0.1.0/viveka_kosha.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
VIVEKASUTRA PROPRIETARY SOFTWARE LICENSE
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 VivekaSutra. All rights reserved.
|
|
4
|
+
https://vivekasutra.com/
|
|
5
|
+
|
|
6
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
7
|
+
IMPORTANT: READ THIS LICENSE CAREFULLY BEFORE DOWNLOADING,
|
|
8
|
+
INSTALLING, OR USING THIS SOFTWARE.
|
|
9
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
10
|
+
|
|
11
|
+
By downloading, installing, or using this software ("viveka-kosha"),
|
|
12
|
+
you agree to be bound by the terms of this license agreement.
|
|
13
|
+
If you do not agree, do not download, install, or use this software.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
1. GRANT OF LICENSE
|
|
17
|
+
VivekaSutra grants you a limited, non-exclusive, non-transferable,
|
|
18
|
+
royalty-free license to download and use this software solely for
|
|
19
|
+
your personal or internal business purposes.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
2. RESTRICTIONS
|
|
23
|
+
You may NOT:
|
|
24
|
+
a) Copy, modify, merge, or create derivative works of this software.
|
|
25
|
+
b) Distribute, sell, sublicense, rent, lease, or transfer this
|
|
26
|
+
software or any rights in it to any third party.
|
|
27
|
+
c) Reverse engineer, decompile, disassemble, or attempt to derive
|
|
28
|
+
the source code of this software.
|
|
29
|
+
d) Remove, alter, or obscure any proprietary notices, labels,
|
|
30
|
+
or markings on this software.
|
|
31
|
+
e) Use this software to build a competing product or service.
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
3. INTELLECTUAL PROPERTY
|
|
35
|
+
This software and all copies thereof are proprietary to VivekaSutra.
|
|
36
|
+
All title, ownership rights, and intellectual property rights in and
|
|
37
|
+
to this software remain exclusively with VivekaSutra. This license
|
|
38
|
+
does not grant you any rights to trademarks, service marks, or trade
|
|
39
|
+
names of VivekaSutra.
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
4. NO WARRANTY
|
|
43
|
+
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
46
|
+
NON-INFRINGEMENT.
|
|
47
|
+
|
|
48
|
+
VIVEKASUTRA DOES NOT WARRANT THAT:
|
|
49
|
+
a) THE SOFTWARE WILL MEET YOUR REQUIREMENTS.
|
|
50
|
+
b) THE SOFTWARE WILL OPERATE UNINTERRUPTED OR ERROR-FREE.
|
|
51
|
+
c) ANY ERRORS IN THE SOFTWARE WILL BE CORRECTED.
|
|
52
|
+
d) THE SOFTWARE IS COMPATIBLE WITH YOUR SYSTEMS OR ENVIRONMENT.
|
|
53
|
+
|
|
54
|
+
YOU ASSUME ALL RISK ASSOCIATED WITH THE USE OF THIS SOFTWARE.
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
5. LIMITATION OF LIABILITY
|
|
58
|
+
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
|
59
|
+
VIVEKASUTRA, ITS DIRECTORS, EMPLOYEES, PARTNERS, AGENTS, SUPPLIERS,
|
|
60
|
+
OR AFFILIATES BE LIABLE FOR ANY:
|
|
61
|
+
|
|
62
|
+
a) DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
63
|
+
CONSEQUENTIAL DAMAGES.
|
|
64
|
+
b) LOSS OF PROFITS, REVENUE, DATA, BUSINESS, OR GOODWILL.
|
|
65
|
+
c) BUSINESS INTERRUPTION OR LOSS OF BUSINESS OPPORTUNITY.
|
|
66
|
+
d) COST OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES.
|
|
67
|
+
e) ANY OTHER COMMERCIAL OR ECONOMIC LOSS.
|
|
68
|
+
|
|
69
|
+
THIS LIMITATION APPLIES REGARDLESS OF WHETHER SUCH DAMAGES ARISE
|
|
70
|
+
FROM CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY, OR ANY OTHER
|
|
71
|
+
LEGAL THEORY, AND EVEN IF VIVEKASUTRA HAS BEEN ADVISED OF THE
|
|
72
|
+
POSSIBILITY OF SUCH DAMAGES.
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
6. INDEMNIFICATION
|
|
76
|
+
You agree to indemnify, defend, and hold harmless VivekaSutra and
|
|
77
|
+
its affiliates from and against any claims, liabilities, damages,
|
|
78
|
+
losses, and expenses (including legal fees) arising out of or in
|
|
79
|
+
connection with your use of this software or your violation of
|
|
80
|
+
this license.
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
7. TERMINATION
|
|
84
|
+
This license is effective until terminated. Your rights under this
|
|
85
|
+
license will terminate automatically and without notice from
|
|
86
|
+
VivekaSutra if you fail to comply with any of its terms and
|
|
87
|
+
conditions. Upon termination, you must immediately cease all use
|
|
88
|
+
of this software and destroy all copies in your possession.
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
8. GOVERNING LAW
|
|
92
|
+
This license shall be governed by and construed in accordance with
|
|
93
|
+
applicable laws. Any disputes arising under this license shall be
|
|
94
|
+
subject to the exclusive jurisdiction of the competent courts.
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
9. ENTIRE AGREEMENT
|
|
98
|
+
This license constitutes the entire agreement between you and
|
|
99
|
+
VivekaSutra with respect to this software and supersedes all prior
|
|
100
|
+
or contemporaneous understandings, agreements, or representations.
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
104
|
+
For licensing inquiries or permissions beyond the scope of this
|
|
105
|
+
license, please contact us at: https://vivekasutra.com/
|
|
106
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
107
|
+
|
|
108
|
+
VivekaSutra
|
|
109
|
+
https://vivekasutra.com/
|
|
110
|
+
Copyright (c) 2025. All rights reserved.
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: viveka-kosha
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Spring JPA-inspired async database library for Python — built on SQLAlchemy async and Redis
|
|
5
|
+
License: VIVEKASUTRA PROPRIETARY SOFTWARE LICENSE
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2025 VivekaSutra. All rights reserved.
|
|
8
|
+
https://vivekasutra.com/
|
|
9
|
+
|
|
10
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
11
|
+
IMPORTANT: READ THIS LICENSE CAREFULLY BEFORE DOWNLOADING,
|
|
12
|
+
INSTALLING, OR USING THIS SOFTWARE.
|
|
13
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
14
|
+
|
|
15
|
+
By downloading, installing, or using this software ("viveka-kosha"),
|
|
16
|
+
you agree to be bound by the terms of this license agreement.
|
|
17
|
+
If you do not agree, do not download, install, or use this software.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
1. GRANT OF LICENSE
|
|
21
|
+
VivekaSutra grants you a limited, non-exclusive, non-transferable,
|
|
22
|
+
royalty-free license to download and use this software solely for
|
|
23
|
+
your personal or internal business purposes.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
2. RESTRICTIONS
|
|
27
|
+
You may NOT:
|
|
28
|
+
a) Copy, modify, merge, or create derivative works of this software.
|
|
29
|
+
b) Distribute, sell, sublicense, rent, lease, or transfer this
|
|
30
|
+
software or any rights in it to any third party.
|
|
31
|
+
c) Reverse engineer, decompile, disassemble, or attempt to derive
|
|
32
|
+
the source code of this software.
|
|
33
|
+
d) Remove, alter, or obscure any proprietary notices, labels,
|
|
34
|
+
or markings on this software.
|
|
35
|
+
e) Use this software to build a competing product or service.
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
3. INTELLECTUAL PROPERTY
|
|
39
|
+
This software and all copies thereof are proprietary to VivekaSutra.
|
|
40
|
+
All title, ownership rights, and intellectual property rights in and
|
|
41
|
+
to this software remain exclusively with VivekaSutra. This license
|
|
42
|
+
does not grant you any rights to trademarks, service marks, or trade
|
|
43
|
+
names of VivekaSutra.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
4. NO WARRANTY
|
|
47
|
+
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|
48
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
49
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
|
|
50
|
+
NON-INFRINGEMENT.
|
|
51
|
+
|
|
52
|
+
VIVEKASUTRA DOES NOT WARRANT THAT:
|
|
53
|
+
a) THE SOFTWARE WILL MEET YOUR REQUIREMENTS.
|
|
54
|
+
b) THE SOFTWARE WILL OPERATE UNINTERRUPTED OR ERROR-FREE.
|
|
55
|
+
c) ANY ERRORS IN THE SOFTWARE WILL BE CORRECTED.
|
|
56
|
+
d) THE SOFTWARE IS COMPATIBLE WITH YOUR SYSTEMS OR ENVIRONMENT.
|
|
57
|
+
|
|
58
|
+
YOU ASSUME ALL RISK ASSOCIATED WITH THE USE OF THIS SOFTWARE.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
5. LIMITATION OF LIABILITY
|
|
62
|
+
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
|
63
|
+
VIVEKASUTRA, ITS DIRECTORS, EMPLOYEES, PARTNERS, AGENTS, SUPPLIERS,
|
|
64
|
+
OR AFFILIATES BE LIABLE FOR ANY:
|
|
65
|
+
|
|
66
|
+
a) DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
67
|
+
CONSEQUENTIAL DAMAGES.
|
|
68
|
+
b) LOSS OF PROFITS, REVENUE, DATA, BUSINESS, OR GOODWILL.
|
|
69
|
+
c) BUSINESS INTERRUPTION OR LOSS OF BUSINESS OPPORTUNITY.
|
|
70
|
+
d) COST OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES.
|
|
71
|
+
e) ANY OTHER COMMERCIAL OR ECONOMIC LOSS.
|
|
72
|
+
|
|
73
|
+
THIS LIMITATION APPLIES REGARDLESS OF WHETHER SUCH DAMAGES ARISE
|
|
74
|
+
FROM CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY, OR ANY OTHER
|
|
75
|
+
LEGAL THEORY, AND EVEN IF VIVEKASUTRA HAS BEEN ADVISED OF THE
|
|
76
|
+
POSSIBILITY OF SUCH DAMAGES.
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
6. INDEMNIFICATION
|
|
80
|
+
You agree to indemnify, defend, and hold harmless VivekaSutra and
|
|
81
|
+
its affiliates from and against any claims, liabilities, damages,
|
|
82
|
+
losses, and expenses (including legal fees) arising out of or in
|
|
83
|
+
connection with your use of this software or your violation of
|
|
84
|
+
this license.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
7. TERMINATION
|
|
88
|
+
This license is effective until terminated. Your rights under this
|
|
89
|
+
license will terminate automatically and without notice from
|
|
90
|
+
VivekaSutra if you fail to comply with any of its terms and
|
|
91
|
+
conditions. Upon termination, you must immediately cease all use
|
|
92
|
+
of this software and destroy all copies in your possession.
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
8. GOVERNING LAW
|
|
96
|
+
This license shall be governed by and construed in accordance with
|
|
97
|
+
applicable laws. Any disputes arising under this license shall be
|
|
98
|
+
subject to the exclusive jurisdiction of the competent courts.
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
9. ENTIRE AGREEMENT
|
|
102
|
+
This license constitutes the entire agreement between you and
|
|
103
|
+
VivekaSutra with respect to this software and supersedes all prior
|
|
104
|
+
or contemporaneous understandings, agreements, or representations.
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
108
|
+
For licensing inquiries or permissions beyond the scope of this
|
|
109
|
+
license, please contact us at: https://vivekasutra.com/
|
|
110
|
+
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
111
|
+
|
|
112
|
+
VivekaSutra
|
|
113
|
+
https://vivekasutra.com/
|
|
114
|
+
Copyright (c) 2025. All rights reserved.
|
|
115
|
+
|
|
116
|
+
Project-URL: Homepage, https://vivekasutra.com/
|
|
117
|
+
Project-URL: Source, https://github.com/vivekasutra/viveka-kosha
|
|
118
|
+
Keywords: database,orm,sqlalchemy,async,fastapi,jpa,repository,transactional
|
|
119
|
+
Classifier: Development Status :: 3 - Alpha
|
|
120
|
+
Classifier: Intended Audience :: Developers
|
|
121
|
+
Classifier: License :: Other/Proprietary License
|
|
122
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
123
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
124
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
125
|
+
Classifier: Topic :: Database
|
|
126
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
127
|
+
Classifier: Framework :: AsyncIO
|
|
128
|
+
Classifier: Framework :: FastAPI
|
|
129
|
+
Classifier: Typing :: Typed
|
|
130
|
+
Requires-Python: >=3.11
|
|
131
|
+
Description-Content-Type: text/markdown
|
|
132
|
+
License-File: LICENSE.txt
|
|
133
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0
|
|
134
|
+
Requires-Dist: redis>=5.0
|
|
135
|
+
Requires-Dist: starlette>=0.35
|
|
136
|
+
Requires-Dist: viveka-grantha>=0.1.0
|
|
137
|
+
Provides-Extra: postgres
|
|
138
|
+
Requires-Dist: asyncpg>=0.29; extra == "postgres"
|
|
139
|
+
Provides-Extra: mysql
|
|
140
|
+
Requires-Dist: aiomysql>=0.2; extra == "mysql"
|
|
141
|
+
Provides-Extra: dev
|
|
142
|
+
Requires-Dist: build; extra == "dev"
|
|
143
|
+
Requires-Dist: twine; extra == "dev"
|
|
144
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
145
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
146
|
+
Dynamic: license-file
|
|
147
|
+
|
|
148
|
+
# viveka-kosha
|
|
149
|
+
|
|
150
|
+
A Spring JPA-inspired async database library for Python. Provides declarative entity mapping, generic repositories, transaction management, and cache decorators — built on SQLAlchemy async and Redis.
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Design Philosophy
|
|
155
|
+
|
|
156
|
+
viveka-kosha maps Spring JPA concepts to Python idioms:
|
|
157
|
+
|
|
158
|
+
| Spring JPA | viveka-kosha |
|
|
159
|
+
|---|---|
|
|
160
|
+
| `@Entity` + `@Table` | `@entity` + `@table` |
|
|
161
|
+
| `JpaRepository<T, ID>` | `DbRepo[T]` |
|
|
162
|
+
| `@Repository` | `@repository` |
|
|
163
|
+
| `@Transactional` | `@transactional` |
|
|
164
|
+
| `@Cacheable` | `@cacheable` |
|
|
165
|
+
| `@CacheEvict` | `@cache_evict` |
|
|
166
|
+
| `OpenSessionInView` filter | `DbMiddleware` |
|
|
167
|
+
| `ThreadLocal` session | `ContextVar` session |
|
|
168
|
+
|
|
169
|
+
The library is **async-first**. Every repository method, cache call, and session operation is `async/await`. This makes it a natural fit for FastAPI / Starlette services.
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Architecture
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
viveka-kosha/
|
|
177
|
+
└── viveka_db/
|
|
178
|
+
├── base/
|
|
179
|
+
│ ├── db_base.py # VivekaDbBase — shared SQLAlchemy declarative base
|
|
180
|
+
│ └── db_repo.py # DbRepo[T] — generic CRUD repository
|
|
181
|
+
├── decorator/
|
|
182
|
+
│ └── db_decorator.py # @entity, @table, @repository, @transactional,
|
|
183
|
+
│ # @cacheable, @cache_evict
|
|
184
|
+
├── session/
|
|
185
|
+
│ └── db_session.py # DatabaseSessionFactory — singleton engine + session factory
|
|
186
|
+
├── middleware/
|
|
187
|
+
│ └── db_middleware.py # DbMiddleware — per-request session lifecycle
|
|
188
|
+
├── context/
|
|
189
|
+
│ └── db_context.py # ContextVar session store (async-safe ThreadLocal equivalent)
|
|
190
|
+
└── cache/
|
|
191
|
+
└── db_cache.py # RedisCache — async Redis wrapper
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Session Lifecycle
|
|
195
|
+
|
|
196
|
+
viveka-kosha manages sessions in two distinct paths:
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
HTTP Request
|
|
200
|
+
│
|
|
201
|
+
▼
|
|
202
|
+
DbMiddleware.dispatch()
|
|
203
|
+
│ creates AsyncSession
|
|
204
|
+
│ stores in ContextVar
|
|
205
|
+
▼
|
|
206
|
+
Route Handler → Service → @transactional method
|
|
207
|
+
│ detects session in ContextVar
|
|
208
|
+
│ uses it directly (no new session)
|
|
209
|
+
▼
|
|
210
|
+
DbMiddleware
|
|
211
|
+
│ commit on success
|
|
212
|
+
│ rollback on exception
|
|
213
|
+
└ close + clear ContextVar
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
Background Task / Async Worker
|
|
217
|
+
│
|
|
218
|
+
▼
|
|
219
|
+
@transactional method
|
|
220
|
+
│ no session in ContextVar
|
|
221
|
+
│ opens a new session via DatabaseSessionFactory
|
|
222
|
+
│ commit on success / rollback on exception / close
|
|
223
|
+
└ logs a warning so async task usage is visible
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
This means `@transactional` works transparently in both HTTP and non-HTTP contexts with no configuration change.
|
|
227
|
+
|
|
228
|
+
### Entity Registration
|
|
229
|
+
|
|
230
|
+
`@entity` registers the class in a global `_entity_registry`. `@table(name)` sets `__tablename__` and triggers SQLAlchemy's declarative mapping. `VivekaDbBase.__init_subclass__` marks any subclass without a `__tablename__` as `__abstract__` so SQLAlchemy skips unmapped intermediary classes.
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Installation
|
|
235
|
+
|
|
236
|
+
Add to your service's `requirements.txt`:
|
|
237
|
+
|
|
238
|
+
```
|
|
239
|
+
viveka-kosha
|
|
240
|
+
sqlalchemy[asyncio]
|
|
241
|
+
asyncpg # for PostgreSQL
|
|
242
|
+
aiomysql # for MySQL
|
|
243
|
+
redis # for cache decorators
|
|
244
|
+
starlette # for DbMiddleware
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
---
|
|
248
|
+
|
|
249
|
+
## Configuration
|
|
250
|
+
|
|
251
|
+
viveka-kosha reads from your `ConfigService` (from `viveka-grantha`).
|
|
252
|
+
|
|
253
|
+
`config.ini`:
|
|
254
|
+
|
|
255
|
+
```ini
|
|
256
|
+
[database]
|
|
257
|
+
url = postgresql+asyncpg://user:pass@localhost/mydb
|
|
258
|
+
pool_size = 10
|
|
259
|
+
max_overflow = 20
|
|
260
|
+
echo = false
|
|
261
|
+
|
|
262
|
+
[cache]
|
|
263
|
+
url = redis://localhost:6379/0
|
|
264
|
+
enabled = true
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Usage
|
|
270
|
+
|
|
271
|
+
### 1. Initialize the session factory at startup
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
from viveka_db.session.db_session import DatabaseSessionFactory
|
|
275
|
+
from viveka_common.config.config_service import ConfigService
|
|
276
|
+
|
|
277
|
+
config = ConfigService()
|
|
278
|
+
DatabaseSessionFactory(config_service=config)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### 2. Register the middleware (FastAPI / Starlette)
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
from fastapi import FastAPI
|
|
285
|
+
from viveka_db.middleware.db_middleware import DbMiddleware
|
|
286
|
+
from viveka_db.session.db_session import DatabaseSessionFactory
|
|
287
|
+
|
|
288
|
+
app = FastAPI()
|
|
289
|
+
engine = DatabaseSessionFactory.get_instance().engine
|
|
290
|
+
app.add_middleware(DbMiddleware, engine=engine)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### 3. Define an entity
|
|
294
|
+
|
|
295
|
+
```python
|
|
296
|
+
from sqlalchemy import Column, Integer, String
|
|
297
|
+
from viveka_db.base.db_base import VivekaDbBase
|
|
298
|
+
from viveka_db.decorator.db_decorator import entity, table
|
|
299
|
+
|
|
300
|
+
@entity
|
|
301
|
+
@table("users")
|
|
302
|
+
class User(VivekaDbBase):
|
|
303
|
+
id = Column(Integer, primary_key=True)
|
|
304
|
+
name = Column(String(100), nullable=False)
|
|
305
|
+
email = Column(String(255), unique=True, nullable=False)
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
### 4. Define a repository
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
from viveka_db.base.db_repo import DbRepo
|
|
312
|
+
from viveka_db.decorator.db_decorator import repository, transactional, cacheable, cache_evict
|
|
313
|
+
|
|
314
|
+
@repository
|
|
315
|
+
class UserRepo(DbRepo[User]):
|
|
316
|
+
|
|
317
|
+
def __init__(self):
|
|
318
|
+
super().__init__(User)
|
|
319
|
+
|
|
320
|
+
@transactional
|
|
321
|
+
async def save(self, user: User) -> User:
|
|
322
|
+
result = await self.insert_all([user])
|
|
323
|
+
return result[0]
|
|
324
|
+
|
|
325
|
+
@transactional
|
|
326
|
+
@cacheable(key_prefix="user", ttl=3600)
|
|
327
|
+
async def get(self, user_id: int):
|
|
328
|
+
return await self.get_by_id(user_id)
|
|
329
|
+
|
|
330
|
+
@transactional
|
|
331
|
+
@cache_evict(key_prefix="user")
|
|
332
|
+
async def remove(self, user_id: int) -> bool:
|
|
333
|
+
return await self.delete(user_id)
|
|
334
|
+
|
|
335
|
+
@transactional
|
|
336
|
+
async def find_by_email(self, email: str):
|
|
337
|
+
return await self.find_by_field("email", email)
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### 5. Use the repository in a service
|
|
341
|
+
|
|
342
|
+
```python
|
|
343
|
+
class UserService:
|
|
344
|
+
|
|
345
|
+
def __init__(self):
|
|
346
|
+
self.user_repo = UserRepo()
|
|
347
|
+
|
|
348
|
+
async def register(self, name: str, email: str) -> User:
|
|
349
|
+
user = User(name=name, email=email)
|
|
350
|
+
return await self.user_repo.save(user)
|
|
351
|
+
|
|
352
|
+
async def get_user(self, user_id: int) -> User:
|
|
353
|
+
return await self.user_repo.get(user_id)
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
---
|
|
357
|
+
|
|
358
|
+
## Built-in Repository Methods
|
|
359
|
+
|
|
360
|
+
`DbRepo[T]` provides these methods out of the box — all require `@transactional` on the calling method:
|
|
361
|
+
|
|
362
|
+
| Method | Description |
|
|
363
|
+
|---|---|
|
|
364
|
+
| `insert_all(entities)` | Bulk insert, returns saved entities |
|
|
365
|
+
| `get_by_id(id)` | Fetch by primary key |
|
|
366
|
+
| `get_all(skip, limit)` | Paginated fetch (default: 0, 100) |
|
|
367
|
+
| `update(entity)` | Merge and flush |
|
|
368
|
+
| `delete(id)` | Delete by primary key, returns `bool` |
|
|
369
|
+
| `delete_entity(entity)` | Delete a loaded entity instance |
|
|
370
|
+
| `exists(id)` | Check existence by primary key |
|
|
371
|
+
| `count()` | Total row count |
|
|
372
|
+
| `find_by_field(field, value)` | First match on any field |
|
|
373
|
+
| `find_all_by_field(field, value)` | All matches on any field |
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## Decorators Reference
|
|
378
|
+
|
|
379
|
+
### `@entity`
|
|
380
|
+
Marks a class as a mapped entity. Registers it in the global entity registry. The class must inherit from `VivekaDbBase`.
|
|
381
|
+
|
|
382
|
+
### `@table(name)`
|
|
383
|
+
Sets the database table name and activates SQLAlchemy mapping. Must be used together with `@entity`.
|
|
384
|
+
|
|
385
|
+
```python
|
|
386
|
+
@entity
|
|
387
|
+
@table("orders")
|
|
388
|
+
class Order(VivekaDbBase):
|
|
389
|
+
...
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
### `@repository`
|
|
393
|
+
Marks a class as a repository for auto-discovery. No runtime behaviour beyond flagging.
|
|
394
|
+
|
|
395
|
+
### `@transactional`
|
|
396
|
+
Injects the active `AsyncSession` into `self.db` before the method runs.
|
|
397
|
+
|
|
398
|
+
- **HTTP context**: reuses the session created by `DbMiddleware` — middleware commits/rolls back.
|
|
399
|
+
- **Background task context**: opens a dedicated session, commits on success, rolls back on exception.
|
|
400
|
+
|
|
401
|
+
```python
|
|
402
|
+
@transactional
|
|
403
|
+
async def do_something(self):
|
|
404
|
+
# self.db is an AsyncSession here
|
|
405
|
+
result = await self.db.execute(select(MyModel))
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
### `@cacheable(key_prefix, ttl=3600)`
|
|
409
|
+
Cache-aside read. Checks cache before calling the method. Caches the result on a miss.
|
|
410
|
+
Cache key is `{key_prefix}:{first_arg}`.
|
|
411
|
+
|
|
412
|
+
```python
|
|
413
|
+
@transactional
|
|
414
|
+
@cacheable(key_prefix="product", ttl=1800)
|
|
415
|
+
async def get_product(self, product_id: int):
|
|
416
|
+
return await self.get_by_id(product_id)
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### `@cache_evict(key_prefix)`
|
|
420
|
+
Runs the method first, then deletes all keys matching `{key_prefix}:*` from cache.
|
|
421
|
+
|
|
422
|
+
```python
|
|
423
|
+
@transactional
|
|
424
|
+
@cache_evict(key_prefix="product")
|
|
425
|
+
async def update_product(self, product: Product):
|
|
426
|
+
return await self.update(product)
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
---
|
|
430
|
+
|
|
431
|
+
## Dependencies
|
|
432
|
+
|
|
433
|
+
| Package | Role |
|
|
434
|
+
|---|---|
|
|
435
|
+
| `sqlalchemy[asyncio]` | ORM, async engine, session |
|
|
436
|
+
| `asyncpg` / `aiomysql` | Async DB driver (PostgreSQL / MySQL) |
|
|
437
|
+
| `redis` | Async Redis client (`redis.asyncio`) |
|
|
438
|
+
| `starlette` | `BaseHTTPMiddleware` for `DbMiddleware` |
|
|
439
|
+
| `viveka-grantha` | `ConfigService`, `VivekaLogManager`, `VivekaCacheManager` |
|
|
440
|
+
|
|
441
|
+
---
|
|
442
|
+
|
|
443
|
+
## License
|
|
444
|
+
|
|
445
|
+
Copyright (c) 2025 VivekaSutra. All rights reserved.
|
|
446
|
+
|
|
447
|
+
This software is distributed under the **VivekaSutra Proprietary Software License**.
|
|
448
|
+
|
|
449
|
+
- Free to download and use for personal or commercial purposes
|
|
450
|
+
- Modification, redistribution, and reverse engineering are not permitted
|
|
451
|
+
- Provided "AS IS" — no warranty of any kind
|
|
452
|
+
- VivekaSutra is not liable for any damages arising from use
|
|
453
|
+
|
|
454
|
+
See [LICENSE.txt](LICENSE.txt) for the full license text.
|
|
455
|
+
For licensing inquiries visit [vivekasutra.com](https://vivekasutra.com/)
|