Connect Addon with sqlite db

Hi there,

I am really really new to Addon Developement

What i’m doing is an addon, that fetches data from a website and stores it in a database in our local Network. So that you can access data from every computer in this Network.

What i tried to do is to connect to an sqlite database but everything I found seamt to be old and didn’t work.

Last thing I tried was to install a node.js module named “sqlite3” into the addon. That worked fine.

But including the module with var sqlite = require("sqlite3").verbose(); like seen in this tutorial [http://blog.modulus.io/nodejs-and-sqlite][1]

running the addon with jpm run gives me this Errormessage:

Module ‘path’ is not found at resource://gre/modules/commonjs/path.js

Here, the whole Error:

Now i have some questions:

  1. Is that a right way to access a sqlite db? If not, can anyone give me an introduction or a tutorial link or something like that, that helps me connecting to a sqlite?

  2. If using the node.js module is a good way, how can i solve the error?

I hope, I can solve this with your help.

Michael
[1]: http://blog.modulus.io/nodejs-and-sqlite

What I think is causing this error is simply that Firefox isn’t Node.js
Most node modules are going to be dependent on Node.
What the “node_pre_gyp” module is doing is requiring the module “path”, a core module of node and not a module of Firefox.

You should look for a SQLite library that is meant for the front-end instead of the back-end.

Isn’t it possible to use node.js in an add-on?

To use sqlite from an addon, you can use the Sqlite.jsm javascript code module. From an sdk addon, you might need to load the Components.utils object as described here.

var {Cu} = require('chrome');

Where Cu will be equal to Components.utils.

No, don’t know why it would.
Unless the the node module isn’t dependent on anything of Node itself.
They both use the CommonJS standard, so that part should be compatible.
Maybe you are mixing up CommonJS and Node.js?

BTW, from looking at the links @Endyl posted, it seems that SQLite is focused on local storage.
Are you sure it’s sufficient for your use?

SQLite is a file-based db. It works without any db-server. I hope, i can give my add-on a filepath to the SQLite-file on one of our servers. So anybody in the company can use the addon and has access to the same data.

SQLite can have problems with databases exposed over the network depending upon how well the file-locking semantics are implemented by the file server/protocol in use. See https://www.sqlite.org/faq.html#q5 for more information.

Assuming it works, using a shared database directly like this can also lead to versioning problems. If you need to upgrade the schema of the database, you have to have some plan in-place for dealing with multiple versions of your add-on, etc. The simplest strategy would just be to switch to a new file/path whenever you issue a breaking schema change.

If you’d like to avoid compatibility and versioning issues, running a simple node.js app on a server on your network could work a lot better.

The gateway to SQLite from Firefox add-ons is the Storage API.

Hey Guys,

hope, you can help me open the sqlite file.

I’ve tried

var file = FileUtils.getFile("V://Tools//data//", ["SITop100.sqlite"]);

to get the File, which is located on one of our Servers. But jpm returns an error and i couldn’t find an example for my case.

Can You show me, how getFile has to look like?

The first parameter to getFile() is not a path, but a “key” that indicates a particular special location. In most cases, use “ProfD” which indicates the profile directory for the currently running browser. Then you specify the filename in the second parameter, which you have done correctly. You can see a list of special location keys here:

If you want to specify an explicit path, you can do so to the FileUtils.File() constructor. Don’t do this because it isn’t platform independent. It is for rare cases where you might have obtained a native path by some means.

Well, since “V://Tools//data//” is not an “nsIDirectoryService key” (see a list of those here), you cannot use it in FileUtils.getFile().
As the documentation of FileUtils states, if you have an absolute path, you should use the File constructor like so:

 var f = new FileUtils.File(mypath);

But that is not even necessary, because Sqlite.jsm expects a path to the database file (an absolute path, or a relative one (that is relative to the current profile directory)), not an nsIFile instance. So you don’t need to instantiate one, just give the path to Sqlite.jsm.

Hey Lithopsian,

thank you for your answer. In my case, the Filepath is fix. This Addon is only for internal use, where I know, that all Users have the same Path to the File.

I will try the FileUtils.File().

@Endyl:

So the right way is:

 var f = new FileUtils.File("V://Tools//data//SITop100.sqlite");

?

To create an nsIFile instance, yes, that is the way. But for Sqlite.jsm you only need something like this:

Sqlite.openConnection({ path: "V://Tools//data//SITop100.sqlite", sharedMemoryCache: false }).then(
  function onConnection(connection) {
    // connection is the opened SQLite connection (see below for API).
  },
  function onError(error) {
    // The connection could not be opened. error is an Error describing what went wrong.
  }
);

Also, don’t forget to import Sqlite.jsm :smile:

Components.utils.import("resource://gre/modules/Sqlite.jsm");
1 Like

OK, thank you very much.

I will try that.

I would still recommend that you don’t specify a platform-dependant absolute path. openConnection will accept a relative path, relative to the profile directory. Use a link if you need to. You’ll be cursing that absolute path one day when v: has a different letter.

@Lithopsian: Can you please give me an example?

if your sqlite file is on the desktop get it like this:

var myfile = Services.dirsvc.get('Desk', Ci.nsIFile);
myfile.append('name-of-file.sqlite');

or with FileUtils, this is like 2ms slower though:

var myfile = FileUtils.getFile('Desk', ['name-of-file.sqlite']);

Here’s a list of special key words: https://gist.github.com/Noitidart/715840fa5008ee032017#file-_ff-addon-snippet-specialdirpaths-js-L29

Also OS.Constants.Path has some special paths, thats my preferred way.

var myfile = OS.Constants.Path.join(OS.Constants.Path.desktopDir, 'name-of-file.sqlite');

Thank you for the list, but i am really new to this topic, so for me it is really hard to understand, how to get my File from the Server.

I think, i understood, how to get Files from special directories like my profile, but how do i get a file from our internal network?

I don’t know, if i schould start a new thread, but another great gap in know how, belonging to this project, is to upload files on a ftp-server. Maybe, you can give me a first hint, where to find an example for file-Upload. If it doesn’t fit here, i will open an new thread for that. I only need a simple upload of defined files to a defined remote directory.

Ah ok if its a path on your network, you have no other choice then to use the hard coded path. if that path changes in the future (due to you guys changing your network) then you should update the addon