Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing JSON #8

Closed
bleonard opened this issue Sep 4, 2015 · 10 comments
Closed

Storing JSON #8

bleonard opened this issue Sep 4, 2015 · 10 comments

Comments

@bleonard
Copy link

bleonard commented Sep 4, 2015

React uses AsyncStorage and we can just write an object (json) to disk.

I'm thinking I want to do that in Realm. That is, any given table would have a "document" column and I'd put a bunch of JSON in there. The other columns would only be things I wanted to filter or sort by.

Is that crazy? Here are some of the benefits

  • I can actually have a null value :-)
  • I can do simple nesting - which is a easier and more reliable than making object graphs in the db
  • less migrations
  • more or less can cache what comes from the server

The downsides are probably something like the downsides to NoSQL in general

  • there are still "migrations" - it's just internal versioning stuff

If it is a not-crazy idea, would I just store a String of the json or is there some more native way to do it?
Is there a string size limit? I see something about 16 MB in the docs for a property.

@alazier
Copy link
Contributor

alazier commented Sep 4, 2015

It's not a crazy idea. We have been planning on supporting a generalized dictionary type with query support (where values can be any supported type) across all the bindings but it has been lower on the list of priorities.

If you want support for any valid JSON, then you would also need support for arrays, which goes beyond what we have previously discussed. The easiest way to make this work would be to store your data it in a string property, and to add a wrapper property to get/set your json object which automatically converts to/from the stored string property. Something like this:

function MyObject() {}
MyObject.prototype.schema = {
  name: 'MyObject',
  properties: [
    {name: 'jsonString', type: RealmType.String},
  ]
};
Object.defineProperty(MyObject.prototype, "json", {
  get: function()    { return JSON.parse(this.jsonString); },
  set: function(val) { this.jsonString = JSON.stringify(val); }
});

@bleonard
Copy link
Author

bleonard commented Sep 4, 2015

Yeah, that's what I did last night.
Working well.

@bleonard bleonard closed this as completed Sep 4, 2015
bdash added a commit that referenced this issue Oct 26, 2015
Support migrating required columns to optional, preserving their contents
alazier added a commit that referenced this issue Nov 14, 2015
7701ba1 Merge pull request #16 from realm/tg-close
883ef12 Add Realm::close() and call it in RealmCache::clear()
e9ca54e Merge pull request #12 from realm/tg-core-0.94.4
0823a62 Merge pull request #13 from realm/tg-is-empty
62f59d9 Merge pull request #15 from realm/tg-file-error-path
b93e5ce Include the path of the file which actually failed to open in exceptions
e1e9cd8 Add ObjectStore::is_empty()
52e7e61 Update for core 0.94.4
271432b Merge pull request #11 from realm/kd-rename-delegate-to-bindingcontext
db36ca5 Remove Realm suffix
70e1967 Rename realm delegate in transact_log_handler as well
4973827 Rename RealmDelegate to RealmBindingContext
347145b Merge pull request #8 from realm/mar-migrate-required-to-optional
0b45772 Add a test showing our behavior when migrating from an optional column to a required column.
9f1702a Support migrating required columns to optional, preserving their contents.
f5b790c Merge pull request #6 from realm/tg-impl
6dfeaf8 Move things which are not part of the API to an impl directory/namespace
513e483 Merge pull request #4 from realm/tg-realm
f646777 Send changes_available() even if autorefresh is enabled
21d32bf Add a bit of documentation for RealmDelegate
95c80c9 Fix error in cleanup after an error during a migration
b7936bb Simplify column shifting for removed properties a little
a0f1dab Add a Schema class, move lookup by name and internal-consistency checks there
b381437 Make Realm::compact() more robust
6133eeb Reduce the scope of a variable
0c111a2 Fix a comment
ba278c5 Fix checks for what types of columns can be optional
7de20ea USe more const refs to avoid copies
dbac77f Make a bunch of things const
1400450 Remove an unused function
06e0ff8 Share ExternalCommitHelpers between Realm instances for a single path
f79dec9 Allow more nullable property types when supported
0eb0bd1 Honor is_nullable when creating columns
ea5c475 Refactor schema initialization a bit
e4f29fe Move the interprocess notification functionality to the object store
b129ebe Shuffle stuff around and clean some things up
eeb2ddd Improve array KVO performance a bit
c3649fb Skip PK uniqueness checking when first creating a Realm file
0a41c85 Improve performance of realm_requires_update() and make more things const
efdfa08 Port some of the KVO support functionality to the object store
65e1eb5 Add the ability to bypass the Realm cache entirely
3f226cf Rework change notifications
045c7b2 Add Realm::get_schema_version()
e4377bb Change realm::Schema to a vector rather than a map
cae4cf2 Remove property.hpp include from object_schema.hpp
55e6cca Convert RLMRealmConfiguration to a wrapper around Realm::Config
563a837 Use NSDMIs for realm::Property
0ae1bb1 Don't cache dynamic realms in the ObjectStore cache either
25a6734 Eliminate some copies
45890f2 Use NSDMIs for Realm
348f4a7 Reduce s_init_mutex's scope
b4f856b Use NSDMIs for Realm::Config and make it moveable
a91839b Store a copy of the encryption key
0700428 Merge pull request #3 from realm/al-bugfixes
b084335 clear Realm cache between tests
cb8364c property copy schema from cached realms
8712c8b fixes for latest object store changes
453e4d8 Fix crash when adding a property to a model without updating the schema version.

git-subtree-dir: src/object-store
git-subtree-split: 7701ba1
@ismdcf
Copy link

ismdcf commented Aug 1, 2016

So what about the second problem

Is there a string size limit? I see something about 16 MB in the docs for a property.

will the string size create an issue.

@alazier
Copy link
Contributor

alazier commented Aug 2, 2016

@ismdcf if you want to store larger blocks of data you will need to break it up into smaller pieces or store it outside of Realm.

@brunobraga95
Copy link

@alazier Is there a new solution for this? I also want to store a JSON value that will constantly change and contain arrays and such. Is using string still the best method?

@alazier
Copy link
Contributor

alazier commented Nov 21, 2016

@brunobraga95 no new solution as of yet.

@brunobraga95
Copy link

@alazier does that mean everytime I need to do a write/query I will have to retrieve and save the whole data?

@cmbasnett
Copy link

Has nothing been done for this? Why was this closed? The solution posted by @alazier doesn't appear to work anymore.

I have nested JSON objects coming from an API; some of those fields are simple JSON. I have no need to search the JSON fields, I merely want to read and write it naturally without needing to serialize/deserialize manually.

@Dhirajbhujbal
Copy link

I have JSON comming from API it contains the fields whose data type is String and Object.
How to store this nested JSON in realm DB column

@AmalMenachery
Copy link

@karthikpala This doesn't seem closed, could please expain the fix

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants