masonite-framework-orm 3.0.1__py3-none-any.whl
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.
- masonite_framework_orm-3.0.1.dist-info/METADATA +87 -0
- masonite_framework_orm-3.0.1.dist-info/RECORD +116 -0
- masonite_framework_orm-3.0.1.dist-info/WHEEL +5 -0
- masonite_framework_orm-3.0.1.dist-info/entry_points.txt +3 -0
- masonite_framework_orm-3.0.1.dist-info/licenses/LICENSE +21 -0
- masonite_framework_orm-3.0.1.dist-info/top_level.txt +1 -0
- masoniteorm/__init__.py +1 -0
- masoniteorm/collection/Collection.py +605 -0
- masoniteorm/collection/__init__.py +1 -0
- masoniteorm/commands/CanOverrideConfig.py +16 -0
- masoniteorm/commands/CanOverrideOptionsDefault.py +22 -0
- masoniteorm/commands/Command.py +6 -0
- masoniteorm/commands/Entry.py +43 -0
- masoniteorm/commands/MakeMigrationCommand.py +57 -0
- masoniteorm/commands/MakeModelCommand.py +78 -0
- masoniteorm/commands/MakeModelDocstringCommand.py +37 -0
- masoniteorm/commands/MakeObserverCommand.py +54 -0
- masoniteorm/commands/MakeSeedCommand.py +54 -0
- masoniteorm/commands/MigrateCommand.py +46 -0
- masoniteorm/commands/MigrateFreshCommand.py +41 -0
- masoniteorm/commands/MigrateRefreshCommand.py +41 -0
- masoniteorm/commands/MigrateResetCommand.py +25 -0
- masoniteorm/commands/MigrateRollbackCommand.py +26 -0
- masoniteorm/commands/MigrateStatusCommand.py +51 -0
- masoniteorm/commands/SeedRunCommand.py +35 -0
- masoniteorm/commands/ShellCommand.py +205 -0
- masoniteorm/commands/__init__.py +18 -0
- masoniteorm/commands/stubs/create_migration.stub +20 -0
- masoniteorm/commands/stubs/create_seed.stub +9 -0
- masoniteorm/commands/stubs/model.stub +9 -0
- masoniteorm/commands/stubs/observer.stub +101 -0
- masoniteorm/commands/stubs/table_migration.stub +19 -0
- masoniteorm/config.py +123 -0
- masoniteorm/connections/BaseConnection.py +101 -0
- masoniteorm/connections/ConnectionFactory.py +59 -0
- masoniteorm/connections/ConnectionResolver.py +132 -0
- masoniteorm/connections/MSSQLConnection.py +176 -0
- masoniteorm/connections/MySQLConnection.py +232 -0
- masoniteorm/connections/PostgresConnection.py +225 -0
- masoniteorm/connections/SQLiteConnection.py +179 -0
- masoniteorm/connections/__init__.py +6 -0
- masoniteorm/exceptions.py +38 -0
- masoniteorm/expressions/__init__.py +1 -0
- masoniteorm/expressions/expressions.py +288 -0
- masoniteorm/factories/Factory.py +112 -0
- masoniteorm/factories/__init__.py +1 -0
- masoniteorm/helpers/__init__.py +0 -0
- masoniteorm/helpers/misc.py +22 -0
- masoniteorm/migrations/Migration.py +330 -0
- masoniteorm/migrations/__init__.py +1 -0
- masoniteorm/models/MigrationModel.py +9 -0
- masoniteorm/models/Model.py +1209 -0
- masoniteorm/models/Model.pyi +1366 -0
- masoniteorm/models/Pivot.py +5 -0
- masoniteorm/models/__init__.py +1 -0
- masoniteorm/observers/ObservesEvents.py +27 -0
- masoniteorm/observers/__init__.py +1 -0
- masoniteorm/pagination/BasePaginator.py +10 -0
- masoniteorm/pagination/LengthAwarePaginator.py +34 -0
- masoniteorm/pagination/SimplePaginator.py +28 -0
- masoniteorm/pagination/__init__.py +2 -0
- masoniteorm/providers/ORMProvider.py +39 -0
- masoniteorm/providers/__init__.py +1 -0
- masoniteorm/query/EagerRelation.py +42 -0
- masoniteorm/query/QueryBuilder.py +2486 -0
- masoniteorm/query/__init__.py +1 -0
- masoniteorm/query/grammars/BaseGrammar.py +1027 -0
- masoniteorm/query/grammars/MSSQLGrammar.py +194 -0
- masoniteorm/query/grammars/MySQLGrammar.py +238 -0
- masoniteorm/query/grammars/PostgresGrammar.py +213 -0
- masoniteorm/query/grammars/SQLiteGrammar.py +228 -0
- masoniteorm/query/grammars/__init__.py +4 -0
- masoniteorm/query/processors/MSSQLPostProcessor.py +58 -0
- masoniteorm/query/processors/MySQLPostProcessor.py +48 -0
- masoniteorm/query/processors/PostgresPostProcessor.py +49 -0
- masoniteorm/query/processors/SQLitePostProcessor.py +49 -0
- masoniteorm/query/processors/__init__.py +4 -0
- masoniteorm/relationships/BaseRelationship.py +161 -0
- masoniteorm/relationships/BelongsTo.py +124 -0
- masoniteorm/relationships/BelongsToMany.py +604 -0
- masoniteorm/relationships/HasMany.py +66 -0
- masoniteorm/relationships/HasManyThrough.py +269 -0
- masoniteorm/relationships/HasOne.py +111 -0
- masoniteorm/relationships/HasOneThrough.py +275 -0
- masoniteorm/relationships/MorphMany.py +152 -0
- masoniteorm/relationships/MorphOne.py +156 -0
- masoniteorm/relationships/MorphTo.py +111 -0
- masoniteorm/relationships/MorphToMany.py +108 -0
- masoniteorm/relationships/__init__.py +10 -0
- masoniteorm/schema/Blueprint.py +1161 -0
- masoniteorm/schema/Column.py +144 -0
- masoniteorm/schema/ColumnDiff.py +0 -0
- masoniteorm/schema/Constraint.py +5 -0
- masoniteorm/schema/ForeignKeyConstraint.py +28 -0
- masoniteorm/schema/Index.py +5 -0
- masoniteorm/schema/Schema.py +359 -0
- masoniteorm/schema/Table.py +94 -0
- masoniteorm/schema/TableDiff.py +86 -0
- masoniteorm/schema/__init__.py +3 -0
- masoniteorm/schema/platforms/MSSQLPlatform.py +367 -0
- masoniteorm/schema/platforms/MySQLPlatform.py +513 -0
- masoniteorm/schema/platforms/Platform.py +97 -0
- masoniteorm/schema/platforms/PostgresPlatform.py +551 -0
- masoniteorm/schema/platforms/SQLitePlatform.py +481 -0
- masoniteorm/schema/platforms/__init__.py +4 -0
- masoniteorm/scopes/BaseScope.py +6 -0
- masoniteorm/scopes/SoftDeleteScope.py +56 -0
- masoniteorm/scopes/SoftDeletesMixin.py +13 -0
- masoniteorm/scopes/TimeStampsMixin.py +12 -0
- masoniteorm/scopes/TimeStampsScope.py +47 -0
- masoniteorm/scopes/UUIDPrimaryKeyMixin.py +8 -0
- masoniteorm/scopes/UUIDPrimaryKeyScope.py +51 -0
- masoniteorm/scopes/__init__.py +8 -0
- masoniteorm/scopes/scope.py +15 -0
- masoniteorm/seeds/Seeder.py +42 -0
- masoniteorm/seeds/__init__.py +1 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from ..collection import Collection
|
|
2
|
+
from .BaseRelationship import BaseRelationship
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class BelongsTo(BaseRelationship):
|
|
6
|
+
"""Belongs To Relationship Class."""
|
|
7
|
+
|
|
8
|
+
def __init__(self, fn, local_key=None, foreign_key=None):
|
|
9
|
+
if isinstance(fn, str):
|
|
10
|
+
self.fn = None
|
|
11
|
+
self.local_key = fn or "id"
|
|
12
|
+
self.foreign_key = local_key
|
|
13
|
+
else:
|
|
14
|
+
self.fn = fn
|
|
15
|
+
self.local_key = local_key or "id"
|
|
16
|
+
self.foreign_key = foreign_key
|
|
17
|
+
|
|
18
|
+
def set_keys(self, owner, attribute):
|
|
19
|
+
self.local_key = self.local_key or f"{attribute}_id"
|
|
20
|
+
self.foreign_key = self.foreign_key or "id"
|
|
21
|
+
return self
|
|
22
|
+
|
|
23
|
+
def apply_query(self, foreign, owner):
|
|
24
|
+
"""Apply the query and return a dictionary to be hydrated
|
|
25
|
+
|
|
26
|
+
Arguments:
|
|
27
|
+
foreign {oject} -- The relationship object
|
|
28
|
+
owner {object} -- The current model oject.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
dict -- A dictionary of data which will be hydrated.
|
|
32
|
+
"""
|
|
33
|
+
return foreign.where(
|
|
34
|
+
self.foreign_key, owner.__attributes__[self.local_key]
|
|
35
|
+
).first()
|
|
36
|
+
|
|
37
|
+
def query_has(self, current_query_builder, method="where_exists"):
|
|
38
|
+
related_builder = self.get_builder()
|
|
39
|
+
|
|
40
|
+
getattr(current_query_builder, method)(
|
|
41
|
+
related_builder.where_column(
|
|
42
|
+
f"{related_builder.get_table_name()}.{self.foreign_key}",
|
|
43
|
+
f"{current_query_builder.get_table_name()}.{self.local_key}",
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
return related_builder
|
|
48
|
+
|
|
49
|
+
def query_where_exists(self, builder, callback, method="where_exists"):
|
|
50
|
+
query = self.get_builder()
|
|
51
|
+
getattr(builder, method)(
|
|
52
|
+
callback(
|
|
53
|
+
query.where_column(
|
|
54
|
+
f"{query.get_table_name()}.{self.foreign_key}",
|
|
55
|
+
f"{builder.get_table_name()}.{self.local_key}",
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
)
|
|
59
|
+
return query
|
|
60
|
+
|
|
61
|
+
def get_related(self, query, relation, eagers=(), callback=None):
|
|
62
|
+
"""Gets the relation needed between the relation and the related builder. If the relation is a collection
|
|
63
|
+
then will need to pluck out all the keys from the collection and fetch from the related builder. If
|
|
64
|
+
relation is just a Model then we can just call the model based on the value of the related
|
|
65
|
+
builders primary key.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
relation (Model|Collection):
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Model|Collection
|
|
72
|
+
"""
|
|
73
|
+
builder = self.get_builder().with_(eagers)
|
|
74
|
+
if callback:
|
|
75
|
+
callback(builder)
|
|
76
|
+
|
|
77
|
+
if isinstance(relation, Collection):
|
|
78
|
+
return builder.where_in(
|
|
79
|
+
f"{builder.get_table_name()}.{self.foreign_key}",
|
|
80
|
+
Collection(relation._get_value(self.local_key)).unique(),
|
|
81
|
+
).get()
|
|
82
|
+
|
|
83
|
+
else:
|
|
84
|
+
return builder.where(
|
|
85
|
+
f"{builder.get_table_name()}.{self.foreign_key}",
|
|
86
|
+
getattr(relation, self.local_key),
|
|
87
|
+
).first()
|
|
88
|
+
|
|
89
|
+
def register_related(self, key, model, collection):
|
|
90
|
+
related = collection.get(getattr(model, self.local_key), None)
|
|
91
|
+
|
|
92
|
+
model.add_relation({key: related[0] if related else None})
|
|
93
|
+
|
|
94
|
+
def map_related(self, related_result):
|
|
95
|
+
return related_result.group_by(self.foreign_key)
|
|
96
|
+
|
|
97
|
+
def attach(self, current_model, related_record):
|
|
98
|
+
foreign_key_value = getattr(related_record, self.foreign_key)
|
|
99
|
+
if not current_model.is_created():
|
|
100
|
+
current_model.fill({self.local_key: foreign_key_value})
|
|
101
|
+
return current_model.create(
|
|
102
|
+
current_model.all_attributes(), cast=True
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
current_model.update({self.local_key: foreign_key_value})
|
|
106
|
+
return current_model
|
|
107
|
+
|
|
108
|
+
def detach(self, current_model, related_record):
|
|
109
|
+
return current_model.update({self.local_key: None})
|
|
110
|
+
|
|
111
|
+
def relate(self, related_record):
|
|
112
|
+
return (
|
|
113
|
+
self.get_builder()
|
|
114
|
+
.where(
|
|
115
|
+
self.foreign_key, related_record.__attributes__[self.local_key]
|
|
116
|
+
)
|
|
117
|
+
._set_creates_related(
|
|
118
|
+
{
|
|
119
|
+
self.foreign_key: related_record.__attributes__[
|
|
120
|
+
self.local_key
|
|
121
|
+
]
|
|
122
|
+
}
|
|
123
|
+
)
|
|
124
|
+
)
|