capacitor-community / sqlite

⚡Capacitor plugin for native & electron SQLite databases.
MIT License
495 stars 118 forks source link

Web Angular/Ionic; Error: no connection open for data.db #286

Closed marnec closed 2 years ago

marnec commented 2 years ago

Describe the bug After following the web documentation at: https://github.com/capacitor-community/sqlite/blob/master/docs/Web-Usage.md

Whenever i try to run

this.sqlite.createConnection(
      'data.db',
      false,
      'no-encryption',
      1
    );
    await db.open();

I get the following error:

core.mjs:6485 ERROR Error: Uncaught (in promise): Error: no connection open for data.db
Error: no connection open for data.db
    at SQLiteService.<anonymous> (sqlite.service.ts:314:29)
...

where sqlite.service.ts is copy-pasted from the documentation itself

Further notes: data.db file doesn't exist. I would manually create it but I don't know the default location the plugin looks for the file and the location cannot be configured. So I just assume that if the file doesn't exist, the createConnection() method would create one.

To Reproduce Steps to reproduce the behavior:

  1. create a new angular/ionic project
  2. follow steps documented on https://github.com/capacitor-community/sqlite/blob/master/docs/Web-Usage.md
  3. try to open the connection to a DB
  4. See error

Expected behavior I expected to open a connection to a data.db file or create one if non exists.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

jepiqueau commented 2 years ago

@marnec can you tell me which version of sql.js has been installed it should not be greater than 1.6.2. Otherwise look at angular-sqlite-app-starter

marnec commented 2 years ago

checking the package.json of sql.js in my node_modules i see "version"="1.7.0",

Apparently this is due to the fact that /jeep-sqlite specifies the sql.js dependency as ^1.6.2 instead of 1.6.2.

I'll try to pin the 1.6.2 version in my package.json

Here you have the complete sql.js package.json

{
  "_from": "sql.js@^1.6.2",
  "_id": "sql.js@1.7.0",
  "_inBundle": false,
  "_integrity": "sha512-qAfft3xkSgHqmmfNugWTp/59PsqIw8gbeao5TZmpmzQQsAJ49de3iDDKuxVixidYs6dkHNksY8m27v2dZNn2jw==",
  "_location": "/sql.js",
  "_phantomChildren": {},
  "_requested": {
    "type": "range",
    "registry": true,
    "raw": "sql.js@^1.6.2",
    "name": "sql.js",
    "escapedName": "sql.js",
    "rawSpec": "^1.6.2",
    "saveSpec": null,
    "fetchSpec": "^1.6.2"
  },
  "_requiredBy": [
    "/jeep-sqlite"
  ],
  "_resolved": "https://registry.npmjs.org/sql.js/-/sql.js-1.7.0.tgz",
  "_shasum": "cadfed9773b643fbba0fc1ed1847e3a647e1fcb9",
  "_spec": "sql.js@^1.6.2",
  "_where": "C:\\Projects\\GTD\\gtd_ionic\\node_modules\\jeep-sqlite",
  "bugs": {
    "url": "https://github.com/sql-js/sql.js/issues"
  },
  "bundleDependencies": false,
  "contributors": [
    {
      "name": "Ophir LOJKINE",
      "email": "pere.jobs@gmail.com",
      "url": "https://github.com/lovasoa"
    },
    {
      "name": "@kripken"
    },
    {
      "name": "@hankinsoft"
    },
    {
      "name": "@firien"
    },
    {
      "name": "@dinedal"
    },
    {
      "name": "@taytay"
    },
    {
      "name": "@kaizhu256"
    },
    {
      "name": "@brodybits"
    }
  ],
  "dependencies": {},
  "deprecated": false,
  "description": "SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).",
  "devDependencies": {
    "clean-jsdoc-theme": "^3.3.4",
    "eslint": "^7.32.0",
    "eslint-config-airbnb-base": "^14.2.1",
    "eslint-plugin-import": "^2.26.0",
    "jsdoc": "^3.6.10",
    "puppeteer": "^10.2.0",
    "test": ">=0.6"
  },
  "homepage": "http://github.com/sql-js/sql.js",
  "keywords": [
    "sql",
    "sqlite",
    "stand-alone",
    "relational",
    "database",
    "RDBMS",
    "data",
    "query",
    "statement",
    "emscripten",
    "asm",
    "asm.js"
  ],
  "license": "MIT",
  "main": "./dist/sql-wasm.js",
  "name": "sql.js",
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/sql-js/sql.js.git"
  },
  "scripts": {
    "build": "make",
    "clean": "make clean",
    "doc": "jsdoc -c .jsdoc.config.json",
    "lint": "eslint .",
    "prettify": "eslint . --fix",
    "rebuild": "npm run clean && npm run build",
    "test": "npm run lint && npm run test-asm && npm run test-asm-debug && npm run test-wasm && npm run test-wasm-debug && npm run test-asm-memory-growth",
    "test-asm": "node --unhandled-rejections=strict test/all.js asm",
    "test-asm-debug": "node --unhandled-rejections=strict test/all.js asm-debug",
    "test-asm-memory-growth": "node --unhandled-rejections=strict test/all.js asm-memory-growth",
    "test-wasm": "node --unhandled-rejections=strict test/all.js wasm",
    "test-wasm-debug": "node --unhandled-rejections=strict test/all.js wasm-debug"
  },
  "version": "1.7.0"
}

EDIT: the same error remains after:

EDIT 2: I cloned your example repo and it works, I'm trying to trace back to what can possibly be the cause of the error by trying to set the same exact configuration as that repo step by step:

calling db.open() in ngOnInit -> Error: no connection open for data.db calling db.open in constructor:

TypeError: Cannot read properties of undefined (reading 'apply')
TypeError: Cannot read properties of undefined (reading 'apply')
    at e.stackAlloc (jeep-sqlite.entry.js:2661:506)
....

EDIT 3: I tried to upgrade dependencies in your repo: the result is that the app initializes without errors but some of the tests fail

jepiqueau commented 2 years ago

@marnec i just published the release 1.5.3 which is using strictly sql.js@1.6.2 can you try and report

marnec commented 2 years ago

@jepiqueau thanks for your quick response but I've got the same error as before:

TypeError: Cannot read properties of undefined (reading 'apply')
TypeError: Cannot read properties of undefined (reading 'apply')
    at e.stackAlloc (jeep-sqlite.entry.js:2661:506)
...
jepiqueau commented 2 years ago

@marnec difficult to understand can you share your project or a small repro project

marnec commented 2 years ago

I tried to create a new project from scratches in order to share a minimal reproucible environent where the error occurs but everything seem to work fine in the new project (while it keeps not working on the original project).

I gradually moved all the pieces of the original project to the new project in order to pinpoint what change triggers the error but the error never occurs in the new project.

Sadly, I don't have the time nor the energy to understand what causes the error in the original project and I'll just keep working on the new one. I consider the issue closed as I'm not able to reproduce the error myself. If you wish to try I can share the original project (after some modification to obfuscate sensitive data).

jepiqueau commented 2 years ago

@marnec i am happy that its works. I am on a personal project at this time and quite busy so i do not have too much time to give to support sqlite plugin right now . Good luck with your dev.