Hi!
For the auralia project I’m working on I would like to use a module that exports multiple interfaces (msnodesqlv8). Below is a screenshot to visualise how the file looks:
Before implementing this module into this aurelia project I had tried using this module by running it on a node server. There I could simply use this code:
import { SqlClient } from “msnodesqlv8”;
const sql: SqlClient = require(“msnodesqlv8”);
const connectionString =
“Driver={SQL Server Native Client 11.0};Server={localhost};Database={MyDatabase};Trusted_Connection={yes};”;
sql.query(connectionString, “SELECT * FROM EMPLOYEE”, (err, rows) => {
console.log(rows);
});
Now however, I would like to integrate this into my aurelia project but I’m not sure how I can do this. If I use dependency injection like the code below I get an error that says that this.sql is undefined.
import { SqlClient } from “msnodesqlv8”;
import { autoinject } from “aurelia-framework”;
@autoinject
export class App {
public employees: any;
public connectionString =
“Driver={SQL Server Native Client 11.0};Server={localhost};Database={MyDatabase};Trusted_Connection={yes};”;
constructor(public sql: SqlClient) {}
public attached() {
this.sql.query(this.connectionString, “SELECT * FROM EMPLOYEE”, (err, rows) => {
console.log(rows);
this.employees = rows;
});
}
}
If I instead use the inline require like I did in the node application I get the error: “Error: Module name “msnodesqlv8” has not been loaded yet for context”.
Can someone help me understand how I can use this module?
1 Like
The above line means you probably need to import it like this:
import SqlClient from “msnodesqlv8”;
or this
import * as SqlClient from “msnodesqlv8”;
Sadly, both don’t work.
The first option results in build errors. I suppose this happens because it only exports interfaces and no classes or so.
The second option doens’t work either because I simply want to get an SqlClient object from the module and SqlClient is a single interface in the module.
I wonder if this has to do with the typescript configs in both projects since the node example uses common js and the aurelia project uses amd.
Here is the config of the node example:
{
“compilerOptions”: {
“module”: “commonjs”,
“esModuleInterop”: true,
“target”: “es6”,
“moduleResolution”: “node”,
“sourceMap”: true,
“outDir”: “dist”
},
“lib”: [“es2015”]
}
And here is the tsconfig of the aurelia project:
{
“compileOnSave”: false,
“compilerOptions”: {
“module”: “amd”,
“noImplicitAny”: false,
“declaration”: false,
“typeRoots”: [“./node_modules/@types”],
“removeComments”: true,
“emitDecoratorMetadata”: true,
“experimentalDecorators”: true,
“sourceMap”: true,
“target”: “es5”,
“lib”: [“es2015”, “dom”],
“moduleResolution”: “node”,
“baseUrl”: “src”,
“resolveJsonModule”: true,
“allowJs”: true
},
“include”: [“./src//*.ts", "./test//.ts", "./types/**/.d.ts”],
“atom”: {
“rewriteTsconfig”: false
}
}
1 Like
If its a build error: easy. just cast to any and cast to the type, to see if it even works.
import * as $SqlClient from '...';
import { SqlClient } from '...';
const SqlClient = $SqlClient as unknown as SqlClient;
Doing so results in this build error. What can you conclude out of this?
1 Like
Hold on are you sure this package works in the browser at all? I would expect this to be node/electron only
1 Like
I don’t. I haven’t yet found an example where this gets implemented without a node server. Would there really be no way how to implement this module in aurelia though?
1 Like
Well the issue hasn’t really lots to do with Aurelia itself. A backend dependency like seemingly this package needs to run on the server as opposed to the client, which Aurelia is. This is because it most likely need local file permissions and other features not available from within a browser. What backend is your project using? Express app or something similar? Essentially there you would have to expose an API which you can fetch with the client app.
1 Like
Thank you for the response.
I was trying to create an electron - aurelia - SQL Server application where when building the aurelia application the index.html and the scripts get copied into the electron folder so it can host the aurelia application. I wanted to directly communicate with an Sql server from within the aurelia application but I guess I can instead try to create the API on the electron application since runs on a node server.
1 Like
Yes you can certainly do it in your main process and forward to the renderer via ipc calls
1 Like