tt9
tt9

Reputation: 6069

Local NoSQL database for desktop application

Is there a NoSQL database solution for desktop applications similar to Sqlite where the database is a file on the user's machine? This database would be called by a nodejs application that is used on the desktop.

Upvotes: 5

Views: 19762

Answers (3)

Ewout Stortenbeker
Ewout Stortenbeker

Reputation: 305

I see this is an old question, but a newer option for you would be AceBase which is a fast, low memory, transactional, index & query enabled NoSQL database engine and server for node.js and browser. Definitely a good NoSQL alternative for SQLite and very easy to use:

const { AceBase } = require('acebase');
const db = new AceBase('mydb');

// Add question to database:
const questionRef = await db.ref('stackoverflow/questions').push({ 
   title: 'Local NoSQL database for desktop application',
   askedBy: 'tt9',
   date: new Date(),
   question: 'Is there a NoSQL database solution for desktop applications similar to Sqlite where the database is a file on the user\'s machine? ..'
});

// questionRef is now a reference to the saved database path,
// eg: "stackoverflow/questions/ky9v13mr00001s7b829tmwk1"

// Add my answer to it:
const answerRef = await questionRef.child('answers').push({
   text: 'Use AceBase!'
});

// answerRef is now reference to the saved answer in the database, 
// eg: "stackoverflow/questions/ky9v13mr00001s7b829tmwk1/answers/ky9v5atd0000eo7btxid7uic"

// Load the question (and all answers) from the database:
const questionSnapshot = await questionRef.get();

// A snapshot contains the value and relevant metadata, such as the used reference:
console.log(`Got question from path "${questionSnapshot.ref.path}":`, questionSnapshot.val());

// We can also monitor data changes in realtime
// To monitor new answers being added to the question:
questionRef.child('answers').on('child_added').subscribe(newAnswerSnapshot => {
   console.log(`A new answer was added:`, newAnswerSnapshot.val());
});

// Monitor any answer's number of upvotes:
answerRef.child('upvotes').on('value').subscribe(snapshot => {
   const prevValue = snapshot.previous();
   const newValue = snapshot.val();
   console.log(`The number of upvotes changed from ${prevValue} to ${newValue}`);
});

// Updating my answer text:
await answerRef.update({ text: 'I recommend AceBase!' });

// Or, using .set on the text itself:
await answerRef.child('text').set('I can really recommend AceBase');

// Adding an upvote to my answer using a transaction:
await answerRef.child('upvotes').transaction(snapshot => {
   let upvotes = snapshot.val();
   return upvotes + 1; // Return new value to store
});

// Query all given answers sorted by upvotes:
let querySnapshots = await questionRef.child('answers')
   .query()
   .sort('upvotes', false) // descending order, most upvotes first
   .get();

// Limit the query results to the top 10 with "AceBase" in their answers:
querySnapshots = await questionRef.child('answers')
   .query()
   .filter('text', 'like', '*AceBase*')
   .take(10)
   .sort('upvotes', false) // descending order, most upvotes first
   .get();

// We can also load the question in memory and make it "live":
// The in-memory value will automatically be updated if the database value changes, and
// all changes to the in-memory object will automatically update the database:

const questionProxy = await questionRef.proxy();
const liveQuestion = questionProxy.value;

// Changing a property updates the database automatically:
liveQuestion.tags = ['node.js','database','nosql'];

// ... db value of tags is updated in the background ...

// And changes to the database will update the liveQuestion object:
let now = new Date();
await questionRef.update({ edited: now });

// In the next tick, the live proxy value will have updated:
process.nextTick(() => {
   liveQuestion.edited === now; // true
});

I hope this is of help to anyone reading this, AceBase is a fairly new kid on the block that is starting to make waves!

Note that AceBase is also able to run in the browser, and as a remote database server with full authentication and authorization options. It can synchronize with server and other clients in realtime and upon reconnect after having been offline.

For more info and documentation check out AceBase at GitHub

If you want to quickly try the above examples, you can copy/paste the code into the editor at RunKit: https://npm.runkit.com/acebase

Upvotes: 5

Terry
Terry

Reputation: 1570

you can also try couchdb. There is an example along with electron

http://blog.couchbase.com/build-a-desktop-app-with-github-electron-and-couchbase

Upvotes: 0

Nick
Nick

Reputation: 229

I use a mongodb local instance. It is super easy to setup. Here is an easy how to guide on setting up MongoDB

Upvotes: 1

Related Questions