ReferenceDb Storage

DB Storage

Mastra synced records in your Postgres database, and uses Prisma as an access layer to that data.

Access Layer

You can access the DB storage layer using the DB object in the framework. For more, see the Prisma docs.

Here’s an example:

const record = await mastra.dataLayer.db.record.findUnique({
  where: {
    id: recordId,
  },
});

DB Schema

In order to allow you to continue to control migrations on your database, while letting Mastra use it to sync data into, Mastra uses the Postgres concept of schemas.

For more details, look at /src/prisma/schema.prisma in the core package.

Records

Records represent individual data entries in the system. Each record has the following fields:

FieldTypeDescription
idString Unique identifier for the record, automatically generated using CUID
externalIdString (optional)An optional external identifier for the record
dataJsonJSON object storing the record’s data, defaults to an empty object
sourceStringThe source of the record, defaults to “MANUAL”
entityTypeStringThe type of entity associated with this record
entityEntity (optional)Relation to an Entity object
entityIdString (optional)The ID of the related Entity
statusRecordStatusThe current status of the record, defaults to ACTIVE
enrichmentStatusRecordEnrichmentStatusThe enrichment status of the record, defaults to UNAPPLIED
deletedAtDateTime (optional)Timestamp when the record was deleted (null if not deleted)
createdAtDateTimeTimestamp when the record was created, automatically set to the current time
updatedAtDateTimeTimestamp when the record was last updated, defaults to the creation time

Connections

Connections represent one user in one external system.

FieldTypeDescription
idStringUnique identifier for the entity, automatically generated using CUID
nameStringThe name of the entity
issuesString[]An array of issues associated with the entity, defaults to an empty array
syncConfigJson (optional)JSON object storing synchronization configuration for the entity
connectionIdStringA reference identifier for the entity
createdAtDateTimeTimestamp when the entity was created, automatically set to the current time
updatedAtDateTime (optional)Timestamp when the entity was last updated
lastSyncAtDateTime (optional)Timestamp of the last synchronization for this entity
credentialCredential (optional)Relation to a Credential object
subscriptionIdString (optional)The ID of the subscription associated with this entity
entitiesEntity[]Relation to other Entity objects (likely representing a hierarchical structure)