Using CloudKit v1 you could save a record like this:
function demoSaveRecord(recordName,recordChangeTag,zoneName,name,location,asset) {
var container = CloudKit.getDefaultContainer();
var privateDB = container.privateCloudDatabase;
var record = {
recordType: 'Items',
fields: {
name: { value: name }, ...
}
};
return privateDB.saveRecord(record,options)
.then(function(response) {
if (response.hasErrors) {
// Handle the errors in your app.
throw response.errors[0];
} else {
// It worked
var createdRecord = response.records[0];
}
});
}
But in CloudKit JS v2 they removed the .saveRecord option.
So now I'm stuck because I can't figure out how to save a record in v2. If anybody can help me, or can show me good documentation for v2, please help.
CloudKit JS API diffs v2 https://developer.apple.com/library/content/releasenotes/General/CloudKitJS_v2_APIDiffs/JavaScript/CloudKitJS.html
Apparently you have to use .newRecordsBatch().create(record).commit()
instead.
In my example it would look like this:
function demoSaveRecord(recordName,recordChangeTag,zoneName,name,location,asset) {
var container = CloudKit.getDefaultContainer();
var privateDB = container.privateCloudDatabase;
var record = {
recordType: 'Items',
fields: {
name: { value: name }, ...
}
};
return privateDB.newRecordsBatch().create(record).commit()
.then(function(response) {
if (response.hasErrors) {
// Handle the errors in your app.
throw response.errors[0];
} else {
// It worked
var createdRecord = response.records[0];
}
});
}
You can use the same method to create, update and delete records like this:
myDatabase.newRecordsBatch()
.create(someRecord)
.update(someOtherRecord)
.delete(aThirdRecord)
.commit()
Documentation can be found here: https://developer.apple.com/documentation/cloudkitjs/cloudkit.recordsbatchbuilder