Skip to content

Commit

Permalink
docs: adds OR sample (#1092)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
telpirion and gcf-owl-bot[bot] authored Mar 27, 2023
1 parent d14f87e commit 8eb857d
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ system-test/*key.json
.DS_Store
package-lock.json
__pycache__
.vscode
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-datastore/t
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
Expand Down
20 changes: 20 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [Import](#import)
* [Indexes.get](#indexes.get)
* [Indexes.list](#indexes.list)
* [Create a union between two filters](#create-a-union-between-two-filters)
* [Quickstart](#quickstart)
* [Add Task](#add-task)
* [Delete Task](#delete-task)
Expand Down Expand Up @@ -142,6 +143,25 @@ __Usage:__



### Create a union between two filters

Create a union between two filters (logical OR operator)

View the [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)

__Usage:__


`node samples/queryFilterOr.js`


-----




### Quickstart

View the [source code](https://github.com/googleapis/nodejs-datastore/blob/main/samples/quickstart.js).
Expand Down
53 changes: 53 additions & 0 deletions samples/queryFilterOr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.

'use strict';

// sample-metadata:
// title: Create a union between two filters
// description: Create a union between two filters (logical OR operator)

async function main() {
// [START datastore_query_filter_or]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = "your Google Cloud project id";

// Imports the Cloud Datastore
const {Datastore, PropertyFilter, or} = require('@google-cloud/datastore');

async function queryFilterOr() {
// Instantiate the Datastore
const datastore = new Datastore();
const query = datastore
.createQuery('Task')
.filter(
or([
new PropertyFilter('description', '=', 'Buy milk'),
new PropertyFilter('description', '=', 'Feed cats'),
])
);

const [entities] = await datastore.runQuery(query);
for (const entity of entities) {
console.log(`Entity found: ${entity['description']}`);
}
}

await queryFilterOr();
// [END datastore_query_filter_or]
}

exports.queryFilterOr = main;
73 changes: 73 additions & 0 deletions samples/test/queryFilterOr.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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
//
// 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.

/* eslint-disable */

'use strict';

const path = require('path');
const {assert} = require('chai');
const {describe, it, after, before} = require('mocha');
const sinon = require('sinon');
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();

const {queryFilterOr} = require('../queryFilterOr');
let taskKey1, taskKey2;

describe('Creating a union query', () => {
const stubConsole = function () {
sinon.stub(console, 'error');
sinon.stub(console, 'log');
};

const restoreConsole = function () {
console.log.restore();
console.error.restore();
};

beforeEach(stubConsole);
afterEach(restoreConsole);

before(async () => {
taskKey1 = datastore.key('Task');
const entity1 = {
key: taskKey1,
data: {
description: 'Buy milk',
},
};

taskKey2 = datastore.key('Task');
const entity2 = {
key: taskKey2,
data: {
description: 'Feed cats',
},
};

await datastore.upsert(entity1);
await datastore.upsert(entity2);
});

after(async () => {
await datastore.delete(taskKey1);
await datastore.delete(taskKey2);
});

it('should get a combination of items from the Datastore', async () => {
await queryFilterOr();
assert.include(console.log.firstCall.args[0], 'Entity');
});
});
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ import {Transform, pipeline} from 'stream';
import {entity, Entities, Entity, EntityProto, ValueProto} from './entity';
import Key = entity.Key;
export {Entity, Key};

import {PropertyFilter, and, or} from './filter';
export {PropertyFilter, and, or};
import {
GetIndexesCallback,
GetIndexesOptions,
Expand Down

0 comments on commit 8eb857d

Please sign in to comment.