Page MenuHomeSealhub

No OneTemporary

Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/src/app/base-chips/field-types/date.test.ts b/src/app/base-chips/field-types/date.test.ts
new file mode 100644
index 00000000..a9d2efa0
--- /dev/null
+++ b/src/app/base-chips/field-types/date.test.ts
@@ -0,0 +1,33 @@
+import assert from "assert";
+import {
+ type TestAppConstructor,
+ withRunningApp,
+} from "../../../test_utils/with-test-app.js";
+import { Collection, FieldTypes } from "../../../main.js";
+import { TestApp } from "../../../test_utils/test-app.js";
+
+describe("date", () => {
+ it('should support a "less than" query', async () =>
+ withRunningApp(
+ (t: TestAppConstructor) => {
+ return class extends t {
+ collections = {
+ ...TestApp.BaseCollections,
+ events: new (class extends Collection {
+ name = "events";
+ fields = { date: new FieldTypes.Date() };
+ })(),
+ };
+ };
+ },
+ async ({ app }) => {
+ await app.collections.events.suCreate({ date: "2026-10-10" });
+ await app.collections.events.suCreate({ date: "2026-12-12" });
+ const { items } = await app.collections.events
+ .suList()
+ .filter({ date: { "<": "2026-11-11" } })
+ .fetch();
+ assert.strictEqual(items.length, 1);
+ }
+ ));
+});
diff --git a/src/app/base-chips/field-types/date.ts b/src/app/base-chips/field-types/date.ts
index 8d74e040..f6ea81f7 100644
--- a/src/app/base-chips/field-types/date.ts
+++ b/src/app/base-chips/field-types/date.ts
@@ -1,114 +1,118 @@
import { Field, Context, Errors } from "../../../main.js";
import { OpenApiTypes } from "../../../schemas/open-api-types.js";
import humanComparatorToQuery, {
type ComparatorObject,
type HumanComparator,
type DBComparator,
} from "../../../utils/human-comparator-to-query.js";
const DAY = 1000 * 60 * 60 * 24;
function dateStrToDayInt(date_str: string) {
return Date.parse(date_str) / DAY;
}
export const DATE_FIELD_TYPE_NAME = "date";
/** Stores a date - without hours/minutes, just the day.
*
* **Params**: None
*/
export default class DateField extends Field<string, string | number, number> {
typeName = DATE_FIELD_TYPE_NAME;
open_api_type: OpenApiTypes = OpenApiTypes.DATE;
async hasIndex(): Promise<boolean> {
return true;
}
async isProperValue(context: Context, value: string) {
if (value == null) {
return Field.valid();
}
const date_in_string = value.toString();
const regex = /^([0-9]{4})-(0?[1-9]|1[0-2])-([0-2]?[0-9]|30|31)$/; // granulation_per_day
if (
regex.test(date_in_string) === false ||
isNaN(Date.parse(date_in_string))
) {
return Field.invalid(
context.i18n`Value '${value}' is not date calendar format. Expected value in standard IS0 8601 (YYYY-MM-DD) format.`
);
}
return Field.valid();
}
async encode(_: Context, value: string | null) {
if (value === null) {
return null;
}
const date_str = value.toString();
// value is already confirmed to be properly formatted
return dateStrToDayInt(date_str);
}
async getMatchQueryValue(
_: Context,
field_filter: string | ComparatorObject<string>
) {
if (typeof field_filter !== "object") {
return {
$eq: dateStrToDayInt(field_filter),
};
}
// treating filter as a query here
const new_filter: { [comparator in DBComparator]?: number } = {};
for (const comparator in field_filter) {
if (!comparator) {
continue;
}
const new_comparator = humanComparatorToQuery(
comparator as HumanComparator
);
if (new_comparator === undefined) {
throw new Errors.ValidationError(
`Unknown comparator: '${comparator}'.`
);
}
const value =
field_filter[comparator as HumanComparator]?.toString();
if (value === undefined) {
continue;
}
- new_filter[new_comparator] = parseInt(value, 10);
+ // eslint-disable-next-line no-await-in-loop
+ new_filter[new_comparator] = (await this.encode(
+ _,
+ value
+ )) as number;
}
return new_filter;
}
async decode(
_: Context,
value_in_db: number | null,
old_value: string,
format: never
) {
if (value_in_db === null) {
return value_in_db;
}
const d = new Date(value_in_db * DAY);
const month = d.getMonth() + 1;
let month_str = month.toString();
if (month < 10) {
month_str = "0" + month_str;
}
return `${d.getFullYear()}-${month_str}-${
d.getDate() < 10 ? "0" : ""
}${d.getDate()}`;
}
getPostgreSqlFieldDefinitions(): string[] {
return [`"${this.name}" DATE`];
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Nov 28, 02:04 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1224034
Default Alt Text
(4 KB)

Event Timeline