UNPKG

@google-cloud/datastore

Version:
71 lines 3.11 kB
"use strict"; // Copyright 2025 Google LLC // // 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.buildPropertyTransforms = buildPropertyTransforms; const entity_1 = require("../../entity"); const protos_1 = require("../../../protos/protos"); var ServerValue = protos_1.google.datastore.v1.PropertyTransform.ServerValue; /** * This method takes the user supplied transforms object and returns a list of * corresponding transforms that are instead in the shape of the protos so that * they can be included in the grpc request to the service directly. * * @param transforms The transforms supplied by the user that the user intends * to apply. */ function buildPropertyTransforms(transforms) { const propertyTransforms = []; transforms.forEach((transform) => { const property = transform.property; // If the user's transform has a setToServerValue property then ensure the // propertyTransforms sent in the request have a setToServerValue transform. if (transform.setToServerValue) { propertyTransforms.push({ property, setToServerValue: ServerValue.REQUEST_TIME, }); } // If the transform has an 'increment', 'maximum' or 'minimum' property then // add the corresponding property transform to the propertyTransforms in the // request. ['increment', 'maximum', 'minimum'].forEach(type => { const castedType = type; if (transform[castedType]) { propertyTransforms.push({ property, [castedType]: entity_1.entity.encodeValue(parseFloat(transform[castedType]), property), }); } }); // If the transform has an 'appendMissingElements' or 'removeAllFromArray' // property then add the corresponding property transform to the // propertyTransforms in the request. ['appendMissingElements', 'removeAllFromArray'].forEach(type => { const castedType = type; if (transform[castedType]) { propertyTransforms.push({ property, [castedType]: { values: transform[castedType].map(element => { return entity_1.entity.encodeValue(element, property); }), }, }); } }); }); return propertyTransforms; } //# sourceMappingURL=buildPropertyTransforms.js.map