Skip to content

Commit 8eb857d

Browse files
docs: adds OR sample (#1092)
* docs: adds OR sample * fixed tests * per linter * exclude IDE stuff * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * remove unneeded stuff * git is hard * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d14f87e commit 8eb857d

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ system-test/*key.json
1212
.DS_Store
1313
package-lock.json
1414
__pycache__
15+
.vscode

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-datastore/t
108108
| Import | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/import.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/import.js,samples/README.md) |
109109
| Indexes.get | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/indexes.get.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/indexes.get.js,samples/README.md) |
110110
| Indexes.list | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/indexes.list.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/indexes.list.js,samples/README.md) |
111+
| Create a union between two filters | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/queryFilterOr.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/queryFilterOr.js,samples/README.md) |
111112
| Quickstart | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) |
112113
| Add Task | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/tasks.add.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/tasks.add.js,samples/README.md) |
113114
| Delete Task | [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/tasks.delete.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/tasks.delete.js,samples/README.md) |

samples/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [Import](#import)
1919
* [Indexes.get](#indexes.get)
2020
* [Indexes.list](#indexes.list)
21+
* [Create a union between two filters](#create-a-union-between-two-filters)
2122
* [Quickstart](#quickstart)
2223
* [Add Task](#add-task)
2324
* [Delete Task](#delete-task)
@@ -142,6 +143,25 @@ __Usage:__
142143

143144

144145

146+
### Create a union between two filters
147+
148+
Create a union between two filters (logical OR operator)
149+
150+
View the [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/queryFilterOr.js).
151+
152+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-datastore&page=editor&open_in_editor=samples/queryFilterOr.js,samples/README.md)
153+
154+
__Usage:__
155+
156+
157+
`node samples/queryFilterOr.js`
158+
159+
160+
-----
161+
162+
163+
164+
145165
### Quickstart
146166

147167
View the [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/quickstart.js).

samples/queryFilterOr.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: Create a union between two filters
19+
// description: Create a union between two filters (logical OR operator)
20+
21+
async function main() {
22+
// [START datastore_query_filter_or]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = "your Google Cloud project id";
27+
28+
// Imports the Cloud Datastore
29+
const {Datastore, PropertyFilter, or} = require('@google-cloud/datastore');
30+
31+
async function queryFilterOr() {
32+
// Instantiate the Datastore
33+
const datastore = new Datastore();
34+
const query = datastore
35+
.createQuery('Task')
36+
.filter(
37+
or([
38+
new PropertyFilter('description', '=', 'Buy milk'),
39+
new PropertyFilter('description', '=', 'Feed cats'),
40+
])
41+
);
42+
43+
const [entities] = await datastore.runQuery(query);
44+
for (const entity of entities) {
45+
console.log(`Entity found: ${entity['description']}`);
46+
}
47+
}
48+
49+
await queryFilterOr();
50+
// [END datastore_query_filter_or]
51+
}
52+
53+
exports.queryFilterOr = main;

samples/test/queryFilterOr.test.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/* eslint-disable */
16+
17+
'use strict';
18+
19+
const path = require('path');
20+
const {assert} = require('chai');
21+
const {describe, it, after, before} = require('mocha');
22+
const sinon = require('sinon');
23+
const {Datastore} = require('@google-cloud/datastore');
24+
const datastore = new Datastore();
25+
26+
const {queryFilterOr} = require('../queryFilterOr');
27+
let taskKey1, taskKey2;
28+
29+
describe('Creating a union query', () => {
30+
const stubConsole = function () {
31+
sinon.stub(console, 'error');
32+
sinon.stub(console, 'log');
33+
};
34+
35+
const restoreConsole = function () {
36+
console.log.restore();
37+
console.error.restore();
38+
};
39+
40+
beforeEach(stubConsole);
41+
afterEach(restoreConsole);
42+
43+
before(async () => {
44+
taskKey1 = datastore.key('Task');
45+
const entity1 = {
46+
key: taskKey1,
47+
data: {
48+
description: 'Buy milk',
49+
},
50+
};
51+
52+
taskKey2 = datastore.key('Task');
53+
const entity2 = {
54+
key: taskKey2,
55+
data: {
56+
description: 'Feed cats',
57+
},
58+
};
59+
60+
await datastore.upsert(entity1);
61+
await datastore.upsert(entity2);
62+
});
63+
64+
after(async () => {
65+
await datastore.delete(taskKey1);
66+
await datastore.delete(taskKey2);
67+
});
68+
69+
it('should get a combination of items from the Datastore', async () => {
70+
await queryFilterOr();
71+
assert.include(console.log.firstCall.args[0], 'Entity');
72+
});
73+
});

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import {Transform, pipeline} from 'stream';
4242
import {entity, Entities, Entity, EntityProto, ValueProto} from './entity';
4343
import Key = entity.Key;
4444
export {Entity, Key};
45-
45+
import {PropertyFilter, and, or} from './filter';
46+
export {PropertyFilter, and, or};
4647
import {
4748
GetIndexesCallback,
4849
GetIndexesOptions,

0 commit comments

Comments
 (0)