Created
September 23, 2021 07:59
-
-
Save przbadu/7de3c61574aa3e0837e66da2002480ad to your computer and use it in GitHub Desktop.
Example of Realm association
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Realm = require("realm"); | |
const Post = { | |
name: "Post", | |
properties: { | |
timestamp: 'date', | |
content: 'string', | |
title: "string", | |
comments: 'Comment[]' | |
}, | |
}; | |
const Comment = { | |
name: 'Comment', | |
properties: { | |
username: 'string', | |
comment: 'string', | |
post: { | |
type: 'linkingObjects', | |
objectType: 'Post', | |
property: 'comments' | |
} | |
} | |
} | |
// open a local realm with the 'Cat' schema | |
Realm.open({ | |
schemaVersion: 2, | |
path: 'blog.realm', | |
schema: [Post, Comment], | |
}).then(realm => { | |
// create | |
realm.write(() => { | |
realm.create('Post', { | |
title: 'firstPost', | |
content: "my first post", | |
timestamp: new Date(), | |
comments: [ | |
{username: 'john', comment: 'This is my first comment'} | |
] | |
}) | |
}) | |
// read | |
const posts = realm.objects('Post'); | |
console.log('List of posts', posts.length) | |
}) | |
.catch(error => console.error('Error opening realm db', error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment