Skip to content

Commit

Permalink
feat: implement typedsql for raw queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Aug 27, 2024
1 parent e6e84f0 commit e7e0675
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 88 deletions.
68 changes: 16 additions & 52 deletions app/routes/app.$assetslug.$entry._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
json
} from '@remix-run/node'
import {Link, useLoaderData} from '@remix-run/react'
import {type Entry} from '@prisma/client'
import {
getEntryValues,

Check failure on line 9 in app/routes/app.$assetslug.$entry._index.tsx

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Module '"@prisma/client/sql"' has no exported member 'getEntryValues'.
getEntryRevisions,

Check failure on line 10 in app/routes/app.$assetslug.$entry._index.tsx

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Module '"@prisma/client/sql"' has no exported member 'getEntryRevisions'.
getEntryRelations

Check failure on line 11 in app/routes/app.$assetslug.$entry._index.tsx

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Module '"@prisma/client/sql"' has no exported member 'getEntryRelations'.
} from '@prisma/client/sql'

import {ensureUser} from '~/lib/utils/ensure-user'
import {getPrisma} from '~/lib/prisma.server'
Expand Down Expand Up @@ -39,39 +43,14 @@ export const loader = async ({request, params}: LoaderFunctionArgs) => {
})
)

const values = await time(
'getValues',
'Get Values',
() => prisma.$queryRaw<
Array<{
id: string
value: string
order: number
type: string
meta: string
fieldId: string
fieldName: string
}>
>`SELECT Value.id, Value.value, AssetField."order", Field.type, Field.meta, Value.fieldId, Field.name as fieldName FROM Value
INNER JOIN Entry ON Entry.Id = Value.entryId
INNER JOIN Asset on Asset.Id = Entry.assetId
INNER JOIN AssetField on AssetField.assetId = Asset.id AND AssetField.fieldId = Value.fieldId
INNER JOIN Field on Field.id = Value.fieldId
WHERE entryId = ${entry.id}
ORDER BY AssetField."order" ASC`
const values = await time('getValues', 'Get Values', () =>
prisma.$queryRawTyped(getEntryValues(entry.id))
)

const relations = await time(
'getRelations',
'Get Relations',
() => prisma.$queryRaw<
Array<
Entry & {value: string; slug: string; entryId: string; icon: string}
>
>`SELECT * FROM Entry
INNER JOIN Value value ON fieldId = (SELECT nameFieldId FROM Asset WHERE Asset.id = entry.assetId) AND entryId = Entry.id
INNER JOIN Asset ON Entry.assetId = Asset.id
WHERE Entry.id IN (SELECT entryId FROM Value WHERE value LIKE ${`%${entry.id}%`}) AND deleted = false`
const relations = await time('getRelations', 'Get Relations', () =>
prisma.$queryRawTyped(
getEntryRelations(`%${entry.id}%`, user.role, user.id)
)
)

const documents = await time('getDocuments', 'Get Documents', () =>
Expand All @@ -80,23 +59,8 @@ export const loader = async ({request, params}: LoaderFunctionArgs) => {
})
)

const revisions = await time(
'getRevisions',
'Get Revisions',
() => prisma.$queryRaw<
Array<{
id: string
createdAt: string
changeNote: string
fieldName: string
userName: string
}>
>`SELECT ValueHistory.id, ValueHistory.createdAt, ValueHistory.changeNote, Field.name as fieldName, User.name as userName FROM ValueHistory
INNER JOIN Value on Value.id = ValueHistory.valueId
INNER JOIN Field on Field.id = Value.fieldId
INNER JOIN User on User.id = ValueHistory.editedById
WHERE Value.entryId = ${params.entry}
ORDER BY ValueHistory.createdAt DESC`
const revisions = await time('getRevisions', 'Get Revisions', () =>
prisma.$queryRawTyped(getEntryRevisions(entry.id))
)

const name = values.reduce((n, v) => {
Expand Down Expand Up @@ -164,11 +128,11 @@ const AssetEntry = () => {
<div className="flex flex-wrap gap-2">
{relations.length === 0
? 'No Linked Entries'
: relations.map(({entryId, value, slug, icon}) => {
: relations.map(({id, value, slug, icon}) => {

Check failure on line 131 in app/routes/app.$assetslug.$entry._index.tsx

View workflow job for this annotation

GitHub Actions / ʦ TypeScript

Argument of type '({ id, value, slug, icon }: { id: any; value: any; slug: any; icon: any; }) => Element' is not assignable to parameter of type '(value: unknown, index: number, array: unknown[]) => Element'.
return (
<Link
key={entryId}
to={`/app/${slug}/${entryId}`}
key={id}
to={`/app/${slug}/${id}`}
className="bg-gray-300 p-2 rounded"
>
{icon} {value}
Expand Down
78 changes: 44 additions & 34 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"@arcath/utils": "^3.15.0",
"@epic-web/totp": "^1.1.2",
"@prisma/client": "^5.18.0",
"@prisma/client": "^5.19.0",
"@remix-run/node": "^2.11.1",
"@remix-run/react": "^2.11.1",
"@remix-run/serve": "^2.11.1",
Expand Down Expand Up @@ -68,7 +68,7 @@
"eslint-plugin-react-hooks": "^4.6.2",
"postcss": "^8.4.41",
"prettier": "^3.3.3",
"prisma": "^5.18.0",
"prisma": "^5.19.0",
"tailwindcss": "^3.4.10",
"typescript": "^5.5.4",
"vite-tsconfig-paths": "^4.3.2",
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

generator client {
provider = "prisma-client-js"
previewFeatures = ["typedSql"]
}

datasource db {
Expand Down
18 changes: 18 additions & 0 deletions prisma/sql/getEntryRelations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- @param {String} $1:entryId The ID of the entry to collect relations for
-- @param {String} $2:userRole The role of the current user
-- @param {String} $3:userId The ID of the current user
SELECT Entry.id, Value.value, Asset.icon, Asset.slug FROM Entry
INNER JOIN Value value ON fieldId = (SELECT nameFieldId FROM Asset WHERE Asset.id = entry.assetId) AND entryId = Entry.id
INNER JOIN Asset ON Entry.assetId = Asset.id
WHERE
Entry.id IN (SELECT entryId FROM Value WHERE value LIKE $1)
AND
deleted = false
AND
Entry.aclId IN (SELECT aclId FROM ACLEntry
WHERE read = true AND (
(type = "role" AND target = $2)
OR
(type = "user" AND target = $3)
)
)
7 changes: 7 additions & 0 deletions prisma/sql/getEntryRevisions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- @param {String} $1:entryId The ID of the entry to collect revisions for
SELECT ValueHistory.id, ValueHistory.createdAt, ValueHistory.changeNote, Field.name as fieldName, User.name as userName FROM ValueHistory
INNER JOIN Value on Value.id = ValueHistory.valueId
INNER JOIN Field on Field.id = Value.fieldId
INNER JOIN User on User.id = ValueHistory.editedById
WHERE Value.entryId = $1
ORDER BY ValueHistory.createdAt DESC
8 changes: 8 additions & 0 deletions prisma/sql/getEntryValues.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- @param {String} $1:entryId The ID of the entry to collect values for
SELECT Value.id, Value.value, AssetField."order", Field.type, Field.meta, Value.fieldId, Field.name as fieldName FROM Value
INNER JOIN Entry ON Entry.Id = Value.entryId
INNER JOIN Asset on Asset.Id = Entry.assetId
INNER JOIN AssetField on AssetField.assetId = Asset.id AND AssetField.fieldId = Value.fieldId
INNER JOIN Field on Field.id = Value.fieldId
WHERE entryId = $1
ORDER BY AssetField."order" ASC

0 comments on commit e7e0675

Please sign in to comment.