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.
Files changed (116) hide show
  1. masonite_framework_orm-3.0.1.dist-info/METADATA +87 -0
  2. masonite_framework_orm-3.0.1.dist-info/RECORD +116 -0
  3. masonite_framework_orm-3.0.1.dist-info/WHEEL +5 -0
  4. masonite_framework_orm-3.0.1.dist-info/entry_points.txt +3 -0
  5. masonite_framework_orm-3.0.1.dist-info/licenses/LICENSE +21 -0
  6. masonite_framework_orm-3.0.1.dist-info/top_level.txt +1 -0
  7. masoniteorm/__init__.py +1 -0
  8. masoniteorm/collection/Collection.py +605 -0
  9. masoniteorm/collection/__init__.py +1 -0
  10. masoniteorm/commands/CanOverrideConfig.py +16 -0
  11. masoniteorm/commands/CanOverrideOptionsDefault.py +22 -0
  12. masoniteorm/commands/Command.py +6 -0
  13. masoniteorm/commands/Entry.py +43 -0
  14. masoniteorm/commands/MakeMigrationCommand.py +57 -0
  15. masoniteorm/commands/MakeModelCommand.py +78 -0
  16. masoniteorm/commands/MakeModelDocstringCommand.py +37 -0
  17. masoniteorm/commands/MakeObserverCommand.py +54 -0
  18. masoniteorm/commands/MakeSeedCommand.py +54 -0
  19. masoniteorm/commands/MigrateCommand.py +46 -0
  20. masoniteorm/commands/MigrateFreshCommand.py +41 -0
  21. masoniteorm/commands/MigrateRefreshCommand.py +41 -0
  22. masoniteorm/commands/MigrateResetCommand.py +25 -0
  23. masoniteorm/commands/MigrateRollbackCommand.py +26 -0
  24. masoniteorm/commands/MigrateStatusCommand.py +51 -0
  25. masoniteorm/commands/SeedRunCommand.py +35 -0
  26. masoniteorm/commands/ShellCommand.py +205 -0
  27. masoniteorm/commands/__init__.py +18 -0
  28. masoniteorm/commands/stubs/create_migration.stub +20 -0
  29. masoniteorm/commands/stubs/create_seed.stub +9 -0
  30. masoniteorm/commands/stubs/model.stub +9 -0
  31. masoniteorm/commands/stubs/observer.stub +101 -0
  32. masoniteorm/commands/stubs/table_migration.stub +19 -0
  33. masoniteorm/config.py +123 -0
  34. masoniteorm/connections/BaseConnection.py +101 -0
  35. masoniteorm/connections/ConnectionFactory.py +59 -0
  36. masoniteorm/connections/ConnectionResolver.py +132 -0
  37. masoniteorm/connections/MSSQLConnection.py +176 -0
  38. masoniteorm/connections/MySQLConnection.py +232 -0
  39. masoniteorm/connections/PostgresConnection.py +225 -0
  40. masoniteorm/connections/SQLiteConnection.py +179 -0
  41. masoniteorm/connections/__init__.py +6 -0
  42. masoniteorm/exceptions.py +38 -0
  43. masoniteorm/expressions/__init__.py +1 -0
  44. masoniteorm/expressions/expressions.py +288 -0
  45. masoniteorm/factories/Factory.py +112 -0
  46. masoniteorm/factories/__init__.py +1 -0
  47. masoniteorm/helpers/__init__.py +0 -0
  48. masoniteorm/helpers/misc.py +22 -0
  49. masoniteorm/migrations/Migration.py +330 -0
  50. masoniteorm/migrations/__init__.py +1 -0
  51. masoniteorm/models/MigrationModel.py +9 -0
  52. masoniteorm/models/Model.py +1209 -0
  53. masoniteorm/models/Model.pyi +1366 -0
  54. masoniteorm/models/Pivot.py +5 -0
  55. masoniteorm/models/__init__.py +1 -0
  56. masoniteorm/observers/ObservesEvents.py +27 -0
  57. masoniteorm/observers/__init__.py +1 -0
  58. masoniteorm/pagination/BasePaginator.py +10 -0
  59. masoniteorm/pagination/LengthAwarePaginator.py +34 -0
  60. masoniteorm/pagination/SimplePaginator.py +28 -0
  61. masoniteorm/pagination/__init__.py +2 -0
  62. masoniteorm/providers/ORMProvider.py +39 -0
  63. masoniteorm/providers/__init__.py +1 -0
  64. masoniteorm/query/EagerRelation.py +42 -0
  65. masoniteorm/query/QueryBuilder.py +2486 -0
  66. masoniteorm/query/__init__.py +1 -0
  67. masoniteorm/query/grammars/BaseGrammar.py +1027 -0
  68. masoniteorm/query/grammars/MSSQLGrammar.py +194 -0
  69. masoniteorm/query/grammars/MySQLGrammar.py +238 -0
  70. masoniteorm/query/grammars/PostgresGrammar.py +213 -0
  71. masoniteorm/query/grammars/SQLiteGrammar.py +228 -0
  72. masoniteorm/query/grammars/__init__.py +4 -0
  73. masoniteorm/query/processors/MSSQLPostProcessor.py +58 -0
  74. masoniteorm/query/processors/MySQLPostProcessor.py +48 -0
  75. masoniteorm/query/processors/PostgresPostProcessor.py +49 -0
  76. masoniteorm/query/processors/SQLitePostProcessor.py +49 -0
  77. masoniteorm/query/processors/__init__.py +4 -0
  78. masoniteorm/relationships/BaseRelationship.py +161 -0
  79. masoniteorm/relationships/BelongsTo.py +124 -0
  80. masoniteorm/relationships/BelongsToMany.py +604 -0
  81. masoniteorm/relationships/HasMany.py +66 -0
  82. masoniteorm/relationships/HasManyThrough.py +269 -0
  83. masoniteorm/relationships/HasOne.py +111 -0
  84. masoniteorm/relationships/HasOneThrough.py +275 -0
  85. masoniteorm/relationships/MorphMany.py +152 -0
  86. masoniteorm/relationships/MorphOne.py +156 -0
  87. masoniteorm/relationships/MorphTo.py +111 -0
  88. masoniteorm/relationships/MorphToMany.py +108 -0
  89. masoniteorm/relationships/__init__.py +10 -0
  90. masoniteorm/schema/Blueprint.py +1161 -0
  91. masoniteorm/schema/Column.py +144 -0
  92. masoniteorm/schema/ColumnDiff.py +0 -0
  93. masoniteorm/schema/Constraint.py +5 -0
  94. masoniteorm/schema/ForeignKeyConstraint.py +28 -0
  95. masoniteorm/schema/Index.py +5 -0
  96. masoniteorm/schema/Schema.py +359 -0
  97. masoniteorm/schema/Table.py +94 -0
  98. masoniteorm/schema/TableDiff.py +86 -0
  99. masoniteorm/schema/__init__.py +3 -0
  100. masoniteorm/schema/platforms/MSSQLPlatform.py +367 -0
  101. masoniteorm/schema/platforms/MySQLPlatform.py +513 -0
  102. masoniteorm/schema/platforms/Platform.py +97 -0
  103. masoniteorm/schema/platforms/PostgresPlatform.py +551 -0
  104. masoniteorm/schema/platforms/SQLitePlatform.py +481 -0
  105. masoniteorm/schema/platforms/__init__.py +4 -0
  106. masoniteorm/scopes/BaseScope.py +6 -0
  107. masoniteorm/scopes/SoftDeleteScope.py +56 -0
  108. masoniteorm/scopes/SoftDeletesMixin.py +13 -0
  109. masoniteorm/scopes/TimeStampsMixin.py +12 -0
  110. masoniteorm/scopes/TimeStampsScope.py +47 -0
  111. masoniteorm/scopes/UUIDPrimaryKeyMixin.py +8 -0
  112. masoniteorm/scopes/UUIDPrimaryKeyScope.py +51 -0
  113. masoniteorm/scopes/__init__.py +8 -0
  114. masoniteorm/scopes/scope.py +15 -0
  115. masoniteorm/seeds/Seeder.py +42 -0
  116. 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
+ )