iyobo / pouchorm

The definitive ORM for working with PouchDB. Native support for Typescript.
MIT License
40 stars 4 forks source link

What is the reasoning behind using chance.guid for the doc ids? #7

Closed AKRFranko closed 3 years ago

AKRFranko commented 3 years ago

Great simple and useful lib you got here, got me started really quick with getting my head around some data modeling for a new project. With all these new fangled NoSQL/GraphQL complexotrons it's refreshing to find something simple, easy an concise.

I'm curious as to what your reasoning is behind using chance.guid for the doc ids? Do you plan for a way to have it be provided by the user of the library if I want to roll my own?

P.S: I thought I myself could label this issue as "question" but apparently not.

iyobo commented 3 years ago

Hey @AKRFranko,

Thanks! Sorry for the late response. You can totally provide your own ids by defining the _id field with your unique ID when running upsert(...) in your collection.

userCollection.upsert({
   _id: "GENERATE_YOUR_OWN"
  name: "Tony Montana"
});

And I agree. chancejs is probably really heavy just for guid generation. Being an internal optimization, I could consider replacing it with something lighter.

iyobo commented 3 years ago

But this question does spark an epiphany. it makes sense to define a function in a collection which allows developers define how they want new _ids to be automatically generated.

So Something like ...

userCollection.idGenerator = ()=>`user_${someIdGenerator()}`

const newUser = userCollection.upsert({
  name: "Tony Montana"
});

//newUser._id === 'user_cnvvvifjkemnkkjd'

Expect this change shortly!

iyobo commented 3 years ago

@AKRFranko PouchORM v1.3.0 now no longer uses the chance library for default uuid generation, but uses uuid.

Also please see docs @ https://github.com/iyobo/pouchorm#custom-id-generation for how to define custom id generation per collection.