How to use the realm.createTemplateObject function in realm

To help you get started, we’ve selected a few realm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github realm / realm-js / tests / js / realm-tests.js View on Github external
testCreateTemplateObject: function() {
        var realm = new Realm({schema: [
            schemas.AllTypes,
            schemas.DefaultValues,
            schemas.TestObject,
            schemas.LinkToAllTypes
        ]});
        realm.beginTransaction();

        // Test all simple data types
        let template = Realm.createTemplateObject(schemas.AllTypes);
        TestCase.assertEqual(Object.keys(template).length, 7);
        let unmanagedObj = Object.assign(template, { boolCol: true });
        let managedObj = realm.create(schemas.AllTypes.name, unmanagedObj) ;
        TestCase.assertEqual(managedObj.boolCol, true);

        // Default values
        unmanagedObj = Realm.createTemplateObject(schemas.DefaultValues);
        TestCase.assertEqual(Object.keys(unmanagedObj).length, 10);
        managedObj = realm.create(schemas.DefaultValues.name, unmanagedObj);
        TestCase.assertEqual(managedObj.boolCol, true);
        TestCase.assertEqual(managedObj.intCol, -1);
        TestCase.assertEqualWithTolerance(managedObj.floatCol, -1.1, 0.000001);
        TestCase.assertEqualWithTolerance(managedObj.doubleCol, -1.11, 0.000001);
        TestCase.assertEqual(managedObj.stringCol, 'defaultString');
        TestCase.assertEqual(managedObj.dateCol.getTime(), 1);
        TestCase.assertEqual(managedObj.dataCol.byteLength, 1);
github realm / realm-js / tests / js / realm-tests.js View on Github external
async testRealmCreateOrUpdate_diffedUpdatesOnlyTriggerNotificationsForChangedValues() {
        const realm = new Realm({schema: [schemas.AllPrimaryTypes, schemas.TestObject]});

        let resolve;
        realm.objects('AllPrimaryTypesObject').addListener((objects, changes) => {
            resolve([objects, changes]);
            resolve = undefined;
        });

        let [objects, changes] = await new Promise(r => resolve = r);
        TestCase.assertEqual(changes.insertions.length, 0);
        TestCase.assertEqual(objects.length, 0);

        let template = Realm.createTemplateObject(schemas.AllPrimaryTypes);

        // First notification -> Object created
        realm.write(() => {
            // Create Initial object
            realm.create('AllPrimaryTypesObject', Object.assign(template, {
                primaryCol: '35',
                boolCol: false,
            }));
            realm.create('AllPrimaryTypesObject', Object.assign(template, {
                primaryCol: '36',
                boolCol: false,
            }));
        });
        [objects, changes] = await new Promise(r => resolve = r);
        TestCase.assertEqual(changes.insertions.length, 2);
        TestCase.assertEqual(objects[0].boolCol, false);
github realm / realm-js / tests / js / realm-tests.js View on Github external
schemas.AllTypes,
            schemas.DefaultValues,
            schemas.TestObject,
            schemas.LinkToAllTypes
        ]});
        realm.beginTransaction();

        // Test all simple data types
        let template = Realm.createTemplateObject(schemas.AllTypes);
        TestCase.assertEqual(Object.keys(template).length, 7);
        let unmanagedObj = Object.assign(template, { boolCol: true });
        let managedObj = realm.create(schemas.AllTypes.name, unmanagedObj) ;
        TestCase.assertEqual(managedObj.boolCol, true);

        // Default values
        unmanagedObj = Realm.createTemplateObject(schemas.DefaultValues);
        TestCase.assertEqual(Object.keys(unmanagedObj).length, 10);
        managedObj = realm.create(schemas.DefaultValues.name, unmanagedObj);
        TestCase.assertEqual(managedObj.boolCol, true);
        TestCase.assertEqual(managedObj.intCol, -1);
        TestCase.assertEqualWithTolerance(managedObj.floatCol, -1.1, 0.000001);
        TestCase.assertEqualWithTolerance(managedObj.doubleCol, -1.11, 0.000001);
        TestCase.assertEqual(managedObj.stringCol, 'defaultString');
        TestCase.assertEqual(managedObj.dateCol.getTime(), 1);
        TestCase.assertEqual(managedObj.dataCol.byteLength, 1);
        TestCase.assertEqual(managedObj.objectCol.doubleCol, 1);
        TestCase.assertEqual(managedObj.nullObjectCol, null);
        TestCase.assertEqual(managedObj.arrayCol[0].doubleCol, 2);
    },