realm是多平台的。支持java语言,object-c语言,swift语言,js语言。每种语言建立schema即表结构的写法都是不同的。这里着重讨论js
下面代码摘抄自realm官网: https://realm.io/docs/javascript/latest/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25const Realm = require('realm');
const CarSchema = {
name: 'Car',
properties: {
make: 'string',
model: 'string',
miles: {type: 'int', default: 0},
}
};
const PersonSchema = {
name: 'Person',
properties: {
name: 'string',
birthday: 'date',
cars: 'Car[]'
picture: 'data?', // optional property
}
};
// Initialize a Realm with Car and Person models
Realm.open({schema: [CarSchema, PersonSchema]})
.then(realm => {
// ... use the realm instance to read and modify data
})
看到了木有。表 Person 的column: picture就是选填字段,因为定义其类型后面加了问号 ‘data?’ 加问号就是代表该字段选填,不加就代表是必填项了。