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

Creating shared memory database fails #1032

Closed
AxelTerizaki opened this issue Aug 12, 2018 · 3 comments
Closed

Creating shared memory database fails #1032

AxelTerizaki opened this issue Aug 12, 2018 · 3 comments

Comments

@AxelTerizaki
Copy link

Hello.

I'm trying to create a database in memory, run some queries on it to create tables, and then attach that same database to another one.

According to the sqlite3 documentaiton, you can create shared memory databases.

https://www.sqlite.org/inmemorydb.html see the "In-memory Databases And Shared Cache" section.

I tried something like that, but it fails :

> var sqlite3 = require('sqlite3').verbose();
undefined
> var db = new sqlite3.Database('file::memory:?cache=shared');
undefined
> events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: SQLITE_CANTOPEN: unable to open database file

I could always attach the memory database first and then run queries on it, but since I'm reusing queries I already ran elsewhere, I'd have to copy/paste them to change table names (table1 to memorydb.table1) for example.

This works fine when using a sqlite3 client in bash.

Would it be possible to implement it ?

@gms1
Copy link
Contributor

gms1 commented Sep 19, 2018

At the moment this is only possible if libsqlite3.so has been compiled with "SQLITE_USE_URI=1" and node-sqlite3 has been installed using the options "--build-from-source" and "--sqlite=/path/to/sqlite3/prefix"

I have been using this method for some time now, so I am able to open a named shared memory database using an URI like:

'file:sqlite3orm?mode=memory&cache=shared'

see enable URI format

Today I was browsing in the sqlite documentation, which other possibilities would exist, since the activation of the above compile-time option would be a breaking change for node-sqlite3. luckily one would only need to propagate the SQLITE_OPEN_URI constant to JavaScript

Flags For File Open Operations

I will therefore create a related (mini) PR; possibly also with other missing constants, such as SQLITE_OPEN_SHAREDCACHE and SQLITE_OPEN_PRIVATECACHE.
This shared cache mode is generally intended for use in embedded systems and not only required for shared memory databases

@AxelTerizaki
Copy link
Author

Thanks for your informative reply :)

I'm not able to compile from source though where I am. I think the best would be indeed to have the pre-built binaries with this setting enabled. I'm not sure if this is a breaking change since we're basically just enabling a new option though?

@gms1
Copy link
Contributor

gms1 commented Sep 19, 2018

yes, with this solution we would just provide additional constants for use in JavaScript and would not change existing functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants