Live-Poll icon indicating copy to clipboard operation
Live-Poll copied to clipboard

Database?

Open shiffman opened this issue 5 years ago • 8 comments

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!

shiffman avatar Oct 03 '20 16:10 shiffman

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.compactDatafile which 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 with yourDatabase.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 Poll object
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

dipamsen avatar Oct 03 '20 17:10 dipamsen

I have made a super-simple database called kvaluedb: https://npmjs.com/package/kvaluedb

Poll IDs as the keys, options as the values

simon-tiger avatar Oct 05 '20 17:10 simon-tiger

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

crunchypi avatar Oct 06 '20 08:10 crunchypi

@dipamsen this is a great idea, would gladly accept a pull request!!!! 💫

shiffman avatar Oct 10 '20 16:10 shiffman

@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.

shiffman avatar Nov 23 '20 15:11 shiffman

@all-contributors add @dipamsen for ideas!

shiffman avatar Nov 23 '20 15:11 shiffman

@shiffman

I've put up a pull request to add @dipamsen! :tada:

allcontributors[bot] avatar Nov 23 '20 15:11 allcontributors[bot]

@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.

crunchypi avatar Dec 05 '20 18:12 crunchypi