fluxera / Fluxera.Repository

A generic repository implementation.
MIT License
17 stars 3 forks source link

LiteDB In Memory Database #142

Open chrisbewz opened 6 days ago

chrisbewz commented 6 days ago

Hi, recently I was trying to use this framework in some of my applications and came up with a question about lite db database provider and context classes.

I know that LiteDB supports the usage of streams to run as a in-mem database. But while attempting to setup this using this framework I noticed the options class for lite db repository only has a single parameter DatabaseName available to specify the path for .db file to use.

Also checked that DatabaseProvider uses this to open or creating an empty database on disk, i thought that overriding the logic on DatabaseProvider would solve this but the class is sealed. So how I'm supposed to do enable usage of memory streams w/ LiteDb?

mgernand commented 6 days ago

Hi, I've never thought of this use case before. It sounds interesting and I will have a look at it tomorrow. Maybe it's not that hard to extend the configuration, to run LiteDB in-memory.

mgernand commented 5 days ago

Hi, I've added a very simple LiteDB in-memory configuration. See this Pull Request. I will add the following features in the coming days:

@chrisbewz What features do you need when running LiteDB in-memory?

chrisbewz commented 5 days ago

While checking some parts of LiteDB source code related to database instantiation, I've noticed that they already have some string patterns on connection string (:temp: and memory) to internally handle in-memory and temp databases when LiteEngine is created from within LiteDatabase:

        // source: LiteDB/Engine/EngineSettings.cs
    internal IStreamFactory CreateDataFactory(bool useAesStream = true)
    {
        if (DataStream != null)
        {
            return new StreamFactory(DataStream, Password);
        }
        if (Filename == ":memory:")
        {
            return new StreamFactory(new MemoryStream(), Password);
        }
        if (Filename == ":temp:")
        {
            return new StreamFactory(new TempStream(null, 10485760L), Password);
        }
        if (!string.IsNullOrEmpty(Filename))
        {
            return new FileStreamFactory(Filename, Password, ReadOnly, hidden: false, useAesStream);
        }
        throw new ArgumentException("EngineSettings must have Filename or DataStream as data source");
    }

Correct me if I'm wrong but maybe the PR changes could be far more simple like just removing the EnsureEndsWith on DatabaseProvider to enable passing those patterns to db constructors, letting then do the rest :raised_eyebrow:

But it still would be nice to have the option to pass a stream directly to support cases like .db files embedded as resources for example (As this usually happens on desktop apps when deploying apps with embedded databases that not necessarly are ment to be copied to end user machine).