Replies: 3 comments 1 reply
-
Yes, |
Beta Was this translation helpful? Give feedback.
-
So with
I want to disable it because I parse custom types in Slonik as well: for example Postgis Also what happens to joined props (references)? I have two different cases to cover: in the first one I have just the id of the referenced entity and in the second one the whole entity has been prefixed in the query result.
Query result (after Slonik parsing):
Does it create a reference for the author? Or should I create it with If it creates a reference for the publisher I'm pretty sure it won't be loaded, so here is what I do:
At this point I should be able to cast Is there any sugar syntax to create the reference directly form the entity I want to reference? Like |
Beta Was this translation helpful? Give feedback.
-
Another option would be to leave type JSType = Point | undefined;
type DBType = string | undefined | Point; // It's Point if it has already been parsed by slonik
export class PointType extends Type<JSType, DBType> {
convertToJSValue(value: DBType): JSType {
if (value == null) {
return value;
}
// Already parsed by slonik
if (value instanceof Point) {
return value;
}
const parsed = JSON.parse(value);
[...]
}
[...]
}
Taking advantage of PostgreSQL specific features not supported by either knew or Query Builder: my only option is to use plain SQL. From the previous book example let's say the frontend queried the following: {
books(authorName: 'Mark') {
name
author: {
id
name
}
publisher: {
id
vatId
}
}
} Let's say this was a very complex query and I had to use Slonik because of otherwise unsupported Postgres specific features. Let's have a look at a slightly more complex example: GraphQL query 1 {
chats(oderBy: 'lastMessage') {
id
propOne
propTwo
}
} For the first query we are forced to join with messages, order them and retrieve the last message. I could theoretically throw away the last message, but have a look at query 2: GraphQL query 2 {
chats(oderBy: 'lastMessage') {
[...]
lastMessage: {
[...]
}
}
} We still have to retrieve the last message in the chats resolver because of the ordering, but the next resolver will have to fetch the lastMessage so we shouldn't throw it away. We can convert it to an entity but we can't store it into book.lastMessage because that's not a valid database field (it's only a valid GraphQL field with no direct match to database columns). So I additionally return an helper object with the lastMessage entity that later resolvers can use, if available. GraphQL Query 3 {
chats() {
[...]
lastMessage: {
[...]
}
}
} In this case the chats resolvers won't have to fetch the messages and so the lastMessage resolver will have to actually fire a query to return it. Slonik queries are typed (it uses |
Beta Was this translation helpful? Give feedback.
-
I was looking at the
EntityManager
map()
implementation and I started having headaches atgetResultMapper
and theFunction() Constructor
:)I want to map a PostgreSQL database result (not from
QueryBuilder
and not fromknex
, it's fromSlonik
) to an Entity and I've always usedem.map(Entity, row)
. I've recently started to useSlonik Interceptors
and in particular one which automatically transforms field names to camel case (user_id -> userId).If I understood the source code correctly
mapResult()
converts the field names into property names whilemerge()
makes it managed. So I guess I can simply skipmapResult
? Something likeem.merge(Entity, row)
would do the trick? What's therefresh
prop for?I've also added
type parsers
inSlonik
(for example timestamptz -> Date), would that be a problem for merge? I've seen aconvertCustomTypes
prop, so I guess that should befalse
and MAYBE for the built-in types it is smart enough to figure out if they are already parsed or not?Beta Was this translation helpful? Give feedback.
All reactions