firebase / firebase/firebase-tools

[firestore-emulator: datastore-mode] Namespace queries return empty array

Open
#6,932 4 comments 0 reactions 1 assignee Claimed by @harshyyy21 View on GitHub
emulators: firestore type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info

**firebase-tools:** v13.6.0

**Platform:** macOS Sonoma 14.4

**node:** v20.10.0

**gcloud:** v470.0.0

**cloud-firestore-emulator:** v1.19.4

### [REQUIRED] Test case

MCVE - https://github.com/aalej/issues-6932

Alternatively, make a directory containing the ff files:

src/index.ts

```ts
import { Datastore } from "@google-cloud/datastore";

const datastore = new Datastore({
projectId: "some-testproject",
});

export async function getNamespaces() {
const query = datastore.createQuery("__namespace__").select("__key__");
const [entities] = await datastore.runQuery(query);
const namespaces = entities.map((entity) => entity[datastore.KEY].name);
return namespaces;
}

export async function addEntity(namespace: string, kind: string[], data: any) {
const entityKey = datastore.key({
path: kind,
namespace,
});
const entity = {
key: entityKey,
data,
};
await datastore.save(entity);
}

async function main() {
await addEntity("Tasks", ["Work Tasks"], {
category: "Personal",
done: false,
priority: 4,
description: "Learn Cloud Datastore",
});

const namespaces = await getNamespaces();
console.log(namespaces);
}

main();
```

tsconfig.json

```json
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"outDir": "lib"
},
"include": ["src"],
"exclude": []
}
```

package.json

```json
{
"version": "1.0.0",
"description": "",
"main": "lib/index.js",
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch"
},
"devDependencies": {
"@google-cloud/datastore": "^8.6.0",
"typescript": "^5.4.3"
}
}
```

### [REQUIRED] Steps to reproduce

Testing while connected to the emulator

1. Run `npm i`
1. Run `gcloud emulators firestore start --database-mode=datastore-mode --host-port=127.0.0.1:8081`
1. On a separate terminal, run `export DATASTORE_EMULATOR_HOST=127.0.0.1:8081`
1. Run `npm run build`
1. Run `node .` or `node lib/index.js`

### [REQUIRED] Expected behavior

Console logs should show the namespaces on Datastore emulator.

When connecting to production, console logs show the namespaces on Datastore:

```
[ 'Tasks' ]
```

Steps to test on a production environment:

1. Run `unset DATASTORE_EMULATOR_HOST`
1. Run `export GOOGLE_APPLICATION_CREDENTIALS=`
1. Run `npm run build`
1. Run `node .` or `node lib/index.js`

### [REQUIRED] Actual behavior

When connected to the emulator, console log outputs an empty array

```
[]
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.