Skip to content

Instantly share code, notes, and snippets.

@przbadu
Created September 23, 2021 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save przbadu/7de3c61574aa3e0837e66da2002480ad to your computer and use it in GitHub Desktop.
Save przbadu/7de3c61574aa3e0837e66da2002480ad to your computer and use it in GitHub Desktop.
Example of Realm association
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