Skip to content

Commit

Permalink
SQLite utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Aug 24, 2024
1 parent 6db054f commit d24cf58
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
5 changes: 1 addition & 4 deletions src/plugins/bans.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import lodash from 'lodash';
import { sql } from 'kysely';
import { UserNotFoundError } from '../errors/index.js';
import Page from '../Page.js';
import { now } from '../utils/sqlite.js';

const { clamp } = lodash;

/** @type {import('kysely').Expression<Date>} */
const now = sql`(strftime('%FT%T', 'now'))`;

class Bans {
#uw;

Expand Down
39 changes: 11 additions & 28 deletions src/plugins/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Page from '../Page.js';
import routes from '../routes/playlists.js';
import { randomUUID } from 'node:crypto';
import { sql } from 'kysely';
import { jsonb, jsonEach, jsonLength } from '../utils/sqlite.js';

/**
* @typedef {import('../schema.js').UserID} UserID
Expand Down Expand Up @@ -154,14 +155,14 @@ class PlaylistsRepository {

const playlist = await db.selectFrom('playlists')
.where('userID', '=', user.id)
.where('playlists.id', '=', id)
.where('id', '=', id)
.select([
'playlists.id',
'playlists.userID',
'playlists.name',
'playlists.createdAt',
'playlists.updatedAt',
(eb) => sql`json_array_length(${eb.ref('playlists.items')})`.as('size'),
'id',
'userID',
'name',
'createdAt',
'updatedAt',
(eb) => jsonLength(eb.ref('items')).as('size'),
])
.executeTakeFirst();

Expand Down Expand Up @@ -214,7 +215,7 @@ class PlaylistsRepository {
.select([
'id',
'name',
(eb) => sql`json_array_length(${eb.ref('items')})`.as('size'),
(eb) => jsonLength(eb.ref('items')).as('size'),
'createdAt',
])
.execute();
Expand Down Expand Up @@ -329,24 +330,6 @@ class PlaylistsRepository {
async getPlaylistItems(playlist, filter, pagination) {
const { db } = this.#uw;

/**
* @template {unknown[]} T
* @param {import('kysely').Expression<T>} expr
* @returns {import('kysely').RawBuilder<{
* key: unknown,
* value: T[0],
* type: string,
* atom: T[0],
* id: number,
* parent: number,
* fullkey: string,
* path: string,
* }>}
*/
function jsonEach(expr) {
return sql`json_each(${expr})`
}

let query = db.selectFrom('playlists')
.innerJoin(
(eb) => jsonEach(eb.ref('playlists.items')).as('playlistItemIDs'),
Expand All @@ -372,7 +355,7 @@ class PlaylistsRepository {
.limit(pagination.limit);

const totalQuery = db.selectFrom('playlists')
.select(sql`json_array_length(items)`.as('count'))
.select((eb) => jsonLength(eb.ref('items')).as('count'))
.where('id', '=', playlist.id)
.executeTakeFirstOrThrow();

Expand Down Expand Up @@ -521,7 +504,7 @@ class PlaylistsRepository {
const toInsert = unknownMedias.map((media) => /** @type {Media} */ ({
sourceType: media.sourceType,
sourceID: media.sourceID,
sourceData: media.sourceData,
sourceData: jsonb(media.sourceData),
artist: media.artist,
title: media.title,
duration: media.duration,
Expand Down
37 changes: 37 additions & 0 deletions src/utils/sqlite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { sql } from 'kysely';

/**
* @template {unknown[]} T
* @param {import('kysely').Expression<T>} expr
* @returns {import('kysely').RawBuilder<{
* key: unknown,
* value: T[0],
* type: string,
* atom: T[0],
* id: number,
* parent: number,
* fullkey: string,
* path: string,
* }>}
*/
export function jsonEach(expr) {
return sql`json_each(${expr})`
}

/**
* @template {unknown[]} T
* @param {import('kysely').Expression<T>} expr
* @returns {import('kysely').RawBuilder<number>}
*/
export function jsonLength(expr) {
return sql`json_array_length(${expr})`
}

/** @param {import('type-fest').Jsonifiable} value */
export function jsonb(value) {
return sql`jsonb(${JSON.stringify(value)})`;
}

/** @type {import('kysely').RawBuilder<Date>} */
export const now = sql`(strftime('%FT%T', 'now'))`;

0 comments on commit d24cf58

Please sign in to comment.