Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityKlass id not set when saving or querying entities #172

Closed
jacobg opened this issue Jul 19, 2019 · 1 comment · Fixed by #180
Closed

EntityKlass id not set when saving or querying entities #172

jacobg opened this issue Jul 19, 2019 · 1 comment · Fixed by #180

Comments

@jacobg
Copy link

jacobg commented Jul 19, 2019

I have a simple unit test that saved a model I defined:

    const foo = await new models.Foo({
      bar: 1,
    }).save()

The returned foo is an instance of an EntityKlass. EntityKlass has, among many others, the following two properties:

    entityKey: entity.Key;
    id: number | string;

It looks like entityKey is set after saving, but id is not. Thus:

    console.log(`Saved foo entity key id ${foo.entityKey.id}`)
    console.log(`Saved foo id ${foo.id}`)

prints this out:

Saved foo entity key id 1
Saved foo id undefined

Also, the entity.Key class is defined in @google-cloud/datastore with the id property always being a string, even if it's auto-allocated as numeric. This is confusing. It seems that the EntityKlass.id property perhaps is intended to parse it to its intended type, but as mentioned above it's not doing that here.

The same issue also occurs when calling model.list({ format: "ENTITY"}).

Here's a unit test illustrating this issue:

  it('should be able to save and return the entity with key', async () => {
    const gstore = createGstore()

    type EntityType = {
      someField: string
    }
    const EntitySchema = new gstore.Schema<EntityType>({
      someField: { type: String, required: true }
    })
    const EntityModel = gstore.model<EntityType>('EntityType', EntitySchema)

    const someFieldValue = faker.random.alphaNumeric()
    const createdEntity = new EntityModel({
      someField: someFieldValue
    })
    const savedEntity = await createdEntity.save()

    // This is what a saved entity looks like
    expect(savedEntity.entityKey).to.be.instanceOf(entity.Key)
    expect(savedEntity.entityKey.id).to.be.a('string')
    expect(Number.parseInt(savedEntity.entityKey.id as string)).to.not.be.NaN
    expect(savedEntity.id).to.be.undefined // Well, this is weird!
    expect(savedEntity.someField).to.equal(someFieldValue)
    expect(savedEntity.entityData.someField).to.equal(someFieldValue)
    expect(savedEntity)
  })
@jacobg jacobg changed the title EntityKlass id not set when saving entity EntityKlass id not set when saving or querying entities Jul 19, 2019
@sebelga
Copy link
Owner

sebelga commented Sep 10, 2019

Hi,
Thanks for reporting this issue! I just opened a PR that should fix it. It will be available in the next major version. Please let me know if the issue persists 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants