{"_id":"@baileys-md/keyed-db","name":"@baileys-md/keyed-db","dist-tags":{"latest":"0.2.5"},"versions":{"0.2.5":{"name":"@baileys-md/keyed-db","version":"0.2.5","description":"Lightweight library to store an in-memory DB","homepage":"https://github.com/adiwajshing/keyed-db","main":"lib/KeyedDB.js","types":"lib/KeyedDB.d.ts","keywords":["db","key"],"author":{"name":"Adhiraj Singh"},"license":"MIT","repository":{"url":"git+ssh://git@github.com/adiwajshing/keyed-db.git"},"devDependencies":{"@types/mocha":"5.2.7","@types/node":"^14.0.14","assert":"^2.0.0","mocha":"^8.0.1","ts-node-dev":"^1.0.0-pre.61","typescript":"^4.0.0"},"_id":"@baileys-md/keyed-db@0.2.5","gitHead":"2e429cfbbafa0cae42f6adba96ecfc72bced809f","bugs":{"url":"https://github.com/adiwajshing/keyed-db/issues"},"_nodeVersion":"20.14.0","_npmVersion":"10.7.0","dist":{"integrity":"sha512-c6EMNGxH8ri8DRlWWoqYEJhrYM3cBO3b3IRZn6ds3KRwlZk2E3bZ8ecC5L+SlXPVG9QFT3kMhnOxMmbpjShEMQ==","shasum":"34705689e208b4b202dd9782b731ea4619ceb0f0","tarball":"https://registry.npmjs.org/@baileys-md/keyed-db/-/keyed-db-0.2.5.tgz","fileCount":6,"unpackedSize":13831,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDtnZN9cfgZJi4s1rVs7I99OcxKAtu/AqJcJLrakFp+SgIhAIfQWESaRKyYIaRnpuEsGUmpb77aPE4wi56emUPavBsR"}]},"_npmUser":{"name":"rulihenderson","email":"matsunagahisahde@gmail.com"},"directories":{},"maintainers":[{"name":"rulihenderson","email":"matsunagahisahde@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/keyed-db_0.2.5_1752275482874_0.1604397978060963"},"_hasShrinkwrap":false}},"time":{"created":"2025-07-11T23:11:22.763Z","0.2.5":"2025-07-11T23:11:23.105Z","modified":"2025-07-11T23:11:23.436Z"},"maintainers":[{"name":"rulihenderson","email":"matsunagahisahde@gmail.com"}],"description":"Lightweight library to store an in-memory DB","homepage":"https://github.com/adiwajshing/keyed-db","keywords":["db","key"],"repository":{"url":"git+ssh://git@github.com/adiwajshing/keyed-db.git"},"author":{"name":"Adhiraj Singh"},"bugs":{"url":"https://github.com/adiwajshing/keyed-db/issues"},"license":"MIT","readme":"# Keyed DB\n\nA light-weight node library to manage a sorted & indexed collection with pagination support. \nAll done using Binary Search. Based off my swift code for [Queenfisher](https://github.com/adiwajshing/Queenfisher)\n\n## Install\n\n`npm i github:adiwajshing/keyed-db`\n\n## Running Tests\n\n`npm test`\n\n## Functions\n\n``` ts\n\ndb = new KeyedDB<T> (t => t.uniqueNumberKeyProperty, t => t.optionalUniqueIDProperty)\n// compare with a custom function\ndb = new KeyedDB<T> ({  \n    key: t => t.someProperty,\n    compare: (t1, t2) => someComputation(t1, t2) // return -1 if t1 < t2, 0 if t1=t2 & 1 if t1 > t2\n}, t => t.optionalUniqueIDProperty)\n\ndb.insert (value) // insert value in DB\ndb.upsert (value) // upserts value\ndb.insertIfAbsent (value) // only inserts if not already present in DB\ndb.delete (value) // delete value\ndb.deleteById (value.optionalUniqueIDProperty) // delete value by referencing the ID\n// update the key of a value, \n// will automatically place object after key change\ndb.updateKey (value, value => value.uniqueKeyProperty = newValue) \ndb.paginated (someCursor, 20) // get X results after the given cursor (null for the first X results)\n\n```\n\n## Usage\n\n``` ts\nimport KeyedDB from '@adiwajshing/keyed-db'\n\n// Let's use the db to sort & maintain a list of chats\n// Chats must be accessed quickly via the chatID (the person you're chatting with)\n// Chats must be sorted by recency\ntype Chat = {\n    timestamp: Date\n    chatID: string\n}\n\n// first argument -- sorting property, second argument -- ID property\nconst db = new KeyedDB<Chat>(value => value.timestamp.getTime()*-1, value => value.chatID)\n\nfor (let i = 0; i < 1000;i++) {\n    // insert data\n    db.insert (\n        {\n            timestamp: new Date( new Date().getTime() - Math.random()*10000 ), \n            chatID: `person ${i}`\n        }\n    )\n}\nconsole.log (db.all()) // return internal sorted array\nconsole.log (db.paginated(null, 20)) // return first 20 chats\nconsole.log (db.paginated(null, 20, null, 'before')) // return last 20 chats\nconsole.log (db.paginated(null, 20, chat => chat.chatID.includes('something'))) // return first 20 chats where the chatID contains 'something'\n\nconst someDate = new Date().getTime()\nconst cursorPaginated = db.paginated(someDate, 20)\nconsole.log (cursorPaginated) // return 20 chats after the specified date\n\ndb.delete (cursorPaginated[0]) // delete paginated chats \n\n// update chat timestamp\ndb.updateKey(cursorPaginated[1], value => value.timestamp = new Date().getTime()) \n\n```\n\n## Time Complexity\n\n| Operation      | Time Complexity |\n|----------------|-----------------|\n| db.insert()    | O(logN)         |\n| db.delete()    | O(logN)         |\n| db.get()       | O(1)            |\n| db.updateKey() | O(logN)         |","readmeFilename":"README.md","_rev":"1-aa949370162babe01a8df965dc73402a"}