Edge Agent SQL Server Express Connection

Hi Losant team,

We need to use GEA (v1.48.1) to query an MSSQL 2019 Express (v15.0) database. My current understanding is that the relevant difference between standard SQL server & express is that the latter requires an instance name as part of the host, e.g., “192.168.1.x\\sqlexpress”. This seems to be problematic for the Losant SQL node - attempting to connect w/o instance identifier throws “NotFoundError - Could not connect (sequence)”, and attempting w/ instance ID throws “SQLNodeValidationError - Cannot resolve host”. Other tweaks / variations were tried to no avail.

As a workaround, we also tried invoking custom JS via the Function node that uses the (already built-in) mssql library, as this would give us full control over the connection string. However, this also yielded very little, as we were unable to run even watered-down async functions and get it to mutate the payload properly. These were all tested against a local DB with known, valid credentials, wrapped in the simplest possible workflow (virtual button → function → debug) that didn’t write to the payload or even log anything beyond the first await.

SQL example:

const sql = require('mssql');

const config = {
	user: 'sa',
	password: 'pw',
	server: 'ip',
	database: 'db',
	port: 1433,
	options: {
		encrypt: false,
		trustServerCertificate: true
	}
};

(async () => {
	try {
		console.log('Connecting...');
		await sql.connect(config);

		console.log('Executing query...');
		const result = await sql.query('SELECT COUNT(*) FROM dbo.users');

        console.log('Query executed'(
		payload.result = result;
	} catch (err) {
		console.log('Error:', err);
	} finally {
		await sql.close();
		console.log('Connection closed.');
	}
})();

Also tried treating these as synchronous which showed the full logging, including opening / closing the connection, but didn’t surface a result set in the payload as sql.query returns a promise.

Simple async function that logs & sleeps which didn’t behave as it does outside of the Losant sandboxed-context:

const f = async (s) => {
	return new Promise((resolve) => {
		setTimeout(() => {
			console.log(s)
			resolve()
		}, 1000)
	})
}

(async () => {
	try {
		console.log('Start');
		await f('1')
		await f('2')
	} catch (err) {
		console.log('Error:', err);
	} finally {
		console.log('End')
	}
})();

The behavior here is difficult to pin down, and we found no helpful caveats called out in the docs.

We’re probably missing something obvious here - any direction on how to connect using the native SQL node, how to wield async functions properly within an edge workflow, or alternative paths would be greatly appreciated.

–NY

Noah,

I dug into this a bit, as we have not specifically tested MSSQL Express before. I grabbed the official docker image (https://hub.docker.com/r/microsoft/mssql-server/#how-to-use-this-image) and ran the Express edition, and was able to connect to it just fine - it did not seem to require an “instance” string.

But reading up online, it certainly seems that the instance is sometimes required, so I have a potential workaround for you. As a note, I have not specifically tested this, as I do not have a Express server available that requires an instance. The underlying driver does allow setting the instance name as a separate configuration property, named instanceName. And, as a quirk of how the SQL Node is written, for MSSQL, the “Additional Configuration” section for “Connection Encryption” can be used for setting configuration other than for encryption/ssl. So I would suggest giving the following a shot:

  • Put just the IP or hostname in the SQL Address Template
  • Check the checkbox “Secure Connection”
  • Pick JSON Template as the “Additional Configuration Method”
  • In “SSL/Encryption Configuration JSON Template” put the following:
{
  "encrypt": false,
  "trustServerCertificate": true,
  "instanceName": "<your instance name here>"
}

(I put encrypt as false and trustServerCertificate as true as that is what you had in your example above - by default, when Secure Connection is checked we will set encrypt to true, but the additional configuration can override that).

Let me know if that allows you to properly connect!

1 Like

Michael,

Amazing. I had no idea the SSL checkbox→JSON could be used for other config options. Worked like a charm - thank you so much!

–NY

Noah,

Glad it worked! It certainly wasn’t the intention of that field to be for other config options - just for the plethora of SSL options that are sometimes needed - but it came in handy here. I’m going to file a ticket to make Instance Name a specific separate field on the SQL node for MSSQL, because having to put it in the additional ssl configuration is not intuitive at all.

1 Like

Tuesday’s platform update, including GEA version 1.58.0, now includes an “Instance Name” field when using the SQL Node to connect to MSSQL databases.