Use Realm
How to see Realm database
Install Realm Browser
How to know my Realm file path
[js]
let YourRealmDB = new Realm({schema: [YourSchema]});
console.log(‘YourRealmDB path =’, YourRealmDB.path);
[/js]
How to change a date field to Realm date
[js]var date = new Date(data.pubDate.toString());[/js]
Error: Missing Realm constructor
Clean and rebuild project
How to avoid duplicate insertion
- Use primary key, if same primary key record is about to be inserted, no insertion is done. That record will be updated!
- Realm doesn’t support compound primary keys. Instead, use a String field as the PrimaryKey and combine multi keys together into a string.
How to avoid UPDATE
[js]realm.write(() => {
realm.create(TodoItem.schema.name, {
id: uuid.v1(),
value,
createdTimestamp: new Date()
})
}, update:false)[/js]
Create Realm DB
[js]
import Realm from ‘realm’
class TodoItem {
static get () { return realm.objects(TodoItem.schema.name) }
static schema = {
name: ‘TodoItem’,
primaryKey: ‘id’,
properties: {
id: {type: ‘string’},
value: {type: ‘string’},
completed: {type: ‘bool’, default: false},
createdTimestamp: {type: ‘date’}
}
}
}
// Create Realm DB
const realm = new Realm({schema: [TodoItem]})
[/js]
[js]
// Retrieves all todo items in sorted(reversed) order
export const getTodoItems = () => {
const todoItems = TodoItem.get().sorted(‘createdTimestamp’, true)
return todoItems
}
// Retrieve a single todo item
export const getTodoItem = (id) => {
const todoItem = realm.objectForPrimaryKey(TodoItem, id)
return todoItem
}
// Update todo item. todoItem parameter must be of Realm.Object
export const updateTodoItem = (todoItem, value, completed) => {
realm.write(() => {
try {
todoItem.value = value
todoItem.completed = completed
} catch (e) {
console.warn(e)
}
})
}
// Creates a new TodoItem
export const createTodoItem = (value) => {
realm.write(() => {
realm.create(TodoItem.schema.name, {
id: uuid.v1(),
value,
createdTimestamp: new Date()
})
})
}
// Deletes a todo Item. todoItem parameter must be Realm.Object
export const deleteTodoItem = (todoItem) => {
realm.write(() => {
realm.delete(todoItem)
})
}
[/js]
https://realm.io/docs/javascript/latest/
https://realm.io/docs/javascript/latest/api/
https://github.com/bosung90/RNStorage
https://github.com/andpor/react-native-sqlite-storage
https://medium.com/@bosung90/tackling-react-native-storage-part-1-d27b2bfa480f
[js]
const CategorySchema = {
name: ‘Category’,
properties: {
title: ‘string’,
color: ‘string’,
rss: ‘string’,
}
};
const ItemSchema = {
name: ‘Item’,
properties: {
title: ‘string’,
image: ‘string’,
date: ‘date’,
category: {type: ‘Category’},
}
};
Realm.open({
schema: [CategorySchema, ItemSchema]
}).then(realm => {
// Create Realm objects and write to local storage
realm.write(() => {
const category = realm.create(‘Category’, {
title: ‘AAAA’,
color: ‘#adfefe’,
rss: ‘https://aaaaa’,
});
//myCar.miles += 20; // Update a property value
});
// Add another category
realm.write(() => {
const category = realm.create(‘Category’, {
title: ‘AAAA’,
color: ‘#adfefe’,
rss: ‘https://aaaaa’,
});
});
});
[/js]
Leave a Reply