mirror of
https://github.com/status-im/realm-js.git
synced 2025-02-04 10:43:29 +00:00
Merge pull request #1874 from realm/cm/template-method
* Add support for creating template objects
This commit is contained in:
commit
f5a8805d1e
25
CHANGELOG.md
25
CHANGELOG.md
@ -1,3 +1,25 @@
|
|||||||
|
X.Y.Z Release notes
|
||||||
|
=============================================================
|
||||||
|
### Compatibility
|
||||||
|
* Sync protocol: 24
|
||||||
|
* Server-side history format: 4
|
||||||
|
* File format: 7
|
||||||
|
* Realm Object Server: 3.0.0 or later
|
||||||
|
|
||||||
|
### Breaking changes
|
||||||
|
* None.
|
||||||
|
|
||||||
|
### Enhancements
|
||||||
|
* Added `Realm.createTemplateObject(objectSchema)` (#1870).
|
||||||
|
|
||||||
|
### Bug fixes
|
||||||
|
* None.
|
||||||
|
|
||||||
|
### Internals
|
||||||
|
* Realm Core v5.6.2.
|
||||||
|
* Realm Sync v3.5.5.
|
||||||
|
|
||||||
|
|
||||||
2.9.0 Release notes (2018-6-19)
|
2.9.0 Release notes (2018-6-19)
|
||||||
=============================================================
|
=============================================================
|
||||||
### Compatibility
|
### Compatibility
|
||||||
@ -16,7 +38,8 @@
|
|||||||
* Fix incorrect documentation of the `shouldCompactOnLaunch` parameters.
|
* Fix incorrect documentation of the `shouldCompactOnLaunch` parameters.
|
||||||
|
|
||||||
### Internals
|
### Internals
|
||||||
* None.
|
* Realm Core v5.6.2.
|
||||||
|
* Realm Sync v3.5.5.
|
||||||
|
|
||||||
|
|
||||||
2.8.5 Release notes (2018-6-18)
|
2.8.5 Release notes (2018-6-18)
|
||||||
|
@ -132,6 +132,15 @@ class Realm {
|
|||||||
*/
|
*/
|
||||||
static automaticSyncConfiguration(user) {}
|
static automaticSyncConfiguration(user) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a template object for a Realm model class where all optional fields are `undefined` and all required
|
||||||
|
* fields have the default value for the given data type, either the value set by the `default` property in the
|
||||||
|
* schema or the default value for the datatype if the schema doesn't specify one, i.e. `0`, false and `""`.
|
||||||
|
*
|
||||||
|
* @param {Realm~ObjectSchema} schema object describing the class
|
||||||
|
*/
|
||||||
|
static createTemplateObject(objectSchema) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Closes this Realm so it may be re-opened with a newer schema version.
|
* Closes this Realm so it may be re-opened with a newer schema version.
|
||||||
* All objects and collections from this Realm are no longer valid after calling this method.
|
* All objects and collections from this Realm are no longer valid after calling this method.
|
||||||
|
@ -126,6 +126,47 @@ module.exports = function(realmConstructor) {
|
|||||||
callback(error);
|
callback(error);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
createTemplateObject(objectSchema) {
|
||||||
|
let obj = {};
|
||||||
|
for (let key in objectSchema.properties) {
|
||||||
|
|
||||||
|
let type;
|
||||||
|
if (typeof objectSchema.properties[key] === 'string' || objectSchema.properties[key] instanceof String) {
|
||||||
|
// Simple declaration of the type
|
||||||
|
type = objectSchema.properties[key];
|
||||||
|
} else {
|
||||||
|
// Advanced property setup
|
||||||
|
const property = objectSchema.properties[key];
|
||||||
|
|
||||||
|
// if optional is set, it wil take precedence over any `?` set on the type parameter
|
||||||
|
if (property.optional === true) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a default value is explicitly set, always set the property
|
||||||
|
if (property.default !== undefined) {
|
||||||
|
obj[key] = property.default;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
type = property.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the default value for all required primitive types.
|
||||||
|
// Lists are always treated as empty if not specified and references to objects are always optional
|
||||||
|
switch (type) {
|
||||||
|
case 'bool': obj[key] = false; break;
|
||||||
|
case 'int': obj[key] = 0; break;
|
||||||
|
case 'float': obj[key] = 0.0; break;
|
||||||
|
case 'double': obj[key] = 0.0; break;
|
||||||
|
case 'string': obj[key] = ""; break;
|
||||||
|
case 'data': obj[key] = new ArrayBuffer(0); break;
|
||||||
|
case 'date': obj[key] = new Date(0); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// Add sync methods
|
// Add sync methods
|
||||||
|
6
lib/index.d.ts
vendored
6
lib/index.d.ts
vendored
@ -620,6 +620,12 @@ declare class Realm {
|
|||||||
*/
|
*/
|
||||||
static automaticSyncConfiguration(user?: Realm.Sync.User): string;
|
static automaticSyncConfiguration(user?: Realm.Sync.User): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Realm.ObjectSchema} object schema describing the object that should be created.
|
||||||
|
* @returns {T}
|
||||||
|
*/
|
||||||
|
static createTemplateObject<T>(objectSchema: Realm.ObjectSchema): T;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete the Realm file for the given configuration.
|
* Delete the Realm file for the given configuration.
|
||||||
* @param {Configuration} config
|
* @param {Configuration} config
|
||||||
|
@ -1256,6 +1256,38 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
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);
|
||||||
|
TestCase.assertEqual(managedObj.objectCol.doubleCol, 1);
|
||||||
|
TestCase.assertEqual(managedObj.nullObjectCol, null);
|
||||||
|
TestCase.assertEqual(managedObj.arrayCol[0].doubleCol, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// FIXME: reanble test
|
// FIXME: reanble test
|
||||||
/*
|
/*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user