Revert schema plumbing, need to revisit.

This commit is contained in:
Sam Rose 2024-07-29 18:20:49 +01:00
parent e1ef66bf56
commit 25ab2e2689
No known key found for this signature in database
2 changed files with 77 additions and 34 deletions

View File

@ -205,39 +205,28 @@ class InternalBuilder {
return identifier return identifier
} }
private parse(input: any, schema: FieldSchema) { private parse(input: any) {
if (Array.isArray(input)) {
return JSON.stringify(input)
}
if (input == undefined) { if (input == undefined) {
return null return null
} }
if (typeof input !== "string") {
if (isPlainObject(input)) {
for (const [key, value] of Object.entries(input)) {
input[key] = this.parse(value, schema)
}
return input return input
} }
if (schema.type === FieldType.DATETIME && schema.timeOnly) {
if (this.client === SqlClient.ORACLE) {
return new Date(`1970-01-01 ${input}`)
}
}
if (typeof input === "string") {
if (isInvalidISODateString(input)) { if (isInvalidISODateString(input)) {
return null return null
} }
if (isValidISODateString(input)) { if (isValidISODateString(input)) {
return new Date(input.trim()) return new Date(input.trim())
} }
}
return input return input
} }
private parseBody(body: any) { private parseBody(body: any) {
for (let [key, value] of Object.entries(body)) { for (let [key, value] of Object.entries(body)) {
body[key] = this.parse(value, this.table.schema[key]) body[key] = this.parse(value)
} }
return body return body
} }
@ -246,23 +235,77 @@ class InternalBuilder {
if (!filters) { if (!filters) {
return {} return {}
} }
for (let [key, value] of Object.entries(filters)) {
for (const [_, filter] of Object.entries(filters)) { let parsed
for (const [key, value] of Object.entries(filter)) { if (typeof value === "object") {
const { column } = new ColumnSplitter([this.table]).run(key) parsed = this.parseFilters(value)
const schema = this.table.schema[column] } else {
if (!schema) { parsed = this.parse(value)
throw new Error(
`Column ${key} does not exist in table ${this.table._id}`
)
} }
filter[key] = this.parse(value, schema) // @ts-ignore
filters[key] = parsed
} }
}
return filters return filters
} }
// private parse(input: any, schema: FieldSchema) {
// if (input == undefined) {
// return null
// }
// if (isPlainObject(input)) {
// for (const [key, value] of Object.entries(input)) {
// input[key] = this.parse(value, schema)
// }
// return input
// }
// if (schema.type === FieldType.DATETIME && schema.timeOnly) {
// if (this.client === SqlClient.ORACLE) {
// return new Date(`1970-01-01 ${input}`)
// }
// }
// if (typeof input === "string") {
// if (isInvalidISODateString(input)) {
// return null
// }
// if (isValidISODateString(input)) {
// return new Date(input.trim())
// }
// }
// return input
// }
// private parseBody(body: any) {
// for (let [key, value] of Object.entries(body)) {
// body[key] = this.parse(value, this.table.schema[key])
// }
// return body
// }
// private parseFilters(filters: SearchFilters | undefined): SearchFilters {
// if (!filters) {
// return {}
// }
// for (const [_, filter] of Object.entries(filters)) {
// for (const [key, value] of Object.entries(filter)) {
// const { column } = new ColumnSplitter([this.table]).run(key)
// const schema = this.table.schema[column]
// if (!schema) {
// throw new Error(
// `Column ${key} does not exist in table ${this.table._id}`
// )
// }
// filter[key] = this.parse(value, schema)
// }
// }
// return filters
// }
// right now we only do filters on the specific table being queried // right now we only do filters on the specific table being queried
addFilters( addFilters(
query: Knex.QueryBuilder, query: Knex.QueryBuilder,

View File

@ -2405,7 +2405,7 @@ describe.each([
await createRows([{ date: earlyDate }, { date: laterDate }]) await createRows([{ date: earlyDate }, { date: laterDate }])
}) })
it.only("should be able to handle a date search", async () => { it("should be able to handle a date search", async () => {
await expectSearch({ await expectSearch({
query: { query: {
range: { range: {