-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DAL): support for booleans (#542)
This PR introduces support for booleans in the DAL. --------- Co-authored-by: David Martos <[email protected]>
- Loading branch information
1 parent
3ae3f30
commit 318b26d
Showing
34 changed files
with
1,295 additions
and
273 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"electric-sql": patch | ||
"@electric-sql/prisma-generator": patch | ||
--- | ||
|
||
Adds client-side support for booleans. |
11 changes: 11 additions & 0 deletions
11
clients/typescript/src/client/conversions/datatypes/boolean.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,11 @@ | ||
// Serialises a boolean to a number (0 for false and 1 for true) | ||
export function serialiseBoolean(v: boolean): number { | ||
return v ? 1 : 0 | ||
} | ||
|
||
// Deserialises a SQLite boolean (i.e. 0 or 1) into a boolean value | ||
export function deserialiseBoolean(v: number): boolean { | ||
if (v === 0) return false | ||
else if (v === 1) return true | ||
else throw new Error(`Could not parse boolean. Value is not 0 or 1: ${v}`) | ||
} |
76 changes: 76 additions & 0 deletions
76
clients/typescript/src/client/conversions/datatypes/date.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,76 @@ | ||
import { PgDateType } from '../types' | ||
|
||
// Serialises a `Date` object into a SQLite compatible date string | ||
export function serialiseDate(v: Date, pgType: PgDateType): string { | ||
switch (pgType) { | ||
case PgDateType.PG_TIMESTAMP: | ||
// Returns local timestamp | ||
return ignoreTimeZone(v).toISOString().replace('T', ' ').replace('Z', '') | ||
|
||
case PgDateType.PG_TIMESTAMPTZ: | ||
// Returns UTC timestamp | ||
return v.toISOString().replace('T', ' ') | ||
|
||
case PgDateType.PG_DATE: | ||
// Returns the local date | ||
return extractDateAndTime(ignoreTimeZone(v)).date | ||
|
||
case PgDateType.PG_TIME: | ||
// Returns the local time | ||
return extractDateAndTime(ignoreTimeZone(v)).time | ||
|
||
case PgDateType.PG_TIMETZ: | ||
// Returns UTC time | ||
return extractDateAndTime(v).time | ||
} | ||
} | ||
|
||
// Deserialises a SQLite compatible date string into a `Date` object | ||
export function deserialiseDate(v: string, pgType: PgDateType): Date { | ||
const parse = (v: any) => { | ||
const millis = Date.parse(v) | ||
if (isNaN(millis)) | ||
throw new Error(`Could not parse date, invalid format: ${v}`) | ||
else return new Date(millis) | ||
} | ||
|
||
switch (pgType) { | ||
case PgDateType.PG_TIMESTAMP: | ||
case PgDateType.PG_TIMESTAMPTZ: | ||
case PgDateType.PG_DATE: | ||
return parse(v) | ||
|
||
case PgDateType.PG_TIME: | ||
// interpret as local time | ||
return parse(`1970-01-01 ${v}`) | ||
|
||
case PgDateType.PG_TIMETZ: | ||
// interpret as UTC time | ||
return parse(`1970-01-01 ${v}+00`) | ||
} | ||
} | ||
|
||
/** | ||
* Corrects the provided `Date` such that | ||
* the current date is set as UTC date. | ||
* e.g. if it is 3PM in GMT+2 then it is 1PM UTC. | ||
* This function would return a date in which it is 3PM UTC. | ||
*/ | ||
function ignoreTimeZone(v: Date): Date { | ||
// `v.toISOString` returns the UTC time but we want the time in this timezone | ||
// so we get the timezone offset and subtract it from the current time in order to | ||
// compensate for the timezone correction done by `toISOString` | ||
const offsetInMs = 1000 * 60 * v.getTimezoneOffset() | ||
return new Date(v.getTime() - offsetInMs) | ||
} | ||
|
||
type ExtractedDateTime = { date: string; time: string } | ||
function extractDateAndTime(v: Date): ExtractedDateTime { | ||
const regex = /([0-9-]*)T([0-9:.]*)Z/g | ||
const [_, date, time] = regex.exec(v.toISOString())! as unknown as [ | ||
string, | ||
string, | ||
string | ||
] | ||
return { date, time } | ||
} |
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,28 @@ | ||
export enum PgBasicType { | ||
PG_BOOL = 'BOOL', | ||
PG_INT = 'INT', | ||
PG_INT2 = 'INT2', | ||
PG_INT4 = 'INT4', | ||
PG_INT8 = 'INT8', | ||
PG_INTEGER = 'INTEGER', | ||
PG_REAL = 'REAL', | ||
PG_FLOAT4 = 'FLOAT4', | ||
PG_FLOAT8 = 'FLOAT8', | ||
PG_TEXT = 'TEXT', | ||
PG_VARCHAR = 'VARCHAR', | ||
PG_CHAR = 'CHAR', | ||
PG_UUID = 'UUID', | ||
} | ||
|
||
/** | ||
* Union type of all Pg types that are represented by a `Date` in JS/TS. | ||
*/ | ||
export enum PgDateType { | ||
PG_TIMESTAMP = 'TIMESTAMP', | ||
PG_TIMESTAMPTZ = 'TIMESTAMPTZ', | ||
PG_DATE = 'DATE', | ||
PG_TIME = 'TIME', | ||
PG_TIMETZ = 'TIMETZ', | ||
} | ||
|
||
export type PgType = PgBasicType | PgDateType |
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
Oops, something went wrong.