TimelordUK / node-sqlserver-v8

branched from node-sqlserver, SQL server driver compatible with all versions of Node
Other
140 stars 43 forks source link

cppDriver.Connection is not a constructor #167

Closed frfancha closed 2 years ago

frfancha commented 4 years ago

stack trace:

sql.open(connectionString, (err, con) => {

open D:/Dev/solv2/node_modules/msnodesqlv8/lib/connection.js:436

436 | return openFrom('open', params, callback)

openFrom D:/Dev/solv2/node_modules/msnodesqlv8/lib/connection.js:377

377 | const c = new PrivateConnection(getConnectObject(params), callback, nextID)

new PrivateConnection D:/Dev/solv2/node_modules/msnodesqlv8/lib/connection.js:331> 331 | const native = new cppDriver.Connection()

Call is done from a React component in Electron (in dev, no packaging yet)

node is V12.14.0 package.json.zip

TimelordUK commented 4 years ago

hello

it looks like to support React the module would need specific changes to export the API

https://microsoft.github.io/react-native-windows/docs/native-modules

I have tried testing msnodesqlv8 against a non react electron application and this works fine. I will take a look at seeing how hard it will be to wrap the module to work with React but from the above link this does not appear to be a quick change.

In the meanwhile unfortunately it does not appear React with cooperate with this module.

frfancha commented 4 years ago

Hi, I'm not sure the link is applicable for me, as I don't use React native but just React. Actually I'm not sure React itself plays a role in my issue. Meanwhile I could get a working solution by putting the msnodesqlv8 calls in the main process and by using them from react through ipc calls. It seems that msnodesqlv8 is the only package supporting SQL Server trusted connection (using the windows token) and that was a requirement for my app, reason why I selected your package.

TimelordUK commented 4 years ago

Ah indeed possibly I am mistaken re the provided link I do not know much regarding framework myself.

The problem is when I look in chrome Dev tools the msnodesqlv8 module is referenced under a different name and you can see the connection object does not exist on the native imported module.

When I look in Dev tools with an electron app without react I clearly see the module haS connection object on it

I will try and dig further into issue

Sent from my iPad

On 27 Jul 2020, at 12:52, Fred notifications@github.com wrote:

 Hi, I'm not sure the link is applicable for me, as I don't use React native but just React. Actually I'm not sure React itself plays a role in my issue. Meanwhile I could get a working solution by putting the msnodesqlv8 calls in the main process and by using them from react through ipc calls. It seems that msnodesqlv8 is the only package supporting SQL Server trusted connection (using the windows token) and that was a requirement for my app, reason why I selected your package.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

TimelordUK commented 4 years ago

ok so this example seems to work

git clone https://github.com/electron-react-boilerplate/electron-react-boilerplate cd electron-react-boilerplate yarn cd ./app yarn add msnodesqlv8

then in main.dev.ts

import 'core-js/stable';
import 'regenerator-runtime/runtime';
import path from 'path';
import { app, BrowserWindow } from 'electron';
import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import { SqlClient } from 'msnodesqlv8';
import MenuBuilder from './menu';

const sql: SqlClient = require('msnodesqlv8');

const connectionString =
  'Driver={SQL Server Native Client 11.0}; Server=(localdb)\\node; Database={master}; Trusted_Connection=Yes;';
const query = 'SELECT name FROM sys.databases';

sql.query(connectionString, query, (err, rows) => {
  console.log(rows);
});

start app cd .. yarn dev

and i see the query results in console

[
  { name: 'master' },
  { name: 'tempdb' },
  { name: 'model' },
  { name: 'msdb' },
  { name: 'scratch' }
]
frfancha commented 4 years ago

Correct, and that works for me as well. Problem is when I try to put the sql code in the renderer process

TimelordUK commented 4 years ago

would this work using the technique described here the ipcRenderer

https://dev.to/abulhasanlakhani/connecting-to-sql-server-from-electron-react-1kdi

i.e. if we try and emuate this and create a msnodesqlv8 service and he registers service in main

ipcMain.handle('getproducts', getProducts)

I have not tried this yet but can try to see if would work

mtriff commented 4 years ago

I'll take a look into this as well. I have this package working in the renderer process with a Vue application. It shouldn't be an issue doing the same with React.

mtriff commented 4 years ago

I'm able to run this module in the render with electron-react-boilerplate. Have you followed the instructions in the README for Electron use and installed/run electron-rebuild inside /app/?

frfancha commented 4 years ago

Yes I have done it. Before that using msnodesqlv8 gave the expected error "node version..." and after that it worked. => That from the main process! From the renderer process I always get the same error "not a constructor" before and after applying electron-rebuild

TimelordUK commented 4 years ago

@Matt,

Can you provide your boilerplate component working with the module from the renderer I will apply it to my build to see if I can also get it working – just so it is absolutely clear

From: Fred notifications@github.com Sent: 30 July 2020 08:29 To: TimelordUK/node-sqlserver-v8 node-sqlserver-v8@noreply.github.com Cc: TimelordUK sjames8888@gmail.com; Comment comment@noreply.github.com Subject: Re: [TimelordUK/node-sqlserver-v8] cppDriver.Connection is not a constructor (#167)

Yes I have done it. Before that using msnodesqlv8 gave the expected error "node version..." and after that it worked. => That from the main process! From the renderer process I always get the same error "not a constructor" before and after applying electron-rebuild

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/TimelordUK/node-sqlserver-v8/issues/167#issuecomment-666188296 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABXWJG4RIJRWIDDWXX4RG2TR6EONBANCNFSM4PHUCRCQ .

mtriff commented 4 years ago

Here are the commands I used:

git clone --depth 1 --single-branch https://github.com/electron-react-boilerplate/electron-react-boilerplate.git erb-msnodesqlv8
cd .\erb-msnodesqlv8\
yarn
cd .\app\
yarn
yarn add --dev electron-rebuild
yarn add msnodesqlv8
.\node_modules\.bin\electron-rebuild.cmd
cd ..
yarn dev

Here is my app\components\Home.tsx file:

import React from 'react';
import { Link } from 'react-router-dom';
import routes from '../constants/routes.json';
import { SqlClient } from 'msnodesqlv8';
import styles from './Home.css';

export default function Home(): JSX.Element {
  const sql: SqlClient = require('msnodesqlv8');

  const connectionString =
    'Driver={SQL Server Native Client 11.0}; Server=(localdb)\\node; Database={master}; Trusted_Connection=Yes;';
  const query = 'SELECT name FROM sys.databases';

  sql.query(connectionString, query, (err, rows) => {
    console.log(rows);
  });

  return (
    <div className={styles.container} data-tid="container">
      <h2>Home</h2>
      <Link to={routes.COUNTER}>to Counter</Link>
    </div>
  );
}

Output in the Electron/Chrome DevTools console:

Array(5)
  0: {name: "master"}
  1: {name: "tempdb"}
  2: {name: "model"}
  3: {name: "msdb"}
  4: {name: "scratch"}
frfancha commented 4 years ago

Thanks a lot for all these efforts to help me to solve that. I will definitively try to reproduce the working scenario with your last instructions. Seems there is hard-coded yarn command in the electron-react-boilerplate I haven't installed yarn yet being faithful to npm ... Will do so (but don't like such hard-coded commands) And the boiler plate is set to use electron 8 while all my tests were done with electron 9 until now... That could explain things?

TimelordUK commented 4 years ago

thanks @mtriff I can confirm this works for me also - copied your file above i now see output on app console

TimelordUK commented 4 years ago

Ill add to the README and Wiki to show how to get boilerplate going

mtriff commented 4 years ago

@frfancha, for Electron 9, if you haven't already, you will also need to set app.allowRendererProcessReuse to false for now (see https://www.electronjs.org/docs/api/app#appallowrendererprocessreuse).

This won't be needed permanently, we're tracking this issue on #132 (which I hope to address soon).

frfancha commented 4 years ago

"Your" boilerplate works also with electron 9 on my machine (needing indeed set app.allowRendererProcessReuse to false). Why it is (was ?) not working in my initial project is more and more a mystery. I'll try again that one. If it still doesn't work I will push it to github for you.

frfancha commented 4 years ago

Still same issue: cppDriver.Connection So I'm trying now to do the same that was required to make require(ipcRenderer) working: add this line to preload.js:

window.sql = require("msnodesqlv8");

(it already had this line : window.ipcRenderer = require("electron").ipcRenderer; )

preload.js is used because of:

mainWindow = new BrowserWindow({ width: isDev ? 1600 : 1200, height: 600, webPreferences: { nodeIntegration: true, preload: __dirname + "/preload.js", }, });

Satisfy typescript by adding:

declare global { interface Window { ipcRenderer: any; sql: any; } }

Then I get a different error Module did not self-register:

electron/js2c/renderer_init.js:114 Unable to load preload script: D:\Dev\solv2\public/preload.js (anonymous) @ electron/js2c/renderer_init.js:114 ./lib/renderer/init.ts @ electron/js2c/renderer_init.js:114 __webpack_require__ @ electron/js2c/renderer_init.js:2 (anonymous) @ electron/js2c/renderer_init.js:2 ___electron_webpack_init__ @ electron/js2c/renderer_init.js:2 (anonymous) @ electron/js2c/renderer_init.js:199 NativeModule.compile @ internal/bootstrap/loaders.js:287 NativeModule.compileForPublicLoader @ internal/bootstrap/loaders.js:222 loadNativeModule @ internal/modules/cjs/helpers.js:23 Module._load @ internal/modules/cjs/loader.js:698 Module._load @ electron/js2c/asar.js:769 Module.runMain @ internal/modules/cjs/loader.js:1056 (anonymous) @ internal/main/run_main_module.js:17 electron/js2c/renderer_init.js:114 Error: Module did not self-register: '\\?\D:\Dev\solv2\node_modules\msnodesqlv8\build\Release\sqlserverv8.node'. at process.func [as dlopen] (electron/js2c/asar.js:140) at Object.Module._extensions..node (internal/modules/cjs/loader.js:1034) at Object.func [as .node] (electron/js2c/asar.js:140) at Module.load (internal/modules/cjs/loader.js:815) at Module._load (internal/modules/cjs/loader.js:727) at Function.Module._load (electron/js2c/asar.js:769) at Module.require (internal/modules/cjs/loader.js:852) at require (internal/modules/cjs/helpers.js:74) at D:\Dev\solv2\node_modules\msnodesqlv8\lib\connection.js:10 at Object.<anonymous> (D:\Dev\solv2\node_modules\msnodesqlv8\lib\connection.js:446)

mtriff commented 4 years ago

At this stage, I don't think this is an issue with msnodesqlv8. If you push your repository to GitHub I can try to help debug your project there.

mtriff commented 4 years ago

FYI @frfancha, I was doing some testing and also received this error, but it disappeared after running electron-rebuild.cmd again.

frfancha commented 4 years ago

Uninstalled msnodesqlv8 installed msnodesqlv8 (is now 2.0.1, was 2.0.0) electron-rebuild run the app (version with require in the preload.js): => error is different now, it is a pop up from C++ saying: Microsoft Visual C++ Runtime Library Program: ...modules\msnodesqlv8\build\Release\sqlserverv8.node File: C:\Users\Stephen\dev\js\v8\node_module...\OdbcHandle.cpp Line:44 Expression: handle == SQL_NULL_HANDLE For information on how your program can cause an assertion failure, see Visual C++ documentation on asserts (Please Retry to debug the application - JIT must be enabled)

P.S.: version with require in the app.tsx code still gives "cppDriver.Connection is not a connector" and version with all sql code in the main process still ok

Re P.S. repo with the code: https://github.com/frfancha/electronreactsql

TimelordUK commented 2 years ago

closing.