1
import type { Knex } from 'knex';
2
import type { FnHelperOptions } from '../types.js';
3
import { FnHelper } from '../types.js';
5
const parseLocaltime = (columnType?: string) => {
6
if (columnType === 'timestamp') {
10
return `, 'localtime'`;
13
export class FnHelperSQLite extends FnHelper {
14
year(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
15
return this.knex.raw(`CAST(strftime('%Y', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
21
month(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
22
return this.knex.raw(`CAST(strftime('%m', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
28
week(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
29
return this.knex.raw(`CAST(strftime('%W', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
35
day(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
36
return this.knex.raw(`CAST(strftime('%d', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
42
weekday(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
43
return this.knex.raw(`CAST(strftime('%w', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
49
hour(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
50
return this.knex.raw(`CAST(strftime('%H', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
56
minute(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
57
return this.knex.raw(`CAST(strftime('%M', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
63
second(table: string, column: string, options?: FnHelperOptions): Knex.Raw {
64
return this.knex.raw(`CAST(strftime('%S', ??.?? / 1000, 'unixepoch'${parseLocaltime(options?.type)}) AS INTEGER)`, [
70
count(table: string, column: string, options?: FnHelperOptions): Knex.Raw<any> {
71
const collectionName = options?.originalCollectionName || table;
72
const type = this.schema.collections?.[collectionName]?.fields?.[column]?.type ?? 'unknown';
74
if (type === 'json') {
75
return this.knex.raw(`json_array_length(??.??, '$')`, [table, column]);
78
if (type === 'alias') {
79
return this._relationalCount(table, column, options);
82
throw new Error(`Couldn't extract type from ${table}.${column}`);