Skip to content

Commit

Permalink
packages/modeldb: add js-like number type
Browse files Browse the repository at this point in the history
  • Loading branch information
raykyri committed Jul 12, 2024
1 parent 4809e20 commit f8c389d
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"react-chessboard": "^4.6.0",
"typescript": "^5.4.5",
"veaury": "^2.4.0",
"vite": "^5.3.3",
"vite": "^5.3.3",
"vitepress": "^1.2.3",
"vue": "^3.3.7",
"vue-markdown-render": "^2.2.1"
Expand Down
1 change: 1 addition & 0 deletions packages/modeldb-pg/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
const primitiveColumnTypes = {
integer: "BIGINT",
float: "DECIMAL",
number: "DOUBLE PRECISION",
string: "TEXT",
bytes: "BYTEA",
boolean: "BOOLEAN",
Expand Down
4 changes: 2 additions & 2 deletions packages/modeldb-pg/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function encodePrimitiveValue(
} else {
throw new TypeError(`${modelName}/${property.name} must be a safely representable integer`)
}
} else if (property.type === "float") {
} else if (property.type === "number" || property.type === "float") {
if (typeof value === "number") {
return value
} else {
Expand Down Expand Up @@ -176,7 +176,7 @@ export function decodePrimitiveValue(modelName: string, property: PrimitivePrope
console.error("expected integer, got", value)
throw new Error(`internal error - invalid ${modelName}/${property.name} value (expected integer)`)
}
} else if (property.type === "float") {
} else if (property.type === "number" || property.type === "float") {
if (typeof value === "string") {
return parseFloat(value)
} else {
Expand Down
1 change: 1 addition & 0 deletions packages/modeldb-sqlite/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Params = Record<`p${string}`, string | number | Buffer | null>
const primitiveColumnTypes = {
integer: "INTEGER",
float: "FLOAT",
number: "NUMERIC",
string: "TEXT",
bytes: "BLOB",
boolean: "INTEGER",
Expand Down
4 changes: 2 additions & 2 deletions packages/modeldb-sqlite/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function encodePrimitiveValue(
} else {
throw new TypeError(`${modelName}/${property.name} must be a safely representable integer`)
}
} else if (property.type === "float") {
} else if (property.type === "number" || property.type === "float") {
if (typeof value === "number") {
return value
} else {
Expand Down Expand Up @@ -175,7 +175,7 @@ export function decodePrimitiveValue(modelName: string, property: PrimitivePrope
console.error("expected integer, got", value)
throw new Error(`internal error - invalid ${modelName}/${property.name} value (expected integer)`)
}
} else if (property.type === "float") {
} else if (property.type === "number" || property.type === "float") {
if (typeof value === "number") {
return value
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/modeldb/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function parseConfig(init: ModelSchema): Config {
return { relations, models }
}

export const primitivePropertyPattern = /^(integer|float|string|bytes|boolean|json)(\??)$/
export const primitivePropertyPattern = /^(integer|float|number|string|bytes|boolean|json)(\??)$/
export const referencePropertyPattern = /^@([a-z0-9.-]+)(\??)$/
export const relationPropertyPattern = /^@([a-z0-9.-]+)\[\]$/

Expand Down
1 change: 1 addition & 0 deletions packages/modeldb/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const booleanOrder: Order = {
export const primitiveTypeOrders: Record<PrimitiveType, Order> = {
integer: numberOrder,
float: numberOrder,
number: numberOrder,
string: stringOrder,
bytes: byteOrder,
boolean: booleanOrder,
Expand Down
2 changes: 1 addition & 1 deletion packages/modeldb/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// These are "init types" for the `models` value used to initialize the database.

export type PrimaryKeyType = "primary"
export type PrimitiveType = "integer" | "float" | "string" | "bytes" | "boolean" | "json"
export type PrimitiveType = "integer" | "float" | "number" | "string" | "bytes" | "boolean" | "json"
export type OptionalPrimitiveType = `${PrimitiveType}?`
export type ReferenceType = `@${string}`
export type OptionalReferenceType = `@${string}?`
Expand Down
2 changes: 1 addition & 1 deletion packages/modeldb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function validatePropertyValue(modelName: string, property: Property, val
} else if (!Number.isSafeInteger(value)) {
throw new TypeError(`write to db.${modelName}.${property.name}: must be a valid Number.isSafeInteger()`)
}
} else if (property.type === "float") {
} else if (property.type === "number" || property.type === "float") {
if (typeof value !== "number") {
throw new TypeError(`write to db.${modelName}.${property.name}: expected a number, received a ${typeof value}`)
}
Expand Down

0 comments on commit f8c389d

Please sign in to comment.