-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
142 additions
and
35 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
20 changes: 20 additions & 0 deletions
20
packages/persistence/src/underlying/conversion/strategies/any-to-date-range.strategy.ts
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,20 @@ | ||
import type { Field } from "@undb/table" | ||
import { getDateRangeFieldName } from "../../underlying-table.util" | ||
import { UnderlyingConversionStrategy } from "../conversion.interface" | ||
|
||
export class AnyToDateRangeStrategy extends UnderlyingConversionStrategy { | ||
convert(field: Field, previousField: Field): void | Promise<void> { | ||
if (field.type !== "dateRange" || previousField.type !== "date") { | ||
return | ||
} | ||
const { start, end } = getDateRangeFieldName(field) | ||
|
||
const fieldId = field.id.value | ||
|
||
const addColumns = [this.tb.addColumn(start, "timestamp").compile(), this.tb.addColumn(end, "timestamp").compile()] | ||
|
||
const dropColumns = this.tb.dropColumn(fieldId).compile() | ||
|
||
this.addSql(...addColumns, dropColumns) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/persistence/src/underlying/conversion/strategies/date-to-date-range.strategy.ts
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,28 @@ | ||
import type { Field } from "@undb/table" | ||
import { getDateRangeFieldName } from "../../underlying-table.util" | ||
import { UnderlyingConversionStrategy } from "../conversion.interface" | ||
|
||
export class DateToDateRangeStrategy extends UnderlyingConversionStrategy { | ||
convert(field: Field, previousField: Field): void | Promise<void> { | ||
if (field.type !== "dateRange" || previousField.type !== "date") { | ||
return | ||
} | ||
const { start, end } = getDateRangeFieldName(field) | ||
|
||
const fieldId = field.id.value | ||
|
||
const addColumns = [this.tb.addColumn(start, "timestamp").compile(), this.tb.addColumn(end, "timestamp").compile()] | ||
|
||
const updated = this.qb | ||
.updateTable(this.table.id.value) | ||
.set((eb) => ({ | ||
[start]: eb.ref(fieldId), | ||
[end]: eb.ref(fieldId), | ||
})) | ||
.compile() | ||
|
||
const dropColumns = this.tb.dropColumn(fieldId).compile() | ||
|
||
this.addSql(...addColumns, updated, dropColumns) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/persistence/src/underlying/conversion/strategies/number-to-date-range.strategy.ts
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,29 @@ | ||
import type { Field } from "@undb/table" | ||
import { sql } from "kysely" | ||
import { getDateRangeFieldName } from "../../underlying-table.util" | ||
import { UnderlyingConversionStrategy } from "../conversion.interface" | ||
|
||
export class NumberToDateRangeStrategy extends UnderlyingConversionStrategy { | ||
convert(field: Field): void | Promise<void> { | ||
if (field.type !== "dateRange") { | ||
return | ||
} | ||
const { start, end } = getDateRangeFieldName(field) | ||
|
||
const fieldId = field.id.value | ||
|
||
const addColumns = [this.tb.addColumn(start, "timestamp").compile(), this.tb.addColumn(end, "timestamp").compile()] | ||
|
||
const updated = this.qb | ||
.updateTable(this.table.id.value) | ||
.set((eb) => ({ | ||
[start]: sql`datetime(${sql.ref(fieldId)}, 'unixepoch')`, | ||
[end]: sql`datetime(${sql.ref(fieldId)}, 'unixepoch')`, | ||
})) | ||
.compile() | ||
|
||
const dropColumns = this.tb.dropColumn(fieldId).compile() | ||
|
||
this.addSql(...addColumns, updated, dropColumns) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/persistence/src/underlying/conversion/strategies/string-to-date-range.strategy.ts
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,29 @@ | ||
import type { Field } from "@undb/table" | ||
import { sql } from "kysely" | ||
import { getDateRangeFieldName } from "../../underlying-table.util" | ||
import { UnderlyingConversionStrategy } from "../conversion.interface" | ||
|
||
export class StringToDateRangeStrategy extends UnderlyingConversionStrategy { | ||
convert(field: Field): void | Promise<void> { | ||
if (field.type !== "dateRange") { | ||
return | ||
} | ||
const { start, end } = getDateRangeFieldName(field) | ||
|
||
const fieldId = field.id.value | ||
|
||
const addColumns = [this.tb.addColumn(start, "timestamp").compile(), this.tb.addColumn(end, "timestamp").compile()] | ||
|
||
const updated = this.qb | ||
.updateTable(this.table.id.value) | ||
.set((eb) => ({ | ||
[start]: sql`DATE(${sql.ref(fieldId)})`, | ||
[end]: sql`DATE(${sql.ref(fieldId)})`, | ||
})) | ||
.compile() | ||
|
||
const dropColumns = this.tb.dropColumn(fieldId).compile() | ||
|
||
this.addSql(...addColumns, updated, dropColumns) | ||
} | ||
} |
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