mirror of
https://github.com/directus/directus.git
synced 2026-02-19 10:14:33 -05:00
Add fields support for date functions
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { Knex } from 'knex';
|
||||
|
||||
export interface IBaseHelper {
|
||||
year(table: string, column: string): Knex.Raw;
|
||||
month(table: string, column: string): Knex.Raw;
|
||||
week(table: string, column: string): Knex.Raw;
|
||||
day(table: string, column: string): Knex.Raw;
|
||||
weekday(table: string, column: string): Knex.Raw;
|
||||
hour(table: string, column: string): Knex.Raw;
|
||||
minute(table: string, column: string): Knex.Raw;
|
||||
second(table: string, column: string): Knex.Raw;
|
||||
export interface HelperFn {
|
||||
year(table: string, column: string, alias?: string): Knex.Raw;
|
||||
month(table: string, column: string, alias?: string): Knex.Raw;
|
||||
week(table: string, column: string, alias?: string): Knex.Raw;
|
||||
day(table: string, column: string, alias?: string): Knex.Raw;
|
||||
weekday(table: string, column: string, alias?: string): Knex.Raw;
|
||||
hour(table: string, column: string, alias?: string): Knex.Raw;
|
||||
minute(table: string, column: string, alias?: string): Knex.Raw;
|
||||
second(table: string, column: string, alias?: string): Knex.Raw;
|
||||
}
|
||||
|
||||
@@ -1,42 +1,74 @@
|
||||
import { Knex } from 'knex';
|
||||
import { IBaseHelper } from '../base';
|
||||
import { HelperFn } from '../base';
|
||||
|
||||
export class HelperPostgres implements IBaseHelper {
|
||||
export class HelperPostgres implements HelperFn {
|
||||
private knex: Knex;
|
||||
|
||||
constructor(knex: Knex) {
|
||||
this.knex = knex;
|
||||
}
|
||||
|
||||
year(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(YEAR FROM ??.??) as ??', [table, column, `${column}_year`]);
|
||||
year(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(YEAR FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(YEAR FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
month(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(MONTH FROM ??)', [column]);
|
||||
month(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(MONTH FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(MONTH FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
week(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(WEEK FROM ??)', [column]);
|
||||
week(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(WEEK FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(WEEK FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
day(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(DAY FROM ??)', [column]);
|
||||
day(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(DAY FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(DAY FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
weekday(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(DOW FROM ??)', [column]);
|
||||
weekday(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(DOW FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(DOW FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
hour(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(HOUR FROM ??)', [column]);
|
||||
hour(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(HOUR FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(HOUR FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
minute(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(MINUTE FROM ??)', [column]);
|
||||
minute(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(MINUTE FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(MINUTE FROM ??.??)', [table, column]);
|
||||
}
|
||||
|
||||
second(table: string, column: string): Knex.Raw {
|
||||
return this.knex.raw('EXTRACT(SECOND FROM ??)', [column]);
|
||||
second(table: string, column: string, alias?: string): Knex.Raw {
|
||||
if (alias) {
|
||||
return this.knex.raw('EXTRACT(SECOND FROM ??.??) as ??', [table, column, alias]);
|
||||
}
|
||||
|
||||
return this.knex.raw('EXTRACT(SECOND FROM ??.??)', [table, column]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { clone, cloneDeep, pick, uniq } from 'lodash';
|
||||
import { PayloadService } from '../services/payload';
|
||||
import { Item, Query, SchemaOverview } from '../types';
|
||||
import { AST, FieldNode, NestedCollectionNode } from '../types/ast';
|
||||
import { applyFunctionToColumnName } from '../utils/apply-function-to-column-name';
|
||||
import applyQuery from '../utils/apply-query';
|
||||
import { getColumn } from '../utils/get-column';
|
||||
import { stripFunction } from '../utils/strip-function';
|
||||
@@ -419,7 +420,9 @@ function removeTemporaryFields(
|
||||
);
|
||||
}
|
||||
|
||||
item = fields.length > 0 ? pick(rawItem, fields) : rawItem[primaryKeyField];
|
||||
const fieldsWithFunctionsApplied = fields.map((field) => applyFunctionToColumnName(field));
|
||||
|
||||
item = fields.length > 0 ? pick(rawItem, fieldsWithFunctionsApplied) : rawItem[primaryKeyField];
|
||||
|
||||
items.push(item);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user