Skip to main content

Native queries

Execute raw SQL or other database-specific queries directly against the database.

Native queries are useful when you need to perform operations that are not easily accomplished with the Squid Client SDK alone.

While the Client SDK has powerful querying functionality, you might need to expand these capabilities. For example, some SQL queries aren't available for NoSQL, like subselects and complex joins, so those queries won't be represented in the SDK. Or your database integration might have unique capabilities that other databases don't have, so those features also won't be in the codebase.

When you need to extend the functionality of Client SDK queries, you can use native queries.

Native relational queries

If using a SQL database integration, you can execute raw SQL using the executeNativeRelationalQuery method, passing your database's integration ID and the SQL query.

The following example runs a native SQL query:

Client code
async function runNativeQuery(queryParam: string): Promise<any> {
const response = await this.squid.executeNativeRelationalQuery(
'DATABASE_INTEGRATION_ID',
'SELECT * FROM SQUIDS WHERE YEAR = ${year}',
{ year: queryParam }
);
return response;
}

To secure your native queries, use a Squid Service function in the Squid backend. To learn more, view the documentation on securing data access.

Native MongoDB queries

The executeNativeMongoQuery method executes a native MongoDB aggregation pipeline with the given parameters and returns a promise with the result.

The following example runs a Mongo aggregation pipeline:

Client code
async function countDocuments(): Promise<any> {
const response = await this.squid.executeNativeMongoQuery(
'DATABASE_INTEGRATION_ID',
'COLLECTION_NAME',
[{ $count: 'totalApplications' }]
);
return response;
}

To secure your native MongoDB queries, use a Squid Service function in the Squid backend. To learn more, check out the documentation on securing data access