Mirror and sync Django models in a json in another database.
Clone then:
pip install -r django-jsonmirror/requirements.txt
Add "jsonmirror",
to installed apps.
Declare the databases:
SITE_SLUG = "my_site"
MIRROR_DATABASES = {
"main": {
"name": SITE_SLUG,
"type": "sqlite"
},
"alt": {
"name": SITE_SLUG,
"type": "rethinkdb"
}
}
The SITE_SLUG
setting is required
Declare the models you want to mirror in settings.py
and set the database options for each model to mirror:
MIRRORED_MODELS = {
'django.contrib.auth.models.User': {
"alt": {
"table": "users",
"imutable": False
}
},
'myapp.models.MyModel': {
"main": {
"table": "mytable",
"imutable": False,
"soft_delete": False
},
"alt": {
"table": "pages"
}
}
}
Note that table
is required
Set to True
by default: the database is write only
When imutability is turned of the soft delete
option, enabled by default, will mark documents as deleted.
Set it to False
to delete the documents in the database
Settings:
# required
MIRROR_DB = "mydb"
MIRROR_TABLE = "mymirrortable"
# optional:
RETHINKDB_HOST = "127.0.0.1" # default
RETHINKDB_USER = "admin" # default is None
RETHINKDB_PASSWORD = "mypassword" # default is None
Settings:
# required
MIRROR_SQL_ENGINE = 'sqlite:https:///mirror.db'
Refer to SqlAlchemy docs for how to setup db urls
Note: the sql backend is limited for now: it saves the data to an unique table named mirror
with django model path and
the instance pk to use as compound index to retrieve the data.
The models declared for mirroring will be auto synchronized in the secondary databases at each save/delete operation performed on them
The command python manage.py sync_models
will mirror all the models declared in the MIRRORED_MODELS
setting.
They will be auto created/updated in the secondary databases using this command.
- SqlAlchemy backend
- Redis backend
- Support DRF serializers
- Support custom serializers
- Better handling of connection failure