nestjs-addons/in-memory-db

Feature: Allow ids of type string

Closed this issue · 7 comments

Is your feature request related to a problem? Please describe.
Right now, ids are statically typed to number. In reality, it's possible a back-end we're mocking against could be using randomly-generated string ids.

Describe the solution you'd like
Add a prop in InMemoryDBConfig like idType, which accepts a value of "string" or "number". If "number" (or nothing) is set, behave as-is. If "string" is set, randomly generate a string id on the id key for entities, and store entities in the record map using this alphanumeric string key.

Additional context
I already have a prototype of this working locally; I don't mind cleaning up and submitting a PR for this issue.

What is being used to randomly generate the string? Will a hashing algorithm be used or something more along the lines of uuid/v4?

My only concern is about possible collisions with the generated strings.

@Coly010 I was thinking something along the lines of crypto.randomBytes.

Per @itayod we will use the generic property type accessor like T['id']

What about making the getNextId() configurable instead of setting idType to string or number?

We could export some helpers like getNextNumberId() and getNextStringId() to make it easy to use, but it would allow a custom implementation when needed.

@timdeschryver What would the type for id be then?

@jordanpowell88 a string | id?
I don't know enough about this library, perhaps this can't be done tho.

Agree @timdeschryver, let's close this one.