-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
993ac3a
commit 3e4a9e4
Showing
11 changed files
with
122 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { assertAuthenticated } from "@/auth"; | ||
import type { Context, Location } from "@/schema"; | ||
import Dataloader from "dataloader"; | ||
import { sql } from "./postgres"; | ||
|
||
export default (ctx: Omit<Context, "orm">) => | ||
new Dataloader<string, Location>(async keys => { | ||
assertAuthenticated(ctx); | ||
|
||
const rows = await sql<Location[]>` | ||
SELECT | ||
l.locationuuid AS id, | ||
l.locationnameid AS name_id, | ||
p.locationuuid AS parent_id, | ||
s.locationuuid AS site_id | ||
FROM public.location AS l | ||
INNER JOIN public.location AS s | ||
ON l.locationsiteid = s.locationid | ||
LEFT JOIN public.location AS p | ||
ON l.locationparentid = p.locationid | ||
WHERE l.locationuuid IN ${sql(keys)}; | ||
`; | ||
|
||
const byId = rows.reduce( | ||
(acc, row) => acc.set(row.id as string, row), | ||
new Map<string, Location>(), | ||
); | ||
|
||
return keys.map(key => byId.get(key) ?? new Error(`${key} does not exist`)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,47 @@ | ||
import { assertAuthenticated } from "@/auth"; | ||
import type { LocationResolvers, Name } from "@/schema"; | ||
import { sql } from "@/datasources/postgres"; | ||
import type { LocationResolvers } from "@/schema"; | ||
import { isValue } from "@/util"; | ||
|
||
export const Location: LocationResolvers = { | ||
async name(parent, _, ctx) { | ||
async children(parent, { options }, ctx) { | ||
assertAuthenticated(ctx); | ||
const childIds = await sql<{ id: string }[]>` | ||
SELECT locationuuid AS id | ||
FROM public.location | ||
WHERE | ||
locationparentid = ( | ||
SELECT locationid | ||
FROM public.location | ||
WHERE locationuuid = ${parent.id} | ||
) | ||
${ | ||
options?.cornerstone | ||
? sql`AND locationiscornerstone = ${true}` | ||
: sql`` | ||
} | ||
${options?.site ? sql`AND locationistop = ${true}` : sql``} | ||
`; | ||
const children = await ctx.orm.location.loadMany(childIds.map(c => c.id)); | ||
return children.filter(isValue); | ||
}, | ||
name(parent, _, ctx) { | ||
assertAuthenticated(ctx); | ||
return ctx.orm.name.load({ | ||
id: parent.name_id as string, | ||
language_id: ctx.user.language_id, | ||
}); | ||
}, | ||
parent(parent, _, ctx) { | ||
assertAuthenticated(ctx); | ||
return ctx.orm.location.load(parent.parent_id as string); | ||
}, | ||
site(parent, _, ctx) { | ||
assertAuthenticated(ctx); | ||
return ctx.orm.location.load(parent.site_id as string); | ||
}, | ||
tags(parent, _, ctx) { | ||
assertAuthenticated(ctx); | ||
return []; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { QueryResolvers } from "@/schema"; | ||
|
||
export const location: NonNullable<QueryResolvers["location"]> = async ( | ||
_, | ||
{ id }, | ||
{ orm }, | ||
) => { | ||
return await orm.location.load(id); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function isError<T>(e: T | Error): e is Error { | ||
return e instanceof Error; | ||
} | ||
|
||
export function isValue<T>(v: T | Error): v is T { | ||
return !isError(v); | ||
} |