FaunaDB数据类型 #
一、数据类型概述 #
1.1 类型分类 #
text
FaunaDB数据类型:
├── 标量类型
│ ├── String(字符串)
│ ├── Number(数字)
│ ├── Boolean(布尔)
│ └── Null(空值)
├── 集合类型
│ ├── Array(数组)
│ └── Object(对象)
├── 特殊类型
│ ├── Ref(引用)
│ ├── Time(时间戳)
│ ├── Date(日期)
│ └── Set(集合引用)
└── 其他类型
├── Bytes(字节)
└── Credentials(凭证)
二、标量类型 #
2.1 字符串(String) #
javascript
// 字符串字面量
"Hello World"
'Hello World'
// Unicode支持
"你好世界"
"🎉"
// 转义字符
"Line1\nLine2"
"Tab\there"
"Quote: \"text\""
// 字符串函数
Concat(["Hello", " ", "World"]) // "Hello World"
Lower("HELLO") // "hello"
Upper("hello") // "HELLO"
Trim(" hello ") // "hello"
Length("Hello") // 5
Substring("Hello World", 0, 5) // "Hello"
ReplaceStr("Hello", "llo", "y") // "Hey"
Split("a,b,c", ",") // ["a", "b", "c"]
2.2 数字(Number) #
javascript
// 整数
42
-100
0
// 浮点数
3.14159
-0.5
2.5e10
// 数学函数
Add(1, 2, 3) // 6
Subtract(10, 3) // 7
Multiply(2, 3, 4) // 24
Divide(10, 2) // 5
Modulo(10, 3) // 1
Abs(-5) // 5
Max(1, 5, 3) // 5
Min(1, 5, 3) // 1
Round(3.14159, 2) // 3.14
Ceil(3.2) // 4
Floor(3.8) // 3
Sqrt(16) // 4
Pow(2, 10) // 1024
// 比较函数
GT(5, 3) // true
GTE(5, 5) // true
LT(3, 5) // true
LTE(5, 5) // true
2.3 布尔(Boolean) #
javascript
// 布尔值
true
false
// 逻辑运算
And(true, true) // true
And(true, false) // false
Or(false, true) // true
Or(false, false) // false
Not(true) // false
// 比较返回布尔
Equals("a", "a") // true
ContainsValue([1, 2, 3], 2) // true
Exists(Ref(Collection("users"), "123"))
// true 或 false
2.4 空值(Null) #
javascript
// Null值
null
// 检查Null
Equals(null, null) // true
// 实际应用
Let(
{
user: Get(Ref(Collection("users"), "123"))
},
If(
Equals(Select(["data", "middleName"], Var("user"), null), null),
"No middle name",
Select(["data", "middleName"], Var("user"))
)
)
三、集合类型 #
3.1 数组(Array) #
javascript
// 数组字面量
[1, 2, 3, 4, 5]
["a", "b", "c"]
[{ name: "Tom" }, { name: "Jerry" }]
[1, "two", true, null] // 混合类型
// 数组函数
Count([1, 2, 3, 4, 5]) // 5
Append([1, 2], [3, 4]) // [1, 2, 3, 4]
Prepend([1, 2], [3, 4]) // [1, 2, 3, 4]
Reverse([1, 2, 3]) // [3, 2, 1]
Take(3, [1, 2, 3, 4, 5]) // [1, 2, 3]
Drop(2, [1, 2, 3, 4, 5]) // [3, 4, 5]
First([1, 2, 3]) // 1
Last([1, 2, 3]) // 3
IsEmpty([]) // true
IsNonEmpty([1, 2, 3]) // true
// 数组元素访问
Select(0, ["a", "b", "c"]) // "a"
Select(2, ["a", "b", "c"]) // "c"
// 数组过滤
Filter(
[1, 2, 3, 4, 5],
Lambda("x", GT(Var("x"), 2))
) // [3, 4, 5]
// 数组映射
Map(
[1, 2, 3],
Lambda("x", Multiply(Var("x"), 2))
) // [2, 4, 6]
// 数组归约
Reduce(
[1, 2, 3, 4, 5],
Lambda(
["acc", "val"],
Add(Var("acc"), Var("val"))
),
0
) // 15
// 数组排序
Sort([3, 1, 4, 1, 5, 9, 2, 6]) // [1, 1, 2, 3, 4, 5, 6, 9]
// 数组去重
Distinct([1, 2, 2, 3, 3, 3]) // [1, 2, 3]
// 检查包含
ContainsValue([1, 2, 3], 2) // true
ContainsPath([1, [2, 3]], [1, 0]) // true (访问 [1][0] = 2)
3.2 对象(Object) #
javascript
// 对象字面量
{
name: "Tom",
age: 30,
active: true
}
// 嵌套对象
{
user: {
name: "Tom",
address: {
city: "Beijing",
country: "China"
}
}
}
// 对象函数
Merge(
{ a: 1, b: 2 },
{ b: 3, c: 4 }
) // { a: 1, b: 3, c: 4 }
// 选择属性
Select("name", { name: "Tom", age: 30 })
// "Tom"
Select(["address", "city"], { address: { city: "Beijing" } })
// "Beijing"
// 带默认值的选择
Select("middleName", { name: "Tom" }, "N/A")
// "N/A"
// 获取所有键
Keys({ a: 1, b: 2, c: 3 }) // ["a", "b", "c"]
// 获取所有值
Values({ a: 1, b: 2, c: 3 }) // [1, 2, 3]
// 检查属性存在
ContainsPath(
{ user: { name: "Tom" } },
["user", "name"]
) // true
// 对象合并
{
...{ a: 1, b: 2 },
...{ b: 3, c: 4 }
} // { a: 1, b: 3, c: 4 }
四、特殊类型 #
4.1 引用(Ref) #
javascript
// 创建引用
Ref(Collection("users"), "123456")
Ref(Database("my_database"))
Ref(Index("users_by_email"))
Ref(Collection("users"), "123456").collection // Collection("users")
Ref(Collection("users"), "123456").id // "123456"
// 文档引用
Let(
{
ref: Ref(Collection("users"), "123456")
},
{
id: Var("ref").id,
collection: Var("ref").collection,
document: Get(Var("ref"))
}
)
// 引用函数
Documents(Collection("users")) // 返回所有文档引用的Set
4.2 时间戳(Time) #
javascript
// 创建时间戳
Time("2024-01-01T00:00:00Z")
Now() // 当前时间
// 时间函数
Epoch(1234567890, "seconds") // Unix时间戳
Time("2024-01-01T00:00:00Z")
// 时间计算
TimeAdd(
Time("2024-01-01T00:00:00Z"),
1,
"day"
) // 2024-01-02T00:00:00Z
TimeSubtract(
Time("2024-01-02T00:00:00Z"),
1,
"day"
) // 2024-01-01T00:00:00Z
// 时间差
TimeDiff(
Time("2024-01-01T00:00:00Z"),
Time("2024-01-02T00:00:00Z"),
"day"
) // 1
// 时间组件
Day(Time("2024-01-15T00:00:00Z")) // 15
Month(Time("2024-01-15T00:00:00Z")) // 1
Year(Time("2024-01-15T00:00:00Z")) // 2024
Hour(Time("2024-01-01T10:30:00Z")) // 10
Minute(Time("2024-01-01T10:30:00Z")) // 30
Second(Time("2024-01-01T10:30:45Z")) // 45
// 时间单位
// "second", "minute", "hour", "day", "month", "year"
4.3 日期(Date) #
javascript
// 创建日期
Date("2024-01-01")
// 从时间戳获取日期
Date(Time("2024-01-01T10:30:00Z")) // 2024-01-01
// 日期计算
DateAdd(
Date("2024-01-01"),
1,
"month"
) // 2024-02-01
DateSubtract(
Date("2024-02-01"),
1,
"month"
) // 2024-01-01
// 日期差
DateDiff(
Date("2024-01-01"),
Date("2024-01-31"),
"day"
) // 30
// 日期组件
Day(Date("2024-01-15")) // 15
Month(Date("2024-01-15")) // 1
Year(Date("2024-01-15")) // 2024
4.4 集合引用(Set) #
javascript
// Set是FaunaDB的查询结果集类型
// 通常由Match、Union、Intersection等操作返回
// Match返回Set
Match(Index("all_users"))
// Set操作
Union(
Match(Index("users_by_status"), "active"),
Match(Index("users_by_status"), "pending")
)
Intersection(
Match(Index("users_by_role"), "admin"),
Match(Index("users_by_status"), "active")
)
Difference(
Match(Index("all_users")),
Match(Index("users_by_status"), "deleted")
)
// Set分页
Paginate(Match(Index("all_users")))
// Set计数
Count(Match(Index("all_users")))
五、其他类型 #
5.1 字节(Bytes) #
javascript
// 创建字节
Bytes("SGVsbG8gV29ybGQ=") // Base64编码
// 字节函数
Length(Bytes("SGVsbG8gV29ybGQ=")) // 字节长度
5.2 凭证(Credentials) #
javascript
// 创建凭证
Credentials({
password: "secret123"
})
// 凭证用于身份验证
Login(
Ref(Collection("users"), "123456"),
{ password: "secret123" }
)
5.3 令牌(Token) #
javascript
// 创建令牌
Create(Tokens(), {
instance: Ref(Collection("users"), "123456")
})
// 令牌用于API访问
六、类型检查函数 #
6.1 类型判断 #
javascript
// 检查类型
IsString("hello") // true
IsNumber(42) // true
IsBoolean(true) // true
IsNull(null) // true
IsArray([1, 2, 3]) // true
IsObject({ a: 1 }) // true
IsRef(Ref(Collection("users"), "1")) // true
IsTime(Now()) // true
IsDate(Date("2024-01-01")) // true
IsSet(Match(Index("all_users"))) // true
IsBytes(Bytes("aGVsbG8=")) // true
IsLambda(Lambda("x", Var("x"))) // true
IsCredentials(...) // true
IsToken(...) // true
// 通用类型检查
IsType("hello", "string") // true
IsType(42, "number") // true
IsType([1, 2], "array") // true
6.2 实际应用 #
javascript
// 安全的类型处理
Let(
{
value: SomeFunction()
},
If(
IsString(Var("value")),
Upper(Var("value")),
If(
IsNumber(Var("value")),
Multiply(Var("value"), 2),
Var("value")
)
)
)
七、类型转换 #
7.1 字符串转换 #
javascript
// 数字转字符串
ToString(42) // "42"
ToString(3.14) // "3.14"
// 布尔转字符串
ToString(true) // "true"
// 时间转字符串
ToString(Now()) // "2024-01-01T00:00:00Z"
ToString(Date("2024-01-01")) // "2024-01-01"
7.2 数字转换 #
javascript
// 字符串转数字
ToNumber("42") // 42
ToNumber("3.14") // 3.14
// 布尔转数字
ToNumber(true) // 1
ToNumber(false) // 0
7.3 时间转换 #
javascript
// 字符串转时间
ToTime("2024-01-01T00:00:00Z")
// 字符串转日期
ToDate("2024-01-01")
// 时间戳转日期
ToDate(Now())
八、文档结构 #
8.1 文档格式 #
javascript
// 文档结构
{
ref: Ref(Collection("users"), "123456789"),
ts: 1704067200000000, // 微秒时间戳
data: {
name: "Tom",
email: "tom@example.com",
age: 30
}
}
// 访问文档属性
Select(["ref"], doc) // Ref
Select(["ts"], doc) // 时间戳
Select(["data", "name"], doc) // "Tom"
8.2 带元数据的文档 #
javascript
// 创建带元数据的文档
Create(Collection("users"), {
data: {
name: "Tom",
email: "tom@example.com"
},
metadata: {
createdBy: "admin",
source: "import"
}
})
// 访问元数据
Select(["metadata", "createdBy"], doc)
九、类型最佳实践 #
9.1 类型安全 #
javascript
// 使用Select的安全访问
Let(
{
user: Get(Ref(Collection("users"), "123"))
},
{
name: Select(["data", "name"], Var("user"), "Unknown"),
age: Select(["data", "age"], Var("user"), 0),
email: Select(["data", "email"], Var("user"), null)
}
)
9.2 类型验证 #
javascript
// 验证输入数据
Create(Collection("users"), {
data: {
name: If(
IsString(Var("name")),
Var("name"),
Abort("Name must be a string")
),
age: If(
And(IsNumber(Var("age")), GTE(Var("age"), 0)),
Var("age"),
Abort("Age must be a non-negative number")
)
}
})
9.3 类型转换最佳实践 #
javascript
// 安全的类型转换
Let(
{
input: Var("someValue"),
converted: If(
IsString(Var("input")),
ToNumber(Var("input")),
If(
IsNumber(Var("input")),
Var("input"),
0
)
)
},
Var("converted")
)
十、总结 #
数据类型要点:
| 类型 | 说明 | 示例 |
|---|---|---|
| String | 字符串 | “hello” |
| Number | 数字 | 42, 3.14 |
| Boolean | 布尔 | true, false |
| Null | 空值 | null |
| Array | 数组 | [1, 2, 3] |
| Object | 对象 | |
| Ref | 引用 | Ref(Collection(“users”), “1”) |
| Time | 时间戳 | Now() |
| Date | 日期 | Date(“2024-01-01”) |
| Set | 集合 | Match(Index(“all”)) |
下一步,让我们学习核心概念!
最后更新:2026-03-27