react native 初始化项目指定version
react-native init projectname --version 0.29.2
react native realm
定义model
var 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: {type: 'list', objectType: 'Car'},
picture: {type: 'data', optional: true}, // optional property
}
};
// Initialize a Realm with Car and Person models
let realm = new Realm({schema: [CarSchema, PersonSchema]});
创建model
let realm = new Realm({schema: [CarSchema]);
realm.write(() => {
realm.create('Car', {make: 'Honda', model: 'Accord', drive: 'awd'});
});
通过主键创建更新对象
realm.write(() => {
// Create a book object
realm.create('Book', {id: 1, title: 'Recipes', price: 35});
// Update book with new price keyed off the id
realm.create('Book', {id: 1, price: 55}, true);
});
查询
let hondas = realm.objects('Car').filtered('make = "Honda"');
let cars = realm.objects('Car');
// get first 5 Car objects
let firstCars = cars.slice(0, 5);