Skip to main content

Class: Squid

The main entry point to the Squid Client SDK.

The Squid class provides a comprehensive array of functionality for accessing the different integrations, executing backend functions, managing data, and more. Upon instantiating the Squid class, you will have access to all of these capabilities. All public Squid functions are bound to this and can be used with a destructuring patterns, like const {setAuthProvider} = useSquid()

Constructors

constructor

new Squid(options)

Creates a new instance of Squid with the given options.

Parameters

NameTypeDescription
optionsSquidOptionsThe options for initializing the Squid instance.

Properties

options

Readonly options: SquidOptions

The options for initializing the Squid instance.

Accessors

observability

get observability(): ObservabilityClient

Returns

ObservabilityClient


schedulers

get schedulers(): SchedulerClient

Returns

SchedulerClient


secrets

get secrets(): SecretClient

Returns

SecretClient

Methods

acquireLock

acquireLock(mutex): Promise<DistributedLock>

Returns a distributed lock for the given mutex. The lock can be used to synchronize access to a shared resource. The lock will be released when the release method on the returned object is invoked or whenever the connection with the server is lost.

Parameters

NameTypeDescription
mutexstringA string that uniquely identifies the lock.

Returns

Promise<DistributedLock>

A promise that resolves with the lock object. The promise will reject if failed to acquire the lock.


ai

ai(): AiClient

Returns a set of AI specific clients.

Returns

AiClient

A set of AI specific clients.


api

api(): ApiClient

Returns

ApiClient


collection

collection<T>(collectionName, integrationId?): CollectionReference<T>

Returns a reference to the collection in the provided integration.

If the integrationId is not provided, the built_in_db integration id will be used.

For more information on the CollectionReference object, please refer to the documentation.

Type parameters

NameTypeDescription
Textends DocumentDataThe type of the documents in the collection.

Parameters

NameTypeDefault valueDescription
collectionNamestringundefinedThe name of the collection.
integrationIdstringIntegrationType.built_in_dbThe id of the integration, default to built_in_db.

Returns

CollectionReference<T>

A reference to the collection in the provided integration.


connectionDetails

connectionDetails(): ConnectionDetails

Provides information about the connection to the Squid Server.

Returns

ConnectionDetails


destruct

destruct(): Promise<void>

Destructs the Squid Client. Unsubscribes from all ongoing queries or requests, and clears the local data. After invoking this method, the Squid client will not be usable.

Returns

Promise<void>

A promise that resolves when the destruct process is complete.


executeFunction

executeFunction<T>(functionName, ...params): Promise<T>

Executes the given backend function with the given parameters and returns a promise with the result.

For more information about backend functions in Squid, please refer to the documentation.

Type parameters

NameTypeDescription
TanyThe type of the result of the function.

Parameters

NameTypeDescription
functionNamestringThe name of the function to execute on the server.
...paramsany[]The parameters to pass to the function.

Returns

Promise<T>

A promise that resolves with the result of the function.


executeNativeMongoQuery

executeNativeMongoQuery<T>(integrationId, collectionName, aggregationPipeline): Promise<T[]>

Executes a native Mongo/built-in-db query with the given pipeline and returns a promise with the result. See https://docs.getsquid.ai/docs/database/native-queries#native-mongodb-queries.

Type parameters

NameType
Tany

Parameters

NameTypeDescription
integrationIdstringThe id of the integration that the query is associated with.
collectionNamestringThe collection to query.
aggregationPipelineany[]The aggregation pipeline for the query.

Returns

Promise<T[]>

A promise that resolves with the result of the query.


executeNativeRelationalQuery

executeNativeRelationalQuery<T>(integrationId, query, params?): Promise<T[]>

Executes a native relational query with the given parameters and returns a promise with the result. See https://docs.getsquid.ai/docs/database/native-queries.

Type parameters

NameType
Tany

Parameters

NameTypeDescription
integrationIdstringThe id of the integration that the query is associated with.
querystringThe raw SQL or other database-specific query to execute.
paramsRecord<string, any>(Optional) The parameters to pass to the query. Defaults to an empty object.

Returns

Promise<T[]>

A promise that resolves with the result of the query.


extraction

extraction(): ExtractionClient

Returns

ExtractionClient


internal

internal(): SquidInternalForGraphQl

An interface provided by Squid to @squidcloud/* packages but not exposed to users. @internal.

Returns

SquidInternalForGraphQl


personalStorage

personalStorage(integrationId): PersonalStorageClient

Parameters

NameType
integrationIdstring

Returns

PersonalStorageClient


queue

queue<T>(topicName, integrationId?): QueueManager<T>

Returns a queue manager for the given topic name and integration id. Using the queue manager you can consume and produce messages

Type parameters

Name
T

Parameters

NameTypeDefault value
topicNamestringundefined
integrationIdstringIntegrationType.built_in_queue

Returns

QueueManager<T>


runInTransaction

runInTransaction<T>(fn): Promise<T>

Runs the given callback as an atomic change. All the mutations that are executed using the provided transactionId will be atomic. Note that mutations for different integrations will not be atomic.

For more information about transactions in Squid, please refer to the documentation.

Type parameters

NameType
Tany

Parameters

NameTypeDescription
fn(transactionId: string) => Promise<T>The callback to run as an atomic change. The function receives a transactionId that should be used for all the mutations that should be atomic. The function should return a promise.

Returns

Promise<T>

A promise that resolves when the transactions are committed on the server.


setAuthProvider

setAuthProvider(authProvider): void

Sets the authorization access token (OAuth2.0) provider that will be sent to the server and will be used for providing the auth object to the security rules.

Parameters

NameTypeDescription
authProviderSquidAuthProviderThe OAuth2.0 access token provider invoked for every backend request by Squid. When the provider returns undefined, no authorization information is sent. When a new provider is set, all future Squid backend requests will use the new token provider, and exising in-flight requests won't be affected.

Returns

void

void.


storage

storage(integrationId?): StorageClient

Parameters

NameTypeDefault value
integrationIdstring'built_in_storage'

Returns

StorageClient


withLock

withLock<T>(mutex, fn): Promise<T>

Executes the given callback while holding a lock for the given mutex. The lock will be released when the callback finishes.

Type parameters

Name
T

Parameters

NameTypeDescription
mutexstringA string that uniquely identifies the lock.
fn(lock: DistributedLock) => Promise<T>The callback to execute while holding the lock.

Returns

Promise<T>

A promise that resolves with the result of the callback. The promise will reject if failed to acquire the lock.


getInstance

Static getInstance(options): Squid

Returns the global Squid instance with the given options, creating a new instance if one with the same options does not exist.

Parameters

NameTypeDescription
optionsSquidOptionsThe options for initializing the Squid instance.

Returns

Squid

A global Squid instance with the given options.


getInstances

Static getInstances(): Squid[]

Returns all the global Squid instances.

Returns

Squid[]

An array of all the global Squid instances.