您好我正在React Native中创建一个字典应用程序,我只想存储一个JSON blob数组,用于保存每个单词的定义。
我非常希望避免对数据进行硬编码并希望我的代码干嘛!
示例JSON blob:
[
{
"word": "triangle",
"definition": "a plane figure with three straight sides and three angles.",
"type": "noun"
},
{
"word": "square",
"definition": "a plane figure with four equal straight sides and four right angles.",
"type": "noun"
},
{
"word": "circle",
"definition": "a round plane figure whose boundary (the circumference) consists of points equidistant from a fixed point (the center).",
"type": "noun"
}
]
存储此数据的最佳策略是:
我认为关系数据库是最好的方法,但我很难搞清楚如何使用数据为数据库设定种子。 React Native上的哪个库用于关系数据库。
感谢您阅读我的问题。
答案 0 :(得分:1)
您可以使用以下架构使用Realm执行您所描述的内容:
let EntrySchema = {
name: 'Entry',
primaryKey: 'word',
properties: {
word: 'string',
definition: 'string',
type: 'string'
}
};
let BookmarkListsSchema = {
name: 'BookmarkList',
properties: {
bookmarks: {type: 'list', objectType: 'Entry'}
}
};
let realm = new Realm({schema: [EntrySchema, BookmarkListsSchema]});
您可以预先填充包含所有词典条目的Realm文件并将其与您的应用程序捆绑在一起,或者您可以下载此文件或JSON并在启动应用程序时初始化您的数据库。
创建/添加书签:
// create your list once
var bookmarkList;
realm.write(() => {
bookmarkList = realm.create('BookmarkList');
});
// add entry for 'triange' to bookmarks
realm.write(() => {
let triangeEntry = realm.objectForPrimaryKey('Entry', 'triangle');
bookmarkList.bookmarks.push(triangleEntry);
});