-
Notifications
You must be signed in to change notification settings - Fork 3
Database
The database module is installed onto both the server and webui projects as a local dependency, i.e. it can be treated like any other imported npm module. One importance difference is that when importing from the database module, you must import from the build/ directory. For example, to import the ComponentType enum, the correct import statement is import ComponentType from "database/build/ComponentType";. Most text editors should select this path by default as long as database has been correctly built beforehand.
Firebase stores its data as a huge JSON object and particular branches of that object are accessed by a path. For example the components for a particular session are stored at /components/.
Each Canvas component (e.g. stocks, variables, flows) including invisible ones (e.g. scenarios) have a ComponentData class which represents the data object that is stored in Firebase under the data field. These are usually interacted with via their FirebaseComponent wrapper class which contains various utilities. Every component has these two classes to represent it.
The FirebaseSchema class contains methods to create paths to the different portions of the database, so for example to get the path to a session's components, call makeAllComponentsForSessionPath(sessionId). FirebaseComponentModel describes the schema for individual component objects like a Stock, Flow, sub-model, etc. as they are stored in the database.
{
"components": {
"Session Id": {
"Component Id": {
type: string,
data: { depends on type }
},
...
},
...
},
"session-ids": [ "Session ID", ... ],
"saved-models: {
"Model ID": {
"Component Id": {
type: string,
data: { depends on type }
},
...
},
...
},
"model-ids": [ "Model ID", ... ]
}
Components maps session IDs to the components for that session. Each session object has keys for each component including invisible ones, and values are objects corresponding to that component's data. This data has a field describing the type of component, and an object containing data for that object which depends on the specific type of object. See more about how different components store data by looking inside FirebaseComponentModel.ts.
This is just a list of all the session IDs without any of their associated data. The reason we store this separately is that Firebase can only give an entire sub-tree when we query it, which means that to get the list of IDs from "components", we'd have to get all of the component data for all of the sessions and then throw away 99% of that data and keep only the session IDs. This allows us to make a faster queries and keep our tree structure flat as recommended by Google.
This is like Components, except that these models can't be edited in real-time. This branch of the database represents the "model library" from which we can import.
Same as Session IDs but for saved models, done for the same reasons.