-
-
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
11 changed files
with
253 additions
and
172 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,60 @@ | ||
import { db } from "../db"; | ||
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 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"), | ||
); | ||
export async function getAssetsCount() { | ||
const { count } = await db | ||
.selectFrom("assets") | ||
.select((eb) => eb.fn.count<number>("id").as("count")) | ||
.executeTakeFirstOrThrow(); | ||
return count; | ||
} | ||
|
||
export async function getAssets(filter: { | ||
page: number; | ||
perPage: number; | ||
orderBy: string; | ||
direction: string; | ||
}) { | ||
const orderBy = mapOrderBy(filter.orderBy); | ||
const direction = mapDirection(filter.direction); | ||
|
||
const assets = await 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") | ||
.limit(filter.perPage) | ||
.offset((filter.page - 1) * filter.perPage) | ||
.orderBy(orderBy, direction) | ||
.execute(); | ||
|
||
return assets.map((asset) => { | ||
return { | ||
...asset, | ||
name: asset.id, | ||
}; | ||
}); | ||
} | ||
|
||
function mapOrderBy(orderBy: string) { | ||
if (orderBy === "name") { | ||
return "id"; | ||
} | ||
if (orderBy === "createdAt") { | ||
return "createdAt"; | ||
} | ||
return "createdAt"; | ||
} | ||
|
||
function mapDirection(direction: string) { | ||
return direction === "asc" || direction === "desc" ? direction : "desc"; | ||
} |
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
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
Oops, something went wrong.