Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/platformatic/talk-choose-your-own-api-adventure-with-platformatic-db
https://github.com/platformatic/talk-choose-your-own-api-adventure-with-platformatic-db
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/platformatic/talk-choose-your-own-api-adventure-with-platformatic-db
- Owner: platformatic
- Created: 2022-10-17T16:39:12.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-16T02:46:10.000Z (about 1 year ago)
- Last Synced: 2024-05-06T12:24:35.912Z (8 months ago)
- Language: JavaScript
- Size: 444 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# talk-choose-your-own-api-adventure-with-platformatic-db
## Steps: Before the talk
```bash
mkdir adventure-appcd adventure-app
npm init --yes
npm install platformatic
```- [ ] Open project in VS Code
- [ ] Open `package.json`
- [ ] Open terminal
- [ ] Toggle on Screencast Mode
- [ ] Open Firefox## Steps: Live
### Create a Platformatic DB application
```bash
# Create an initial Platformatic DB application
npx platformatic db init# Copy paste command to install dependencies for types
# Explore platformatic.db.json
# Explore migrations
# Run migration
npx platformatic db migrate# Start the server
npx platformatic db start# Click on URL in terminal to open in browser
# Explore REST API documentation: /documentation
# Create a new movie: POST /movies
# Request all movies with cURL
curl http://127.0.0.1:3042/movies/ --verbose
```### Add movie quotes
```sql
-- Create 002.do.sql in migrations directoryCREATE TABLE IF NOT EXISTS quotes (
id INTEGER PRIMARY KEY,
quote TEXT NOT NULL,
said_by VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
movie_id INTEGER NOT NULL REFERENCES movies(id)
);
``````sql
-- Create 002.undo.sql in migrations directoryDROP TABLE quotes;
``````bash
# Apply migrations
npx platformatic db migrate# Restart the server
touch platformatic.db.json# Refresh /documentation in the browser
# Explore nested relationships
# Explore /graphiql
# Look at docs in sidebar
``````graphql
# Add query to get all quotes
query getAllQuotes {
quotes {
id
quote
saidBy
movie {
title
}
}
}# Run getAllQuotes query
# Add mutation to create a quote
mutation createQuote {
saveQuote(
input: {movieId: 1, quote: "Toto, I don't think we're in Kansas anymore", saidBy: "Dorothy"}
) {
id
quote
saidBy
movie {
title
}
}
}# Run createQuote mutation
``````bash
# Request all quotes with curl
curl http://127.0.0.1:3042/quotes/ --verbose
```### Add custom functionality
```sql
-- Create 003.do.sql in migrations directoryALTER TABLE quotes ADD COLUMN likes INTEGER default 0;
``````sql
-- Create 003.undo.sql in migrations directoryALTER TABLE quotes DROP COLUMN quotes;
``````bash
# Apply migrations
npx platformatic db migrate# Restart the server
touch platformatic.db.json# Re-fetch GraphQL schema in GraphiQL
# Add `likes` to getAllQuotes query
# Explore platformatic.db.json and plugin.js
# Explore types in global.d.ts
``````javascript
// Add a new query to the GraphQL schema in plugin.js// Use an SQL Mapper entity to retrieve database records.
app.graphql.extendSchema(`
extend type Query {
topQuotes: [Quote]
}
`)app.graphql.defineResolvers({
Query: {
topQuotes: async () => {
return await app.platformatic.entities.quote.find({
where: {
likes: {
gte: 3
}
}
})
}
}
})
``````graphql
# Re-fetch GraphQL schema in GraphiQLquery getTopQuotes {
topQuotes {
id
quote
saidBy
likes
movie {
title
}
}
}
``````javascript
// Add a new mutation to the GraphQL schema in plugin.js// Use the SQL Mapper database wrapper and query builder
// to update a database record.app.graphql.extendSchema(`
// ...extend type Mutation {
likeQuote(id: ID!): Int
}
`)app.graphql.defineResolvers({
// ...,
Mutation: {
likeQuote: async (_, { id }) => {
const { db, sql } = app.platformaticconst result = await db.query(sql`
UPDATE quotes SET likes = likes + 1 WHERE id=${id} RETURNING likes
`)return result[0]?.likes
}
}
})
``````graphql
# Re-fetch GraphQL schema in GraphiQL# Run the getTopQuotes query
mutation likeWizardOfOzQuote {
likeQuote(id: 1)
}# Run the likeWizardOfOzQuote mutation 4 times
# Run the getTopQuotes query
```