Skip to content

Commit

Permalink
feat: use prisma
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Apr 9, 2023
1 parent 778f58b commit 6c300d6
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 159 deletions.
4 changes: 1 addition & 3 deletions packages/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
},
"dependencies": {
"animegarden": "workspace:*",
"itty-router": "^3.0.12",
"kysely": "^0.24.2",
"kysely-d1": "^0.3.0"
"itty-router": "^3.0.12"
},
"devDependencies": {
"@prisma/client": "^4.12.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Warnings:
- You are about to drop the column `teamId` on the `Resource` table. All the data in the column will be lost.
- You are about to drop the column `userId` on the `Resource` table. All the data in the column will be lost.
- Added the required column `publisherId` to the `Resource` table without a default value. This is not possible if the table is not empty.
*/
-- DropForeignKey
ALTER TABLE `Resource` DROP FOREIGN KEY `Resource_teamId_fkey`;

-- DropForeignKey
ALTER TABLE `Resource` DROP FOREIGN KEY `Resource_userId_fkey`;

-- AlterTable
ALTER TABLE `Resource` DROP COLUMN `teamId`,
DROP COLUMN `userId`,
ADD COLUMN `fansubId` INTEGER NULL,
ADD COLUMN `publisherId` INTEGER NOT NULL;

-- AddForeignKey
ALTER TABLE `Resource` ADD CONSTRAINT `Resource_fansubId_fkey` FOREIGN KEY (`fansubId`) REFERENCES `Team`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Resource` ADD CONSTRAINT `Resource_publisherId_fkey` FOREIGN KEY (`publisherId`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
20 changes: 10 additions & 10 deletions packages/worker/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ model User {
}

model Resource {
title String
href String @id
type String
magnet String
size String
teamId Int
userId Int
fansub Team? @relation(fields: [teamId], references: [id])
publisher User @relation(fields: [userId], references: [id])
createdAt Int
title String
href String @id
type String
magnet String
size String
fansubId Int?
publisherId Int
fansub Team? @relation(fields: [fansubId], references: [id])
publisher User @relation(fields: [publisherId], references: [id])
createdAt Int
}
8 changes: 0 additions & 8 deletions packages/worker/src/database.ts

This file was deleted.

67 changes: 28 additions & 39 deletions packages/worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,53 @@
import { Router } from 'itty-router';
import { PrismaClient } from '@prisma/client/edge';

import type { Env } from './types';

import { makeDatabase } from './database';
import { handleScheduled } from './scheduled';

const prisma = new PrismaClient();

const router = Router();

router.get('/', () => makeResponse({ message: 'This is AnimeGarden' }));

router.get('/resources', async (request, env: Env) => {
const database = makeDatabase(env.database);

const params = resolveParams();
if (!params) {
return makeErrorResponse({ message: 'Request is not valid' });
}
const { page, pageSize } = params;

const sql = database
.selectFrom('resource')
.innerJoin('user', 'user.id', 'resource.publisher')
.leftJoin('team', 'team.id', 'resource.fansub')
.select([
'title',
'href',
'type',
'magnet',
'size',
'user.id as publisher_id',
'user.name as publisher_name',
'team.id as fansub_id',
'team.name as fansub_name',
'createdAt'
])
.offset((page - 1) * pageSize)
.limit(pageSize)
.orderBy('createdAt', 'desc');

const resources = (await sql.execute()).map((r) => ({
const plan = await prisma.resource.findMany({
skip: (page - 1) * pageSize,
take: pageSize,
orderBy: {
createdAt: 'desc'
},
include: {
fansub: true,
publisher: true
}
});

const resources = plan.map((r) => ({
title: r.title,
href: r.href,
type: r.type,
magnet: r.magnet,
size: r.size,
createdAt: new Date(r.createdAt),
fansub:
r.fansub_id && r.fansub_name
? {
id: r.fansub_id,
name: r.fansub_name,
href: `https://share.dmhy.org/topics/list/team_id/${r.fansub_id}`
}
: undefined,
fansub: r.fansub
? {
id: r.fansub.id,
name: r.fansub.name,
href: `https://share.dmhy.org/topics/list/team_id/${r.fansub.id}`
}
: undefined,
publisher: {
id: r.publisher_id,
name: r.publisher_name,
href: `https://share.dmhy.org/topics/list/user_id/${r.publisher_id}`
id: r.publisher.id,
name: r.publisher.name,
href: `https://share.dmhy.org/topics/list/user_id/${r.publisher.id}`
}
}));

Expand All @@ -83,14 +74,12 @@ router.put('/resources', async (request, env: Env) => {
});

router.get('/users', async (request, env: Env) => {
const database = makeDatabase(env.database);
const users = await database.selectFrom('user').selectAll().execute();
const users = await prisma.user.findMany();
return makeResponse({ users });
});

router.get('/teams', async (request, env: Env) => {
const database = makeDatabase(env.database);
const teams = await database.selectFrom('team').selectAll().execute();
const teams = await prisma.team.findMany();
return makeResponse({ teams });
});

Expand Down
82 changes: 40 additions & 42 deletions packages/worker/src/scheduled.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { makeDatabase } from './database';
import type { Env } from './types';

import { fetchPage } from 'animegarden';
import { PrismaClient } from '@prisma/client/edge';

const prisma = new PrismaClient();

export async function handleScheduled(env: Env) {
const database = makeDatabase(env.database);
const teams = new Set<number>();
const users = new Set<number>();

Expand All @@ -14,61 +15,58 @@ export async function handleScheduled(env: Env) {
// Check teams and users
for (const r of res) {
if (!users.has(+r.publisher.id)) {
const u = await database
.selectFrom('user')
.selectAll()
.where('id', '=', +r.publisher.id)
.executeTakeFirst();
const u = await prisma.user.findUnique({
where: {
id: +r.publisher.id
}
});
if (u) {
users.add(u.id);
} else {
await database
.insertInto('user')
.values({ id: +r.publisher.id, name: r.publisher.name })
.executeTakeFirst();
await prisma.user.create({
data: {
id: +r.publisher.id,
name: r.publisher.name
}
});
users.add(+r.publisher.id);
}
}
if (r.fansub && !teams.has(+r.fansub.id)) {
const u = await database
.selectFrom('team')
.selectAll()
.where('id', '=', +r.fansub.id)
.executeTakeFirst();
const u = await prisma.team.findUnique({
where: {
id: +r.fansub.id
}
});
if (u) {
teams.add(u.id);
} else {
await database
.insertInto('team')
.values({ id: +r.fansub.id, name: r.fansub.name })
.executeTakeFirst();
await prisma.team.create({
data: {
id: +r.fansub.id,
name: r.fansub.name
}
});
teams.add(+r.fansub.id);
}
}
}

const query = database
.insertInto('resource')
.values(
res.map((r) => ({
title: r.title,
href: r.href,
type: r.type,
magnet: r.magnet,
size: r.size,
// Convert to UTC+8
createdAt: new Date(r.createdAt).getTime() - 8 * 60 * 60 * 1000,
fansub: r.fansub ? +r.fansub.id : undefined,
publisher: +r.publisher.id
}))
)
.onConflict((oc) => oc.doNothing());
const insert = await query.execute();
const inserted = insert.reduce((acc, r) => acc + (r.numInsertedOrUpdatedRows ?? 0n), 0n);
if (inserted === 0n) {
break;
}
const { count } = await prisma.resource.createMany({
data: res.map((r) => ({
title: r.title,
href: r.href,
type: r.type,
magnet: r.magnet,
size: r.size,
// Convert to UTC+8
createdAt: new Date(r.createdAt).getTime() - 8 * 60 * 60 * 1000,
fansubId: r.fansub ? +r.fansub.id : undefined,
publisherId: +r.publisher.id
})),
skipDuplicates: true
});

console.log(`There are ${inserted} resources inserted`);
console.log(`There are ${count} resources inserted`);
}
}
38 changes: 0 additions & 38 deletions packages/worker/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,3 @@
export interface Env {
database: D1Database;
}

export interface TeamTable {
id: number;

name: string;
}

export interface UserTable {
id: number;

name: string;
}

export interface ResourceTable {
title: string;

href: string;

type: string;

magnet: string;

size: string;

fansub?: number;

publisher: number;

createdAt: number;
}

export interface Database {
user: UserTable;

team: TeamTable;

resource: ResourceTable;
}
19 changes: 0 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c300d6

Please sign in to comment.