Skip to content

Commit

Permalink
Merge pull request #73 from /issues/33
Browse files Browse the repository at this point in the history
feat: seedするスクリプトを追加
  • Loading branch information
SatooRu65536 authored Nov 12, 2024
2 parents de76cae + 9235948 commit 2a731b4
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 68 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"build": "next build",
"start": "next start",
"format": "prettier --write ./src/",
"lint": "eslint --ext .ts,.tsx ./src"
"lint": "eslint --ext .ts,.tsx ./src",
"seed:no-rest": "dotenvx run --env-file=.env.local -- jiti ./supabase/seed/index.ts",
"seed": "dotenvx run --env-file=.env.local -- jiti ./supabase/seed/index.ts",
"preseed": "yarn supabase db reset"
},
"dependencies": {
"@supabase/ssr": "0.4.0",
Expand All @@ -19,6 +22,7 @@
},
"devDependencies": {
"@types/node": "^20",
"@types/pg": "^8.11.10",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/react-syntax-highlighter": "^15",
Expand All @@ -37,6 +41,8 @@
"eslint-plugin-react-hooks": "4.6.2",
"eslint-plugin-react-refresh": "0.4.9",
"eslint-plugin-unused-imports": "4.0.1",
"jiti": "^2.4.0",
"pg": "^8.13.1",
"prettier": "3.3.3",
"sass": "1.77.8",
"supabase": "1.187.10",
Expand Down
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"
65 changes: 0 additions & 65 deletions supabase/seed.sql
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);
15 changes: 15 additions & 0 deletions supabase/seed/client.ts
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;
55 changes: 55 additions & 0 deletions supabase/seed/data.ts
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[];
54 changes: 54 additions & 0 deletions supabase/seed/index.ts
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();
14 changes: 14 additions & 0 deletions supabase/seed/type.ts
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];
}
Loading

0 comments on commit 2a731b4

Please sign in to comment.