I have deployed an Azure Static Web App (SWA) using the Svelte framework. The SWA is connected to an Azure SQL database and uses the GraphQL endpoint to query the database. I want to replace the GraphQL queries with SQL queries because the current query is very slow. The first query takes around 10-11 seconds on a ~21000-row table. Subsequent queries still execute around 5-6 seconds (connection is probably cached).
So I decided to use the mssql package to speed up the querying:
const sql = require('mssql'); const config = { server: '<server_name>.database.windows.net', database: '<database_name>', user: process.env.USER, password: process.env.PASSWORD, options: { encrypt: true } }; onMount(async () => { const result = await sql .connect(config) .then(() => sql.query('SELECT * FROM <my_table>')) .then((result) => result.recordset) .catch((error) => console.warn('Error querying database: ', error)); console.log(result); });
Unfortunately, I got an error during the import phase.
Uncaught (in promise) ReferenceError: require is not defined
This makes sense because Svelte uses the type module
instead of commonjs
under the package.json
file:
{ ..."type": "module","dependencies": { ... }}
Therefore, I replaced the const sql = require('mssql');
with the import sql from 'mssql';
import statement. But I got a warning during the build:
(!) Some chunks are larger than 500 kB after modification.
If I ignore this warning, the following error occurs:
ReferenceError: Buffer is not defined
I have found the Skipping larger chunks while running "Npm run build" answer which fixes the warning by updating the vite.config.js
file. However, I got another "500 Internal server error" during the import with this error message:
TypeError: Class extends value undefined is not a constructor or null
Is there a simple way to use the mssql
package without a lot of workaround during the import statement?