staticbackendhq / core

Backend server API handling user mgmt, database, storage and real-time component
https://staticbackend.com
MIT License
700 stars 66 forks source link

[core & cli] Save dev database between CLI restart #82

Closed dstpierre closed 1 year ago

dstpierre commented 1 year ago

In development mode the CLI uses the memory database provider, making everything versatile.

It could be helpful to have an option to save the memory database from a flag on the CLI server command.

Overview for this change

In the core package:

Two function could be added to the memory/memory.go file to load and save the database.

The Memory type in memory.go:31 contains a map[string]map[string][]byte (ho yeah, I did that). This is basically what's used as the in-memory database.

The two new functions in this file could be (m *Memory) Load(filename string) error and (m *Memory) Save(filename string) error. We could use gob to encode/decode this DB to the file.

Ideally, I'd not want to have the Persister interface to have those two function. Maybe we could create a new interface with those 2 and check when it's time to call those function if the runtime implementation has those (pseudo code):

type DBReaderWriter interface {
  Load(string) error
  Save(string) error
}

p.s. writing this makes me wonder if the Memory type could not juste implement io.Reader and io.Writer instead of having our own interface.

Nonetheless, somewhere would be this:

rdr, ok := db.(memory.DBReaderWriter)
if ok {
  rdr.Load(filename)
}

in the cli program:

This is an idea of how this could be implemented. Open to any suggestion, do not hesitate to speak your idea/suggestion in here.

dstpierre commented 1 year ago

The addition of sqlite as a data provider made this extremely simple.

The cli will have a new flag --persist-data which will use sqlite instead of the memory data provider and will persist data without any more changes.

This will be available in the v1.5 release.