-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from /issues/33
feat: seedするスクリプトを追加
- Loading branch information
Showing
8 changed files
with
296 additions
and
68 deletions.
There are no files selected for viewing
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
6 changes: 4 additions & 2 deletions
6
supabase/migrations/20241029092513_set-user-table-uid-primary-key.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
alter table "public"."user" drop constraint "user_pkey" | ||
drop index if exists "public"."user_pkey" | ||
alter table "public"."user" | ||
drop constraint "user_pkey"; | ||
|
||
drop index if exists "public"."user_pkey" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +0,0 @@ | ||
-- user を追加する | ||
INSERT INTO | ||
"user" (uid, name, created_at) | ||
VALUES | ||
( | ||
'12000000-0000-0000-0000-000000000000', | ||
'Mary', | ||
NOW () | ||
); | ||
|
||
-- language を追加する | ||
INSERT INTO | ||
"language" (name) | ||
VALUES | ||
('TypeScript'); | ||
|
||
INSERT INTO | ||
"language" (name) | ||
VALUES | ||
('Python'); | ||
|
||
-- post を追加する | ||
INSERT INTO | ||
"post" (code, description, lang_id, title, user_uid) | ||
VALUES | ||
( | ||
'console.log("Hello, World!");', | ||
'Hello, World! を表示する', | ||
1, | ||
'Hello, World! by ts', | ||
'12000000-0000-0000-0000-000000000000' | ||
); | ||
|
||
INSERT INTO | ||
"post" (code, description, lang_id, title, user_uid) | ||
VALUES | ||
( | ||
'print("Hello, World!")', | ||
'Hello, World! を表示する', | ||
2, | ||
'Hello, World! by python', | ||
'12000000-0000-0000-0000-000000000000' | ||
); | ||
|
||
-- post_tag を追加する | ||
INSERT INTO | ||
"post_tag" (post_id, tag) | ||
VALUES | ||
(1, 'React'); | ||
|
||
INSERT INTO | ||
"post_tag" (post_id, tag) | ||
VALUES | ||
(2, 'CPython'); | ||
|
||
-- crazy_score を追加する | ||
INSERT INTO | ||
"crazy_score" (checked_user_uid, post_id, score) | ||
VALUES | ||
('12000000-0000-0000-0000-000000000000', 1, 5); | ||
|
||
INSERT INTO | ||
"crazy_score" (checked_user_uid, post_id, score) | ||
VALUES | ||
('12000000-0000-0000-0000-000000000000', 2, 3); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createClient } from '@supabase/supabase-js'; | ||
|
||
const { SEED_SUPABASE_URL, SEED_SERVICE_ROLE_KEY } = process.env; | ||
|
||
if (SEED_SUPABASE_URL === undefined) throw new Error('SEED_SUPABASE_URL is not defined'); | ||
if (SEED_SERVICE_ROLE_KEY === undefined) throw new Error('SEED_SERVICE_ROLE_KEY is not defined'); | ||
|
||
const supabase = createClient(SEED_SUPABASE_URL, SEED_SERVICE_ROLE_KEY, { | ||
auth: { | ||
autoRefreshToken: false, | ||
persistSession: false, | ||
}, | ||
}); | ||
|
||
export const adminAuthClient = supabase.auth.admin; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import type { Post, User } from './type'; | ||
|
||
export const USERS = [ | ||
{ | ||
uid: '00000000-0000-0000-0000-000000000000', | ||
name: 'Alice', | ||
}, | ||
{ | ||
uid: '00000000-0000-0000-0000-000000000001', | ||
name: 'Bob', | ||
}, | ||
{ | ||
uid: '00000000-0000-0000-0000-000000000002', | ||
name: 'Charlie', | ||
}, | ||
] as const satisfies User[]; | ||
|
||
export const LANGUAGES = [ | ||
'TypeScript', | ||
'JavaScript', | ||
'HTML', | ||
'CSS', | ||
'Python', | ||
'Ruby', | ||
'Java', | ||
'C', | ||
'C++', | ||
'C#', | ||
'Go', | ||
'Rust', | ||
'Swift', | ||
'Kotlin', | ||
'Scala', | ||
'Perl', | ||
'PHP', | ||
'Shell', | ||
'R', | ||
] as const satisfies string[]; | ||
|
||
export const POSTS = [ | ||
{ | ||
user_uid: USERS[0].uid, | ||
title: 'Hello, world with TypeScript!', | ||
description: 'This is a simple "Hello, world!" program in TypeScript.', | ||
code: 'console.log("Hello, world!");', | ||
lang: 'TypeScript', | ||
}, | ||
{ | ||
user_uid: USERS[1].uid, | ||
title: 'Hello, world with Python!', | ||
description: 'This is a simple "Hello, world!" program in Python.', | ||
code: 'print("Hello, world!")', | ||
lang: 'Python', | ||
}, | ||
] as const satisfies Post[]; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Client } from 'pg'; | ||
import { LANGUAGES, POSTS, USERS } from './data'; | ||
import type { Post, User } from './type'; | ||
|
||
const client = new Client({ | ||
host: process.env.SEED_SUPABASE_HOST, | ||
database: process.env.SEED_SUPABASE_NAME, | ||
port: Number(process.env.SEED_SUPABASE_PORT), | ||
user: process.env.SEED_SUPABASE_USER, | ||
password: process.env.SEED_SUPABASE_PASS, | ||
}); | ||
|
||
async function addUsers(users: User[]) { | ||
await client.query(` | ||
INSERT INTO "user" (uid, name) | ||
VALUES ${users.map((user) => `('${user.uid}', '${user.name}')`).join(', ')} | ||
`); | ||
} | ||
|
||
async function addLanguages(languages: string[]) { | ||
await client.query(` | ||
INSERT INTO language (name) | ||
VALUES ${languages.map((lang) => `('${lang}')`).join(', ')} | ||
`); | ||
} | ||
|
||
async function addPost(posts: Post[]) { | ||
await Promise.all( | ||
posts.map(async (post) => { | ||
const res = await client.query(`SELECT id FROM language WHERE name = '${post.lang}'`); | ||
const langId = res.rows[0].id as number; | ||
await client.query(` | ||
INSERT INTO post (user_uid, title, description, code, lang_id) | ||
VALUES ('${post.user_uid}', '${post.title}', '${post.description}', '${post.code}', ${langId}) | ||
`); | ||
}), | ||
); | ||
} | ||
|
||
async function seed() { | ||
await addUsers(USERS); | ||
await addLanguages(LANGUAGES); | ||
await addPost(POSTS); | ||
} | ||
|
||
async function main() { | ||
await client.connect(); | ||
|
||
await seed(); | ||
|
||
await client.end(); | ||
} | ||
|
||
void main(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { LANGUAGES, USERS } from './data'; | ||
|
||
export interface User { | ||
uid: string; | ||
name: string; | ||
} | ||
|
||
export interface Post { | ||
user_uid: (typeof USERS)[number]['uid']; | ||
title: string; | ||
description: string; | ||
code: string; | ||
lang: (typeof LANGUAGES)[number]; | ||
} |
Oops, something went wrong.