sensedeep / dynamodb-onetable

DynamoDB access and management for one table designs with NodeJS
https://doc.onetable.io/
MIT License
682 stars 109 forks source link

Setting "uuid: true" on an attribute in your Schema causes an error to be thrown upon creating that entity type #370

Closed ClaytonAndTheClayTones closed 2 years ago

ClaytonAndTheClayTones commented 2 years ago

Describe the bug

Here is the simple schema being created:

const Schema = {
  version: '0.0.1',
  indexes: {
    primary: { hash: 'pk' },
  },
  models: {
    Webhook: {
      pk: { type: String, value: 'webhook-${clientId}#${eventType}#${serviceType}#${endpoint}' },
      id: { type: String, uuid: true },
      clientId: { type: String, required: true },
      endpoint: { type: String, required: true },
      eventType: { type: String, required: true },
      serviceType: { type: String, required: true }
    },
  },
}

Here is the table/model creation:

const client = new Dynamo({ client: dbClient })

export const TABLE_NAME = 'webhooks-service-3'

export const table = new Table({
  client: client,
  name: TABLE_NAME,
  schema: Schema,
  logger: (level, message, context) => {
    console.log(level, message, context)
  },
})

export type WebhookModel = Entity<typeof Schema.models.Webhook>
export const WebhookDatabaseModel: Model<WebhookModel> = table.getModel('Webhook')

When I call WebhookDatabaseModel.create, I get the following error:

    "error": "TypeError",
    "cause": "{"errorType":"TypeError","errorMessage":"this.table.generate is not a function","trace":["TypeError: this.table.generate is not a function","    at Model.setDefaults (/var/task/index.js: 30056: 34)","    at Model.collectProperties (/var/task/index.js: 29940: 10)","    at Model.prepareProperties (/var/task/index.js: 29869: 20)","    at Model.putItem (/var/task/index.js: 29732: 25)","    at Model.create (/var/task/index.js: 29452: 27)","    at WebhookAccessor.create (/var/task/index.js: 32046: 47)","    at WebhookManager.create (/var/task/index.js: 32076: 47)","    at Runtime.executeCreate [as handler
    ] (/var/task/index.js: 32063: 32)","    at Runtime.handleOnce (file: ///var/runtime/index.mjs:548:29)"]}"

When I remove the "uuid: true" from "id", there is no error and everything works fine.

I have not given specific repro code becuase I do not have the time to brew up the mocks required and it's a pretty simple double blind test.

  1. Create a working model that can call model.create fine
  2. add a "uuid: true" to any property
  3. watch the error return.
  4. remove "uuid: true"
  5. get a succesful creation

Environment (please complete the following information):

OS - Lambda Node - NODE_JS_16x OneTable Version - 2.3.8 TypeScript Version - 4.7.4

mobsense commented 2 years ago

Use generate: 'ulid' or generate: 'uuid'.

The uuid: true is old syntax.

ClaytonAndTheClayTones commented 2 years ago

Thank you, that does work!