Class

CloudKit.Database

A CloudKit.Database object represents a public or private database in an app container.

Overview

Each container has a public database whose data is accessible to all users and, if the current user is signed in, a private database whose data is accessible only by the current user. A database object applies operations to records, subscriptions, and zones within a database.

You do not create database objects yourself, nor should you subclass the CloudKit.Database class. You get a database object using either the publicCloudDatabase or privateCloudDatabase properties in the CloudKit.Container class. You get a CloudKit.Container object using methods in the CloudKit namespace. For example, use CloudKit.getDefaultContainer to get the default container object.

var container = CloudKit.getDefaultContainer();
var publicDatabase = container.publicCloudDatabase;
var privateDatabase = container.privateCloudDatabase;

Read access to the public database doesn’t require that the user sign in. Your web app may fetch records and perform queries on the public database, but by default your app may not save changes to the public database without a signed-in user. Access to the private database requires that the user sign in. To determine whether a user is authenticated, see setUpAuth in CloudKit.Container.

The asynchronous methods in this class return a Promise object that resolves when the operation completes or is rejected due to an error. For a description of the Promise class returned by these methods, go to Mozilla Developer Network: Promise.

This class is similar to the CKDatabase class in the CloudKit framework.

Creating Your Schema

Before you can access records, you must create a schema from your native app using just-in-time schema (see Creating a Database Schema by Saving Records) or using CloudKit Dashboard (see Using CloudKit Dashboard to Manage Databases). Use CloudKit Dashboard to verify that the record types and fields appear in your app’s containers before you test your JavaScript code.

Topics

Getting the Container ID

containerIdentifier

A unique identifier for the container that this database resides in.

Accessing Records

saveRecords

Saves records to the database.

fetchRecords

Fetches one or more records.

deleteRecords

Deletes one or more records.

performQuery

Fetches records by using a query.

newRecordsBatch

Creates records batch builder object for modifying multiple records.

Accessing Record Zones

saveRecordZones

Creates one or more zones in the database.

fetchRecordZones

Fetches one or more zones.

fetchAllRecordZones

Fetches all zones in the database.

deleteRecordZones

Deletes the specified zones.

Subscribing to Changes

saveSubscriptions

Saves one or more subscriptions to record changes.

fetchSubscriptions

Fetches one or more subscriptions.

fetchAllSubscriptions

Fetches all subscriptions in the schema.

deleteSubscriptions

Deletes one or more subscriptions.

Fetching Changes

databaseScope

The type of database (public, private, or shared).

fetchDatabaseChanges

Fetch changed record zones in the database.

fetchRecordZoneChanges

Fetch changes to the specified record zones in the database.

Sharing Records

shareWithUI

Presents a UI to the user which lets them share a record with other users.

Logging

toString

Returns a string representation of this object.