realm
Version:
Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores
83 lines • 4.22 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 errors_1 = require("./errors");
const assert_1 = require("./assert");
const schema_1 = require("./schema");
/**
* Validate the fields of a user-provided Realm configuration.
* @internal
*/
function validateConfiguration(config) {
assert_1.assert.object(config, "realm configuration", { allowArrays: false });
const { path, schema, schemaVersion, inMemory, readOnly, fifoFilesFallbackPath, sync, openSyncedRealmLocally, shouldCompact, deleteRealmIfMigrationNeeded, disableFormatUpgrade, encryptionKey, onMigration, migrationOptions, } = config;
assert_1.assert.undefined(sync, "sync");
if (path !== undefined) {
assert_1.assert.string(path, "'path' on realm configuration");
(0, assert_1.assert)(path.length > 0, "The path cannot be empty. Provide a path or remove the field.");
}
if (schema !== undefined) {
(0, schema_1.validateRealmSchema)(schema);
}
if (schemaVersion !== undefined) {
assert_1.assert.number(schemaVersion, "'schemaVersion' on realm configuration");
(0, assert_1.assert)(schemaVersion >= 0 && Number.isInteger(schemaVersion), "'schemaVersion' on realm configuration must be 0 or a positive integer.");
}
if (inMemory !== undefined) {
assert_1.assert.boolean(inMemory, "'inMemory' on realm configuration");
}
if (readOnly !== undefined) {
assert_1.assert.boolean(readOnly, "'readOnly' on realm configuration");
}
if (fifoFilesFallbackPath !== undefined) {
assert_1.assert.string(fifoFilesFallbackPath, "'fifoFilesFallbackPath' on realm configuration");
}
if (onMigration !== undefined) {
assert_1.assert.function(onMigration, "'onMigration' on realm configuration");
}
if (openSyncedRealmLocally !== undefined) {
// Internal use
(0, assert_1.assert)(openSyncedRealmLocally === true, "'openSyncedRealmLocally' on realm configuration is only used internally and must be true if defined.");
}
if (shouldCompact !== undefined) {
assert_1.assert.function(shouldCompact, "'shouldCompact' on realm configuration");
}
if (deleteRealmIfMigrationNeeded !== undefined) {
assert_1.assert.boolean(deleteRealmIfMigrationNeeded, "'deleteRealmIfMigrationNeeded' on realm configuration");
}
if (disableFormatUpgrade !== undefined) {
assert_1.assert.boolean(disableFormatUpgrade, "'disableFormatUpgrade' on realm configuration");
}
if (encryptionKey !== undefined) {
(0, assert_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 ${errors_1.TypeAssertionError.deriveType(encryptionKey)}.`);
}
if (migrationOptions) {
validateMigrationOptions(migrationOptions);
}
}
exports.validateConfiguration = validateConfiguration;
function validateMigrationOptions(options) {
assert_1.assert.object(options, "'migrationOptions'", { allowArrays: false });
const { resolveEmbeddedConstraints } = options;
if (resolveEmbeddedConstraints !== undefined) {
assert_1.assert.boolean(resolveEmbeddedConstraints, "'resolveEmbeddedConstraints' on 'migrationOptions'");
}
}
//# sourceMappingURL=Configuration.js.map