WatermelonDB下一代 React 數(shù)據(jù)庫
WatermelonDB,下一代 React 數(shù)據(jù)庫,可構(gòu)建功能強大的 React 和 React Native 應用程序,可在保持快速的同時從數(shù)百個記錄擴展到數(shù)萬個記錄。
WatermelonDB 是一種在 React Native 和 React Web 應用中處理用戶數(shù)據(jù)的新方法。它針對在 React/React Native 中構(gòu)建復雜應用進行優(yōu)化,其首要目標是提高實際性能。簡單來說,就是保證的的應用必須快速啟動。
當你開始擴展到數(shù)千或數(shù)萬個數(shù)據(jù)庫記錄時,會發(fā)現(xiàn)對應用的啟動速度有很大影響,尤其是在速度較慢的 Android 設備上。WatermelonDB 通過惰性(lazy)來解決問題 —— 除非有要求,否則不加載。而且由于所有查詢都是直接以單獨的線程在穩(wěn)定的 SQLite 數(shù)據(jù)庫上執(zhí)行的,所以即使是在較慢的 Android 設備上,多數(shù)查詢也會在不到1毫秒的時間內(nèi)解析,即使有 10,000 條記錄!
Usage
首先,定義模型
class Post extends Model {
@field('name') name
@field('body') body
@children('comments') comments
}
class Comment extends Model {
@field('body') body
@field('author') author
}
然后,將組件與數(shù)據(jù)連接:
const Comment = ({ comment }) => (
<View style={styles.commentBox}>
<Text>{comment.body} — by {comment.author}</Text>
</View>
)
// This is how you make your app reactive! ?
const enhance = withObservables(['comment'], ({ comment }) => ({
comment: comment.observe()
}))
const EnhancedComment = enhance(Comment)
即可進行渲染
const Post = ({ post, comments }) => (
<View>
<Text>{post.name}</Text>
<Text>Comments:</Text>
{comments.map(comment =>
<Comment key={comment.id} comment={comment} />
)}
</View>
)
const enhance = withObservables(['post'], ({ post }) => ({
post: post.observe(),
comments: post.comments.observe()
}))評論
圖片
表情
