Skip to content

Export DateUtils in typeorm module #3948

Closed

Description

@jacobg

Issue type:

[ ] question
[ ] bug report
[X] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[X ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

It would be very useful to export DateUtils in the typeorm module in order to aid in raw sql queries. Raw queries are particularly useful for me, because I am in the gradual process of moving sql tables into TypeORM, but it's just not feasible (or CI/CD-approach) to do it all in one release of my app. If I should elaborate more, please let me know.

Activity

  1. Kononnable commented on Apr 20, 2019

    @Kononnable
    Contributor

    Right now you can import it, but it requires a bit more complex statement: import { DateUtils } from "typeorm/util/DateUtils";. If you want to make it exported directyle from typeorm - make a PR :)

  2. jacobg commented on Apr 23, 2019

    @jacobg
    ContributorAuthor

    Thanks @Kononnable. I think the import you suggested is fine. I didn't realize that anyway typeorm is not pre-bundled, so it's not necessary to export from typeorm.

  3. reopened this on May 13, 2026
  4. jacobg commented on May 13, 2026

    @jacobg
    ContributorAuthor

    Re-opening this issue, as TypeORM is now pre-bundled.

  5. alumni commented on Jul 13, 2026

    @alumni
    Collaborator

    TypeORM is now pre-bundled.
    @jacobg what do you mean? there hasn't been any recent change in overall file structure of the published package, nor in the exports defined in package.json.

    BTW, how are you using DateUtils?

    We need to improve the way data types (especially dates) are handled - this might happen in a minor (not a major) release, depending if the overall change is breaking or not (but I would not consider DateUtils as public API, so if it breaks, it breaks).

  6. jacobg commented on Jul 13, 2026

    @jacobg
    ContributorAuthor

    @alumni

    This:

    import { DateUtils } from 'typeorm/util/DateUtils'

    causes this error:

    Cannot find module 'typeorm/util/DateUtils' or its corresponding type declarations.ts(2307)
    

    unless I do this:

    declare module 'typeorm/util/DateUtils'

    Is it because now typeorm's package.json defines its exports?

    typeorm/package.json

    Lines 19 to 45 in 0caca21

    "exports": {
    ".": {
    "types": "./index.d.ts",
    "react-native": {
    "default": "./browser/index.js"
    },
    "node": {
    "types": "./index.d.ts",
    "import": "./index.mjs",
    "require": "./index.js"
    },
    "browser": {
    "require": "./index.js",
    "import": "./browser/index.js",
    "default": "./index.js"
    }
    },
    "./browser": {
    "types": "./index.d.ts",
    "default": "./browser/index.js"
    },
    "./*.js": "./*.js",
    "./*": {
    "require": "./*.js",
    "import": "./*"
    }
    },

    That was added in 2022 when ESM support was added:
    3a694dd

    Our app uses DateUtils to help with db migration like this:

    export function prepareValueForMigration(value: any, column: TableColumnOptions) {
      if (value === 'true') return 1
      if (value === 'false') return 0
      if (value === '') return null
    
      if (column.type === 'datetime') {
        const epoch = Date.parse(value)
        if (!isNaN(epoch)) {
          // Make sure we save date as string in TypeORM's expected format of
          // '2018-03-14 02:33:33.906'. Because if it's in ISO 8601 format,
          // it can corrupt the string by adding a Z to a datetime string that
          // already has timezone at the end.
          // https://github.com/typeorm/typeorm/blob/master/src/driver/sqlite-abstract/AbstractSqliteDriver.ts#L273
          value = DateUtils.mixedDateToUtcDatetimeString(new Date(epoch))
        }
      }
    
      return value
    }
  7. alumni commented on Jul 13, 2026

    @alumni
    Collaborator

    @jacobg Okay, so you are doing something like Driver.preparePersistentValue. You are then passing this as a parameter, right? Via e.g. QueryRunner.query?

    Any reason why you can't use that function directly?

  8. jacobg commented on Jul 14, 2026

    @jacobg
    ContributorAuthor

    @jacobg Okay, so you are doing something like Driver.preparePersistentValue. You are then passing this as a parameter, right? Via e.g. QueryRunner.query?

    Any reason why you can't use that function directly?

    I'm not sure if we'll be able to get the ColumnMetadata that preparePersistentValue requires, since an older migration might not have the same metadata as current entity class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions