Skip to content

Cbonif/edit-sql-page #7599

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

Merged
merged 4 commits into from
May 15, 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
2 changes: 2 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@
"Geofences",
"Geofencing",
"geolocation",
"GEOMFROMTEXT",
"geospatial",
"getAllKeys",
"getApplicationContext",
Expand Down Expand Up @@ -1224,6 +1225,7 @@
"spinner.js",
"SQLite",
"src",
"SRID",
"snstopic",
"snstopicemailsub",
"SSECustomerAlgorithm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ If your RDS database exists within a VPC, it must be configured to be `Publicly

**To generate the TypeScript representation of your database schema:**

If your database is protected by a VPC, you will need to add an **Inbound Rule** for the database port from your local IP address. The `npx ampx generate schema-from-database` command connects to your database from your local workstation to read schema information.
If your database is protected by a VPC, you will need to add an **Inbound Rule** for the database port from your local IP address. The `npx ampx generate schema-from-database` command connects to your database from your local workstation to read schema information.

If you are connecting to an RDS Proxy, the machine you run the `generate schema-from-database` command must be in the same VPC as the proxy itself, or you must connect to it via VPN. Simply opening an **Inbound Rule** for the database port is not sufficient.

Expand Down Expand Up @@ -165,7 +165,7 @@ const combinedSchema = a.combine([schema, sqlSchema]);
export type Schema = ClientSchema<typeof combinedSchema>;

export const data = defineData({
// Update the data definition to use the combined schema, instead of just
// Update the data definition to use the combined schema, instead of just
// your DynamoDB-backed schema
// highlight-next-line
schema: combinedSchema
Expand Down Expand Up @@ -222,7 +222,7 @@ const sqlSchema = generatedSqlSchema.authorization(allow => allow.guest())
.renameModels(() => [
//⌄⌄⌄⌄⌄ existing model name based on table name
['event', 'Event']
// ^^^^^^ renamed data model name
// ^^^^^^ renamed data model name
])
// highlight-end
```
Expand Down Expand Up @@ -250,7 +250,7 @@ Use the `.addToSchema(...)` to add in additional queries, mutations, and subscri

<Callout info>

Note: you can't add additional data models via `a.model()`. They should be exclusively generated via `npx ampx generate schema-from-database`.
Note: you can't add additional data models via `a.model()`. They should be exclusively generated via `npx ampx generate schema-from-database`.

</Callout>

Expand All @@ -262,7 +262,7 @@ const sqlSchema = generatedSqlSchema.authorization(allow => allow.guest())
// highlight-start
.addToSchema({
listEventsWithDecodedLatLong: a.query()
// reference custom types added to the schema
// reference custom types added to the schema
.returns(a.ref("EventWithDecodedCoord").array())
.handler(a.handler.inlineSql(
`SELECT
Expand Down Expand Up @@ -311,8 +311,21 @@ const sqlSchema = generatedSqlSchema.authorization(allow => allow.guest())
```

Next, add a corresponding sql file to handle the request:
```sql

<BlockSwitcher>
<Block name="MySQL">
```sql title="createNewLocationWithLongLat.sql"
INSERT INTO locations (name, address, geom)
VALUES (:name, :address, ST_GEOMFROMTEXT(CONCAT('POINT (', :long, ' ', :lat, ')'), 4326));
```
</Block>
<Block name="PostgreSQL">
```sql title="createNewLocationWithLongLat.sql"
INSERT INTO locations (name, address, geom)
VALUES (:name, :address, ST_SetSRID(ST_MakePoint(:long, :lat), 4326))
```
</Block>
</BlockSwitcher>

## How does it work?

Expand Down
Loading