Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LiteDB In Memory Database #142

Open
chrisbewz opened this issue Nov 23, 2024 · 4 comments · May be fixed by #143
Open

LiteDB In Memory Database #142

chrisbewz opened this issue Nov 23, 2024 · 4 comments · May be fixed by #143
Assignees
Labels
enhancement New feature or request

Comments

@chrisbewz
Copy link

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 mgernand self-assigned this Nov 23, 2024
@mgernand mgernand added the enhancement New feature or request label Nov 23, 2024
@mgernand
Copy link
Contributor

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
Copy link
Contributor

mgernand commented Nov 24, 2024

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:

  • Load a database from file, but use the content in-memory.
  • Use an in-memory database and allow writing of the file to disk.

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

@mgernand mgernand linked a pull request Nov 24, 2024 that will close this issue
@chrisbewz
Copy link
Author

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 🤨

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

@mgernand
Copy link
Contributor

mgernand commented Dec 3, 2024

Thanks for the research. I am planning to support both approaches. I hope I have something to look at by the end of this week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging a pull request may close this issue.

2 participants