Database?
I don't actually need a database b/c I can just store the votes in memory for a real-time poll. But what if I want to restart the server and not lose the votes? Currently I'm using nedb just as a simple beginner-friendly database example, please add your suggestions and thoughts to this thread!
NeDB is fine to use, it will automatically remove unnecessary data from time to time
From the docs:
-
Under the hood, NeDB's persistence uses an append-only format, meaning that all updates and deletes actually result in lines added at the end of the datafile, for performance reasons. The database is automatically compacted (i.e. put back in the one-line-per-document format) every time you load each database within your application.
-
You can manually call the compaction function with
yourDatabase.persistence.compactDatafilewhich takes no argument. It queues a compaction of the datafile in the executor, to be executed sequentially after all pending operations. The datastore will fire a compaction.done event once compaction is finished. -
You can also set automatic compaction at regular intervals with
yourDatabase.persistence.setAutocompactionInterval(interval), interval in milliseconds (a minimum of 5s is enforced), and stop automatic compaction withyourDatabase.persistence.stopAutocompaction(). -
Keep in mind that compaction takes a bit of time (not too much: 130ms for 50k records on a typical development machine) and no other operation can happen when it does, so most projects actually don't need to use it.
Some suggestions (for using with NeDB):
- Better
Pollobject
const poll = {
question: "What should we do now?",
description: "this may not be required",
options: {
a: "Live Poll Overlay",
b: "Twitter API",
c: "Tenor GIF API",
d: "Spin the Wheel!"
},
votes: {
a: 7,
b: 2,
c: 5,
d: 8
},
_id: "sdhjgdhdkjfhlhuwkhshsm"
}
// options and votes can also be Arrays, that way inserting a new option will be a bit easier
I have made a super-simple database called kvaluedb: https://npmjs.com/package/kvaluedb
Poll IDs as the keys, options as the values
SQLite might be a good option for the following reasons:
- simple
- fast
- well tested (sqlite3 itself)
- pre-installed, at least on osx
Added a pull request with a simple interface here: https://github.com/CodingTrain/Live-Poll/pull/9
@dipamsen this is a great idea, would gladly accept a pull request!!!! 💫
@crunchypi are you familiar with https://enmap.evie.dev/ by any chance? I see it is based on sqlite and started playing around with it.
@all-contributors add @dipamsen for ideas!
@shiffman apologies for the late reply, the notification got caught by the spam filter somehow :< Haven't tried enmap, unfortunately; is your impression that it could replace nedb in this repo? Would happily create a performance comparison in the near future after I've wrapped up another project (~a week) if that's the case.