Skip to content

Commit

Permalink
upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbuechele committed Sep 10, 2024
1 parent cce1d25 commit 7665608
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 9,611 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM oven/bun:1.1.18
FROM oven/bun:1.1.27

WORKDIR /usr/src/app
COPY package.json bun.lockb prisma/schema.prisma ./
Expand All @@ -9,4 +9,4 @@ COPY --from=node:18 /usr/local/bin/node /usr/local/bin/node
COPY . .
RUN bunx prisma generate

CMD ["bun", "run", "src/index.ts"]
CMD ["bun", "run", "src/index.ts"]
Binary file modified bun.lockb
Binary file not shown.
59 changes: 29 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,69 +15,68 @@
"update-interactive": "npx npm-check-updates --interactive --format group"
},
"devDependencies": {
"@maizzle/framework": "4.8.4",
"@sentry/cli": "^2.31.0",
"@maizzle/framework": "4.8.9",
"@sentry/cli": "^2.36.1",
"@types/date-fns": "^2.6.0",
"@types/jsonwebtoken": "^9.0.6",
"@types/lodash.merge": "^4.6.9",
"@types/nodemailer": "^6.4.14",
"@types/nodemailer": "^6.4.15",
"@types/totp-generator": "^0.0.8",
"bun-types": "latest",
"prettier": "^3.2.5",
"prisma": "^5.12.1",
"tailwindcss-box-shadow": "^2.0.1",
"prettier": "^3.3.3",
"prisma": "^5.19.1",
"tailwindcss-box-shadow": "^2.0.3",
"tailwindcss-email-variants": "^2.0.3",
"tailwindcss-mso": "^1.4.1",
"tailwindcss-mso": "^1.4.3",
"@jridgewell/resolve-uri": "^3.1.2",
"@jridgewell/sourcemap-codec": "^1.4.15",
"@jridgewell/sourcemap-codec": "^1.5.0",
"fastq": "^1.17.1",
"typescript": "^5.4.5"
"typescript": "^5.6.2"
},
"dependencies": {
"@aws-sdk/client-ses": "^3.556.0",
"@aws-sdk/client-ses": "^3.645.0",
"@aws-sdk/util-utf8-browser": "^3.259.0",
"@envelop/sentry": "^7.0.0",
"@hono/sentry": "^1.0.1",
"@pothos/core": "^3.41.1",
"@pothos/plugin-prisma": "^3.65.1",
"@pothos/plugin-relay": "^3.46.0",
"@pothos/plugin-scope-auth": "^3.22.0",
"@prisma/client": "^5.12.1",
"@sentry/node": "^7.111.0",
"@sentry/tracing": "^7.111.0",
"@hono/sentry": "^1.2.0",
"@pothos/core": "^4.2.0",
"@pothos/plugin-prisma": "^4.2.1",
"@pothos/plugin-relay": "^4.2.1",
"@pothos/plugin-scope-auth": "^4.1.0",
"@prisma/client": "^5.19.1",
"@sentry/node": "^8.30.0",
"@sentry/tracing": "^7.114.0",
"@types/basic-auth": "^1.1.8",
"bandcamp-scraper": "^1.5.0",
"basic-auth": "^2.0.1",
"bignumber.js": "^9.1.2",
"crc-32": "^1.2.2",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"dotenv-vault": "^1.26.1",
"dotenv-vault": "^1.26.2",
"email-validator": "^2.0.4",
"env-var": "^7.4.1",
"env-var": "^7.5.0",
"form-data": "^4.0.0",
"googleapis": "^134.0.0",
"graphile-worker": "0.16",
"graphql": "^16.8.1",
"graphql": "^16.9.0",
"graphql-scalars": "^1.23.0",
"graphql-yoga": "^5.3.0",
"hono": "^4.2.6",
"graphql-yoga": "^5.7.0",
"hono": "^4.5.11",
"is-empty-obj": "^1.0.13",
"jimp": "^0.22.12",
"json-stable-stringify": "^1.1.1",
"jsonwebtoken": "^9.0.2",
"lodash.merge": "^4.6.2",
"markdown-to-txt": "^2.0.1",
"nodemailer": "^6.9.13",
"nodemailer": "^6.9.15",
"normalize-url": "6",
"object-inspect": "^1.13.1",
"object-inspect": "^1.13.2",
"samlify": "^2.8.11",
"sax": "^1.3.0",
"sax": "^1.4.1",
"slugify": "^1.6.6",
"split2": "^4.2.0",
"totp-generator": "^1.0.0",
"ts-ics": "^1.3.4",
"ts-proto": "^1.172.0",
"zod": "^3.23.0"
"ts-ics": "^1.4.2",
"ts-proto": "^2.2.0",
"zod": "^3.23.8"
}
}
7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import saml from './routes/saml';
import owntracks from './routes/owntracks';
import slack from './routes/slack';
import {createYoga} from 'graphql-yoga';
import {useSentry} from '@envelop/sentry';
import {Context} from './context';
import {sentry} from '@hono/sentry';
import {serveStatic} from 'hono/bun';
import {cors} from 'hono/cors';
import kultWiki from './kult.wiki';
import {Toucan} from 'toucan-js';
import prismaClient from './utils/prismaClient';

const app = new Hono<{Variables: Context & {sentry: Toucan}}>();

Expand Down Expand Up @@ -70,11 +68,6 @@ app.on(['GET', 'POST'], '/graphql', async (c) =>
graphqlEndpoint: '',
context: () => c.var,
maskedErrors: false,
plugins: [
useSentry({
skipError: () => false,
}),
],
}).fetch(c.req.raw),
);

Expand Down
80 changes: 20 additions & 60 deletions src/models/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import {SchemaTypes} from '@pothos/core';
import {GraphQLError} from 'graphql';
import {builder} from '../pothos/builder';
import prismaClient from '../utils/prismaClient';
import {resolveOffsetConnection} from '@pothos/plugin-relay';
import {
PrismaModelTypes,
PrismaObjectFieldBuilder,
} from '@pothos/plugin-prisma';

export type DirectusFile = {
id: string;
Expand Down Expand Up @@ -120,8 +125,8 @@ export function pixelImageField<Types extends SchemaTypes, F extends string>(
});
}

export function assetConnection<Types extends SchemaTypes>(
t: PothosSchemaTypes.FieldBuilder<Types, {id: string}, 'Object'>,
export function assetConnection(
t: PrismaObjectFieldBuilder<SchemaTypes, PrismaModelTypes & {id: string}>,
connectionName: string,
) {
return t.connection(
Expand All @@ -133,82 +138,37 @@ export function assetConnection<Types extends SchemaTypes>(
width: t.arg.int(),
height: t.arg.int(),
},
// @ts-ignore
resolve: async (root, {before, after, first, last}) => {
if (last != null || before != null) {
throw new GraphQLError('Not implemented');
}

const limit = first ?? 20;

const assets = await prismaClient.$queryRawUnsafe<[DirectusFile]>(
`SELECT * ${from({
connectionName,
id: root.id,
after,
})} ORDER BY "filename_download" LIMIT ${limit + 1};`,
resolve: async (root, args) => {
const [{count}] = await prismaClient.$queryRawUnsafe<[{count: number}]>(
`SELECT COUNT(*) ${from(connectionName, root.id)};`,
);

let hasNextPage = assets.length > limit;
if (hasNextPage) {
assets.pop();
}

return {
pageInfo: {
hasNextPage,
hasPreviousPage: false, // TODO
startCursor: assets[0]?.filename_download,
endCursor: assets[assets.length - 1]?.filename_download,
},
id: root.id,
edges: assets.map((node) => ({
cursor: node.filename_download,
node,
})),
};
return resolveOffsetConnection(
{args, defaultSize: 20, totalCount: Number(count)},
async ({limit, offset}) =>
prismaClient.$queryRawUnsafe<[DirectusFile]>(
`SELECT * ${from(connectionName, root.id)} ORDER BY "filename_download" LIMIT ${limit} OFFSET ${offset};`,
),
);
},
},
{
fields: (t) => ({
totalCount: t.field({
type: 'Int',
nullable: false,
// @ts-ignore whatever
resolve: async (root) => {
const [{count}] = await prismaClient.$queryRawUnsafe<
[{count: number}]
>(
`SELECT COUNT(*) ${from({
connectionName,
id: root.id,
})};`,
);
return Number(count);
},
resolve: (root) => root.totalCount,
}),
}),
},
);
}

function from({
connectionName,
id,
after,
}: {
connectionName: string;
id: string;
after?: string | null;
first?: number;
}) {
function from(connectionName: string, id: string) {
return ` FROM
"directus"."${s(connectionName)}_files"
JOIN "directus"."directus_files" ON "directus_files_id" = "directus"."directus_files"."id"
WHERE
"${s(connectionName)}_id" = '${s(id)}' AND "filename_download" > '${s(
after,
)}'
WHERE "${s(connectionName)}_id" = '${s(id)}'
`;
}

Expand Down
23 changes: 15 additions & 8 deletions src/models/MarkdownString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import prismaClient from '../utils/prismaClient';
import {DirectusPixelImage, PixelImage} from './Asset';
import {SchemaTypes} from '@pothos/core';
import markdownToTxt from 'markdown-to-txt';
import {
PrismaModelTypes,
PrismaObjectFieldBuilder,
} from '@pothos/plugin-prisma';

const MarkdownString = builder
.objectRef<{
Expand All @@ -22,21 +26,24 @@ const MarkdownString = builder
}),
});

export function markdownField<Types extends SchemaTypes, F extends string>(
t: PothosSchemaTypes.FieldBuilder<Types, {[K in F]: string | null}, 'Object'>,
export function markdownField<F extends string>(
t: PrismaObjectFieldBuilder<
SchemaTypes,
PrismaModelTypes & {[K in F]: string | null}
>,
field: F,
options?: {nullable?: boolean},
) {
return t.field({
type: MarkdownString,
nullable: options?.nullable,
// @ts-ignore
resolve: async (root) => {
let markdown = root[field];
if (!markdown) {
return null;
let text = root[field];
if (!text) {
// We are okay with returning null when nullable is true
return null as any;
}
markdown = markdown.replaceAll(
const markdown = text.replaceAll(
'https://crew.kulturspektakel.de/assets/',
'https://files.kulturspektakel.de/',
);
Expand All @@ -45,7 +52,7 @@ export function markdownField<Types extends SchemaTypes, F extends string>(
return {
images,
plainText: markdownToTxt(markdown),
markdown,
markdown: markdown,
};
},
});
Expand Down
16 changes: 9 additions & 7 deletions src/pothos/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {Context} from '../context';
import {GraphQLError} from 'graphql';

export const builder = new SchemaBuilder<{
DefaultFieldNullability: false;
AuthScopes: {
user: boolean;
device: boolean;
Expand All @@ -26,12 +27,13 @@ export const builder = new SchemaBuilder<{
};
};
}>({
defaultFieldNullability: false,
plugins: [ScopeAuthPlugin, PrismaPlugin, RelayPlugin],
prisma: {
client: prismaClient,
filterConnectionTotalCount: true,
},
relayOptions: {
relay: {
// These will become the defaults in the next major version
clientMutationId: 'omit',
cursorType: 'String',
Expand All @@ -41,18 +43,18 @@ export const builder = new SchemaBuilder<{
},
encodeGlobalID: (typename, id) => `${typename}:${id}`,
},
authScopes: async ({parsedToken}) => ({
user: parsedToken?.iss === 'directus',
device: parsedToken?.iss === 'device',
}),
scopeAuthOptions: {
scopeAuth: {
treatErrorsAsUnauthorized: true,
unauthorizedError: (parent, context, info, result) =>
unauthorizedError: () =>
new GraphQLError(`Not authorized`, {
extensions: {
code: 'UNAUTHORIZED',
},
}),
authScopes: async ({parsedToken}) => ({
user: parsedToken?.iss === 'directus',
device: parsedToken?.iss === 'device',
}),
},
});

Expand Down
Loading

0 comments on commit 7665608

Please sign in to comment.