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 (such as subselects and complex joins) and are therefore unavailable in the SDK. Or your database might have unique capabilities that other databases don't have, which 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 connector, you can execute raw SQL using the executeNativeRelationalQuery
method, passing your database's connector ID and the SQL query.
The following example runs a native SQL query:
async function runNativeQuery(queryParam: string): Promise<any> {
const response = await this.squid.executeNativeRelationalQuery('DATABASE_CONNECTOR_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:
async function countDocuments(): Promise<any> {
const response = await this.squid.executeNativeMongoQuery('DATABASE_CONNECTOR_ID', 'COLLECTION_NAME', [{ $count: 'totalApplications' }]);
return response;
}