Last active
March 29, 2021 16:38
-
-
Save steebchen/5c16c4bd216fa5b8cbcd8d024f7b2992 to your computer and use it in GitHub Desktop.
go client api examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ------------------------ Reading Data ------------------------ | |
// -- Find Records -- | |
// Find all posts | |
posts, err := client.Post.FindMany().Exec(ctx) | |
// Find a user by ID | |
user, err := client.User.FindUnique(db.User.ID.Equals(2)).Exec(ctx) | |
// Find a user by email | |
user, err := client.User.FindUnique( | |
db.User.Email.Equals("[email protected]"), | |
).Exec(ctx) | |
// Find the first user that contains Ada | |
user, err := client.User.FindUnique( | |
db.User.Name.HasPrefix("[email protected]"), | |
).Exec(ctx) | |
// Select specific fields | |
// select is not possible right now | |
// -- Traverse Relations -- | |
// traversing is not possible right now | |
// Return all users and include their posts and profile | |
users, err := client.User.FindMany().With( | |
db.User.Posts.Fetch(), | |
db.User.Profile.Fetch(), | |
).Exec(ctx) | |
// Select all users and all their post titles | |
// select is not possible right now | |
// -- Order By, Limits & Cursors -- | |
// Sort posts alphabetically | |
alphabeticalPosts, err := client.Post.FindMany().OrderBy( | |
db.Post.Title.Order(db.ASC), | |
).Exec(ctx) | |
// Order by most prolific authors | |
// not possible right now | |
// Find the second page of posts | |
secondPagePosts, err := client.Post.FindMany().Take(5).Skip(5).Exec(ctx) | |
// Find the last 5 posts | |
lastPosts, err := client.Post.FindMany().Take(-5).Exec(ctx) | |
// Find the next 5 posts before post id 2 | |
paginatedPosts3, err := client.Post.FindMany().Take(5).Cursor( | |
db.Post.ID.Cursor(2), | |
).Exec(ctx) | |
// Find the last 5 posts before post id 2 | |
paginatedPosts3, err := client.Post.FindMany().Take(-5).Cursor( | |
db.Post.ID.Cursor(11), | |
).Exec(ctx) | |
// -- Aggregations & Group By | |
// not possible right now | |
// ------------------------ Writing Data ------------------------ | |
// -- Create Records -- | |
// Create a user | |
user, err := client.User.CreateOne( | |
db.User.Email.Set("[email protected]"), | |
db.User.Name.Set("Elsa Prima"), | |
db.User.Age.Set(30), | |
db.User.Country.Set("Italy"), | |
).Exec(ctx) | |
// Create a customer and connect to a user | |
profile, err := client.Profile.CreateOne( | |
db.Profile.Bio.Set("Flying Trapeze Artist. Prisma developer."), | |
db.Profile.User.Link( | |
db.User.ID.Equals(10), | |
), | |
).Exec(ctx) | |
// INTERNAL NOTE: remaining ones are not useful to show in Go IMO | |
// -- Update Records -- | |
// Update an existing user | |
alice, err := client.User.FindUnique( | |
db.User.Email.Equals("[email protected]"), | |
).Update( | |
db.User.Role.Set(db.RoleAdmin), | |
).Exec(ctx) | |
// Change the author of a post in a single transaction | |
updatedPost, err := client.Post.FindUnique( | |
db.Post.ID.Equals(2), | |
).Update( | |
db.Post.Author.Link( | |
db.User.Email.Equals("[email protected]"), | |
), | |
).Exec(ctx) | |
// Connect a post to a user, creating the post if it isn't found | |
// not possible right now (connectOrCreate) | |
// Update all users with the country Deutschland | |
result, err := client.User.FindMany( | |
db.User.Country.Equals("Deutschland"), | |
).Update( | |
db.User.Country.Set("Germany"), | |
).Exec(ctx) | |
// -- Delete Records -- | |
// Delete an existing user | |
deletedUser, err := client.User.FindUnique( | |
db.User.Email.Equals("[email protected]"), | |
).Delete().Exec(ctx) | |
// Delete all admins at once | |
result, err := client.User.FindMany( | |
db.User.Role.Equals(db.RoleAdmin), | |
).Delete().Exec(ctx) | |
// -- Upsert Records -- | |
// Create Alice or update her role to admin | |
result, err := client.User.UpsertOne( | |
db.User.Email.Equals("[email protected]"), | |
).Create( | |
db.User.Email.Set("[email protected]"), | |
db.User.Name.Set("Elsa Prima"), | |
db.User.Age.Set(30), | |
db.User.Country.Set("Italy"), | |
db.User.Role.Set(db.RoleAdmin), | |
).Update( | |
db.User.Role.Set(db.RoleAdmin), | |
).Exec(ctx) | |
// ------------------------ Advanced Patterns ------------------------ | |
// -- Middlewares -- | |
// not possible right now | |
// -- Raw Queries -- | |
// Select users by email | |
email := '[email protected]' | |
var posts []db.PostModel | |
err := client.Prisma.QueryRaw(`SELECT * FROM User WHERE email = ?`, email).Exec(ctx, &posts) | |
// Select 5 posts randomly | |
var posts []db.PostModel | |
err := client.Prisma.QueryRaw(`SELECT * FROM "Post" order by random() limit 5`).Exec(ctx, &posts) | |
// Clear the users table | |
result, err := client.Prisma.ExecuteRaw(`TRUNCATE TABLE User`).Exec(ctx) | |
// Drop the users table | |
result, err := client.Prisma.ExecuteRaw(`DROP TABLE IF EXISTS User CASCADE`).Exec(ctx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment