aws-amplify / aws-amplify/amplify-data

Autogenerated fields available for secondary indexes

Open
#630 1 comment 0 reactions 0 assignees View on GitHub
feature-request
Dominant language
TypeScript
Stars
18
Forks
23
Avg merge
26m
Merged PRs (30d)
1

Description

## Problem

Amplify Data (Gen 2) automatically adds the system fields createdAt and updatedAt to every model and returns them in query results.

However, these fields cannot be referenced in .secondaryIndexes() definitions, because the schema compiler only allows explicitly declared fields.

This prevents developers from using `createdAt` or `updatedAt` as sort keys in indexes — a very common pattern (e.g., querying by status ordered by creation time).

Example that fails today:
```ts
Report: a
.model({
id: a.id(),
reviewStatus: a.enum(['DRAFT','IN_REVIEW','APPROVED','REJECTED','NEEDS_WORK']).required(),
})
.secondaryIndexes((index) => [
index('reviewStatus')
.sortKeys(['createdAt']) // ❌ causes InvalidDirectiveError
.queryField('byReviewStatusAndCreatedAt'')
}),
])
```

Deployment error:
```
InvalidDirectiveError: Can't find field 'createdAt' in Substance,
but it was specified in index 'substancesByReviewStatusAndCreatedAt'.
```

## Solution
Allow the **system-managed fields** `createdAt` and `updatedAt` to be referenced in index definitions.

For example:
```ts
.secondaryIndexes((index) => [
index('reviewStatus')
.sortKeys(['createdAt'])
.queryField('byReviewStatusAndCreatedAt')
}),
])
```

## Alternatives considered
- Creating a duplicate field (e.g., createdAtCopy: a.datetime()) and populating it manually or via a Lambda trigger so it can be indexed.
→ Works, but is redundant and easy to forget when inserting data.

- Querying with filters and sorting client-side.
→ Inefficient for large datasets and inconsistent with server-side pagination.

Both alternatives add complexity for something Amplify already tracks internally.

## Additional context
- Amplify Data Gen 2 (@aws-amplify/backend, latest)
- Deployment via amplify sandbox / amplify push
- Node.js 20.x (WSL2 + Windows 11)
- TypeScript schema using `a.schema({ ... })`

Supporting indexing on system fields would simplify many common models (feeds, review queues, dashboards, audit logs) and remove the need for duplicate “createdAtCopy” fields.

Contributor guide

Open the contributing guide

Research direction

Start at the TypeScript schema compiler validation used by .secondaryIndexes(), and trace how the system-managed createdAt and updatedAt fields are represented. Look for existing index-validation tests or the deployment path shown in the issue. Done means indexes can use either system field as a sort key without InvalidDirectiveError.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.