Skip to content

Commit

Permalink
いろいろな仕様を完成した
Browse files Browse the repository at this point in the history
fix build

fix

fix
  • Loading branch information
SuCicada committed Mar 23, 2024
1 parent 89235c8 commit 9b2cd26
Show file tree
Hide file tree
Showing 29 changed files with 1,143 additions and 250 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ next-env.d.ts
.idea
*.iml
package-lock.json
/log.txt
.env
/.vscode/
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
vercel-env-update:
node ./scripts/vercel-env-update.js

import_db:
ts-node -r tsconfig-paths/register --project tsconfig.scripts.json scripts/import_db.ts
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# japanese holy bible

npx prisma init

npx dotenv -e .env.production.local -- npx prisma db pull
npx prisma generate


ALTER TABLE bible
ADD CONSTRAINT unique_combination UNIQUE (book, chapter, verse);


next build
next start -p 3001
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"build": "prisma generate && next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"ts-node": "ts-node"
},
"dependencies": {
"@prisma/client": "^5.10.2",
"@vercel/postgres": "^0.7.2",
"dotenv": "^16.4.4",
"dotenv-cli": "^7.3.0",
"next": "14.1.0",
"pg": "^8.11.3",
"prisma": "^5.10.2",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@swc/core": "^1.4.4",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5"
}
Expand Down
33 changes: 33 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

/// The underlying table does not contain a valid unique identifier and can therefore currently not be handled by Prisma Client.
model test {
name String? @db.VarChar
@@ignore
}

model bible {
id Int @id @default(autoincrement())
book String? @db.VarChar
chapter Int?
verse Int?
text String?
furigana String?
@@unique([book, chapter, verse], map: "unique_combination")
}

model books {
id Int @id @default(autoincrement())
sort Int?
short_name String? @db.VarChar
long_name String? @db.VarChar
}
125 changes: 98 additions & 27 deletions scripts/import_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,107 @@ import dotenv from "dotenv";

import {Bible} from "@/app/bible/data";
import {getJapaneseFurigana} from "@/app/utils/service";
import {Client} from "pg";

import {Prisma, PrismaClient} from "@prisma/client";

let env_file = path.join(__dirname, '../.env.development.local');
dotenv.config({path: env_file});
let start = new Date().getTime();
(async () => {
const {rows} = await sql`SELECT *
FROM bible`;
for (const row of (rows as Bible[])) {
if (row.furigana) {
continue;
}
// row = row as Bible
// row as Bible
const data = await getJapaneseFurigana(row.text)
let res = ""
for (let item of data["data"]) {
const [character, type, hiragana, katakana] = item;
switch (type) {
case 1:
res += `{{${character}|${hiragana}}}`
break;
default:
res += character;
}
}
console.log(res);
await sql`UPDATE bible
SET furigana = ${res}
WHERE id = ${row.id}`;
const prisma = new PrismaClient();

async function import_furigana() {
const {rows} = await sql`SELECT *
FROM bible
where furigana is null`;
for (const row of (rows as Bible[])) {
if (row.furigana) {
continue;
}
// row = row as Bible
// row as Bible
const data = await getJapaneseFurigana(row.text)
let res = ""
for (let item of data["data"]) {
const [character, type, hiragana, katakana] = item;
switch (type) {
case 1:
res += `{{${character}|${hiragana}}}`
break;
default:
res += character;
}
}
console.log(res);
await sql`UPDATE bible
SET furigana = ${res}
WHERE id = ${row.id}`;
}
// console.log(rows);
console.log(`Time: ${new Date().getTime() - start}ms`);
}


async function split_multi(book: string, chapter: number, multi_words: string) {
// let book = "ヨハネによる福音書";
// let chapter = 1;
// let multi_words = `1初めに言があった。言は神と共にあった。言は神であった。 2この言は、初めに神と共にあった。 3万物は言によって成った。成ったもので、言によらずに成ったものは何一つなかった。 4言の内に命があった。命は人間を照らす光であった。`

let bibles: Bible[] = []
let words = Array.from(multi_words.trim().matchAll(/(\d+)(\W+)/g))
for (let w of words) {
let verse = parseInt(w[1])
let text = w[2].trim()
bibles.push({book, chapter, verse, text})

}
// let verse;
// let verse2;
// let m = multi_words.trim().match(/^(\d+)/)
// if (m) {
// verse = parseInt(m[1])
// while (true) {
// verse2 = verse + 1
// let a = multi_words.indexOf(verse.toString()) + verse.toString().length
// let b = multi_words.indexOf(verse2.toString())
// if (b === -1) {
// b = multi_words.length
// }
// let text = multi_words.slice(a, b).trim()
// console.log(text)
// bibles.push({book, chapter, verse, text})
//
// verse = verse2
// if (b === multi_words.length) {
// break
// }
// }
// }

if (bibles.length > 0) {
try {
let res = await prisma.bible.createMany({
data: bibles
});
console.log(res)
} catch (e) {
console.error(e);
}
// console.log(rows);
console.log(`Time: ${new Date().getTime() - start}ms`);
}
}

(async () => {
let book = "マタイによる福音書 4";
let multi_words = `
1さて、イエスは悪魔から誘惑を受けるため、“霊”に導かれて荒れ野に行かれた。
3すると、誘惑する者が来て、イエスに言った。「神の子なら、これらの石がパンになるように命じたらどうだ。」 4イエスはお答えになった。
「『人はパンだけで生きるものではない。
神の口から出る一つ一つの言葉で生きる』
と書いてある。」
`
let chapter = parseInt(book.split(' ')[1])
book = book.split(' ')[0]
await split_multi(book, chapter, multi_words);
await import_furigana();
})();
18 changes: 0 additions & 18 deletions src/app/api/bible.ts

This file was deleted.

62 changes: 62 additions & 0 deletions src/app/api/bible/[book]/[chapter]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// no-config
import { sql } from '@vercel/postgres';

import { createPool } from '@vercel/postgres';
import { NextResponse } from "next/server";

export const dynamic = 'force-dynamic'
import { Prisma, PrismaClient } from "@prisma/client";
import { Bible } from "@/app/bible/data";
// import {Bible} from "@/app/bible/data";

const prisma = new PrismaClient();

export type BibleIndex = {
book: string,
chapter: number
}

export async function GET(
request: Request,
{ params }: { params: { book: string, chapter: string } }
) {
console.log(params)
// console.log(process.env)
// console.log(process.env.POSTGRES_URL)
// console.log(process.env.NEXT_PUBLIC_POSTGRES_URL)
// const pool = createPool({
// connectionString: process.env.POSTGRES_URL,
// user: process.env.POSTGRES_USER,
// database: process.env.POSTGRES_DB,
// password: process.env.POSTGRES_PASSWORD,
// host: process.env.POSTGRES_HOST,
// port: process.env.POSTGRES_PORT,
// ssl: {IN
// rejectUnauthorized: false
// }
// });
// const id = 100;
// A one-shot query
// const {rows} = await sql`SELECT * FROM bible order by book,chapter,verse`;

let words = await prisma.bible.findMany({
where: {
book: params.book,
chapter: parseInt(params.chapter)
},
orderBy: {
verse: 'asc'
}
}) as Bible[]
// Multiple queries on the same connection (improves performance)
// warning: Do not share clients across requests and be sure to release them!
// const client = await sql.connect();
// // const { rows } = await client.sql`SELECT * FROM users WHERE id = ${userId};`;
// await client.sql`UPDATE users SET status = 'satisfied' WHERE id = ${userId};`;
// client.release();
let result = words
// console.log(result);
// res.status(200).json(result);
// return {data: result}
return NextResponse.json(result);
}
29 changes: 29 additions & 0 deletions src/app/api/book/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {NextResponse} from "next/server";

import {PrismaClient} from "@prisma/client";
import * as Prisma from "@prisma/client";

// export const dynamic = 'force-dynamic'
const prisma = new PrismaClient();

export type BibleBooks = {
oldBooks: Prisma.books[],
newBooks: Prisma.books[]
}

export async function GET(
request: Request,
) {
let books = await prisma.books.findMany({
orderBy: {
sort: 'asc'
}
}) as Prisma.books[]

let res : BibleBooks = {
oldBooks: books.filter(book => book.id < 39),
newBooks: books.filter(book => book.id >= 39)
}

return NextResponse.json<BibleBooks>(res);
}
26 changes: 26 additions & 0 deletions src/app/api/chapater/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextResponse } from "next/server";

import { PrismaClient } from "@prisma/client";
import * as Prisma from "@prisma/client";
import { sql } from "@vercel/postgres";

// export const dynamic = 'force-dynamic'
const prisma = new PrismaClient();


export async function GET(
request: Request,
// {params}: {params:{ book: string }}
) {
let params = new URL(request.url).searchParams
let book = params.get("book")
console.log(book)
let chapaters = await sql`select DISTINCT chapter from bible WHERE book = ${book} ORDER BY chapter;`;
let res: number[] = chapaters.rows.map(row => row.chapter)

// let res: BibleBooks = {
// oldBooks: books.filter(book => book.id < 39),
// newBooks: books.filter(book => book.id >= 39)
// }
return NextResponse.json<number[]>(res);
}
Loading

0 comments on commit 9b2cd26

Please sign in to comment.