Document IDs
Document IDs are unique identifiers of a document that can be automatically generated by the server or manually specified by the user.
Squid's built-in database supports two different type of document IDs, while database connectors require an object for their document ID.
Built-in database
In Squid's built-in database, document IDs can be one of two types:
- A string that is unique within the collection. A string is an effective way to identify one document from many others. Often, developers will set string IDs to be randomly generated UUIDs. You can also provide your own unique string:
const collectionRef = squid.collection('users');
const docRef = collectionRef.doc('my_custom_id');
await docRef.insert({ name: 'John Doe', age: 30 });
- Composite primary keys, which take the form of an object. If the user modifies the schema of a collection in the console
and makes the primary key composite, the ID must be in the form of an object, such as
{employeeName: string, deptId: string}
. This allows the user to specify multiple fields as the document ID. For example, if Jane Doe in department 12345 just celebrated her birthday, we may update her age as follows:
const collectionRef = squid.collection('employees');
const docRef = collectionRef.doc({ employeeName: 'Jane Doe', deptId: '12345' });
await docRef.update({ age: 31 });
Auto ID generation
An ID is auto generated by Squid when the user does not specify an ID and the schema does not require the ID to be a composite primary key. In the example below, the ID is automatically generated:
const collectionRef = squid.collection('users');
await collectionRef.doc().insert({ name: 'John Doe', age: 30 });
External database connectors
For any non-built-in connector, the document ID must be in the form of an object, even if the primary key is not composite. This ensures consistency across different integrations and simplifies the handling of document IDs in Squid.
Here's an example of how to create a document with a custom ID in a database connector:
const collectionRef = squid.collection('users', 'CONNECTOR_ID');
const docRef = collectionRef.doc({ id: 'my_custom_id' });
await docRef.insert({ name: 'John Doe', age: 30 });
By understanding how document IDs work in Squid, you can create and manage documents more effectively and efficiently.