Skip to content

Commit

Permalink
Add support for float4 in the DAL
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-dp committed Nov 16, 2023
1 parent 399b232 commit 23ea16e
Show file tree
Hide file tree
Showing 22 changed files with 9,849 additions and 6,188 deletions.
11 changes: 6 additions & 5 deletions clients/typescript/src/cli/migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,15 @@ function addValidator(ln: string): string {

if (field) {
const intValidator = '@zod.number.int().gte(-2147483648).lte(2147483647)'
const float8Validator = '@zod.custom.use(z.number().or(z.nan()))'
const floatValidator = '@zod.custom.use(z.number().or(z.nan()))'

// Map attributes to validators
const attributeValidatorMapping = new Map([
['@db.Uuid', '@zod.string.uuid()'],
['@db.SmallInt', '@zod.number.int().gte(-32768).lte(32767)'],
['@db.Int', intValidator],
['@db.DoublePrecision', float8Validator],
['@db.DoublePrecision', floatValidator],
['@db.Real', floatValidator],
])
const attribute = field.attributes
.map((a) => a.type)
Expand All @@ -371,9 +372,9 @@ function addValidator(ln: string): string {
['Int', intValidator],
['Int?', intValidator],
['Int[]', intValidator],
['Float', float8Validator],
['Float?', float8Validator],
['Float[]', float8Validator],
['Float', floatValidator],
['Float?', floatValidator],
['Float[]', floatValidator],
])
const typeValidator = typeValidatorMapping.get(field.type)

Expand Down
15 changes: 14 additions & 1 deletion clients/typescript/src/client/conversions/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export function toSqlite(v: any, pgType: PgType): any {
// and deserialise it back to `NaN` when reading from the DB.
// cf. https://github.com/WiseLibs/better-sqlite3/issues/1088
return 'NaN'
} else if (
pgType === PgBasicType.PG_FLOAT4 ||
pgType === PgBasicType.PG_REAL
) {
return Math.fround(v)
} else {
return v
}
Expand All @@ -46,10 +51,18 @@ export function fromSqlite(v: any, pgType: PgType): any {
return deserialiseBoolean(v)
} else if (
v === 'NaN' &&
(pgType === PgBasicType.PG_FLOAT8 || pgType === PgBasicType.PG_FLOAT4)
(pgType === PgBasicType.PG_FLOAT8 ||
pgType === PgBasicType.PG_FLOAT4 ||
pgType === PgBasicType.PG_REAL)
) {
// it's a serialised NaN
return NaN
} else if (
pgType === PgBasicType.PG_FLOAT4 ||
pgType === PgBasicType.PG_REAL
) {
// convert to float4 in case someone would have written a bigger value to SQLite directly
return Math.fround(v)
} else {
return v
}
Expand Down
3 changes: 2 additions & 1 deletion clients/typescript/test/client/conversions/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ await tbl.sync()
function setupDB() {
db.exec('DROP TABLE IF EXISTS DataTypes')
db.exec(
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float8' real, 'relatedId' int);"
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float4' real, 'float8' real, 'relatedId' int);"
)

db.exec('DROP TABLE IF EXISTS Dummy')
Expand Down Expand Up @@ -232,6 +232,7 @@ const dateNulls = {
bool: null,
int2: null,
int4: null,
float4: null,
float8: null,
uuid: null,
}
Expand Down
16 changes: 10 additions & 6 deletions clients/typescript/test/client/conversions/sqlite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await tbl.sync()
function setupDB() {
db.exec('DROP TABLE IF EXISTS DataTypes')
db.exec(
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float8' real, 'relatedId' int);"
"CREATE TABLE DataTypes('id' int PRIMARY KEY, 'date' varchar, 'time' varchar, 'timetz' varchar, 'timestamp' varchar, 'timestamptz' varchar, 'bool' int, 'uuid' varchar, 'int2' int2, 'int4' int4, 'float4' real, 'float8' real, 'relatedId' int);"
)
}

Expand Down Expand Up @@ -183,31 +183,35 @@ test.serial('floats are converted correctly to SQLite', async (t) => {
data: [
{
id: 1,
float4: 1.234,
float8: 1.234,
},
{
id: 2,
float4: NaN,
float8: NaN,
},
{
id: 3,
float4: Infinity,
float8: +Infinity,
},
{
id: 4,
float4: -Infinity,
float8: -Infinity,
},
],
})

const rawRes = await electric.db.raw({
sql: 'SELECT id, float8 FROM DataTypes ORDER BY id ASC',
sql: 'SELECT id, float4, float8 FROM DataTypes ORDER BY id ASC',
args: [],
})
t.deepEqual(rawRes, [
{ id: 1, float8: 1.234 },
{ id: 2, float8: 'NaN' },
{ id: 3, float8: Infinity },
{ id: 4, float8: -Infinity },
{ id: 1, float4: Math.fround(1.234), float8: 1.234 },
{ id: 2, float4: 'NaN', float8: 'NaN' },
{ id: 3, float4: Infinity, float8: Infinity },
{ id: 4, float4: -Infinity, float8: -Infinity },
])
})
173 changes: 0 additions & 173 deletions clients/typescript/test/client/generated/client/index-browser.js

This file was deleted.

Loading

0 comments on commit 23ea16e

Please sign in to comment.