遍历db intmap(person) [英] Looping over db intmap(person)

查看:85
本文介绍了遍历db intmap(person)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

type person = { name : string
                ; age : int
              }

db /person : intmap(person)

我知道如何从数据库中获得一个人,但如何获得他们的全部呢?并将它们打印在html表中?

I know how to get a single person from the db, but how do I get them all? and print them in a html table?

谢谢.

推荐答案

我建议您使用Db.intmap_fold_range而不是InMap.fold. 它将比InMap.fold更快,后者需要先在OPA中构建所有地图,然后再对其进行折叠.

I advise you to use Db.intmap_fold_range instead of InMap.fold. It will be faster than InMap.fold which need to build the all map in OPA before folding on it.

http://opalang.org/resources/doc/index.html#db.opa.html/!/value_stdlib.core.db.Db.intmap_fold_range

以下是您的类型的示例:

Here is an example for your type:

type person = { name : string
                ; age : int
              }

db /person : intmap(person)

add(name, age) =
  /person[age] <- { ~name; ~age }

fold_person(acc, id) =
 person = /person[id]
 <>{acc}</><li>{person.name} {person.age}</li>

start() =
  do add("name1", 1)
  do add("name2", 2)
  do add("name3", 3)
  Db.intmap_fold_range(
    @/person,
    fold_person,
    <></>, 0, none, /* acc, starting key, optional max */
    (_ -> true) /* check range */
  )

server = one_page_server("Hello", start)

这篇关于遍历db intmap(person)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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