realm
Version:
Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores
87 lines • 4.82 kB
JavaScript
"use strict";
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateConfiguration = void 0;
const internal_1 = require("./internal");
/**
* Validate the fields of a user-provided Realm configuration.
* @internal
*/
function validateConfiguration(config) {
internal_1.assert.object(config, "realm configuration", { allowArrays: false });
const { path, schema, schemaVersion, inMemory, readOnly, fifoFilesFallbackPath, sync, openSyncedRealmLocally, shouldCompact, deleteRealmIfMigrationNeeded, disableFormatUpgrade, encryptionKey, onMigration, migrationOptions, } = config;
if (path !== undefined) {
internal_1.assert.string(path, "'path' on realm configuration");
(0, internal_1.assert)(path.length > 0, "The path cannot be empty. Provide a path or remove the field.");
}
if (schema !== undefined) {
(0, internal_1.validateRealmSchema)(schema);
}
if (schemaVersion !== undefined) {
internal_1.assert.number(schemaVersion, "'schemaVersion' on realm configuration");
(0, internal_1.assert)(schemaVersion >= 0 && Number.isInteger(schemaVersion), "'schemaVersion' on realm configuration must be 0 or a positive integer.");
}
if (inMemory !== undefined) {
internal_1.assert.boolean(inMemory, "'inMemory' on realm configuration");
}
if (readOnly !== undefined) {
internal_1.assert.boolean(readOnly, "'readOnly' on realm configuration");
}
if (fifoFilesFallbackPath !== undefined) {
internal_1.assert.string(fifoFilesFallbackPath, "'fifoFilesFallbackPath' on realm configuration");
}
if (onMigration !== undefined) {
internal_1.assert.function(onMigration, "'onMigration' on realm configuration");
}
if (sync !== undefined) {
(0, internal_1.assert)(!onMigration, "The realm configuration options 'onMigration' and 'sync' cannot both be defined.");
(0, internal_1.assert)(!migrationOptions, "The realm configuration options 'migrationOptions' and 'sync' cannot both be defined.");
(0, internal_1.assert)(inMemory === undefined, "The realm configuration options 'inMemory' and 'sync' cannot both be defined.");
(0, internal_1.assert)(deleteRealmIfMigrationNeeded === undefined, "The realm configuration options 'deleteRealmIfMigrationNeeded' and 'sync' cannot both be defined.");
(0, internal_1.validateSyncConfiguration)(sync);
}
if (openSyncedRealmLocally !== undefined) {
// Internal use
(0, internal_1.assert)(openSyncedRealmLocally === true, "'openSyncedRealmLocally' on realm configuration is only used internally and must be true if defined.");
}
if (shouldCompact !== undefined) {
internal_1.assert.function(shouldCompact, "'shouldCompact' on realm configuration");
}
if (deleteRealmIfMigrationNeeded !== undefined) {
internal_1.assert.boolean(deleteRealmIfMigrationNeeded, "'deleteRealmIfMigrationNeeded' on realm configuration");
}
if (disableFormatUpgrade !== undefined) {
internal_1.assert.boolean(disableFormatUpgrade, "'disableFormatUpgrade' on realm configuration");
}
if (encryptionKey !== undefined) {
(0, internal_1.assert)(encryptionKey instanceof ArrayBuffer || ArrayBuffer.isView(encryptionKey) || encryptionKey instanceof Int8Array, `Expected 'encryptionKey' on realm configuration to be an ArrayBuffer, ArrayBufferView (Uint8Array), or Int8Array, got ${internal_1.TypeAssertionError.deriveType(encryptionKey)}.`);
}
if (migrationOptions) {
validateMigrationOptions(migrationOptions);
}
}
exports.validateConfiguration = validateConfiguration;
function validateMigrationOptions(options) {
internal_1.assert.object(options, "'migrationOptions'", { allowArrays: false });
const { resolveEmbeddedConstraints } = options;
if (resolveEmbeddedConstraints !== undefined) {
internal_1.assert.boolean(resolveEmbeddedConstraints, "'resolveEmbeddedConstraints' on 'migrationOptions'");
}
}
//# sourceMappingURL=Configuration.js.map