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

How to create a map attribute with unknown keys? #348

Closed
ides15 opened this issue Feb 6, 2024 · 2 comments
Closed

How to create a map attribute with unknown keys? #348

ides15 opened this issue Feb 6, 2024 · 2 comments

Comments

@ides15
Copy link
Contributor

ides15 commented Feb 6, 2024

I'd like to make an entity that looks like

const Person = new Entity(
  {
    model: {
      entity: "person",
      version: "1",
      service: "person",
    },
    attributes: {
      name: {
        type: "string",
        required: true,
      },
      interestsScore: {
        type: "map",
      },
    },
  },
)

where the interestsScore could basically be a TypeScript Record type, where the keys are unknown:

await Person.create({
  name: "John",
  interestsScore: {
    skiing: 8,
    music: 5,
    running: 2
  }
})

await Person.create({
  name: "Alex",
  interestsScore: {
    hiking: 9,
    sleeping: 10
  }
})

Any attribute with type map requires the properties key to be set, but I don't know the values of each property beforehand. If I set properties: {}, the interestsScore field doesn't get type safety - I can set interestsScore to a string, boolean, number, etc.

How can I create this attribute? Bonus points if I can create a map attribute that has string keys, and each value is also a map containing specific properties:

await Person.create({
  name: "Alex",
  interestsScore: {
    hiking: {
      score: 9,
      addedOn: "2024-02-04T12:00:00.000",
    },
    sleeping: {
      score: 10,
      addedOn: "2024-02-04T13:00:00.000",
    }
  }
})

Sorry in advance if this is already mentioned in the docs, I must've missed it. Thanks!

@ides15
Copy link
Contributor Author

ides15 commented Feb 6, 2024

Looks like I can actually use the CustomAttributeType to do what I want here:

const Person = new Entity(
  {
    model: {
      entity: "person",
      version: "1",
      service: "person",
    },
    attributes: {
      name: {
        type: "string",
        required: true,
      },
      interestsScore: {
        type: CustomAttributeType<Record<string, string>>("any"),
      },
    },
  },
)

@ides15 ides15 closed this as completed Feb 6, 2024
@tywalch
Copy link
Owner

tywalch commented Feb 7, 2024

Hi @ides15 👋

This is exactly what I was hoping you'd find! Glad you found the answer, let me know if you have any further questions!

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

No branches or pull requests

2 participants