triniwiz / triniwiz/nativescript-plugins

Couchbase: confusing handling of IDs

Open
#41 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
87
Forks
57
Avg merge
1h 36m
Merged PRs (30d)
4

Description

I'm new to Couchbase Lite, so sorry if this is barking up the wrong tree.

I'm very confused as to why the plugin is putting an ID field in created docs and how it returns it.

Let's start with these docs being created:


database.createDocument({
  name: "steve",
});

database.createDocument({
  id: "bobIdInDoc",
  name: "bob",
});

database.createDocument(
  {
    name: "jane",
  },
  "janeExplicitId"
);

A query with an empty select gives us:

// select: [] =>
[{
  "name": "steve",
  "id": "12bd1d9e-3c20-4d17-899a-a77b1ad8087a"
}, {
  "name": "bob",
  "id": "5661ec42-a214-4302-934d-c25d75f991f6"
}, {
  "name": "jane",
  "id": "janeExplicitId"
}]

Okay, so Steve got an autogenerated ID, Bob got his overwritten, and Jane kept hers. How would I know that the ID of a returned doc is one that was overwritten, one I gave explicitly, or one that was made cause I didn't give one explicitly?

Let's try selecting the ID:

 // select: ["id"] =>
[{
  "id": null
 }, {
 "id": "bobIdInDoc"
 }, {
 "id": null
 }]

Wow, so when I select the id field, the autogenerated ID and explicit ones are gone!

Reading the Couchbase docs, the REAL id is sometimes called "_id", so lets try that

// select: ["_id"] =>
[{
  "_id": "12bd1d9e-3c20-4d17-899a-a77b1ad8087a"
}, {
  "_id": "5661ec42-a214-4302-934d-c25d75f991f6"
}, {
  "_id": "janeExplicitId"
 }]

So, when I use "_id", I'll get back the ids that are in the returned docs when I use [] for select.

I discovered that there is actually a way of getting all fields and the true ID: Link, and finding QueryMeta in the source, I tried select: [QueryMeta.ID, QueryMeta.ALL], though it returns the same as for []

Finally, I tried ["_id", QueryMeta.ALL]:

// select: ["_id", QueryMeta.ALL] =>
[{
  "name": "steve",
  "_id": "301aebcd-ac13-411d-b437-436f6e98ba20"
}, {
  "name": "bob",
  "id": "bobIdInDoc",
  "_id": "a9e1c967-c05c-4c8a-a72a-2f5887911888"
}, {
  "name": "jane",
  "_id": "janeExplicitId"
}]

And there we go! I get back the id that I can be sure is the REAL one, and the id field whose origin I can't be sure of.

Can I please get an explanation for what is going on?

Full source for example: https://pastebin.com/s4q8j1K7

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides a full source example and uses the QueryMeta API; start by reproducing the four select forms shown in the report and compare the returned id and _id values. No repository file or test is named, so completion would require determining the intended ID semantics and documenting or correcting the inconsistent query results.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.