Belphemur / node-json-db

A simple "database" that use JSON file for Node.JS.
MIT License
795 stars 66 forks source link

Any method for unique id generator ? #201

Open aswathshobi opened 3 years ago

EtzBetz commented 3 years ago

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 :/

PointSingularity commented 2 years ago

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.

EtzBetz commented 2 years ago

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.

PointSingularity commented 2 years ago

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

kgDG12 commented 2 years ago

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)