-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.py
63 lines (47 loc) · 1.61 KB
/
driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import logging
from neo4j import GraphDatabase
import config as cfg
class SystemDriver(object):
_instance = None
_driver = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(SystemDriver, cls).__new__(cls)
cls._driver = GraphDatabase.driver(
cfg.neo4j_uri,
auth=cfg.neo4j_auth,
database="system",
)
cls._driver.verify_connectivity()
return cls._instance
def __del__(self):
self._driver.close()
def drop_database(self):
logging.info(f"🗑️ Dropping 'neo4j' database.")
with self._driver.session() as session:
session.run(f"DROP DATABASE neo4j IF EXISTS")
def create_database(self):
logging.info(f"🚧 Creating 'neo4j' database.")
with self._driver.session() as session:
session.run(f"CREATE OR REPLACE DATABASE neo4j")
class Driver(object):
_instance = None
_driver = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(Driver, cls).__new__(cls)
cls._driver = GraphDatabase.driver(
cfg.neo4j_uri,
auth=cfg.neo4j_auth,
)
cls._driver.verify_connectivity()
return cls._instance
def __del__(self):
self._driver.close()
def query(self, query: str):
logging.info(f"Executing query: \n {query}")
with self._driver.session() as session:
res = session.run(query)
if res is not None:
return res.data()
return res