UNPKG

3.67 kBJavaScriptView Raw
1"use strict";
2// Copyright 2023 Google LLC
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.isFilter = exports.PropertyFilter = exports.EntityFilter = exports.or = exports.and = void 0;
17const entity_1 = require("./entity");
18const OP_TO_OPERATOR = new Map([
19 ['=', 'EQUAL'],
20 ['>', 'GREATER_THAN'],
21 ['>=', 'GREATER_THAN_OR_EQUAL'],
22 ['<', 'LESS_THAN'],
23 ['<=', 'LESS_THAN_OR_EQUAL'],
24 ['HAS_ANCESTOR', 'HAS_ANCESTOR'],
25 ['!=', 'NOT_EQUAL'],
26 ['IN', 'IN'],
27 ['NOT_IN', 'NOT_IN'],
28]);
29var CompositeOperator;
30(function (CompositeOperator) {
31 CompositeOperator["AND"] = "AND";
32 CompositeOperator["OR"] = "OR";
33})(CompositeOperator || (CompositeOperator = {}));
34function and(filters) {
35 return new CompositeFilter(filters, CompositeOperator.AND);
36}
37exports.and = and;
38function or(filters) {
39 return new CompositeFilter(filters, CompositeOperator.OR);
40}
41exports.or = or;
42/**
43 * A Filter is a class that contains data for a filter that can be translated
44 * into a proto when needed.
45 *
46 * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#filters| Filters Reference}
47 *
48 */
49class EntityFilter {
50}
51exports.EntityFilter = EntityFilter;
52/**
53 * A PropertyFilter is a filter that gets applied to a query directly.
54 *
55 * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#property_filters| Property filters Reference}
56 *
57 * @class
58 */
59class PropertyFilter extends EntityFilter {
60 /**
61 * Build a Property Filter object.
62 *
63 * @param {string} Property
64 * @param {Operator} operator
65 * @param {any} val
66 */
67 constructor(name, op, val) {
68 super();
69 this.name = name;
70 this.op = op;
71 this.val = val;
72 }
73 /**
74 * Gets the proto for the filter.
75 *
76 */
77 // eslint-disable-next-line
78 toProto() {
79 return {
80 propertyFilter: {
81 property: {
82 name: this.name,
83 },
84 op: OP_TO_OPERATOR.get(this.op),
85 value: entity_1.entity.encodeValue(this.val, this.name),
86 },
87 };
88 }
89}
90exports.PropertyFilter = PropertyFilter;
91/**
92 * A CompositeFilter is a filter that combines other filters and applies that
93 * combination to a query.
94 *
95 * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#composite_filters| Composite filters Reference}
96 *
97 * @class
98 */
99class CompositeFilter extends EntityFilter {
100 /**
101 * Build a Composite Filter object.
102 *
103 * @param {EntityFilter[]} filters
104 */
105 constructor(filters, op) {
106 super();
107 this.filters = filters;
108 this.op = op;
109 }
110 /**
111 * Gets the proto for the filter.
112 *
113 */
114 // eslint-disable-next-line
115 toProto() {
116 return {
117 compositeFilter: {
118 filters: this.filters.map(filter => filter.toProto()),
119 op: this.op,
120 },
121 };
122 }
123}
124function isFilter(filter) {
125 return filter instanceof EntityFilter;
126}
127exports.isFilter = isFilter;
128//# sourceMappingURL=filter.js.map
\No newline at end of file