mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
wip
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
datasource db {
|
datasource db {
|
||||||
provider = "mysql"
|
provider = "postgresql"
|
||||||
url = env("DATABASE_URL")
|
url = env("DATABASE_URL")
|
||||||
|
// uncomment next line if you use Prisma <5.10
|
||||||
|
directUrl = env("DATABASE_URL_UNPOOLED")
|
||||||
relationMode = "prisma"
|
relationMode = "prisma"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,8 +44,6 @@ model user {
|
|||||||
revokedToken revokedToken[]
|
revokedToken revokedToken[]
|
||||||
RefreshToken refreshToken[]
|
RefreshToken refreshToken[]
|
||||||
resetPassword resetPassword?
|
resetPassword resetPassword?
|
||||||
|
|
||||||
@@fulltext([name, email])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model revokedToken {
|
model revokedToken {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export class DecksRepository {
|
|||||||
|
|
||||||
async findMinMaxCards(): Promise<{ min: number; max: number }> {
|
async findMinMaxCards(): Promise<{ min: number; max: number }> {
|
||||||
const result = await this.prisma
|
const result = await this.prisma
|
||||||
.$queryRaw`SELECT MAX(card_count) as maxCardsCount, MIN(card_count) as minCardsCount FROM (SELECT deck.id, COUNT(card.id) as card_count FROM deck LEFT JOIN card ON deck.id = card.deckId GROUP BY deck.id) AS card_counts;`
|
.$queryRaw`SELECT MAX(card_count) as maxCardsCount, MIN(card_count) as minCardsCount FROM (SELECT deck.id, COUNT(card.id) as card_count FROM deck LEFT JOIN card ON deck.id = card."deckId" GROUP BY deck.id) AS card_counts;`
|
||||||
|
|
||||||
return {
|
return {
|
||||||
max: Number(result[0].maxCardsCount),
|
max: Number(result[0].maxCardsCount),
|
||||||
@@ -111,9 +111,10 @@ export class DecksRepository {
|
|||||||
// Prepare the where clause conditions
|
// Prepare the where clause conditions
|
||||||
const conditions = []
|
const conditions = []
|
||||||
|
|
||||||
if (name) conditions.push(`d.name LIKE CONCAT('%', ?, '%')`)
|
if (name) conditions.push(`d.name LIKE ('%' || ? || '%')`)
|
||||||
if (authorId) conditions.push(`d.userId = ?`)
|
if (authorId) conditions.push(`d."userId" = ?`)
|
||||||
if (userId) conditions.push(`(d.isPrivate = FALSE OR (d.isPrivate = TRUE AND d.userId = ?))`)
|
if (userId)
|
||||||
|
conditions.push(`(d."isPrivate" = FALSE OR (d."isPrivate" = TRUE AND d."userId" = ?))`)
|
||||||
|
|
||||||
// Prepare the having clause for card count range
|
// Prepare the having clause for card count range
|
||||||
const havingConditions = []
|
const havingConditions = []
|
||||||
@@ -125,17 +126,30 @@ export class DecksRepository {
|
|||||||
const query = `
|
const query = `
|
||||||
SELECT
|
SELECT
|
||||||
d.*,
|
d.*,
|
||||||
COUNT(c.id) AS cardsCount,
|
COUNT(c.id) AS "cardsCount",
|
||||||
a.id AS authorId,
|
a."id" AS "authorId",
|
||||||
a.name AS authorName
|
a."name" AS "authorName"
|
||||||
FROM deck AS d
|
FROM deck AS "d"
|
||||||
LEFT JOIN card AS c ON d.id = c.deckId
|
LEFT JOIN "card" AS c ON d."id" = c."deckId"
|
||||||
LEFT JOIN user AS a ON d.userId = a.id
|
LEFT JOIN "user" AS a ON d."userId" = a.id
|
||||||
${conditions.length ? `WHERE ${conditions.join(' AND ')}` : ''}
|
${
|
||||||
GROUP BY d.id
|
conditions.length
|
||||||
${havingConditions.length ? `HAVING ${havingConditions.join(' AND ')}` : ''}
|
? `WHERE ${conditions.map((_, index) => `${_.replace('?', `$${index + 1}`)}`).join(' AND ')}`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
|
GROUP BY d."id", a."id"
|
||||||
|
${
|
||||||
|
havingConditions.length
|
||||||
|
? `HAVING ${havingConditions
|
||||||
|
.map((_, index) => `${_.replace('?', `$${conditions.length + index + 1}`)}`)
|
||||||
|
.join(' AND ')}`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
ORDER BY ${orderField} ${orderDirection}
|
ORDER BY ${orderField} ${orderDirection}
|
||||||
LIMIT ? OFFSET ?;
|
LIMIT $${conditions.length + havingConditions.length + 1} OFFSET $${
|
||||||
|
conditions.length + havingConditions.length + 2
|
||||||
|
};
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
// Parameters for fetching decks
|
// Parameters for fetching decks
|
||||||
@@ -164,10 +178,22 @@ export class DecksRepository {
|
|||||||
FROM (
|
FROM (
|
||||||
SELECT d.id
|
SELECT d.id
|
||||||
FROM deck AS d
|
FROM deck AS d
|
||||||
LEFT JOIN card AS c ON d.id = c.deckId
|
LEFT JOIN card AS c ON d.id = c."deckId"
|
||||||
${conditions.length ? `WHERE ${conditions.join(' AND ')}` : ''}
|
${
|
||||||
|
conditions.length
|
||||||
|
? `WHERE ${conditions
|
||||||
|
.map((_, index) => `${_.replace('?', `$${index + 1}`)}`)
|
||||||
|
.join(' AND ')}`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
GROUP BY d.id
|
GROUP BY d.id
|
||||||
${havingConditions.length ? `HAVING ${havingConditions.join(' AND ')}` : ''}
|
${
|
||||||
|
havingConditions.length
|
||||||
|
? `HAVING ${havingConditions
|
||||||
|
.map((_, index) => `${_.replace('?', `$${conditions.length + index + 1}`)}`)
|
||||||
|
.join(' AND ')}`
|
||||||
|
: ''
|
||||||
|
}
|
||||||
) AS subquery;
|
) AS subquery;
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|||||||
@@ -3,37 +3,39 @@ import { PrismaClient } from '@prisma/client'
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PrismaService extends PrismaClient implements OnModuleInit {
|
export class PrismaService extends PrismaClient implements OnModuleInit {
|
||||||
// constructor() {
|
constructor() {
|
||||||
// super({
|
super({
|
||||||
// log: [
|
log: [
|
||||||
// {
|
{
|
||||||
// emit: 'stdout',
|
emit: 'stdout',
|
||||||
// level: 'query',
|
level: 'query',
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// emit: 'stdout',
|
emit: 'stdout',
|
||||||
// level: 'error',
|
level: 'error',
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// emit: 'stdout',
|
emit: 'stdout',
|
||||||
// level: 'info',
|
level: 'info',
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// emit: 'stdout',
|
emit: 'stdout',
|
||||||
// level: 'warn',
|
level: 'warn',
|
||||||
// },
|
},
|
||||||
// ],
|
],
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
|
|
||||||
async onModuleInit() {
|
async onModuleInit() {
|
||||||
await this.$connect()
|
await this.$connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
private exitHandler(app: INestApplication) {
|
private exitHandler(app: INestApplication) {
|
||||||
return async () => {
|
return async () => {
|
||||||
await app.close()
|
await app.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async enableShutdownHooks(app: INestApplication) {
|
async enableShutdownHooks(app: INestApplication) {
|
||||||
process.on('exit', this.exitHandler(app))
|
process.on('exit', this.exitHandler(app))
|
||||||
process.on('beforeExit', this.exitHandler(app))
|
process.on('beforeExit', this.exitHandler(app))
|
||||||
|
|||||||
Reference in New Issue
Block a user