Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify_Amplify_resources #7018

Merged
merged 8 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ a.schema({
Post: a.model({
location: a.customType({
lat: a.float(),
long: a.float()
long: a.float(),
}),
content: a.string(),
})
})
}),
});
```

**Explicit definition:** Specify the "Location" as `a.customType()` in your schema. To use the custom type, reference it through `a.ref()` in the respective field definitions.
Expand All @@ -56,7 +56,7 @@ a.schema({
a.schema({
Location: a.customType({
lat: a.float(),
long: a.float()
long: a.float(),
}),

Post: a.model({
Expand All @@ -65,9 +65,9 @@ a.schema({
}),

User: a.model({
lastKnownLocation: a.ref('Location')
})
})
lastKnownLocation: a.ref('Location'),
}),
});
```

To set or read the location field on the client side, you can expand a nested object and the type system will auto-infer the allowed values.
Expand All @@ -76,9 +76,9 @@ To set or read the location field on the client side, you can expand a nested ob
const newPost = await client.models.Post.create({
location: {
lat: 48.837006,
long: 8.282450
long: 8.282450,
}
})
});

console.log(newPost.location.lat, newPost.location.long)
```
Expand All @@ -91,8 +91,8 @@ a.schema({
Post: a.model({
privacySetting: a.enum(['PRIVATE', 'FRIENDS_ONLY', 'PUBLIC']),
content: string(),
})
})
}),
});
```

```ts
Expand All @@ -105,25 +105,25 @@ a.schema({

Post: a.model({
content: string(),
privacySetting: a.ref('PrivacySetting')
privacySetting: a.ref('PrivacySetting'),
}),

Video: a.model({
privacySetting: a.ref('PrivacySetting')
})
})
privacySetting: a.ref('PrivacySetting'),
}),
});
```

When creating a new item client-side, the enums are also type-enforced:
```ts
client.models.Post.create({
content: "hello",
content: 'hello',
// WORKS - value auto-completed
privacySetting: "PRIVATE"
privacySetting: 'PRIVATE',

// DOES NOT WORK - TYPE ERROR
privacySetting: "NOT_PUBLIC"
})
privacySetting: 'NOT_PUBLIC',
});
```

### List enum values client-side
Expand All @@ -142,9 +142,9 @@ By default, fields are optional. To mark a field as required, use the `.required
```ts
const schema = a.schema({
Todo: a.model({
content: a.string().required()
})
})
content: a.string().required(),
}),
});
```

## Mark fields as arrays
Expand All @@ -155,9 +155,9 @@ Any field can be modified to be an array using the `.array()` modifier.
const schema = a.schema({
Todo: a.model({
content: a.string().required(),
notes: a.string().array()
})
})
notes: a.string().array(),
}),
});
```

## Assign default values for fields
Expand All @@ -167,9 +167,9 @@ You can use the `.default(...)` modifier to specify a default value for optional
```ts
const schema = a.schema({
Todo: a.model({
content: a.string().default("My new Todo")
})
})
content: a.string().default('My new Todo'),
}),
});
```
<Callout>
**Note:** The `.default(...)` modifier can't be applied to required fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const schema = a.schema({
Todo: a.model({
content: a.string(),
completed: a.boolean(),
})
}),
});
```

Expand All @@ -45,11 +45,12 @@ const schema = a.schema({
title: a.string(),
completed: a.boolean(),
}).identifier(['todoId'])
.authorization([a.allow.public()]),
});

const client = generateClient<Schema>();

const todo = await client.models.Todo.create({ todoId: 'MyUniqueTodoId', title: 'Buy Milk', completed: false });
const { data: todo, errors } = await client.models.Todo.create({ todoId: 'MyUniqueTodoId', title: 'Buy Milk', completed: false });
renebrandel marked this conversation as resolved.
Show resolved Hide resolved
console.log(`New Todo created: ${todo.todoId}`); // New Todo created: MyUniqueTodoId
```

Expand All @@ -68,6 +69,7 @@ const schema = a.schema({
zipCode: a.string(),
streetAddress: a.string(),
}).identifier(['tenantId', 'name'])
.authorization([a.allow.public()]),
});

const client = generateClient<Schema>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,52 +34,54 @@ const schema = a.schema({
Team: a.model({
mantra: a.string().required(),
members: a.hasMany('Member'),
}),
})
.authorization([a.allow.public()]),
Member: a.model({
team: a.belongsTo('Team'),
}),
})
.authorization([a.allow.public()]),
});
```

### Create a "Has Many" relationship between records

```ts
const { data: team } = await client.models.Team.create({
mantra: "Hello"
})
mantra: 'Hello',
});

await client.models.Member.update({
id: "SOME_MEMBER_ID",
team
})
id: 'SOME_MEMBER_ID',
team,
});
```

### Update a "Has Many" relationship between records

```ts
await client.models.Member.update({
id: "SOME_MEMBER_ID",
team
})
id: 'SOME_MEMBER_ID',
team,
});
```

### Delete a "Has Many" relationship between records

```ts
await client.models.Member.update({
id: "SOME_MEMBER_ID",
team: undefined
})
id: 'SOME_MEMBER_ID',
team: undefined,
});
```

### Lazy load a "Has Many" relationship

```ts
const { data: team } = await client.models.Team.get({ id: "MY_TEAM_ID"})
const { data: team } = await client.models.Team.get({ id: "MY_TEAM_ID"});

const { data: members } = await team.members()
const { data: members } = await team.members();

members.forEach(member => console.log(member.id))
members.forEach(member => console.log(member.id));
```

### Eagerly load a "Has Many" relationship
Expand All @@ -88,9 +90,9 @@ members.forEach(member => console.log(member.id))
const { data: teamWithMembers } = await client.models.Team.get(
{ id: "MY_TEAM_ID" },
{ selectionSet: ["id", "members.*"] },
)
);

teamWithMembers.members.forEach(member => console.log(member.id))
teamWithMembers.members.forEach(member => console.log(member.id));
```

## Model a "many-to-many" relationship
Expand All @@ -101,11 +103,11 @@ const schema = a.schema({
Post: a.model({
title: a.string(),
content: a.string(),
tags: a.manyToMany('Tag', { relationName: 'PostTags'})
tags: a.manyToMany('Tag', { relationName: 'PostTags'}),
}),
Tag: a.model({
name: a.string(),
posts: a.manyToMany('Post', { relationName: 'PostTags'})
posts: a.manyToMany('Post', { relationName: 'PostTags'}),
}),
});
```
Expand All @@ -118,7 +120,7 @@ Create a one-directional, one-to-one relationship between two models using the `
const schema = a.schema({
Project: a.model({
name: a.string(),
team: a.hasOne('Team')
team: a.hasOne('Team'),
}),
Team: a.model({
name: a.string(),
Expand Down Expand Up @@ -170,7 +172,7 @@ await client.models.Project.update({
### Lazy load a "Has One" relationship

```ts
const { data: project } = await client.models.Project.get({ id: "MY_PROJECT_ID"})
const { data: project } = await client.models.Project.get({ id: "MY_PROJECT_ID"});
const { data: team } = await project.team();
```

Expand Down Expand Up @@ -222,7 +224,7 @@ You can make a "has one" or "has many" relationship bidirectional with the `belo
const schema = a.schema({
Project: a.model({
name: a.string(),
team: a.hasOne('Team')
team: a.hasOne('Team'),
}),
Team: a.model({
name: a.string(),
Expand Down Expand Up @@ -290,7 +292,7 @@ const client = generateClient<Schema>();

const { data: project } = await client.models.Project.get({ id: '1234' });
const projectName = project.name;
const lazyTeamName = (await project.team()).data?.name // project.team is typed as `Lazy<Team>` and can be awaited
const lazyTeamName = (await project.team()).data?.name; // project.team is typed as `Lazy<Team>` and can be awaited

const projectWithTeam = await client.models.Project.get({ id: '1234' }, { selectionSet: ['team.*'] });
const eagerTeamName = project.team.name // All team attributes were requested, so no need to await, it was eagerly loaded
Expand Down
Loading
Loading