mikro-orm-temporal 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2025, Ben Scholzen 'DASPRiD'
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Temporal for Mikro ORM
2
+
3
+ [![Release](https://github.com/DASPRiD/mikro-orm-temporal/actions/workflows/release.yml/badge.svg)](https://github.com/DASPRiD/mikro-orm-temporal/actions/workflows/release.yml)
4
+ [![codecov](https://codecov.io/gh/DASPRiD/mikro-orm-temporal/graph/badge.svg?token=s9yDuZeh7F)](https://codecov.io/gh/DASPRiD/mikro-orm-temporal)
5
+
6
+ This package provides [Mikro ORM](https://github.com/mikro-orm/mikro-orm) types for common
7
+ [Temporal](https://tc39.es/proposal-temporal/) objects.
8
+
9
+ The library exports the following types:
10
+
11
+ - `DurationType`
12
+ - `PlainDateType`
13
+ - `PlainDateTimeType`
14
+ - `PlainTimeType`
15
+ - `OffsetDateTimeType`
16
+
17
+ ## Installation
18
+
19
+ Install via your favorite package manager:
20
+
21
+ ```bash
22
+ npm install mikro-orm-temporal
23
+ # or
24
+ pnpm add mikro-orm-temporal
25
+ # or
26
+ yarn add mikro-orm-temporal
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ```ts
32
+ import { OffsetDateTimeType } from 'mikro-orm-temporal';
33
+ import { Entity, Property } from "@mikro-orm/core";
34
+
35
+ @Entity()
36
+ export class HelloWorld {
37
+ @Property({type: OffsetDateTimeType})
38
+ public dateTime: Temporal.ZonedDateTime;
39
+ }
40
+ ```
41
+
42
+ ## Caveats
43
+
44
+ Due to MikroORM not distinguishing between timestamps with and without timezone and some other quirks, this library
45
+ makes some assumptions and decisions:
46
+
47
+ - Durations are stored as `INTERVAL` in Postgres and as `INTEGER` (in seconds) on all other platforms.
48
+ - `PlainDateTime` is stored as `VARCHAR(19)` on MSSQL, as Tedious introduces timezone issues.
@@ -0,0 +1,7 @@
1
+ import { type EntityProperty, type Platform, Type } from "@mikro-orm/core";
2
+ export declare class DurationType extends Type<Temporal.Duration | null, string | number | null> {
3
+ convertToDatabaseValue(value: Temporal.Duration | null, platform: Platform): string | number | null;
4
+ convertToJSValue(value: string | number | null, platform: Platform): Temporal.Duration | null;
5
+ toJSON(value: Temporal.Duration | null): string | null;
6
+ getColumnType(_prop: EntityProperty, platform: Platform): string;
7
+ }
@@ -0,0 +1,46 @@
1
+ import { Type } from "@mikro-orm/core";
2
+ import { isPostgres } from "./utils.js";
3
+ export class DurationType extends Type {
4
+ convertToDatabaseValue(value, platform) {
5
+ if (value === null) {
6
+ return null;
7
+ }
8
+ if (isPostgres(platform)) {
9
+ return value.toString();
10
+ }
11
+ return value.total({ unit: "seconds" });
12
+ }
13
+ convertToJSValue(value, platform) {
14
+ /* node:coverage disable */
15
+ if (value === null) {
16
+ return null;
17
+ }
18
+ /* node:coverage enable */
19
+ if (typeof value === "number") {
20
+ return Temporal.Duration.from({ seconds: value });
21
+ }
22
+ if (value.startsWith("P")) {
23
+ return Temporal.Duration.from(value);
24
+ }
25
+ if (isPostgres(platform)) {
26
+ const interval = platform.convertIntervalToJSValue(value);
27
+ return Temporal.Duration.from(interval.toISO());
28
+ }
29
+ /* node:coverage disable */
30
+ throw new Error(`Unexpected interval value: ${value}`);
31
+ /* node:coverage enable */
32
+ }
33
+ /* node:coverage disable */
34
+ toJSON(value) {
35
+ return value ? value.toString() : null;
36
+ }
37
+ /* node:coverage enable */
38
+ getColumnType(_prop, platform) {
39
+ if (isPostgres(platform)) {
40
+ // Postgres has a true single-purpose column type for intervals.
41
+ return platform.getIntervalTypeDeclarationSQL({});
42
+ }
43
+ // For all other platforms we store the duration as integer in seconds.
44
+ return platform.getIntegerTypeDeclarationSQL({});
45
+ }
46
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./duration-type.js";
2
+ export * from "./offset-date-time-type.js";
3
+ export * from "./plain-date-time-type.js";
4
+ export * from "./plain-date-type.js";
5
+ export * from "./plain-time-type.js";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./duration-type.js";
2
+ export * from "./offset-date-time-type.js";
3
+ export * from "./plain-date-time-type.js";
4
+ export * from "./plain-date-type.js";
5
+ export * from "./plain-time-type.js";
@@ -0,0 +1,8 @@
1
+ import { type EntityProperty, type Platform, Type } from "@mikro-orm/core";
2
+ export declare class OffsetDateTimeType extends Type<Temporal.ZonedDateTime | null, string | null> {
3
+ convertToDatabaseValue(value: Temporal.ZonedDateTime | null, platform: Platform): string | null;
4
+ convertToJSValue(value: Date | string | null, platform: Platform): Temporal.ZonedDateTime | null;
5
+ compareAsType(): string;
6
+ toJSON(value: Temporal.ZonedDateTime | null): string | null;
7
+ getColumnType(prop: EntityProperty, platform: Platform): string;
8
+ }
@@ -0,0 +1,44 @@
1
+ import { Type } from "@mikro-orm/core";
2
+ import { isMsSql, isMySql } from "./utils.js";
3
+ export class OffsetDateTimeType extends Type {
4
+ convertToDatabaseValue(value, platform) {
5
+ if (value === null) {
6
+ return null;
7
+ }
8
+ if (isMySql(platform)) {
9
+ // Convert to MySQL format
10
+ return value.withTimeZone("UTC").toPlainDateTime().toString().replace("T", " ");
11
+ }
12
+ return value.toInstant().toString();
13
+ }
14
+ convertToJSValue(value, platform) {
15
+ /* node:coverage disable */
16
+ if (value === null) {
17
+ return null;
18
+ }
19
+ /* node:coverage enable */
20
+ if (typeof value === "string") {
21
+ if (isMySql(platform)) {
22
+ // Convert to ISO format
23
+ return Temporal.Instant.from(`${value.replace(" ", "T")}Z`).toZonedDateTimeISO("UTC");
24
+ }
25
+ return Temporal.Instant.from(value).toZonedDateTimeISO("UTC");
26
+ }
27
+ return Temporal.Instant.fromEpochMilliseconds(value.getTime()).toZonedDateTimeISO("UTC");
28
+ }
29
+ compareAsType() {
30
+ return "date";
31
+ }
32
+ /* node:coverage disable */
33
+ toJSON(value) {
34
+ return value ? value.toInstant().toString() : null;
35
+ }
36
+ /* node:coverage enable */
37
+ getColumnType(prop, platform) {
38
+ if (isMsSql(platform)) {
39
+ // Postgres has a true single-purpose column type for timestamps with offset.
40
+ return "DATETIMEOFFSET";
41
+ }
42
+ return platform.getDateTimeTypeDeclarationSQL({ length: prop.length });
43
+ }
44
+ }
@@ -0,0 +1,8 @@
1
+ import { type EntityProperty, type Platform, Type } from "@mikro-orm/core";
2
+ export declare class PlainDateTimeType extends Type<Temporal.PlainDateTime | null, string | null> {
3
+ convertToDatabaseValue(value: Temporal.PlainDateTime | null, platform: Platform): string | null;
4
+ convertToJSValue(value: string | null, platform: Platform): Temporal.PlainDateTime | null;
5
+ compareAsType(): string;
6
+ toJSON(value: Temporal.PlainDateTime | null): string | null;
7
+ getColumnType(prop: EntityProperty, platform: Platform): string;
8
+ }
@@ -0,0 +1,46 @@
1
+ import { Type } from "@mikro-orm/core";
2
+ import { isMsSql, isMySql, isPostgres } from "./utils.js";
3
+ export class PlainDateTimeType extends Type {
4
+ convertToDatabaseValue(value, platform) {
5
+ if (value === null) {
6
+ return null;
7
+ }
8
+ if (isMySql(platform)) {
9
+ // Convert to MySQL format
10
+ return value.toString().replace("T", " ");
11
+ }
12
+ return value.toString();
13
+ }
14
+ convertToJSValue(value, platform) {
15
+ /* node:coverage disable */
16
+ if (value === null) {
17
+ return null;
18
+ }
19
+ /* node:coverage enable */
20
+ if (isMySql(platform)) {
21
+ // Convert to ISO format
22
+ return Temporal.PlainDateTime.from(value.replace(" ", "T"));
23
+ }
24
+ return Temporal.PlainDateTime.from(value);
25
+ }
26
+ compareAsType() {
27
+ return "date";
28
+ }
29
+ /* node:coverage disable */
30
+ toJSON(value) {
31
+ return value ? value.toString() : null;
32
+ }
33
+ /* node:coverage enable */
34
+ getColumnType(prop, platform) {
35
+ if (isPostgres(platform)) {
36
+ // Postgres has a true single-purpose column type for timestamps without timezone.
37
+ return "TIMESTAMP";
38
+ }
39
+ if (isMsSql(platform)) {
40
+ // While MSSQL does have a `datetime2` column, tedious tries to convert it to a `Date` and interprets it
41
+ // as local time, which causes an unwanted time shift.
42
+ return "VARCHAR(19)";
43
+ }
44
+ return platform.getDateTimeTypeDeclarationSQL({ length: prop.length });
45
+ }
46
+ }
@@ -0,0 +1,8 @@
1
+ import { type EntityProperty, type Platform, Type } from "@mikro-orm/core";
2
+ export declare class PlainDateType extends Type<Temporal.PlainDate | null, string | Date | null> {
3
+ convertToDatabaseValue(value: Temporal.PlainDate | null): string | null;
4
+ convertToJSValue(value: string | Date | null): Temporal.PlainDate | null;
5
+ compareAsType(): string;
6
+ toJSON(value: Temporal.PlainDate | null): string | null;
7
+ getColumnType(prop: EntityProperty, platform: Platform): string;
8
+ }
@@ -0,0 +1,35 @@
1
+ import { Type } from "@mikro-orm/core";
2
+ export class PlainDateType extends Type {
3
+ convertToDatabaseValue(value) {
4
+ if (!value) {
5
+ return null;
6
+ }
7
+ return value.toString();
8
+ }
9
+ convertToJSValue(value) {
10
+ /* node:coverage disable */
11
+ if (!value) {
12
+ return null;
13
+ }
14
+ /* node:coverage enable */
15
+ if (value instanceof Date) {
16
+ return Temporal.PlainDate.from({
17
+ year: value.getFullYear(),
18
+ month: value.getMonth() + 1,
19
+ day: value.getDate(),
20
+ });
21
+ }
22
+ return Temporal.PlainDate.from(value);
23
+ }
24
+ compareAsType() {
25
+ return "string";
26
+ }
27
+ /* node:coverage disable */
28
+ toJSON(value) {
29
+ return value ? value.toString() : null;
30
+ }
31
+ /* node:coverage enable */
32
+ getColumnType(prop, platform) {
33
+ return platform.getDateTypeDeclarationSQL(prop.length);
34
+ }
35
+ }
@@ -0,0 +1,8 @@
1
+ import { type EntityProperty, type Platform, Type } from "@mikro-orm/core";
2
+ export declare class PlainTimeType extends Type<Temporal.PlainTime | null, string | null> {
3
+ convertToDatabaseValue(value: Temporal.PlainTime | null): string | null;
4
+ convertToJSValue(value: Date | string | null): Temporal.PlainTime | null;
5
+ compareAsType(): string;
6
+ toJSON(value: Temporal.PlainTime | null): string | null;
7
+ getColumnType(prop: EntityProperty, platform: Platform): string;
8
+ }
@@ -0,0 +1,36 @@
1
+ import { Type } from "@mikro-orm/core";
2
+ export class PlainTimeType extends Type {
3
+ convertToDatabaseValue(value) {
4
+ if (value === null) {
5
+ return null;
6
+ }
7
+ return value.toString();
8
+ }
9
+ convertToJSValue(value) {
10
+ /* node:coverage disable */
11
+ if (value === null) {
12
+ return null;
13
+ }
14
+ /* node:coverage enable */
15
+ if (value instanceof Date) {
16
+ return Temporal.PlainTime.from({
17
+ hour: value.getHours(),
18
+ minute: value.getMinutes(),
19
+ second: value.getSeconds(),
20
+ millisecond: value.getMilliseconds(),
21
+ });
22
+ }
23
+ return Temporal.PlainTime.from(value);
24
+ }
25
+ compareAsType() {
26
+ return "string";
27
+ }
28
+ /* node:coverage disable */
29
+ toJSON(value) {
30
+ return value ? value.toString() : null;
31
+ }
32
+ /* node:coverage enable */
33
+ getColumnType(prop, platform) {
34
+ return platform.getTimeTypeDeclarationSQL(prop.length);
35
+ }
36
+ }
@@ -0,0 +1,4 @@
1
+ import type { Platform } from "@mikro-orm/core";
2
+ export declare const isPostgres: (platform: Platform) => boolean;
3
+ export declare const isMySql: (platform: Platform) => boolean;
4
+ export declare const isMsSql: (platform: Platform) => boolean;
package/dist/utils.js ADDED
@@ -0,0 +1,4 @@
1
+ export const isPostgres = (platform) => platform.constructor.name === "PostgreSqlPlatform";
2
+ export const isMySql = (platform) => platform.constructor.name === "MySqlPlatform" ||
3
+ platform.constructor.name === "MariaDbPlatform";
4
+ export const isMsSql = (platform) => platform.constructor.name === "MsSqlPlatform";
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "mikro-orm-temporal",
3
+ "version": "1.0.0",
4
+ "description": "Temporal types for Mikro ORM",
5
+ "type": "module",
6
+ "author": "Ben Scholzen 'DASPRiD'",
7
+ "license": "BSD-3-Clause",
8
+ "keywords": [
9
+ "temporal",
10
+ "mikro-orm",
11
+ "Interval",
12
+ "duration",
13
+ "PlainDate",
14
+ "PlainDateTime",
15
+ "PlainTime",
16
+ "ZonedDateTime",
17
+ "typescript",
18
+ "mysql",
19
+ "mariadb",
20
+ "postgres",
21
+ "mssql",
22
+ "sqlite"
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/dasprid/mikro-orm-temporal.git"
27
+ },
28
+ "files": [
29
+ "dist/**/*"
30
+ ],
31
+ "exports": {
32
+ ".": {
33
+ "import": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/index.js"
36
+ }
37
+ }
38
+ },
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.build.json",
41
+ "test": "c8 tsx --test",
42
+ "test:ci": "c8 --reporter=lcov tsx --test",
43
+ "format": "biome format . --write",
44
+ "check": "biome check . --write"
45
+ },
46
+ "devDependencies": {
47
+ "@biomejs/biome": "2.0.0",
48
+ "@commitlint/cli": "^19.8.1",
49
+ "@commitlint/config-conventional": "^19.8.1",
50
+ "@mikro-orm/better-sqlite": "^6.4.16",
51
+ "@mikro-orm/core": "^6.4.16",
52
+ "@mikro-orm/mariadb": "^6.4.16",
53
+ "@mikro-orm/mssql": "^6.4.16",
54
+ "@mikro-orm/mysql": "^6.4.16",
55
+ "@mikro-orm/postgresql": "^6.4.16",
56
+ "@tsconfig/node22": "^22.0.2",
57
+ "@types/node": "^24.0.3",
58
+ "c8": "^10.1.3",
59
+ "lefthook": "^1.11.14",
60
+ "temporal-polyfill": "^0.3.0",
61
+ "temporal-spec": "^0.3.0",
62
+ "tsx": "^4.20.3",
63
+ "typescript": "^5.8.3"
64
+ },
65
+ "peerDependencies": {
66
+ "@mikro-orm/core": "^6.0.5"
67
+ },
68
+ "packageManager": "pnpm@10.6.5+sha512.cdf928fca20832cd59ec53826492b7dc25dc524d4370b6b4adbf65803d32efaa6c1c88147c0ae4e8d579a6c9eec715757b50d4fa35eea179d868eada4ed043af",
69
+ "pnpm": {
70
+ "onlyBuiltDependencies": [
71
+ "better-sqlite3",
72
+ "esbuild",
73
+ "lefthook"
74
+ ]
75
+ }
76
+ }