WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.23k stars 390 forks source link

segfault when loading extension spatialite #1149

Closed 1mike12 closed 3 months ago

1mike12 commented 4 months ago

I'm just trying to use spatialite, I can load it fine with the sqlite3 library, but not with better-sqlite3

old working


import {Knex} from "knex";
import path from "path";

const SPATIALITE_PATH = process.env.SPATIALITE_EXTENSION_PATH || 'mod_spatialite'
console.log('Using Spatialite extension at:', SPATIALITE_PATH);

import SegFaultHandler from 'segfault-handler';
SegFaultHandler.registerHandler('crash.log');

export default {
  client: 'sqlite3',
  connection: {
    filename: path.resolve(__dirname, "./data/db.sqlite")
  },
  useNullAsDefault: true,
  migrations: {
    directory: path.resolve(__dirname, './migrations')
  },
  pool: {
    afterCreate: (conn, done) => {
      conn.loadExtension(SPATIALITE_PATH, (err: Error) => {
        if (!err) {
          console.log('Spatialite extension loaded');
          return done(null, conn);
        }
        done(err, conn);
      });
    }
  }
} as Knex.Config

new breaking

import {Knex} from "knex";
import path from "path";

const macosSpatialitePath = '/usr/local/lib/mod_spatialite.dylib';
const SPATIALITE_PATH = process.env.SPATIALITE_EXTENSION_PATH || macosSpatialitePath
console.log('Using Spatialite extension at:', SPATIALITE_PATH);

import SegFaultHandler from 'segfault-handler';
SegFaultHandler.registerHandler('crash.log');

export default {
  client: 'better-sqlite3',
  connection: {
    filename: path.resolve(__dirname, "./data/db.sqlite")
  },
  useNullAsDefault: true,
  migrations: {
    directory: path.resolve(__dirname, './migrations')
  },
  pool: {
    afterCreate: (conn, done) => {
      conn.loadExtension(SPATIALITE_PATH);
      done();
    }
  }
} as Knex.Config

I saw there way a fix here but after installing the latest from master I still got the issue, but it could be that maybe just setting my dependencies to point to the project doesn't work? https://github.com/WiseLibs/better-sqlite3/pull/1137

or maybe my issue is not related to that pull?

 "dependencies": {
    "better-sqlite3": "https://github.com/WiseLibs/better-sqlite3",
shenzhuxi commented 3 months ago

I'm just trying to use spatialite, I can load it fine with the sqlite3 library, but not with better-sqlite3

Are you sure that you can USE spatialte with node-sqlite3 after loading the mod? https://github.com/TryGhost/node-sqlite3/issues/526#issuecomment-1042876134

https://github.com/WiseLibs/better-sqlite3/issues/520 The master branch of better-sqlite3 is working with mod_spatialite and I haven't found problem except with electronjs.

  "dependencies": {
    "better-sqlite3": "WiseLibs/better-sqlite3#master",
JoshuaWise commented 3 months ago

Duplicate of https://github.com/WiseLibs/better-sqlite3/issues/520