Orator To Masonite ORM

Orator To Masonite ORM Guide

This guide will explain how to move from Orator to Masonite ORM. Masonite ORM was made to be pretty much a straight port of Orator but allow the Masonite organization complete creative control of the ORM.
Orator has since been abandoned and Masonite needed a good ORM to keep fresh features and security up to date with the ORM.
Before moving your project over to Masonite ORM please keep in mind some features are not (_at least currently)_ ported over from Orator. These are features that may be ported over in the future.
This list is a continuously evolving list of features and anything we develop will be removed from the list. These features are planned but not yet finished.
Currently these features are:
  • has one through relationship
If you are using Masonite 2 then you will not be able to upgrade to Masonite ORM because of version conflicts between Masonite and Masonite 2 ORM.

Config

The configuration dictionary between Orator and Masonite ORM is identical. The only difference is that Masonite ORM requires a config/database.py file whereas Orator was optional and needed to be explicitly specified in several places like commands.
If you are coming from Masonite already then don't worry, this file is already there. If not you will need to create this config/database.py file.
This is an example of a Masonite ORM config dictionary:
1
import os
2
3
DATABASES = {
4
'default': 'mysql',
5
'mysql': {
6
'driver': 'mysql',
7
'host': os.getenv('MYSQL_DATABASE_HOST'),
8
'user': os.getenv('MYSQL_DATABASE_USER'),
9
'password': os.getenv('MYSQL_DATABASE_PASSWORD'),
10
'database': os.getenv('MYSQL_DATABASE_DATABASE'),
11
'port': os.getenv('MYSQL_DATABASE_PORT'),
12
'prefix': '',
13
'options': {
14
'charset': 'utf8mb4',
15
},
16
'log_queries': True
17
},
18
'postgres': {
19
'driver': 'postgres',
20
'host': os.getenv('POSTGRES_DATABASE_HOST'),
21
'user': os.getenv('POSTGRES_DATABASE_USER'),
22
'password': os.getenv('POSTGRES_DATABASE_PASSWORD'),
23
'database': os.getenv('POSTGRES_DATABASE_DATABASE'),
24
'port': os.getenv('POSTGRES_DATABASE_PORT'),
25
'prefix': '',
26
'log_queries': True
27
},
28
'sqlite': {
29
'driver': 'sqlite',
30
'database': 'orm.sqlite3',
31
'prefix': '',
32
'log_queries': True
33
},
34
'mssql': {
35
'driver': 'mssql',
36
'host': os.getenv('MSSQL_DATABASE_HOST'),
37
'user': os.getenv('MSSQL_DATABASE_USER'),
38
'password': os.getenv('MSSQL_DATABASE_PASSWORD'),
39
'database': os.getenv('MSSQL_DATABASE_DATABASE'),
40
'port': os.getenv('MSSQL_DATABASE_PORT'),
41
'prefix': '',
42
'log_queries': True
43
},
44
}
Copied!
The other thing you will need to do is change the resolver classes. Orator has a configuration structure like this:
1
from orator import DatabaseManager, Model
2
3
DATABASES = {
4
# ...
5
}
6
7
DB = DatabaseManager(DATABASES)
8
Model.set_connection_resolver(DB)
Copied!
Masonite ORM those same resolver classes looks like this:
1
from masoniteorm.connections import ConnectionResolver
2
3
DATABASES = {
4
# ...
5
}
6
7
DB = ConnectionResolver().set_connection_details(DATABASES)
Copied!

Models

Models are identical but the imports are different. Orator requires you to set the model resolver from the configuration file and then you import that model.
In Masonite ORM you import the model directly:
1
# Masonite
2
from masoniteorm.models import Model
3
4
class User(Model):
5
pass
Copied!

Scopes

Scopes are also identical but the import changes:
1
# Orator
2
from orator.orm import scope
3
4
class User(Model):
5
6
@scope
7
def popular(self, query):
8
return query.where('votes', '>', 100)
Copied!
1
# Masonite
2
from masoniteorm.scopes import scope
3
4
class User(Model):
5
6
@scope
7
def popular(self, query):
8
return query.where('votes', '>', 100)
Copied!

Fetching builder relations

In Orator you could do this:
1
user = User.find(1)
2
user.phone().where('active', 1).get()
Copied!
This would delay the relationship call and would instead append the builder before returning the result.
The above call in Masonite ORM becomes:
1
user = User.find(1)
2
user.related('phone').where('active', 1).get()
Copied!
Last modified 7mo ago