node-json-db icon indicating copy to clipboard operation
node-json-db copied to clipboard

Any method for unique id generator ?

Open aswathshobi opened this issue 4 years ago • 5 comments

aswathshobi avatar Jun 28 '21 13:06 aswathshobi

Yea, I'm missing this. When I originally found the lib, I thought that by array support and index searching, it was meant that I could push elements into an array and have them assigned an incrementing id automatically. Since there is also a function getIndex(), which kinda does the other part of that workflow, that's even more of a bummer..

I guess I'll need to build my own index-builder around this now :/

EtzBetz avatar Aug 16 '21 14:08 EtzBetz

Maybe it is not suitable for your needs, but with newer Node versions you can generate v4 UUIDs natively with no need for additional packages.

Fun fact, even browsers have their own crypto global object with similar functionality.

PointSingularity avatar Dec 16 '21 07:12 PointSingularity

Maybe it is not suitable for your needs, but with newer Node versions you can generate v4 UUIDs natively with no need for additional packages.

Uh, that's nice to hear. One less dependency for me. What I would find more interesting to have is if the library itself would work with uuids or similar internally, without us needing to incorporate somehow.

EtzBetz avatar Dec 16 '21 08:12 EtzBetz

Yeah, that would definitely be a nice utility feature to have.

PointSingularity avatar Dec 16 '21 08:12 PointSingularity

I found this temporary solution , we can call this function when creating something it worked for me...

const ObjectId = (m = Math, d = Date, h = 16, s = s => m.floor(s).toString(h)) =>
  s(d.now() / 1000) + ' '.repeat(h).replace(/./g, () => s(m.random() * h))

Like this

db.push("/users[]", {
    _id: ObjectId(),
    username: username
  }, true)

kgDG12 avatar Jun 29 '22 05:06 kgDG12