Skip to content

Commit fe399da

Browse files
committed
use random name instead of random id
1 parent 23e8a2c commit fe399da

File tree

11 files changed

+93
-14
lines changed

11 files changed

+93
-14
lines changed

drizzle.config.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import type { Config } from 'drizzle-kit'
2+
import * as dotenv from 'dotenv'
23

3-
import { env } from '@app/lib/env'
4+
// import { env } from '@app/lib/env'
5+
6+
dotenv.config({ path: '.env.development' })
7+
8+
if (!process.env.SQLITE_DATABASE_NAME) {
9+
throw Error(`process.env.SQLITE_DATABASE_NAME isn't set!`)
10+
}
411

512
// only ran in development so no need to dd a production check
6-
export const url = `${env.SQLITE_DATABASE_NAME}`
13+
export const url = `${process.env.SQLITE_DATABASE_NAME}`
714

815
export default {
916
schema: './src/app/db/schema.ts',

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"db:migrate:prod": "node --env-file .env.production ./scripts/drizzle-migrate.mjs"
2020
},
2121
"dependencies": {
22+
"@ngneat/falso": "^7.2.0",
2223
"@t3-oss/env-nextjs": "^0.9.2",
2324
"better-sqlite3": "^9.4.3",
2425
"drizzle-orm": "^0.29.4",
@@ -34,6 +35,7 @@
3435
"@types/node": "^20.11.20",
3536
"@types/react": "^18.2.58",
3637
"@types/react-dom": "^18.2.19",
38+
"dotenv": "^16.4.5",
3739
"drizzle-kit": "^0.20.14",
3840
"knip": "^5.0.2",
3941
"rimraf": "^5.0.5",

pnpm-lock.yaml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/run.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ set -e
44
# Set the directory of the database in a variable
55
DB_PATH=/data/users.prod.sqlite
66

7-
# Copied from https://pranavmalvawala.com/run-script-only-on-first-start-up
7+
# Set to true only when you want to run the migrate script, i.e, when changing database schema
8+
# Automate it by only setting it to true by tracking `entries.idx` in `migrations/meta/_journal.json`
9+
MIGRATE_DATABASE=false
10+
11+
# Inspired by https://pranavmalvawala.com/run-script-only-on-first-start-up & https://serverfault.com/a/1134812/1078165
812
CONTAINER_FIRST_STARTUP="CONTAINER_FIRST_STARTUP"
9-
if [ ! -e /data/$CONTAINER_FIRST_STARTUP ]; then
13+
if [[ ! -e /data/$CONTAINER_FIRST_STARTUP || $MIGRATE_DATABASE ]]; then
1014
# Place your script that you only want to run on first startup.
1115
echo 'Initialize database first time only'
1216
touch /data/$CONTAINER_FIRST_STARTUP

scripts/seed/insert.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
22

33
import { db } from '@app/db/index'
44
import { userTable } from '@app/db/schema'
5-
import { getId } from '@app/lib/utils'
5+
import { randomNameGenerator } from '@app/lib/utils'
66

77
const seedAdmin = async (db: BetterSQLite3Database<Record<string, never>>) => {
88
const userData: (typeof userTable.$inferInsert)[] = [
9-
{ id: getId() },
10-
{ id: getId() },
9+
{ name: randomNameGenerator() },
10+
{ name: randomNameGenerator() },
1111
]
1212
const users = await db.insert(userTable).values(userData).returning()
1313
console.log({ users })

src/app/api/add/route.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { NextResponse } from 'next/server'
22

33
import { db } from '@app/db/index'
44
import { userTable } from '@app/db/schema'
5-
import { getId } from '@app/lib/utils'
5+
import { randomNameGenerator } from '@app/lib/utils'
66

77
// needed otherwise next.js yells SqliteError: no such table: user
88
export const dynamic = 'force-dynamic'
99

1010
export const POST = async () => {
1111
const type = 'POST /api/add'
1212
console.log(type)
13-
const id = getId()
13+
const name = randomNameGenerator()
1414
try {
15-
const users = await db.insert(userTable).values({ id }).returning()
15+
const users = await db.insert(userTable).values({ name }).returning()
1616
return NextResponse.json({ type, users })
1717
} catch (err) {
1818
console.error(err)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE `user` RENAME COLUMN `id` TO `name`;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"version": "5",
3+
"dialect": "sqlite",
4+
"id": "df7d8b1f-b0f7-48ff-987f-2f6bfabd21b6",
5+
"prevId": "fc517c98-8089-42cf-b391-e1b3aa95abda",
6+
"tables": {
7+
"user": {
8+
"name": "user",
9+
"columns": {
10+
"name": {
11+
"name": "name",
12+
"type": "text",
13+
"primaryKey": true,
14+
"notNull": true,
15+
"autoincrement": false
16+
}
17+
},
18+
"indexes": {},
19+
"foreignKeys": {},
20+
"compositePrimaryKeys": {},
21+
"uniqueConstraints": {}
22+
}
23+
},
24+
"enums": {},
25+
"_meta": {
26+
"schemas": {},
27+
"tables": {},
28+
"columns": {
29+
"\"user\".\"id\"": "\"user\".\"name\""
30+
}
31+
}
32+
}

src/app/db/migrations/meta/_journal.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"when": 1708147002816,
99
"tag": "0000_shallow_wild_pack",
1010
"breakpoints": true
11+
},
12+
{
13+
"idx": 1,
14+
"version": "5",
15+
"when": 1709044448310,
16+
"tag": "0001_sturdy_eddie_brock",
17+
"breakpoints": true
1118
}
1219
]
1320
}

src/app/db/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { text, sqliteTable } from 'drizzle-orm/sqlite-core'
22

33
export const userTable = sqliteTable('user', {
4-
id: text('id').primaryKey(),
4+
name: text('name').primaryKey(),
55
})

0 commit comments

Comments
 (0)