在存储JSON对象引用 [英] Storing object references in JSON

查看:132
本文介绍了在存储JSON对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设,在SQLite数据库中存储数据的pretty复杂AngularJS应用。我的应用程序数据包含对在整个应用程序共享对象的引用。角能够使这个数据,比较它,一切都被罚款至今。

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.

所有在花花公子罚款,直到我意识到有可能出现并发症存储在数据库中这是JSON。 JSON.Stringify() angular.toJSON()作为参考,而不是他们读他们作为个人没有把引用对象。

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.

我希望能获得的最佳方式的一些见解,以preserve这些关系/引用。

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.

选项1:
废钢JSON存储这个的想法,并使用关系数据库来存储一切。因为它开始蚕食的对象呈现在AngularJS灵活的性质,这是不理想。此外,该数据将存储在多个地方(网上和放大器;局部)。这可能美元的数据库方案p $ psent差异这将是一个噩梦调试

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.

选项2:
存储一个唯一的标识符与的每个实例。然后我就可以渲染和创建社团时使用此标识。如果我创建的角度定义过滤器这是可行的,但在全球范围对象被删除时可能是一场噩梦去除引用。

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.

推荐答案

传统的基于表的关系数据库已经解决了这个问题。可能在多种环境中引用的每个记录/文档存储在它自己的表/集合与 ID 。这 ID 被其他物体在自己的藏品引用。

Traditional table based relation databases have solved this problem. Each record/document that could be referenced in multiple contexts is stored in it own table/collection with an id. That id is referenced by other objects in their own collections.

JSON 本身只持有原始数据,所以你注意,改变对象的引用是不是 JSON 可以处理。

JSON itself will only hold raw data, so as you note, object references that change is not something JSON can handle.

于是问题就来了,是该数据一阶对象之间的关系?或者是嵌套/嵌入另一个对象?

So the question becomes, is the data a relationship between first order objects? Or is it nested/embedded in another object?

在你的榜样,一个项目有许多人,一个项目有许多猫,人们有很多的项目。所以,因为人员和项目都需要由多个其他对象引用,每个资源应该是它自己的资源,通过引用外键。

In your example, a project has many people, a project has many cats, and people has many projects. So because people and projects both need to be referenced by multiple other objects, each resource should be it's own resource, reference by a foreign key.

// projects.json
[
  {
    id: 4
    name: 'homework',
    person_ids: [1,2,3],
  }
]

// people.json
[
  {
    id: 1,
    name: 'Bob',
    project_ids: [4,5,6],
  }
]

一个很好的例子嵌入可能是一个项目的意见。在这种情况下,你总是通过访问项目的意见。而由于评论永远只能属于一个项目,嵌入它更有意义。

A good case for embedding might be comments on a project. In this case, you always access the comments through the projects. And because a comment can only ever belong to a single project, embedding it makes more sense.

// projects.json
[
  {
    id: 4
    name: 'homework',
    person_ids: [1,2,3],
    comments: [
      {
         person_id: 1,
         message: 'My dog ate it'
      ]
    ]
  }
]


我建议你在 Mongoid如何处理关系读了。它是MongoDB的红宝石包装,但该文档解释其结构集合,使关系。您可能会发现它非常有助于让你的头围绕如何做到这一点的工作。


I'd recommend reading up on how Mongoid handles relations. It's a ruby wrapper for MongoDB, but the docs explain how it structures the collections to enable relationships. You might find it quite helpful to get your head around how this would work.

这篇关于在存储JSON对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆