Skip to main content

Database

Squid offers the capability to seamlessly integrate with multiple databases to allow easy access to your data. Each database can be configured with different authorization and access controls to ensure that only the data that you want to expose can be accessed by your users.​

Squid's database connectors are incredible for their power and simplicity. Once you add a database connectors, you can access and interact with your data using a streamlined SDK.

Squid provides many database features, including endlessly customizable security functions, Query with AI for asking ad-hoc questions about your data in natural language, and fine-grained data querying that can even join results from separate database connectors.

Connecting to a database​

  1. Navigate to the Connectors tab of the Squid Console.
  2. Click on "Available Connectors" and select the database type.
  3. Enter the necessary connection details and test the connection.
  4. Click "Next" and verify that the displayed schema is accurate. If necessary, remove any tables that are not intended for use with Squid (these can always be added back later).
  5. Click "Save" to finalize the connectors.

Squid's IP address​

To prevent denial-of-service attacks, brute force password attacks, and other forms of malicious activity, some providers recommend restricting your network to allow access only from specific IP addresses. This procedure is commonly known as allowlisting, and it limits access to your resources by only accepting connections from a specific list of endpoints.

Refer to your resource provider to determine if you need to add Squid's IP addresses to your access list. If allowlisting is required, you can find Squid's IP addresses on the application overview page of the Squid Console (located in the Cloud Provider section).

Query with AI​

Query with AI lets you ask questions about your data in natural language. Squid AI will return answers in natural language format along with the database queries and steps taken to get the answer. It allows you to explore your data or ask ad-hoc questions without writing SQL or NoSQL queries. Query with AI is found in the Schema tab of your database connector, or within Squid's AI Agent Studio.

Learn more about what you can do with Query with AI by reading the documentation.

Using a connected database​

To access data from the client, use your connector ID, which is the value you provided when creating the connector. You can view the connector ID on the connectors page of the Squid Console.

Client code
await squid
.collection('COLLECTION_NAME', 'YOUR_CONNECTOR_ID')
.query()
.eq('field', 'value')
.snapshots()
.subscribe((data) => {
// Do something with your data
});

To learn more about how to interact with a database connector in a fullstack application, read the documentation.

Securing your database​

To secure your database connector, write one or more functions that use a database security decorator, @secureDatabase or @secureCollection. Here is a more complex example of a security rule you can add to your backend project to make sure users cannot change the userId field in a document:

Backend code
export class ExampleService extends SquidService {
@secureCollection('table_name', 'update', 'CONNECTOR_ID')
secureUpdate(context: MutationContext): boolean {
// If the user is not authenticated, the update is not allowed.
const auth = this.getUserAuth();
if (!auth) return false;

// If the userId has changed, the update is not allowed.
const { after, before } = context.beforeAndAfterDocs;
if (after.userId !== before.userId) return false;

// The update is only allowed if the user making the request
// matches the user being updated.
return after.userId !== auth.userId;
}
}

To learn more about securing your database connectors, view the documentation on securing data.

Database schema​

Upon connecting your database to Squid, a schema is automatically generated based on the existing schema of data in your database. To view your schema, navigate to your database connector in the Squid Console, click the ... button, and select Schema from the dropdown menu.

Use this interface to verify schema structures, make schema changes, and describe your data for AI features.

Understanding collections and fields​

Collections are the main organizational units in your database, with their underlying structure varying based on the database connector type. In SQL databases, a collection represents a table, while in NoSQL databases a collection represents a collection of documents.

Collections are identified by a unique name within a connector. They comprise one or more documents, with a document representing a row in SQL databases and representing a document in NoSQL databases. A document contains one or more fields that denote different data elements. Each field within a collection is characterized by a specific set of attributes, facilitating streamlined data management and connector.

Here's a closer look at the general structure and attributes of collections and fields:

Collections:

  • Name: A unique identifier within an connector that helps in grouping and referencing a set of related rows or documents.

Documents:

  • ID: A unique identifier within a collection. IDs can be random, but can also be a user ID or other significant data point.

Fields:

  • Name: The name of the field. The name should match the document field name when inserting/updating/querying.
  • Type: Specifies the data category a field belongs to, encompassing types like string, number, boolean, date, map, and array.
  • Primary Key: Indicates if the field serves as a unique identifier within the collection, aiding in distinguishing records.

Squid does not validate your data against the schema: the schema describes the data for queries, agents, and the Console, and your database enforces its own constraints. A collection without a primary key can be queried but not written to through the SDK, and for every connector except the built-in database a document reference must name its full primary key; see Working with document IDs.

Inferring associations​

Schema discovery reads the foreign keys your database declares. Many databases declare none, so Squid can also infer them from the data itself: it samples rows, finds columns whose values reference another table's key, and writes the findings into the stored schema as associations. It can also find discriminator columns and columns that depend on each other.

In the Console, open the connector's Schema and choose Infer Associations from the AI menu, or check the option in the rediscover dialog. Inferred associations can be hidden or deleted from the schema view afterwards.

From code, use the admin API, which requires the app API key:

Backend code
const jobId = await this.squid.admin().integrations().startRelationshipInference('my-postgres', {
minConfidence: 'medium', // the lowest confidence written to the schema (default)
useAi: true, // let the AI propose relationships that column names miss
});

// The run executes in the background; read its progress and findings at any time.
const run = await this.squid.admin().integrations().getRelationshipInference('my-postgres');
console.log(run?.phase, run?.progress, run?.findings.length);

// Stop a run early. It keeps what it found so far.
await this.squid.admin().integrations().cancelRelationshipInference('my-postgres');

Other options: kinds picks what to look for (relationships, dependencies, discriminators), collections limits the tables examined, sampleSize sets the rows sampled per collection (default 1,000), verifyOnAllRows re-checks every finding on all rows, and maxDurationMinutes caps the run (60 by default). Only one run per connector is active at a time.

Generating synthetic data​

In a dev environment, Squid can fill a PostgreSQL database with fictional rows that match your schema, so you can develop against realistic data. Rows are only added, never changed or removed, and each run's rows reference each other consistently. Existing rows are sent to the configured AI provider as format examples.

In the Console, choose Generate Synthetic Data from the connector's menu. From code:

Backend code
const jobId = await this.squid.admin().integrations().startSyntheticDataGeneration('my-postgres', {
prompt: 'A bike shop with customers in Europe', // what the data should look like
size: 'M', // S, M, L or XL: 50, 250, 1,000 or 2,500 rows in total
collections: ['customers', 'orders'], // optional; chosen from the prompt when absent
});

const run = await this.squid.admin().integrations().getSyntheticDataGeneration('my-postgres');
console.log(run?.status, run?.tables);

Outside a dev environment the call fails with a 403. cancelSyntheticDataGeneration() stops a run; the rows written until then stay.