Skip to content

Commit

Permalink
Enh/databases fixes (#2665)
Browse files Browse the repository at this point in the history
* remove extraneous dubquotes

* update front-api to work with new core-api

---------

Co-authored-by: Henry Fontanier <henry@dust.tt>
  • Loading branch information
fontanierh and Henry Fontanier authored Nov 24, 2023
1 parent f23789a commit 8b4ef32
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions core/src/databases/table_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl ToSql for SqlParam {
match self {
SqlParam::Int(i) => i.to_sql(),
SqlParam::Float(f) => f.to_sql(),
SqlParam::Text(s) => Ok(ToSqlOutput::Owned(format!("\"{}\"", s).into())),
SqlParam::Text(s) => s.to_sql(),
SqlParam::Bool(b) => match b {
true => 1.to_sql(),
false => 0.to_sql(),
Expand Down Expand Up @@ -456,7 +456,7 @@ mod tests {

assert_eq!(field1, 1);
assert_eq!(field2, 2.4);
assert_eq!(field3, "\"text\"");
assert_eq!(field3, "text");
assert_eq!(field4, true);
} else {
return Err(anyhow!("No rows found after insert"));
Expand Down
10 changes: 4 additions & 6 deletions front/lib/core_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ export type CoreAPIDatabaseTable = {
};

export type CoreAPIDatabaseRow = {
created: number;
table_id: string;
row_id: string;
content: Record<string, unknown>;
value: Record<string, unknown>;
};

export type CoreAPIDatabaseSchema = Record<
Expand Down Expand Up @@ -1002,14 +1000,14 @@ export const CoreAPI = {
dataSourceName,
databaseId,
tableId,
contents,
rows,
truncate,
}: {
projectId: string;
dataSourceName: string;
databaseId: string;
tableId: string;
contents: Record<string, CoreAPIDatabaseRow["content"]>;
rows: CoreAPIDatabaseRow[];
truncate?: boolean;
}): Promise<CoreAPIResponse<{ success: true }>> {
const response = await fetch(
Expand All @@ -1020,7 +1018,7 @@ export const CoreAPI = {
"Content-Type": "application/json",
},
body: JSON.stringify({
contents: contents,
rows,
truncate: truncate || false,
}),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import logger from "@app/logger/logger";
import { apiError, withLogging } from "@app/logger/withlogging";

const UpsertDatabaseRowsRequestBodySchema = t.type({
contents: t.record(
t.string,
t.record(t.string, t.union([t.string, t.null, t.number, t.boolean]))
rows: t.array(
t.type({
row_id: t.string,
value: t.record(
t.string,
t.union([t.string, t.null, t.number, t.boolean])
),
})
),
truncate: t.union([t.boolean, t.undefined]),
});
Expand Down Expand Up @@ -146,8 +151,8 @@ async function handler(
});
}

const { rows, total } = listRes.value;
return res.status(200).json({ rows, offset, limit, total });
const { rows: rowsList, total } = listRes.value;
return res.status(200).json({ rows: rowsList, offset, limit, total });

case "POST":
const bodyValidation = UpsertDatabaseRowsRequestBodySchema.decode(
Expand All @@ -163,14 +168,14 @@ async function handler(
status_code: 400,
});
}
const { contents, truncate } = bodyValidation.right;
const { rows: rowsToUpsert, truncate } = bodyValidation.right;

const upsertRes = await CoreAPI.upsertDatabaseRows({
projectId: dataSource.dustAPIProjectId,
dataSourceName: dataSource.name,
databaseId,
tableId: tableId,
contents,
rows: rowsToUpsert,
truncate,
});

Expand Down

0 comments on commit 8b4ef32

Please sign in to comment.