1- import { Store , Module as VuexModule } from 'vuex'
1+ import { Store } from 'vuex'
22import { schema as Normalizr } from 'normalizr'
3- import { Constructor } from '../types'
43import { Schema } from '../schema/Schema'
54import { Model } from '../model/Model'
6- import { RootModule } from '../modules/RootModule'
7- import { Module } from '../modules/Module'
5+ import { Relation } from '../model/attributes/relations/Relation'
86import { State } from '../modules/State'
97import { mutations , Mutations } from '../modules/Mutations'
108
@@ -36,15 +34,6 @@ export class Database {
3634 */
3735 started : boolean = false
3836
39- /**
40- * Register the given model.
41- */
42- register ( model : Constructor < Model > ) : void {
43- const instance = new model ( )
44-
45- this . models [ instance . $entity ] = instance
46- }
47-
4837 /**
4938 * Set the store.
5039 */
@@ -67,101 +56,81 @@ export class Database {
6756 * Initialize the database before a user can start using it.
6857 */
6958 start ( ) : void {
70- this . injectStoreToModels ( )
71-
72- this . createSchemas ( )
73-
74- this . registerModules ( )
59+ this . createRootModule ( )
7560
7661 this . started = true
7762 }
7863
7964 /**
80- * Get a model by the specified entity name .
65+ * Register the given model .
8166 */
82- getModel < M extends Model > ( name : string ) : M {
83- return this . models [ name ] as M
84- }
67+ register < M extends Model > ( model : M ) : void {
68+ if ( ! this . models [ model . $entity ] ) {
69+ this . models [ model . $entity ] = model
8570
86- /**
87- * Get schema by the specified entity name.
88- */
89- getSchema ( name : string ) : Normalizr . Entity {
90- return this . schemas [ name ]
91- }
71+ this . createModule ( model )
9272
93- /**
94- * Inject the store instance to all registered models.
95- */
96- private injectStoreToModels ( ) : void {
97- for ( const name in this . models ) {
98- this . models [ name ] . $setStore ( this . store )
99- }
100- }
73+ this . createSchema ( model )
10174
102- /**
103- * Create the schema definition from registered models and set it to the
104- * `schema` property. This schema will be used by the Interpreter to interpret
105- * the data before persisting them to the store.
106- */
107- private createSchemas ( ) : void {
108- for ( const name in this . models ) {
109- this . schemas [ name ] = this . createSchema ( this . models [ name ] )
75+ this . registerRelatedModels ( model )
11076 }
11177 }
11278
11379 /**
114- * Create schema from the given model .
80+ * Register all related models .
11581 */
116- private createSchema < M extends Model > ( model : M ) : Normalizr . Entity {
117- return new Schema ( model ) . one ( )
82+ private registerRelatedModels < M extends Model > ( model : M ) : void {
83+ for ( const name in model . $fields ) {
84+ const attr = model . $fields [ name ]
85+
86+ if ( attr instanceof Relation ) {
87+ attr . getRelateds ( ) . forEach ( ( m ) => {
88+ this . register ( m . $setStore ( this . store ) )
89+ } )
90+ }
91+ }
11892 }
11993
12094 /**
121- * Generate modules and register them to the store .
95+ * Get a model by the specified entity name .
12296 */
123- private registerModules ( ) : void {
124- this . store . registerModule ( this . connection , this . createModule ( ) )
97+ getModel < M extends Model > ( name : string ) : M {
98+ return this . models [ name ] as M
12599 }
126100
127101 /**
128- * Create modules from the registered models and modules .
102+ * Get schema by the specified entity name .
129103 */
130- private createModule ( ) : VuexModule < any , any > {
131- const module = this . createRootModule ( )
132-
133- for ( const name in this . models ) {
134- module . modules [ name ] = this . createSubModule ( )
135- }
136-
137- return module
104+ getSchema ( name : string ) : Normalizr . Entity {
105+ return this . schemas [ name ]
138106 }
139107
140108 /**
141109 * Create root module.
142110 */
143- private createRootModule ( ) : RootModule {
144- return {
145- namespaced : true ,
146- modules : { }
147- }
111+ private createRootModule ( ) : void {
112+ this . store . registerModule ( this . connection , {
113+ namespaced : true
114+ } )
148115 }
149116
150117 /**
151118 * Create sub module.
152119 */
153- private createSubModule ( ) : Module < State , any > {
154- return {
120+ private createModule < M extends Model > ( model : M ) : void {
121+ const preserveState = ! ! this . store . state [ this . connection ] [ model . $entity ]
122+
123+ this . store . registerModule ( [ this . connection , model . $entity ] , {
155124 namespaced : true ,
156- state : this . createSubState ( ) ,
157- mutations : this . createSubMutations ( )
158- }
125+ state : this . createState ( ) ,
126+ mutations : this . createMutations ( )
127+ } , { preserveState } )
159128 }
160129
161130 /**
162131 * Create sub state.
163132 */
164- private createSubState ( ) : State {
133+ private createState ( ) : State {
165134 return {
166135 data : { }
167136 }
@@ -170,7 +139,14 @@ export class Database {
170139 /**
171140 * Create sub mutations.
172141 */
173- private createSubMutations ( ) : Mutations < State > {
142+ private createMutations ( ) : Mutations < State > {
174143 return mutations
175144 }
145+
146+ /**
147+ * Create schema from the given model.
148+ */
149+ private createSchema < M extends Model > ( model : M ) : Normalizr . Entity {
150+ return this . schemas [ model . $entity ] = new Schema ( model ) . one ( )
151+ }
176152}
0 commit comments