diff --git a/lib/browser/objects.js b/lib/browser/objects.js index 908c1e15..e0385f7b 100644 --- a/lib/browser/objects.js +++ b/lib/browser/objects.js @@ -30,6 +30,7 @@ export default class RealmObject { // Non-mutating methods: createMethods(RealmObject.prototype, objectTypes.OBJECT, [ 'isValid', + 'objectSchema' ]); export function clearRegisteredConstructors() { diff --git a/src/js_realm_object.hpp b/src/js_realm_object.hpp index 44ab4039..21823f9a 100644 --- a/src/js_realm_object.hpp +++ b/src/js_realm_object.hpp @@ -18,12 +18,13 @@ #pragma once +#include "object_accessor.hpp" +#include "object_store.hpp" + #include "js_class.hpp" #include "js_types.hpp" #include "js_util.hpp" - -#include "object_accessor.hpp" -#include "object_store.hpp" +#include "js_schema.hpp" namespace realm { namespace js { @@ -49,6 +50,7 @@ struct RealmObjectClass : ClassDefinition { static std::vector get_property_names(ContextType, ObjectType); static void is_valid(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &); + static void get_object_schema(ContextType, FunctionType, ObjectType, size_t, const ValueType [], ReturnValue &); const std::string name = "RealmObject"; @@ -60,6 +62,7 @@ struct RealmObjectClass : ClassDefinition { MethodMap const methods = { {"isValid", wrap}, + {"objectSchema", wrap}, }; }; @@ -68,6 +71,12 @@ void RealmObjectClass::is_valid(ContextType ctx, FunctionType, ObjectType thi return_value.set(get_internal>(this_object)->is_valid()); } +template +void RealmObjectClass::get_object_schema(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { + auto object = get_internal>(this_object); + return_value.set(Schema::object_for_object_schema(ctx, object->get_object_schema())); +} + template typename T::Object RealmObjectClass::create_instance(ContextType ctx, realm::Object realm_object) { static String prototype_string = "prototype"; diff --git a/tests/js/object-tests.js b/tests/js/object-tests.js index 8c5aa816..64c84890 100644 --- a/tests/js/object-tests.js +++ b/tests/js/object-tests.js @@ -470,6 +470,19 @@ module.exports = { obj.doubleCol; }); }, + + testObjectSchema: function() { + var realm = new Realm({schema: [schemas.TestObject]}); + var obj; + realm.write(function() { + obj = realm.create('TestObject', {doubleCol: 1}); + }); + + const schema = obj.objectSchema(); + TestCase.assertEqual(schema.name, schemas.TestObject.name); + TestCase.assertArraysEqual(Object.keys(schema.properties), Object.keys(schemas.TestObject.properties)); + TestCase.assertEqual(schema.properties.doubleCol.type, 'double'); + }, testIgnoredProperties: function() { var realm = new Realm({schema: [schemas.TestObject]});