Coder Social home page Coder Social logo

Unable to open DB read only about node-sqlite HOT 9 CLOSED

ajanian avatar ajanian commented on June 11, 2024
Unable to open DB read only

from node-sqlite.

Comments (9)

ajanian avatar ajanian commented on June 11, 2024

Ok. I got it to work.

I had to install sqlite3 as well. Shouldn't this be a dependency of sqlite? That wasn't very easy because it wouldn't install normally. After poking around I was able to get it to install via npm i [email protected] to avoid the 5.0.1 issue.

Once I did that I had to change my imports a bit - the naming seems to be off here with duplicates that don't all seem to work. My working code:

import { open } from 'sqlite';
import { getLogger } from 'log4js';
import sqlite3 from 'sqlite3';

const log = getLogger();
log.level = 'DEBUG';

// this is a top-level await 
(async (): Promise<void> => {
    log.debug("starting");

    try {
        // open the database
        const db = await open({
            filename: '/Users/andrewjanian/projects/nodethings/things3db.sqlite',
            driver: sqlite3.Database,
            mode: sqlite3.OPEN_READONLY
        });

        log.debug(db);

        const result = await db.all('SELECT * FROM TMArea');

        log.debug(result);

        for (const row of result) {
            log.debug(row);
        }
    } catch (err) {
        log.error(err);
    }

    log.debug("done");

})();

from node-sqlite.

theogravity avatar theogravity commented on June 11, 2024

For some history - v3 and prior used to include sqlite, but this was difficult to maintain because someone would have to bump up the version on it with each release. Not only that, it prevented people from using the alternative sqlite3-offline, which has precompiled binaries in situations where a complier isn't available or to reduce build times like in a CI system.

So for v4, we removed the direct dependency for it and now you specify the sqlite library you want to use. The decoupling allows for greater flexibility because you can now pick the library you want to use with it, and it can be at any reasonable version, and reduces less maintenance on our side because we now do not have to do package bumps for the libraries.

from node-sqlite.

theogravity avatar theogravity commented on June 11, 2024

I'm not sure what you mean by duplicates. From quick glance, the code looks ok

also what is the problem with 5.0.1? Usually a patch-level update shouldn't break any APIs

from node-sqlite.

ajanian avatar ajanian commented on June 11, 2024

@theogravity - not sure exactly what the problem is with 5.0.1 but on my mac I am not able to install 5.0.1 but I can install 5.0.0. Poking around there are others who have the same issue and that's where I got the idea to try pin 5.0.0

Regarding the duplication - duplication is probably not the right word. Before I had sqlite3 installed I was able to get OPEN_READONLY as a type definition from something called sqlite3 but there was a runtime error that sqlite3 couldn't be found. Similarly, Database (the driver) is defined in your code and also sqlite3 but fails silently if sqlite3 isn't installed. I think there are just some namespace overlaps that caused me confusion. Since I didn't have sqlite3 installed I would have assumed that I wouldn't be able to reference the types, etc.

from node-sqlite.

theogravity avatar theogravity commented on June 11, 2024

This is where having that flexibility come in - if this library had an explicit dependency for sqlite3, then it'd be hard for anyone to use the version they want, especially if they encounter issues with whatever version of sqlite3 this library was pinned to.

It's actually good software practice to decouple these kinds of dependencies as much as possible because it allows for such interchangeability.

We actually have the definitions copied into this repo from sqlite3 because we'd have to include the sqlite3 package for the types, which would break the idea above.

If the definitions have changed, you can always open a PR to update.

https://github.com/kriasoft/node-sqlite/blob/master/src/vendor-typings/sqlite3/index.d.ts

from node-sqlite.

ajanian avatar ajanian commented on June 11, 2024

I see. The copying of the types is what threw me off. I guess I don't understand why you would have a "half-dependency" (I'll call it that) on sqlite3. I totally understand the benefit of not depending on sqlite3. OTOH that isn't totally true, right? You still depend on the types, no? I feel like copying types into your package is a code smell. TBH I don't understand enough about the innards of your package and how it works so please feel free to disregard.

from node-sqlite.

theogravity avatar theogravity commented on June 11, 2024

This is no different than if I wrote my own typescript definitions by hand for sqlite3. It'd still be done without the original package being included, but why try to re-create something that already exists by hand?

The package is extremely simple if you look at the source code.

I hope with more practice in your software development, you'll eventually understand why we've decoupled it.

from node-sqlite.

ajanian avatar ajanian commented on June 11, 2024

from node-sqlite.

theogravity avatar theogravity commented on June 11, 2024

That or you can read the readme:

https://github.com/kriasoft/node-sqlite#install-sqlite3

from node-sqlite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.