Skip to content

Commit 3cd6d3f

Browse files
committed
refactor: get service operations
1 parent d60ad6b commit 3cd6d3f

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/operations/getService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type database from "~/database";
22
import type { ServiceDTO } from "~/models/service";
33
import { pluck } from "~/utils/object";
4+
import createRecord from "~/utils/record";
45

56
export default async function getService(db: typeof database, serviceId: string) {
67
const service = await db.query.services.findFirst({
@@ -26,7 +27,6 @@ export default async function getService(db: typeof database, serviceId: string)
2627
networks: {
2728
columns: {
2829
id: true,
29-
kind: true,
3030
},
3131
},
3232
providerlinks: {
@@ -73,9 +73,9 @@ export default async function getService(db: typeof database, serviceId: string)
7373
const { ports, environmentVariables, volumes, networks, providerlinks, clientLinks, ...others } = service;
7474
return {
7575
...others,
76-
environmentVariables: Object.fromEntries(environmentVariables.map(({ key, value }) => [key, value] as const)),
76+
environmentVariables: createRecord(environmentVariables, "key", "value"),
7777
ports: Object.fromEntries(ports.map(({ external, internal }) => [external, internal.toString()] as const)),
78-
volumes: Object.fromEntries(volumes.map(({ id, containerPath }) => [id, containerPath] as const)),
78+
volumes: createRecord(volumes, "id", "containerPath"),
7979
networkIds: pluck(networks, "id"),
8080
providers: pluck(providerlinks, "provider").map(({
8181
networks: providerNetworks,
@@ -84,7 +84,7 @@ export default async function getService(db: typeof database, serviceId: string)
8484
}) => ({
8585
...otherPropsOfProvider,
8686
networkIds: pluck(providerNetworks, "id"),
87-
variables: Object.fromEntries(providerVariables.map(entry => [entry.key, entry.value] as const)),
87+
variables: createRecord(providerVariables, "key", "value"),
8888
})),
8989
clients: pluck(clientLinks, "client"),
9090
} satisfies ServiceDTO;

src/operations/getServiceByRepo.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type database from "~/database";
22
import type { ServiceDTO } from "~/models/service";
33
import { pluck } from "~/utils/object";
4+
import createRecord from "~/utils/record";
45

56
export default async function getServiceByRepo(db: typeof database, repo: string) {
67
const service = await db.query.services.findFirst({
@@ -76,18 +77,18 @@ export default async function getServiceByRepo(db: typeof database, repo: string
7677
const { ports, environmentVariables, volumes, networks, providerlinks, clientLinks, ...others } = service;
7778
return {
7879
...others,
79-
environmentVariables: Object.fromEntries(environmentVariables.map(({ key, value }) => [key, value] as const)),
80+
environmentVariables: createRecord(environmentVariables, "key", "value"),
8081
ports: Object.fromEntries(ports.map(({ external, internal }) => [external, internal.toString()] as const)),
81-
volumes: Object.fromEntries(volumes.map(({ id, containerPath }) => [id, containerPath] as const)),
82-
networkIds: networks.map(network => network.id),
82+
volumes: createRecord(volumes, "id", "containerPath"),
83+
networkIds: pluck(networks, "id"),
8384
providers: pluck(providerlinks, "provider").map(({
8485
networks: providerNetworks,
8586
environmentVariables: providerVariables,
8687
...otherPropsOfProvider
8788
}) => ({
8889
...otherPropsOfProvider,
8990
networkIds: pluck(providerNetworks, "id"),
90-
variables: Object.fromEntries(providerVariables.map(entry => [entry.key, entry.value] as const)),
91+
variables: createRecord(providerVariables, "key", "value"),
9192
})),
9293
clients: pluck(clientLinks, "client"),
9394
} satisfies ServiceDTO;

src/utils/record.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type KeysOfType<T, ValueType> = {
2+
[K in keyof T]: T[K] extends ValueType ? K : never
3+
}[keyof T];
4+
5+
export default function createRecord<
6+
Item,
7+
K extends KeysOfType<Item, string | number>,
8+
V extends keyof Item,
9+
>(
10+
collection: Item[],
11+
key: K,
12+
value: V,
13+
) {
14+
return collection.reduce((bundle, item) => ({
15+
...bundle,
16+
[item[key] as string]: item[value],
17+
}), {} as Record<Item[K] & (string | number), Item[V]>);
18+
}

0 commit comments

Comments
 (0)