Skip to content

Commit

Permalink
データベース接続とスキーマの更新
Browse files Browse the repository at this point in the history
- PostgreSQL接続の設定を有効化し、SQLite3の接続をコメントアウトしました。
- スキーマのオブジェクト名を変更し、新しいフィールドを追加しました(例: UserTable, PostTable)。
- シーダーのロジックを改善し、非同期処理を適切に使用しました。
- 不要なコメントを削除し、コードの可読性を向上させました。
  • Loading branch information
itsumura-h committed Jan 14, 2025
1 parent 0d53bb6 commit 8694d41
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 59 deletions.
4 changes: 2 additions & 2 deletions example/database/connection.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let
maxConnections = getEnv("DB_MAX_CONNECTION").parseInt
timeout = getEnv("DB_TIMEOUT").parseInt

let rdb* = dbOpen(SQLite3, "./db.sqlite3", shouldDisplayLog=true)
# let rdb* = dbOpen(PostgreSQL, database, user, password, pgHost, pgPort, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(SQLite3, "./db.sqlite3", shouldDisplayLog=true)
let rdb* = dbOpen(PostgreSQL, database, user, password, pgHost, pgPort, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(Mariadb, database, user, password, mariaHost, myPort, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(Mariadb, database, user, password, mariaHost, myPort, maxConnections, timeout, shouldDisplayLog=true)
# let rdb* = dbOpen(SurrealDB, "test", "test", "user", "pass", "http://surreal", 8000, 5, 30, shouldDisplayLog=true).waitFor()
4 changes: 2 additions & 2 deletions example/database/develop.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
nim c -d:reset ./migrations/migrate.nim
# nim c database/seeder/develop
nim c ./seeder/develop

./migrations/migrate
# APP_ENV=develop ./database/seeder/develop
./seeder/develop
5 changes: 5 additions & 0 deletions example/database/production.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nim c ./migrations/migrate.nim
nim c database/seeder/production

./migrations/migrate
APP_ENV=production ./database/seeder/production
68 changes: 34 additions & 34 deletions example/database/schema.nim
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
import std/json

type IntRelation* = object
type IntRelationTable* = object
## IntRelation
id*: int


type StrRelation* = object
type StrRelationTable* = object
## StrRelation
uuid*: string


type User* = object
type UserTable* = object
## user
created_at*: int
email*: string
id*: string
name*: string
email*: string
password*: string
created_at*: int
updated_at*: int


type PostTable* = object
## post
id*: string
title*: string
content*: string
user_id*: string
created_at*: int
updated_at*: int


type Types* = object
type TypesTable* = object
## Types
id*: int
integer*: int
smallInteger*: int
mediumInteger*: int
bigInteger*: int
binary*: string
boolean*: bool
char*: string
created_at*: string
date*: string
datetime*: string
decimal*: float
deleted_at*: string
double*: float
enumField*: string
float*: float
id*: int
int_relation_id*: string
integer*: int
json*: JsonNode
longText*: string
mediumInteger*: int
mediumText*: string
smallInteger*: int
str_relation_id*: string
uuid*: string
char*: string
string*: string
text*: string
mediumText*: string
longText*: string
date*: string
datetime*: string
timestamp*: string
created_at*: string
updated_at*: string
uuid*: string


type Post* = object
## post
content*: string
created_at*: int
id*: string
title*: string
updated_at*: int
user_id*: string
deleted_at*: string
binary*: string
boolean*: bool
enumField*: string
json*: JsonNode
int_relation_id*: int
str_relation_id*: string
39 changes: 20 additions & 19 deletions example/database/seeder/data/post_seeder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import ../../connection
import ../../schema

proc postSeeder*() {.async.} =
let postCount = rdb.table("post").count().await
if postCount > 0:
return
seeder(rdb, "post"):
let postCount = rdb.table("post").count().await
if postCount > 0:
return

let users = rdb.table("user").get().orm(UserTable).await
if users.len == 0:
raise newException(ValueError, "No users found in database")
let users = rdb.table("user").get().orm(UserTable).await
if users.len == 0:
raise newException(ValueError, "No users found in database")

var postList: seq[JsonNode]
for i in 1..users.len:
let row = PostTable(
id: $genOid(),
title: &"post {i}",
content: &"content {i}",
userId: users[i-1].id,
createdAt: now().toTime().toUnix(),
updatedAt: now().toTime().toUnix()
)
postList.add(%row)

rdb.table("post").insert(postList).await
var postList: seq[JsonNode]
for i in 1..users.len:
let row = PostTable(
id: $genOid(),
title: &"post {i}",
content: &"content {i}",
userId: users[i-1].id,
createdAt: now().toTime().toUnix(),
updatedAt: now().toTime().toUnix()
)
postList.add(%row)
rdb.table("post").insert(postList).await
1 change: 1 addition & 0 deletions example/database/seeder/develop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ proc seed() {.async.} =
userSeeder().await
postSeeder().await


seed().waitFor()
5 changes: 5 additions & 0 deletions example/database/staging.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nim c -d:reset ./migrations/migrate.nim
nim c database/seeder/staging

./migrations/migrate
APP_ENV=staging ./database/seeder/staging
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ proc count*(self:PostgresQuery):Future[int] {.async.} =
sql = questionToDaller(sql)
self.log.logger(sql)
let response = self.getRow(sql).await

if response.isSome:
return response.get["aggregate"].getInt()
else:
Expand Down Expand Up @@ -639,11 +640,11 @@ template seeder*(rdb:PostgresConnections, tableName:string, body:untyped):untype
## The `seeder` block allows the code in the block to work only when the table is empty.
block:
if rdb.table(tableName).count().waitFor == 0:
body
`body`


template seeder*(rdb:PostgresConnections, tableName, column:string, body:untyped):untyped =
## The `seeder` block allows the code in the block to work only when the table or specified column is empty.
block:
if rdb.table(tableName).select(column).count().waitFor == 0:
body
`body`

0 comments on commit 8694d41

Please sign in to comment.