Last active
January 9, 2019 10:05
-
-
Save aanastasiou/38410df171f6c309cbbfbf0a7724ca5b to your computer and use it in GitHub Desktop.
Short term solution to using Neomodel 3.3.0 with Flask.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is a module that is supposed to be describing the whole datamodel of an application. | |
NOTE: The directory structure that is implied here is: | |
src/ | |
app_models/ | |
__init__.py (THIS FILE) | |
main.py | |
""" | |
import neomodel | |
class ABaseClass(neomodel.StructuredNode): | |
object_id = neomodel.UniqueIdProperty() | |
class SomeClass(ABaseClass): | |
payload = neomodel.StringProperty() | |
class SomeOtherClass(SomeClass): | |
numeric_payload = neomodel.IntegerProperty() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A very simple Flask example to demonstrate saving and restoring the neomodel.db object state. | |
This example is related to issue #378. | |
Please note: | |
1. This technique can be replicated as is but is more likely to lead to the development of a | |
flask-neomodel-manager that will be handling the saving/restoring of the node class registry in the background. | |
2. The application requires the environment variables NEO4J_USERNAME, NEO4J_PASSWORD and NEO4J_BOLT_URL to be | |
set before it can connect to a neo4j instance. | |
""" | |
import os | |
import flask | |
from neomodel import config, db | |
import app_models | |
# NOTE: The application's data model is loaded as a separate module to ensure that it will be made known to neomodel | |
# in its entirety. | |
# Create the application | |
app = flask.Flask(__name__) | |
# Set up its routes | |
@app.route("/", methods=["GET"]) | |
def get_all_items(): | |
# NOTE: PRIOR TO EXECUTING ANY DB OPERATION, RESTORE THE STATE OF THE NEOMODEL.DB OBJECT | |
app_models.neomodel.db._NODE_CLASS_REGISTRY = flask.current_app.config["neomodel_db_NODE_CLASS_REGISTRY"] | |
# From this point onwards, operations are executed as expected. | |
return "Stuff:{}".format(" -- ".join(map(lambda x:x.payload,app_models.SomeClass.nodes.all()))) | |
# At this point, the neomodel.db._NODE_CLASS_REGISTRY has been populated as expected | |
# NOTE: Save the state of neomodel.db._NODE_CLASS_REGISTRY | |
app.config.update(neomodel_db_NODE_CLASS_REGISTRY = db._NODE_CLASS_REGISTRY) | |
config.DATABASE_URL = os.environ["NEO4J_BOLT_URL"] | |
# This is just an initialisation step that executes only during creation of the application just to make sure that | |
# the database is populated with some data. | |
app_models.neomodel.db.cypher_query("MATCH (a) detach delete a;"); | |
app_models.SomeClass(payload="Alpha").save() | |
app_models.SomeClass(payload="Beta").save() | |
app_models.SomeClass(payload="Gamma").save() | |
app_models.SomeClass(payload="Delta").save() | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment