@@ -3,6 +3,7 @@ import ClassSystemUtil from '../../src/util/ClassSystem.mjs';
33import Store from './Store.mjs' ;
44import EdgeModel from './EdgeModel.mjs' ;
55import NodeModel from './NodeModel.mjs' ;
6+ import StorageBase from './storage/Base.mjs' ;
67
78/**
89 * The Database class serves as the core coordinator for the Native Edge Graph Database engine.
@@ -20,6 +21,10 @@ class Database extends Base {
2021 * @protected
2122 */
2223 className : 'Neo.ai.graph.Database' ,
24+ /**
25+ * @member {Boolean} autoSave=true
26+ */
27+ autoSave : true ,
2328 /**
2429 * @member {Object|Neo.data.Store|null} edges_=null
2530 * @reactive
@@ -29,7 +34,13 @@ class Database extends Base {
2934 * @member {Object|Neo.data.Store|null} nodes_=null
3035 * @reactive
3136 */
32- nodes_ : null
37+ nodes_ : null ,
38+ /**
39+ * Database persistence wrapper.
40+ * @member {Object|Neo.ai.graph.storage.Base|null} storage_=null
41+ * @reactive
42+ */
43+ storage_ : null
3344 }
3445
3546 /**
@@ -50,6 +61,18 @@ class Database extends Base {
5061 this . nodes . add ( node ) ;
5162 }
5263
64+ /**
65+ * Triggered after the storage config gets changed.
66+ * @param {Neo.ai.graph.storage.Base } value
67+ * @param {Neo.ai.graph.storage.Base } oldValue
68+ * @protected
69+ */
70+ afterSetStorage ( value , oldValue ) {
71+ if ( value ) {
72+ value . load ( ) ;
73+ }
74+ }
75+
5376 /**
5477 * Triggered before the edges config gets changed.
5578 * @param {Object|Neo.data.Store } value
@@ -59,11 +82,15 @@ class Database extends Base {
5982 */
6083 beforeSetEdges ( value , oldValue ) {
6184 oldValue ?. destroy ( ) ;
62- return ClassSystemUtil . beforeSetInstance ( value , Store , {
85+ let store = ClassSystemUtil . beforeSetInstance ( value , Store , {
6386 autoInitRecords : false ,
6487 indices : [ { property : 'source' } , { property : 'target' } ] ,
6588 model : EdgeModel
6689 } ) ;
90+
91+ store ?. on ( 'mutate' , this . onEdgesMutate , this ) ;
92+
93+ return store ;
6794 }
6895
6996 /**
@@ -75,10 +102,32 @@ class Database extends Base {
75102 */
76103 beforeSetNodes ( value , oldValue ) {
77104 oldValue ?. destroy ( ) ;
78- return ClassSystemUtil . beforeSetInstance ( value , Store , {
105+ let store = ClassSystemUtil . beforeSetInstance ( value , Store , {
79106 autoInitRecords : false ,
80107 model : NodeModel
81108 } ) ;
109+
110+ store ?. on ( 'mutate' , this . onNodesMutate , this ) ;
111+
112+ return store ;
113+ }
114+
115+ /**
116+ * Triggered before the storage config gets changed.
117+ * @param {Object|Neo.ai.graph.storage.Base } value
118+ * @param {Object|Neo.ai.graph.storage.Base } oldValue
119+ * @returns {Neo.ai.graph.storage.Base }
120+ * @protected
121+ */
122+ beforeSetStorage ( value , oldValue ) {
123+ if ( value ) {
124+ value = ClassSystemUtil . beforeSetInstance ( value , StorageBase , {
125+ database : this
126+ } ) ;
127+
128+ value . database = this ;
129+ }
130+ return value ;
82131 }
83132
84133 /**
@@ -130,6 +179,36 @@ class Database extends Base {
130179 return nodes ;
131180 }
132181
182+ /**
183+ * Triggered on edges Store mutations to sync storage
184+ * @param {Object } mutation
185+ */
186+ onEdgesMutate ( mutation ) {
187+ if ( this . autoSave && this . storage ) {
188+ if ( mutation . addedItems ?. length > 0 ) {
189+ this . storage . addEdges ( mutation . addedItems ) ;
190+ }
191+ if ( mutation . removedItems ?. length > 0 ) {
192+ this . storage . removeEdges ( mutation . removedItems ) ;
193+ }
194+ }
195+ }
196+
197+ /**
198+ * Triggered on nodes Store mutations to sync storage
199+ * @param {Object } mutation
200+ */
201+ onNodesMutate ( mutation ) {
202+ if ( this . autoSave && this . storage ) {
203+ if ( mutation . addedItems ?. length > 0 ) {
204+ this . storage . addNodes ( mutation . addedItems ) ;
205+ }
206+ if ( mutation . removedItems ?. length > 0 ) {
207+ this . storage . removeNodes ( mutation . removedItems ) ;
208+ }
209+ }
210+ }
211+
133212 /**
134213 * Removes an edge from the Native Edge Graph Database topology.
135214 * @param {String } edgeId
0 commit comments