Customize primary keys
Customize primary keys
By default, DataStore models have an id
field that is automatically populated on the client with a UUID v4, allowing DataStore to generate non-colliding globally unique identifiers in a scalable way. While UUIDs have desirable properties (they are large, non-sequential and opaque), there are times when a custom primary key, also known as custom identifier, is needed. For instance, to:
- Have friendly/readable identifiers (surrogate/opaque vs. natural keys)
- Define composite primary keys
- Customize data partitioning to optimize for scale (especially important when planning to handle large amounts of data in short periods of time)
- Selectively synchronize data to clients (e.g. by fields like
deviceId
,userId
or similar) - Prioritize the sort order in which objects are returned by the sync queries
- Make existing data consumable and syncable by DataStore
A schema with the typical id
field looks like this:
1type Book @model {2 id: ID!3 title: String!4 description: String5}
You can customize the primary key by adding the @primaryKey
directive to a field:
1type Book @model {2 isbn: ID! @primaryKey3 title: String!4 description: String5}
You can also require multiple fields to define your primary key. When your primary key references multiple fields, it's called a composite key. In the example below, the primary key is defined by the isbn
and title
fields:
1type Book @model {2 isbn: ID! @primaryKey(sortKeyFields: ["title"])3 title: String!4 description: String5}
Determine when the primary key field is auto-populated upon record creation
When you create a record with DataStore, a UUID is automatically populated for the default id: ID!
primary key field. When working with custom primary keys, DataStore will automatically populate the key fields in the following conditions:
Description | Type | Autopopulated with UUID |
---|---|---|
Without |
| ✅ Yes |
Without |
| ✅ Yes |
|
| ❌ No |
Explicit |
| ✅ Yes |
Explicit |
| ✅ Yes |
Explicit |
| ✅ Yes |
@primaryKey with no |
| ❌ No |
Querying records with custom primary keys
A record with a custom primary key can be queried for in the following ways:
With the value of the primary key:
1Amplify.DataStore.query(Book.self, byIdentifier: .identifier(isbn: "12345")) { result in2 switch result {3 case .success(let book):4 guard let book = book else {5 print("Query was successful with empty result")6 return7 }8 print("Query was successful, retrieved book: \(book)")9 case .failure(let error):10 print("Error on query() for type Book with error: \(error)")11 }12}
With the value of QueryPredicate:
1Amplify.DataStore.query(Book.self, where: Book.keys.isbn == "12345") { result in2 switch result {3 case .success(let books):4 print("Query was successful, retrieved books: \(books)")5 case .failure(let error):6 print("Error on query() for type Book with error: \(error)")7 }8}
Deleting records with custom primary keys
A record with a custom primary key can be deleted in the following ways:
With the value of the primary key:
1Amplify.DataStore.delete(Book.self, withIdentifier: .identifier(isbn: "12345")) { result in2 switch result{3 case .success:4 print("Book deleted!")5 case .failure(let error):6 print("Error deleting book: \(error)")7 }8}
With the value of QueryPredicate:
1Amplify.DataStore.delete(Book.self, where: Book.keys.isbn == "12345") { result in2 switch result {3 case .success:4 print("Book deleted!")5 case .failure(let error):6 print("Error deleting book: \(error)")7 }8}