repository_orm
¶
Library to ease the implementation of the repository pattern in Python projects.
AutoIncrementError
¶
Bases: Exception
Raised when the id_ auto increment repository feature fails.
Source code in repository_orm/exceptions.py
12 13 |
|
Entity
¶
Bases: BaseModel
Model of any object no defined by it's attributes whom instead has an identity.
Unlike value objects, they have identity equality. We can change their values, and they are still recognizably the same thing.
An entity with a negative id means that the id needs to be set by the repository.
The _defined_values are used to know which attributes were set by the user at the time of merging objects.
Source code in repository_orm/model.py
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 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 |
|
__gt__(other)
¶
Assert if an object is greater than us.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Entity
|
Entity to compare. |
required |
Source code in repository_orm/model.py
46 47 48 49 50 51 52 53 54 |
|
__hash__()
¶
Create an unique hash of the class object.
Source code in repository_orm/model.py
56 57 58 |
|
__init__(**data)
¶
Initialize the defined values.
Source code in repository_orm/model.py
31 32 33 34 |
|
__lt__(other)
¶
Assert if an object is smaller than us.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Entity
|
Entity to compare. |
required |
Source code in repository_orm/model.py
36 37 38 39 40 41 42 43 44 |
|
__setattr__(attribute, value)
¶
Store the set attribute into the _defined_values.
Source code in repository_orm/model.py
61 62 63 64 65 |
|
clear_defined_values()
¶
Remove all references to defined values.
I tried to return self so that it can be used chained with repo.get(), but I get
a mypy error Incompatible return value type (got "Entity", expected "Entity")
Source code in repository_orm/model.py
103 104 105 106 107 108 109 |
|
defined_values()
property
¶
Return the entity defined values.
Source code in repository_orm/model.py
98 99 100 101 |
|
merge(other)
¶
Update the attributes with the ones manually set by the user of other.
If the other object has default values not set by the user, they won't be
propagated to self
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Entity
|
Entity to compare. |
required |
Source code in repository_orm/model.py
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 |
|
model_name()
property
¶
Return the entity model name.
Source code in repository_orm/model.py
67 68 69 70 |
|
EntityNotFoundError
¶
Bases: Exception
Raised when the search or retrieve of an entity fails.
Source code in repository_orm/exceptions.py
4 5 |
|
FakeRepository
¶
Bases: Repository
Implement the repository pattern using a memory dictionary.
Source code in repository_orm/adapters/data/fake.py
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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
__init__(database_url='')
¶
Initialize the repository attributes.
Source code in repository_orm/adapters/data/fake.py
21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
apply_migrations(migrations_directory)
¶
Run the migrations of the repository schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
migrations_directory |
str
|
path to the directory containing the migration scripts. |
required |
Source code in repository_orm/adapters/data/fake.py
178 179 180 181 182 183 184 |
|
close()
¶
Close the connection to the database.
Source code in repository_orm/adapters/data/fake.py
229 230 231 |
|
commit()
¶
Persist the changes into the repository.
Source code in repository_orm/adapters/data/fake.py
118 119 120 121 122 |
|
delete(entity)
¶
Delete an entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity |
EntityT
|
Entity to remove from the repository. |
required |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If the entity is not found. |
Source code in repository_orm/adapters/data/fake.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
empty()
¶
Remove all entities from the repository.
Source code in repository_orm/adapters/data/fake.py
238 239 240 241 |
|
is_closed()
property
¶
Inform if the connection is closed.
Source code in repository_orm/adapters/data/fake.py
233 234 235 236 |
|
last(model)
¶
Get the biggest entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Entity class to obtain. |
required |
Returns:
Name | Type | Description |
---|---|---|
entity |
EntityT
|
Biggest Entity object that matches a model. |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If there are no entities. |
Source code in repository_orm/adapters/data/fake.py
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 |
|
File
¶
Bases: Entity
, Generic[AnyStr]
Model a computer file.
Source code in repository_orm/model.py
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 |
|
basename()
property
¶
Return the name of the file.
Source code in repository_orm/model.py
137 138 139 140 |
|
content()
property
¶
Return the content of the file.
Returns:
Type | Description |
---|---|
AnyStr
|
The content of the file. |
Raises:
Type | Description |
---|---|
FileContentNotLoadedError
|
if the content is not yet loaded. |
Source code in repository_orm/model.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
dirname()
property
¶
Return the name of the file.
Source code in repository_orm/model.py
142 143 144 145 |
|
extension()
property
¶
Return the name of the file.
Source code in repository_orm/model.py
147 148 149 150 |
|
LocalFileRepository
¶
Bases: FileRepository[AnyStr]
Define the local filesystem adapter.
Source code in repository_orm/adapters/file/local_file.py
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 |
|
__init__(workdir)
¶
Initialize the object.
Creates the working directory if it doesn't exist.
Source code in repository_orm/adapters/file/local_file.py
16 17 18 19 20 21 22 23 |
|
delete(file_)
¶
Delete the file from the persistence system.
Source code in repository_orm/adapters/file/local_file.py
57 58 59 60 61 62 63 64 65 66 |
|
load(file_)
¶
Load the content of the file from the persistence system.
Source code in repository_orm/adapters/file/local_file.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
save(file_)
¶
Save the content of the file into the persistence system.
Source code in repository_orm/adapters/file/local_file.py
43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
PypikaRepository
¶
Bases: Repository
Implement the repository pattern using the Pypika query builder.
Source code in repository_orm/adapters/data/pypika.py
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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
__init__(database_url='')
¶
Initialize the repository attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database_url |
str
|
URL specifying the connection to the database. |
''
|
Source code in repository_orm/adapters/data/pypika.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
apply_migrations(migrations_directory)
¶
Run the migrations of the repository schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
migrations_directory |
str
|
path to the directory containing the migration scripts. |
required |
Source code in repository_orm/adapters/data/pypika.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
|
close()
¶
Close the connection to the database.
Source code in repository_orm/adapters/data/pypika.py
258 259 260 |
|
commit()
¶
Persist the changes into the repository.
Source code in repository_orm/adapters/data/pypika.py
192 193 194 |
|
delete(entity)
¶
Delete an entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity |
EntityT
|
Entity to remove from the repository. |
required |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If the entity is not found. |
Source code in repository_orm/adapters/data/pypika.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
empty()
¶
Remove all entities from the repository.
Source code in repository_orm/adapters/data/pypika.py
262 263 264 265 |
|
is_closed()
property
¶
Inform if the connection is closed.
Source code in repository_orm/adapters/data/pypika.py
280 281 282 283 284 285 286 287 |
|
tables()
property
¶
Return the entity tables of the database.
Source code in repository_orm/adapters/data/pypika.py
267 268 269 270 271 272 273 274 275 276 277 278 |
|
Repository
¶
Bases: abc.ABC
Gather common methods and define the interface of the repositories.
Attributes:
Name | Type | Description |
---|---|---|
database_url |
URL specifying the connection to the database. |
Source code in repository_orm/adapters/data/abstract.py
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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
__init__(database_url='')
abstractmethod
¶
Initialize the repository attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database_url |
str
|
URL specifying the connection to the database. |
''
|
Source code in repository_orm/adapters/data/abstract.py
24 25 26 27 28 29 30 31 32 33 34 35 |
|
add(entities, merge=False)
¶
Append an entity or list of entities to the repository.
If the id is not set, it will automatically increment the last available one.
If merge
is True, added entities will be merged with the existent ones in
the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entities |
EntityOrEntitiesT
|
Entity or entities to add to the repository. |
required |
Returns:
Type | Description |
---|---|
EntityOrEntitiesT
|
entity or entities |
Source code in repository_orm/adapters/data/abstract.py
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 |
|
all(model)
¶
Get all the entities from the repository that match a model.
Also store the entities in the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Entity class or classes to obtain. |
required |
Source code in repository_orm/adapters/data/abstract.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
apply_migrations(migrations_directory)
abstractmethod
¶
Run the migrations of the repository schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
migrations_directory |
str
|
path to the directory containing the migration scripts. |
required |
Source code in repository_orm/adapters/data/abstract.py
241 242 243 244 245 246 247 248 249 |
|
close()
abstractmethod
¶
Close the connection to the database.
Source code in repository_orm/adapters/data/abstract.py
313 314 315 316 |
|
commit()
abstractmethod
¶
Persist the changes into the repository.
Source code in repository_orm/adapters/data/abstract.py
193 194 195 196 |
|
delete(entity)
abstractmethod
¶
Delete an entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity |
EntityT
|
Entity to remove from the repository. |
required |
Source code in repository_orm/adapters/data/abstract.py
95 96 97 98 99 100 101 102 |
|
empty()
abstractmethod
¶
Remove all entities from the repository.
Source code in repository_orm/adapters/data/abstract.py
324 325 326 327 |
|
first(model)
¶
Get the smallest entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Type of entity object to obtain. |
required |
Returns:
Name | Type | Description |
---|---|---|
entity |
EntityT
|
Smallest Entity object that matches a model. |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If there are no entities. |
Source code in repository_orm/adapters/data/abstract.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|
get(id_, model, attribute='id_')
¶
Obtain an entity from the repository by it's ID.
Also save the entity in the cache
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Entity class to obtain. |
required |
id_ |
EntityID
|
ID of the entity to obtain. |
required |
Returns:
Name | Type | Description |
---|---|---|
entity |
EntityT
|
Entity object that matches the id_ |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If the entity is not found. |
TooManyEntitiesError
|
If more than one entity was found. |
Source code in repository_orm/adapters/data/abstract.py
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 |
|
is_closed()
abstractmethod
property
¶
Inform if the connection is closed.
Source code in repository_orm/adapters/data/abstract.py
318 319 320 321 322 |
|
last(model)
¶
Get the biggest entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Entity class to obtain. |
required |
Returns:
Name | Type | Description |
---|---|---|
entity |
EntityT
|
Biggest Entity object that matches a model. |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If there are no entities. |
Source code in repository_orm/adapters/data/abstract.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
next_id(entity)
¶
Return one id unit more than the last entity id in the repository index.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity |
EntityT
|
Entity whose model we want to get the next entity id. |
required |
Source code in repository_orm/adapters/data/abstract.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
search(fields, model)
¶
Get the entities whose attributes match one or several conditions.
Also add the found entities to the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Entity class to obtain. |
required |
fields |
Dict[str, EntityID]
|
Dictionary with the {key}:{value} to search. |
required |
Returns:
Name | Type | Description |
---|---|---|
entities |
List[EntityT]
|
List of Entity object that matches the search criteria. |
Source code in repository_orm/adapters/data/abstract.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
TinyDBRepository
¶
Bases: Repository
Implement the repository pattern using the TinyDB.
Source code in repository_orm/adapters/data/tinydb.py
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 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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
__init__(database_url='')
¶
Initialize the repository attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database_url |
str
|
URL specifying the connection to the database. |
''
|
Source code in repository_orm/adapters/data/tinydb.py
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 |
|
apply_migrations(migrations_directory)
¶
Run the migrations of the repository schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
migrations_directory |
str
|
path to the directory containing the migration scripts. |
required |
Source code in repository_orm/adapters/data/tinydb.py
289 290 291 292 293 294 295 296 |
|
close()
¶
Close the connection to the database.
Source code in repository_orm/adapters/data/tinydb.py
336 337 338 |
|
commit()
¶
Persist the changes into the repository.
Source code in repository_orm/adapters/data/tinydb.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
delete(entity)
¶
Delete an entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity |
EntityT
|
Entity to remove from the repository. |
required |
Source code in repository_orm/adapters/data/tinydb.py
69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
empty()
¶
Remove all entities from the repository.
Source code in repository_orm/adapters/data/tinydb.py
349 350 351 |
|
is_closed()
property
¶
Inform if the connection is closed.
Source code in repository_orm/adapters/data/tinydb.py
340 341 342 343 344 345 346 347 |
|
last(model)
¶
Get the biggest entity from the repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[EntityT]
|
Entity class to obtain. |
required |
Returns:
Name | Type | Description |
---|---|---|
entity |
EntityT
|
Biggest Entity object that matches a model. |
Raises:
Type | Description |
---|---|
EntityNotFoundError
|
If there are no entities. |
Source code in repository_orm/adapters/data/tinydb.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
|
load_file_repository(url='local:.')
¶
Load the FileRepository object that matches the url protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
Url to connect to the storage backend. |
'local:.'
|
Returns:
Type | Description |
---|---|
FileRepository[AnyStr]
|
File Repository that understands the url protocol. |
Source code in repository_orm/services.py
43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
load_repository(database_url='fake://')
¶
Load the Repository object that matches the database url protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
database_url |
str
|
Url to connect to the storage backend. |
'fake://'
|
Returns:
Type | Description |
---|---|
Repository
|
Repository that understands the url protocol. |
Source code in repository_orm/services.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|