-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to do an insert based on a custom type #139
Comments
Hi, have you tried with |
Thanks for your answer @remidewitte. I tried with this but it manages JSON vs. non JSON (plus a cast). A custom type requires a ROW(...) to be used (and for arrays with an explicit cast). I was able to solve it with two mapping functions to map a single row and to map an array: Updated database table to include an array as well: CREATE TABLE app_public.my_field (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
field source_field,
fields source_field[]
); The two helper functions I created that manually build the SQL fragment: const createArray = (rows: SQLFragment[]): SQLFragment =>
new SQLFragment(
['ARRAY[', ...Array.from({ length: rows.length - 1 }, () => ', '), ']'],
rows,
);
/** The type values must be provided in the order of how the SQL type defined its fields */
const createRow = (typeValues: any[], sqlType?: string): SQLFragment =>
new SQLFragment(
[
'ROW(',
...Array.from({ length: typeValues.length - 1 }, () => ', '),
sqlType ? `)::${sqlType}` : ')',
],
typeValues.map((r) => param(r)),
); Then I can use it like this: const row = createRow(
['title', JSON.stringify('some value')],
'source_field',
);
const array = createArray(
[
['title', JSON.stringify('some value')],
['description', JSON.stringify('my description')],
].map((r) => createRow(r, 'source_field')),
);
const res = await insert(
'my_field',
{
field: row,
fields: array,
},
{
returning: ['fields'],
},
).run(txn); I will leave it open - maybe that could be added in a nice way directly to zapatos. But feel free to just close it as I have a solution that works for me. But it is likely not easy to generalize it as the sort order on how to create the row is important so just providing a JS object would not be possible. |
Thanks. It's on my TODO list to take a look at this, but I have a lot going on at present. |
I have the following (simplified) definition of a type and a table:
I tried to do an insert with the following construct:
I get the error:
Using plain SQL the following works:
Is it possible to do this with zapatos? In my case I have an array of such objects so manually doing a manual
sql<...>
insert does not work either (or I don't know how) as I need multiple ROWs.The text was updated successfully, but these errors were encountered: