-
-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postgres): add support for
interval
type
Closes #5181
- Loading branch information
Showing
10 changed files
with
157 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Type } from './Type'; | ||
import type { Platform } from '../platforms'; | ||
import type { EntityProperty } from '../typings'; | ||
|
||
export class IntervalType extends Type<string | null | undefined, string | null | undefined> { | ||
|
||
override getColumnType(prop: EntityProperty, platform: Platform) { | ||
return platform.getIntervalTypeDeclarationSQL(prop); | ||
} | ||
|
||
override convertToJSValue(value: string | null | undefined, platform: Platform): string | null | undefined { | ||
return platform.convertIntervalToJSValue(value!) as string; | ||
} | ||
|
||
override convertToDatabaseValue(value: string | null | undefined, platform: Platform): string | null | undefined { | ||
return platform.convertIntervalToDatabaseValue(value) as string; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Dictionary, Entity, MikroORM, PrimaryKey, Property } from '@mikro-orm/core'; | ||
import { PostgreSqlDriver } from '@mikro-orm/postgresql'; | ||
import { SqliteDriver } from '@mikro-orm/sqlite'; | ||
import { mockLogger } from '../../helpers'; | ||
|
||
@Entity() | ||
class Something { | ||
|
||
@PrimaryKey() | ||
id!: number; | ||
|
||
@Property({ | ||
type: 'interval', | ||
nullable: true, | ||
}) | ||
durationBuggy?: Dictionary | string; | ||
|
||
} | ||
|
||
test('interval columns (postgres)', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Something], | ||
driver: PostgreSqlDriver, | ||
dbName: 'mikro_orm_interval_type', | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
|
||
await expect(orm.schema.getCreateSchemaSQL()).resolves.toMatch('"duration_buggy" interval null'); | ||
|
||
const diff = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff).toBe(''); | ||
|
||
const mock = mockLogger(orm); | ||
orm.em.create(Something, { durationBuggy: '1s' }); | ||
await orm.em.flush(); | ||
await orm.em.flush(); // to check for extra updates | ||
orm.em.clear(); | ||
const r = await orm.em.findOneOrFail(Something, { id: 1 }); | ||
(r.durationBuggy as Dictionary).seconds = 5; | ||
await orm.em.flush(); | ||
await orm.em.flush(); // to check for extra updates | ||
|
||
expect(mock.mock.calls).toHaveLength(7); | ||
expect(mock.mock.calls[0][0]).toMatch('begin'); | ||
expect(mock.mock.calls[1][0]).toMatch('insert into "something" ("duration_buggy") values (\'1s\') returning "id"'); | ||
expect(mock.mock.calls[2][0]).toMatch('commit'); | ||
expect(mock.mock.calls[3][0]).toMatch('select "s0".* from "something" as "s0" where "s0"."id" = 1 limit 1'); | ||
expect(mock.mock.calls[4][0]).toMatch('begin'); | ||
expect(mock.mock.calls[5][0]).toMatch('update "something" set "duration_buggy" = \'5 seconds\' where "id" = 1'); | ||
expect(mock.mock.calls[6][0]).toMatch('commit'); | ||
|
||
await orm.close(true); | ||
}); | ||
|
||
test('interval columns (sqlite)', async () => { | ||
const orm = await MikroORM.init({ | ||
entities: [Something], | ||
driver: SqliteDriver, | ||
dbName: ':memory:', | ||
}); | ||
await orm.schema.refreshDatabase(); | ||
|
||
await expect(orm.schema.getCreateSchemaSQL()).resolves.toMatch('`duration_buggy` interval null'); | ||
|
||
const diff = await orm.schema.getUpdateSchemaSQL({ wrap: false }); | ||
expect(diff).toBe(''); | ||
|
||
const mock = mockLogger(orm); | ||
orm.em.create(Something, { durationBuggy: '1s' }); | ||
await orm.em.flush(); | ||
await orm.em.flush(); // to check for extra updates | ||
orm.em.clear(); | ||
const r = await orm.em.findOneOrFail(Something, { id: 1 }); | ||
r.durationBuggy = '5s'; | ||
await orm.em.flush(); | ||
await orm.em.flush(); // to check for extra updates | ||
|
||
expect(mock.mock.calls).toHaveLength(7); | ||
expect(mock.mock.calls[0][0]).toMatch('begin'); | ||
expect(mock.mock.calls[1][0]).toMatch('insert into `something` (`duration_buggy`) values (\'1s\') returning `id`'); | ||
expect(mock.mock.calls[2][0]).toMatch('commit'); | ||
expect(mock.mock.calls[3][0]).toMatch('select `s0`.* from `something` as `s0` where `s0`.`id` = 1 limit 1'); | ||
expect(mock.mock.calls[4][0]).toMatch('begin'); | ||
expect(mock.mock.calls[5][0]).toMatch('update `something` set `duration_buggy` = \'5s\' where `id` = 1'); | ||
expect(mock.mock.calls[6][0]).toMatch('commit'); | ||
|
||
await orm.close(true); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.