Skip to content

Commit

Permalink
Merge pull request #22 from syumai/fix-last-insert-id
Browse files Browse the repository at this point in the history
fix last_row_id key name of D1
  • Loading branch information
syumai authored Feb 26, 2023
2 parents dd17cf2 + 7c80414 commit 4e34fba
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cloudflare/d1/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (
// LastInsertId returns id of result's last row.
// If lastRowId can't be retrieved, this method returns error.
func (r *result) LastInsertId() (int64, error) {
v := r.resultObj.Get("meta").Get("lastRowId")
v := r.resultObj.Get("meta").Get("last_row_id")
if v.IsNull() || v.IsUndefined() {
return 0, errors.New("d1: lastRowId cannot be retrieved")
}
Expand Down
12 changes: 12 additions & 0 deletions examples/d1-blog-server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ build:
.PHONY: publish
publish:
wrangler publish

.PHONY: generate
generate:
go generate ./...

.PHONY: init-db
init-db:
wrangler d1 execute d1-blog-server --file=./schema.sql

.PHONY: init-db-preview
init-db-preview:
wrangler d1 execute d1-blog-server-preview --file=./schema.sql
14 changes: 10 additions & 4 deletions examples/d1-blog-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,20 @@ This project requires these tools to be installed globally.

### Commands

* Before development, 1. create your own D1 database, 2. set database ID to wrangler.toml and run `wrangler d1 migrations apply [DB Name]`.

```
make dev # run dev server
make build # build Go Wasm binary
# development
make init-db-preview # initialize preview DB (remove all rows)
make generate # generate easyjson models
make dev # run dev server
make build # build Go Wasm binary
# production
make init-db # initialize production DB (remove all rows)
make publish # publish worker
```

* Notice: This example uses raw SQL commands to initialize the DB for simplicity, but in general you should use `wrangler d1 migraions` for your application.

## Author

syumai
Expand Down
24 changes: 15 additions & 9 deletions examples/d1-blog-server/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"time"

"github.com/google/uuid"
"github.com/mailru/easyjson"
"github.com/syumai/workers/cloudflare/d1"
_ "github.com/syumai/workers/cloudflare/d1" // register driver
Expand Down Expand Up @@ -62,22 +61,29 @@ func (h *articleHandler) createArticle(w http.ResponseWriter, req *http.Request,

now := time.Now().Unix()
article := model.Article{
ID: uuid.New().String(),
Title: createArticleReq.Title,
Body: createArticleReq.Body,
CreatedAt: uint64(now),
}

_, err := db.Exec(`
INSERT INTO articles (id, title, body, created_at)
VALUES (?, ?, ?, ?)
`, article.ID, article.Title, article.Body, article.CreatedAt)
result, err := db.Exec(`
INSERT INTO articles (title, body, created_at)
VALUES (?, ?, ?)
`, article.Title, article.Body, article.CreatedAt)
if err != nil {
log.Println(err)
h.handleErr(w, http.StatusInternalServerError,
"failed to save article")
return
}
id, err := result.LastInsertId()
if err != nil {
log.Println(err)
h.handleErr(w, http.StatusInternalServerError,
"failed to get ID of inserted article")
return
}
article.ID = uint64(id)

res := model.CreateArticleResponse{
Article: article,
Expand All @@ -103,15 +109,15 @@ ORDER BY created_at DESC;
articles := []model.Article{}
for rows.Next() {
var (
id, title, body string
createdAt float64 // number value is always retrieved as float64.
title, body string
id, createdAt float64 // number value is always retrieved as float64.
)
err = rows.Scan(&id, &title, &body, &createdAt)
if err != nil {
break
}
articles = append(articles, model.Article{
ID: id,
ID: uint64(id),
Title: title,
Body: body,
CreatedAt: uint64(createdAt),
Expand Down
2 changes: 1 addition & 1 deletion examples/d1-blog-server/app/model/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package model

//easyjson:json
type Article struct {
ID string `json:"id"`
ID uint64 `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
CreatedAt uint64 `json:"createdAt"`
Expand Down
4 changes: 2 additions & 2 deletions examples/d1-blog-server/app/model/model_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions examples/d1-blog-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@ require (

replace github.com/syumai/workers => ../../

require (
github.com/google/uuid v1.3.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
)
require github.com/josharian/intern v1.0.0 // indirect
2 changes: 0 additions & 2 deletions examples/d1-blog-server/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Migration number: 0000 2023-01-09T14:48:53.705Z
CREATE TABLE articles (
id TEXT NOT NULL,
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
body TEXT NOT NULL,
created_at INTEGER NOT NULL
Expand Down
13 changes: 13 additions & 0 deletions examples/d1-blog-server/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DROP TABLE IF EXISTS articles;
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
body TEXT NOT NULL,
created_at INTEGER NOT NULL
);
CREATE INDEX idx_articles_on_created_at ON articles (created_at DESC);
INSERT INTO articles (title, body, created_at) VALUES (
'title of example post',
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
unixepoch()
);
10 changes: 1 addition & 9 deletions examples/d1-blog-server/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@ command = "make build"
binding = "BlogDB"
database_name = "d1-blog-server"
database_id = "c6d5e4c0-3134-42fa-834c-812b24fea3cf"
preview_database_id = "9f44099e-a008-45c2-88e4-aed0bd754a86"

[[ d1_databases ]]
# workaround to migrate preview DB
# - https://github.com/cloudflare/wrangler2/issues/2446
binding = "BlogDB-Preview"
database_name = "d1-blog-server-preview"
database_id = "9f44099e-a008-45c2-88e4-aed0bd754a86"
preview_database_id = "9f44099e-a008-45c2-88e4-aed0bd754a86"
preview_database_id = "777e3713-4386-438e-ba71-30d2f772750d"

0 comments on commit 4e34fba

Please sign in to comment.