Skip to main content

Class: CollectionReference<T>

Holds a reference to a data collection. A collection reference is a reference to a collection in a database. You can use it to read or write data to the collection. A collection can refer to a table in a relational database or a collection in a NoSQL database.

Read more about collection references in the documentation.

Type parameters

NameTypeDescription
Textends DocumentDataThe type of the document data.

Properties

refId

refId: string

A string that uniquely identifies this collection reference.

Methods

deleteMany

deleteMany(docIdsOrReferences, txId?): Promise<void>

Deletes multiple documents from the collection.

Parameters

NameType
docIdsOrReferences(DocIdOrDocIdObj | DocumentReference<T>)[]
txId?string

Returns

Promise<void>


doc

doc(docId?): DocumentReference<T>

Returns a document reference for the given document id. The document id is an object that maps the primary keys of the collection (as defined in the Squid Console) to their values. There are a few exceptions: 1 - String document id is only supported for the built_in_db when a schema was not defined. 2 - Not all the fields in the document id are required. If a field is not provided, it will be generated on the server once the document is created (if the integration supports it). 3 - When a document id is not provided, it will be treated as an empty object or empty string. 4 - When the document id is just a string and not provided (applies only for the built_in_db), it will be generated on the server. 5 - If the collection in the built_in_db does not have a schema, the document id must be provided as a string.

Examples:

// For a collection in the built_in_db that does not have a schema
const docRef = collectionRef.doc('my-doc-id');
const docRef = collectionRef.doc({id: my-doc-id'});

// For a collection with a single primary key field called "id"
const docRef = collectionRef.doc({id: 'my-doc-id'});

// For a collection with a composite primary key
const docRef = collectionRef.doc({id1: 'my-doc-id1', id2: 'my-doc-id2'});
const docRef = collectionRef.doc({id1: 'my-doc-id1'}); // id2 will be generated on the server if the integration
supports it

// For a collection from the `built_in_db` without a defined schema when an id is not provided
const docRef = collectionRef.doc(); // The id will be generated on the server

Parameters

NameTypeDescription
docId?DocIdOrDocIdObjThe document id as an object for the different fields in the primary key or a string.

Returns

DocumentReference<T>

A document reference for the given document id.


insertMany

insertMany(docs, txId?): Promise<void>

Inserts multiple documents into the collection.

Parameters

NameType
docsDocIdAndData<T>[]
txId?string

Returns

Promise<void>


joinQuery

joinQuery<A>(alias): JoinQueryBuilder<Record<A, []>, Record<A, T>, A, A>

Creates a JoinQueryBuilder that can be used to query the collection Note that when using a join query, you have to provide an alias for the query and for every other query participating in the join.

Type parameters

NameType
Aextends string

Parameters

NameTypeDescription
aliasAThe alias for the query.

Returns

JoinQueryBuilder<Record<A, []>, Record<A, T>, A, A>

A JoinQueryBuilder that can be used to query the collection and joins with other queries.


or

or(...builders): SnapshotEmitter<DocumentReference<T>>

Performs or on a list of queries. All the queries need to be on the same collection. The result will be a merge of all the queries sorted by the same sort condition of the first query. Duplicate items will be removed.

Parameters

NameTypeDescription
...buildersQueryBuilder<T>[]The list of query builders to merge. (A query builder can be returned from the query method).

Returns

SnapshotEmitter<DocumentReference<T>>

A query builder that can be used to perform the or operation.


query

query(): QueryBuilder<T>

Creates a QueryBuilder that can be used to query the collection.

Returns

QueryBuilder<T>

A QueryBuilder that can be used to query the collection.