Skip to content

OpenAPI

OpenAPIとsqlcの連携

Documentation for the go-server Generatorで生成したファイルとsqlc.dev | Compile SQL to type-safe Goで生成したファイル、うまく連携できればもっと手数少なくAPIサーバーが作れるのになー・・

現状用意するものとしては、DDLとqueryとAPI定義で、APIのリソースとDBのテーブルが1:1なら決め打ちでかける部分が出てくるはず・・

どちらに寄せるかは、OpenAPI定義側かなぁ。。 OpenAPI定義からDDLが生成できれば、queryはサーバーコードと一緒でも良いと思うし。

Read more

OpenAPI Generator go-server

Documentation for the go-server Generatorで、routerはmuxchiが選べると書いてあって、省略時はmuxとのこと。

chiの指定方法がわからなかったので調べた。

$ wget https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml
$ docker run --rm \
  -v ${PWD}:/local openapitools/openapi-generator-cli generate \
  -i /local/petstore.yaml \
  -g go-server \
  --additional-properties=router=chi \
  -o /local/out

と、--additional-propertiesにつけるらしい。、 他のパラメータ、たとえばsererPortも指定したい場合はカンマ区切りで--additional-properties=router=chi,sererPort=18080こう。

Read more

OpenAPI Generator

Generators Listを眺めていたら、mysql-schemaなんてのがあったので、試してみた。

$ wget https://raw.githubusercontent.com/openapitools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml
$ docker run --rm \
  -v ${PWD}:/local openapitools/openapi-generator-cli generate \
  -i /local/petstore.yaml \
  -g mysql-schema \
  -o /local/out
$ ls -R out/
out/:
Model  README.md  mysql_schema.sql

out/Model:
ApiResponse.sql  Category.sql  Order.sql  Pet.sql  Tag.sql  User.sql

out/mysql_schema.sqlにDDLが作成されていた。

out/Modelには、CRUD用SQLのテンプレートが作られていた。

petstore.yamlcomponents > schemasの定義がもとになっているのかな。

Read more