@react-native-ohos/realm
Version:
Realm by MongoDB is an offline-first mobile database: an alternative to SQLite and key-value stores
70 lines • 2.89 kB
JavaScript
"use strict";
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2024 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.createDictionaryPropertyAccessor = void 0;
const binding_1 = require("../binding");
const assert_1 = require("../assert");
const errors_1 = require("../errors");
const Dictionary_1 = require("../Dictionary");
const TypeHelpers_1 = require("../TypeHelpers");
const Dictionary_2 = require("../collection-accessors/Dictionary");
/** @internal */
function createDictionaryPropertyAccessor({ columnKey, realm, name, type, optional, objectType, getClassHelpers, embedded, }) {
const itemType = (0, TypeHelpers_1.toItemType)(type);
const itemHelpers = (0, TypeHelpers_1.getTypeHelpers)(itemType, {
realm,
name: `value in ${name}`,
getClassHelpers,
objectType,
optional,
objectSchemaName: undefined,
});
const dictionaryAccessor = (0, Dictionary_2.createDictionaryAccessor)({
realm,
typeHelpers: itemHelpers,
itemType,
isEmbedded: embedded,
});
return {
get(obj) {
const internal = binding_1.binding.Dictionary.make(realm.internal, obj, columnKey);
return new Dictionary_1.Dictionary(realm, internal, dictionaryAccessor, itemHelpers);
},
set(obj, value) {
assert_1.assert.inTransaction(realm);
const internal = binding_1.binding.Dictionary.make(realm.internal, obj, columnKey);
// Clear the dictionary before adding new values
internal.removeAll();
assert_1.assert.object(value, `values of ${name}`, { allowArrays: false });
for (const [k, v] of Object.entries(value)) {
try {
dictionaryAccessor.set(internal, k, v);
}
catch (err) {
if (err instanceof errors_1.TypeAssertionError) {
err.rename(`${name}["${k}"]`);
}
throw err;
}
}
},
};
}
exports.createDictionaryPropertyAccessor = createDictionaryPropertyAccessor;
//# sourceMappingURL=Dictionary.js.map