I'm currently building a pretty complex AngularJS application that stores data in a sqlite database. My application data contains references to objects that are shared throughout the app. Angular is able to render this data, compare it, and everything has been fine so far.
What do I mean by all of this? Check out this example for the "boilerplate" of the objects in my application.
var Person = {
name:'some person',
owns:[]
}
var Cat = {
name:'some cat'
}
var project = {
name:'some project',
people:[],
cats:[]
}
My application will allow users to create and manipulate these objects, and create relationships between them. Instances of Person
may be created, and each Person
object may store references to instances of Cat
.
As you may have already guessed, my project
will eventually look like this after the user is done manipulating it.
var project = {
name:'some project',
people:[person, person, person],
cats:[cat, cat cat, cat, cat, cat]
}
console.log(project.people[0].owns)
//logs something like "cat, cat, cat". These are references.
My application then has views set up to view each person
and will list out the owns
property, which contains the instances of Cat
.
All is fine in dandy until I realized that there may be complications storing this as JSON in a database. JSON.Stringify()
and angular.toJSON()
do not treat references as references, instead they read them as individual objects.
I am hoping to gain some insights on the best way to preserve these relationships/references.
Here are the two options that I believe I have.
Option 1: Scrap the idea of storing this in JSON, and use a relational database to store everything. This isn't ideal because it starts to eat away at the flexible nature of object rendering in AngularJS. Additionally, this data will be stored in multiple places (online & locally) which may present differences in the database scheme which would be a debugging nightmare.
Option 2:
Store a unique identifier with each instance of Person
and Cat
. I can then use this identifier when rendering and creating the associations. This will work if I create custom filters in Angular, but globally removing references when objects are deleted could be a nightmare.
project
object. But I don't know about sqllite. What they wouldn't retrieve is any construtor functions that you might use to create these objects. If you're using just plain JS objects, this won't hurt at all.