规范化且不可变的数据模型 [英] Normalized and immutable data model

查看:152
本文介绍了规范化且不可变的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell如何解决规范化的不可变数据结构问题?例如,让我们考虑一个代表前女友/男朋友的数据结构:

  data Man = Man {name :: String,exes :: [Woman]} 

数据女人=女人{name :: String,exes :: [Man]}

一个女人改变了她的名字,她已经和13个男人在一起了?那么所有这13人都应该更新(Haskell的意思)呢?需要某种标准化来避免这些更新。



这是一个非常简单的例子,但想象一个包含20个实体的模型,以及它们之间的任意关系,那么呢?



以不可变的语言表示复杂的,规范化的数据的推荐方式是什么?



例如,可以在此处找到Scala解决方案(请参阅代码如下),它使用引用。 Haskell做什么?

  class RefTo [V](val target:ModelRO [V],val updated:V => ; AnyRef){
def apply()= target()
}

我想知道,如果像上面那个(在Scala中)更通用的解决方案在Haskell中不工作,或者它们不是必需的?如果他们不工作,那为什么不呢?我尝试在Haskell中搜索这样做的库,但它们似乎并不存在。换句话说,如果我想在Haskell中建模规范化的SQL数据库(例如与酸性状态一起使用)是否有一种通用的方式来描述外键?通常我的意思是,不要按照chepner在下面的评论中所建议的那样手动编码ID。

编辑:

换句话说,是否有一个用于在内存中实现SQL /关系数据库的库(对于Haskell或Scala)(可能还使用Event Sourcing进行持久化),以便数据库不可变,并且大多数SQL操作(查询/连接/插入/删除/等)是否实现并且是类型安全的?如果没有这样的图书馆,为什么不呢?这似乎是一个不错的主意。我应该如何创建这样一个库?



编辑2:



一些相关链接:




解决方案

问题在于您正在将数据和关系存储在相同的类型。要正常化,你需要分开。关系数据库101.

  newtype Id a = Id Int  - 类型安全ID。 
data Person = Person {id :: Id Person,name :: String}
data Ex = Ex {personId :: Id Person,exId :: Id Person}

现在,如果某人更改了姓名,则只有一个 Person 值会受到影响。 Ex 条目不关心人的姓名。


How does Haskell solve the "normalized immutable data structure" problem?

For example, let's consider a data structure representing ex girlfriends/boyfriends:

data Man = Man {name ::String, exes::[Woman]}

data Woman = Woman {name :: String, exes::[Man]}

What happens if a Woman changes her name and she had been with 13 man? Then all the 13 man should be "updated" (in the Haskell sense) too? Some kind of normalization is needed to avoid these "updates".

This is a very simple example, but imagine a model with 20 entities, and arbitrary relationships between them, what to do then?

What is the recommended way to represent complex, normalized data in an immutable language?

For example, a Scala solution can be found here (see code below), and it uses references. What to do in Haskell?

class RefTo[V](val target: ModelRO[V], val updated: V => AnyRef) {
  def apply() = target()
}

I wonder, if more general solutions like the one above (in Scala) don't work in Haskell or they are not necessary? If they don't work, then why not? I tried to search for libraries that do this in Haskell but they don't seem to exist.

In other words, if I want to model a normalized SQL database in Haskell (for example to be used with acid-state) is there a general way to describe foreign keys? By general I mean, not hand coding the IDs as suggested by chepner in the comments below.

EDIT:

Yet in other words, is there a library (for Haskell or Scala) that implements an SQL/relational database in memory (possibly also using Event Sourcing for persistence) such that the database is immutable and most of the SQL operations (query/join/insert/delete/etc.) are implemented and are type-safe ? If there is not such a library, why not ? It seems to be a pretty good idea. How should I create such a library ?

EDIT 2:

Some related links:

解决方案

The problem is you are storing data and relationships in the same type. To normalise, you need to separate. Relational databases 101.

newtype Id a = Id Int -- Type-safe ID.
data Person = Person { id :: Id Person, name :: String }
data Ex = Ex { personId :: Id Person, exId :: Id Person }

Now if a person changes their name, only a single Person value is affected. The Ex entries don't care about peoples' names.

这篇关于规范化且不可变的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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