UNPKG

@google-cloud/datastore

Version:
128 lines 3.67 kB
"use strict"; // Copyright 2023 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 // // https://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.isFilter = exports.PropertyFilter = exports.EntityFilter = exports.or = exports.and = void 0; const entity_1 = require("./entity"); const OP_TO_OPERATOR = new Map([ ['=', 'EQUAL'], ['>', 'GREATER_THAN'], ['>=', 'GREATER_THAN_OR_EQUAL'], ['<', 'LESS_THAN'], ['<=', 'LESS_THAN_OR_EQUAL'], ['HAS_ANCESTOR', 'HAS_ANCESTOR'], ['!=', 'NOT_EQUAL'], ['IN', 'IN'], ['NOT_IN', 'NOT_IN'], ]); var CompositeOperator; (function (CompositeOperator) { CompositeOperator["AND"] = "AND"; CompositeOperator["OR"] = "OR"; })(CompositeOperator || (CompositeOperator = {})); function and(filters) { return new CompositeFilter(filters, CompositeOperator.AND); } exports.and = and; function or(filters) { return new CompositeFilter(filters, CompositeOperator.OR); } exports.or = or; /** * A Filter is a class that contains data for a filter that can be translated * into a proto when needed. * * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#filters| Filters Reference} * */ class EntityFilter { } exports.EntityFilter = EntityFilter; /** * A PropertyFilter is a filter that gets applied to a query directly. * * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#property_filters| Property filters Reference} * * @class */ class PropertyFilter extends EntityFilter { /** * Build a Property Filter object. * * @param {string} Property * @param {Operator} operator * @param {any} val */ constructor(name, op, val) { super(); this.name = name; this.op = op; this.val = val; } /** * Gets the proto for the filter. * */ // eslint-disable-next-line toProto() { return { propertyFilter: { property: { name: this.name, }, op: OP_TO_OPERATOR.get(this.op), value: entity_1.entity.encodeValue(this.val, this.name), }, }; } } exports.PropertyFilter = PropertyFilter; /** * A CompositeFilter is a filter that combines other filters and applies that * combination to a query. * * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#composite_filters| Composite filters Reference} * * @class */ class CompositeFilter extends EntityFilter { /** * Build a Composite Filter object. * * @param {EntityFilter[]} filters */ constructor(filters, op) { super(); this.filters = filters; this.op = op; } /** * Gets the proto for the filter. * */ // eslint-disable-next-line toProto() { return { compositeFilter: { filters: this.filters.map(filter => filter.toProto()), op: this.op, }, }; } } function isFilter(filter) { return filter instanceof EntityFilter; } exports.isFilter = isFilter; //# sourceMappingURL=filter.js.map