-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Showing
23 changed files
with
405 additions
and
282 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 |
---|---|---|
@@ -1,15 +1,24 @@ | ||
import { db } from "../db"; | ||
import { executeWithPagination } from "../utils/query-paginate"; | ||
import { executeAsTable } from "../utils/query-table"; | ||
import type { AssetInsert } from "../db/types"; | ||
import type { TableQuery } from "../utils/query-table"; | ||
|
||
export async function createAsset(fields: AssetInsert) { | ||
return await db.insertInto("assets").values(fields).executeTakeFirstOrThrow(); | ||
} | ||
|
||
export async function getAssets(page: number, perPage: number) { | ||
const query = db.selectFrom("assets").select(["id", "groupId", "createdAt"]); | ||
return await executeWithPagination(query, { | ||
page, | ||
perPage, | ||
}); | ||
export async function getAssetsTable(query: TableQuery) { | ||
return await executeAsTable( | ||
query, | ||
db | ||
.selectFrom("assets") | ||
.leftJoin("playables", "playables.assetId", "assets.id") | ||
.select(({ fn }) => [ | ||
"assets.id", | ||
"assets.groupId", | ||
"assets.createdAt", | ||
fn.count<number>("playables.assetId").as("playablesCount"), | ||
]) | ||
.groupBy("assets.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { db } from "../db"; | ||
import type { PlayableInsert } from "../db/types"; | ||
|
||
export async function createPlayable(fields: PlayableInsert) { | ||
return await db | ||
.insertInto("playables") | ||
.values(fields) | ||
.executeTakeFirstOrThrow(); | ||
} |
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 was deleted.
Oops, something went wrong.
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,55 @@ | ||
import { t } from "elysia"; | ||
import type { SelectQueryBuilder, TableNode } from "kysely"; | ||
import type { Static, TSchema } from "elysia"; | ||
import { db } from "../db"; | ||
|
||
export type TableResult<O> = { | ||
rows: O[]; | ||
totalPages: number; | ||
}; | ||
|
||
export const tableQuery = t.Object({ | ||
page: t.Number(), | ||
perPage: t.Number(), | ||
sort: t.String(), | ||
}); | ||
|
||
export type TableQuery = Static<typeof tableQuery>; | ||
|
||
export function getTableObject<T extends TSchema>(schema: T) { | ||
return t.Object({ | ||
page: t.Number(), | ||
totalPages: t.Number(), | ||
rows: t.Array(schema), | ||
}); | ||
} | ||
|
||
export async function executeAsTable<O, DB, TB extends keyof DB>( | ||
query: TableQuery, | ||
qb: SelectQueryBuilder<DB, TB, O>, | ||
) { | ||
const from = qb.toOperationNode().from?.froms[0] as TableNode; | ||
|
||
const { count } = await db | ||
// @ts-expect-error Table string | ||
.selectFrom(from.table.identifier.name) | ||
.select((eb) => eb.fn.count<number>("id").as("count")) | ||
.executeTakeFirstOrThrow(); | ||
|
||
const [sortKey, sortMode] = query.sort.split(":"); | ||
|
||
const rows = await qb | ||
.limit(query.perPage) | ||
.offset((query.page - 1) * query.perPage) | ||
// @ts-expect-error map sortKey | ||
.orderBy(sortKey, sortMode) | ||
.execute(); | ||
|
||
const totalPages = Math.ceil(count / query.perPage); | ||
|
||
return { | ||
page: query.page, | ||
totalPages, | ||
rows, | ||
}; | ||
} |
Oops, something went wrong.