Skip to content

Commit

Permalink
typedSql
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbuechele committed Sep 10, 2024
1 parent a81c17f commit eef3d35
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start": "bun src/index.ts",
"dev": "bun --watch src/index.ts",
"generate:prisma": "prisma generate",
"generate:prisma": "prisma generate --sql",
"generate:mail": "maizzle build production",
"generate:proto": "cd src/proto && protoc --experimental_allow_proto3_optional --plugin=../../node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=. ./*.proto --ts_proto_opt=esModuleInterop=true",
"generate:env": "bun dotenv-vault pull",
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch", "postgresqlExtensions"]
previewFeatures = ["fullTextSearch", "postgresqlExtensions", "typedSql"]
binaryTargets = ["native"]
}

Expand Down
3 changes: 3 additions & 0 deletions prisma/sql/getDirectusFileById.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- @param {String} $1:ID of the Directus file

SELECT * FROM "directus"."directus_files" WHERE "id"=$1::uuid
3 changes: 3 additions & 0 deletions prisma/sql/getViewerFromDirectusId.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- @param {String} $1:ID of the Directus file

SELECT "external_identifier" FROM "directus"."directus_users" WHERE "id"=$1::uuid
5 changes: 2 additions & 3 deletions src/models/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {SchemaTypes} from '@pothos/core';
import {GraphQLError} from 'graphql';
import {builder} from '../pothos/builder';
import prismaClient from '../utils/prismaClient';
import {getDirectusFileById} from '@prisma/client/sql';
import {resolveOffsetConnection} from '@pothos/plugin-relay';
import {
PrismaModelTypes,
Expand Down Expand Up @@ -112,9 +113,7 @@ export function pixelImageField<Types extends SchemaTypes, F extends string>(
return null;
}

const [row] = await prismaClient.$queryRaw<
[DirectusFile?]
>`select * from "directus"."directus_files" where "id"=${id}::uuid`;
const [row] = await prismaClient.$queryRawTyped(getDirectusFileById(id));

if (row == null || !row.height || !row.width) {
return null;
Expand Down
11 changes: 5 additions & 6 deletions src/utils/viewerIdFromToken.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import {ParsedToken} from '../routes/auth';
import prismaClient from './prismaClient';
import {getViewerFromDirectusId} from '@prisma/client/sql';

export default async function viewerIdFromToken(
parsedToken: ParsedToken | undefined,
): Promise<string | undefined> {
if (parsedToken == null) {
return;
} else if (parsedToken.iss === 'directus') {
const user = await prismaClient.$queryRaw<
Array<{
external_identifier: string;
}>
>`select "external_identifier" from "directus"."directus_users" where "id"=${parsedToken.id}::uuid`;
return user.pop()?.external_identifier;
const [user] = await prismaClient.$queryRawTyped(
getViewerFromDirectusId(parsedToken.id),
);
return user?.external_identifier ?? undefined;
} else if (parsedToken.iss === 'owntracks') {
return parsedToken.viewerId;
}
Expand Down

0 comments on commit eef3d35

Please sign in to comment.