datachore icon indicating copy to clipboard operation
datachore copied to clipboard

Please add support for ancestor index !

Open hilnius opened this issue 10 years ago • 10 comments

Hey ! Just found your lib, I like its syntax and it seems to fit what I was looking for. I saw in the Datachore.php file that you don't have support of Ancestor queries yet. I saw it as a TODO so I was wondering if you could give me an idea of when :)

hilnius avatar May 18 '15 14:05 hilnius

I am tackling a few issues right now. If you have any documentation you refer to for ancestor queries that would be great.

pwhelan avatar Sep 03 '15 21:09 pwhelan

For documentation, I'd say https://cloud.google.com/datastore/docs/apis/overview and https://github.com/google/google-api-php-client

hilnius avatar Sep 03 '15 23:09 hilnius

I found nothing there documenting ancestors specifically but I did find it here: https://cloud.google.com/datastore/docs/concepts/entities#Datastore_Ancestor_paths. This is possible but will take quite a bit of a rewrite since it looks like it has to be written right into the record's key (analogous to the primary key in an SQL table but quite a bit more complex).

pwhelan avatar Sep 04 '15 02:09 pwhelan

The thing I still cannot wrap my head around is how a child is related to the parent in the actual data structure.

AFAICT it works like this:

Entity #1 (parent) key:
    path[0]:
        kind: Person
        name: Martin Sheen

Entity #2 (child) key:
    path[0]:
        kind: Person
        name: Martin Sheen
    path[1]:
        kind: Person
        name: Charlie Sheen

And then the Data store will automatically assign the ID's and relate Martin Sheen's actually entity to Charlie Sheen's for us?

pwhelan avatar Sep 04 '15 19:09 pwhelan

If you look at the API here : https://cloud.google.com/datastore/docs/apis/v1beta2/key is how a key is implemented (in JSON) and here : https://cloud.google.com/datastore/docs/apis/v1beta2/datasets/commit there is the full doc on inserting entities. As explained, when you insert an entity, you specify (in your case)

"key": {
  "partitionId": {
    "datasetId": string,
    "namespace": string
  },
  "path": [
    { // this entity is Martin Sheen
      "kind": "Person",
      "id": "1"
    },
    { // this is Charlie Sheen
      "kind": "Person",
      "id": "2"
      // I think you can also specify a name here if you don't want numeric ids but string keys
    }
  ]
},
"properties": {
  "name": {
    "stringValue": "Charlie Sheen"
  },
}

This is probably (not tested) what a query that inserts a "Person", "Charlie Seen" in your DB, with an ancestor path putting Charlie as a son of Martin.

I haven't tried those json structures with the api, but I think the best way to understand how those queries perform is to use the "try it" part of the api doc pages.

Don't believe all I say, as I've never used the datastore, I've just read the docs a lot.

hilnius avatar Sep 05 '15 15:09 hilnius

Setting both the name and the id on path element (the members of the path array property) raises an error, which is where things started to confuse me.

Fatal error: Uncaught exception 'google\appengine\runtime\ApplicationError' 
with message 'Key path element has both id (5804871638843392) and name ("prev").' 
in google_appengine/php/sdk/google/appengine/runtime/RemoteApiProxy.php:84

pwhelan avatar Sep 05 '15 18:09 pwhelan

Yeah. An entity is identifiable either by a numeric "id" OR by a "name" string, but not both. It's like in SQL using a VARCHAR primary key UR an INTEGER primary key. You cannot have two primary keys. If you wanna use numeric ids, you can use the "id" field, and if you want to use string identifiers, you can use "name".

hilnius avatar Sep 05 '15 20:09 hilnius

I just pushed a branch called ancestors which has support for saving entities ancestor keys.

TODO

  • add support for querying entities with ancestors
  • automatically add indexes for ancestor queries to index.yaml via the autoindexer.
  • add tests.

You should be able to pull in the ancestor branch via composer by requiring the version: 'dev-ancestor'.


{
    "require": {
        "datachore/datachore": "dev-ancestor"
    }
}

pwhelan avatar Sep 15 '15 17:09 pwhelan

Cool. I'll check it when I got some time. Thanks a lot !

hilnius avatar Sep 15 '15 22:09 hilnius

The tests are failing in that branch at the moment. I'll fix it soon, PR's are highly appreciated though.

pwhelan avatar Sep 15 '15 22:09 pwhelan