Skip to content

Commit 2cd3b93

Browse files
authored
feat(ndb): New sample for named-database support in Cloud NDB (GoogleCloudPlatform#10637)
1 parent bb95d05 commit 2cd3b93

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

datastore/cloud-ndb/named_db.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
16+
def create_named_db_client():
17+
# [START datastore_ndb_named_db]
18+
import os
19+
20+
from google.cloud import ndb
21+
22+
database = os.environ.get("DATASTORE_DATABASE", "prod-database")
23+
client = ndb.Client(database=database)
24+
# [END datastore_ndb_named_db]
25+
return client
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import os
16+
17+
from named_db import create_named_db_client
18+
19+
20+
def test_named_db():
21+
os.environ["DATASTORE_DATABASE"] = "test"
22+
client = create_named_db_client()
23+
assert client.database == "test"

testing/test-env.tmpl.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ export CONNECTOR=
113113
# Dialogflow examples.
114114
export SMART_REPLY_MODEL=
115115
export SMART_REPLY_ALLOWLIST=
116+
117+
# Datastore/Firestore
118+
export DATASTORE_DATABASE=

0 commit comments

Comments
 (0)