Replies: 1 comment
-
That's pretty much the most straightforward and simplest way to do it, it's how I do it using SvelteKit. Since model queries return models as result we'll have to transform them to JSON. If you're working with large dataset, then I would suggest paginating results or streaming. If you want to abstract away always using the map method on results you can create a custom QueryBuilder and add a new method export class CustomQueryBuilder<M extends Model, R> extends Model.QueryBuilder<M, R> {
toJson() {
return this.runAfter((result: any) => {
if (Array.isArray(result)) {
return result.map((r) => r.toJSON());
} else if (result) {
return result.toJSON();
}
return result;
});
}
} export class BaseModel extends Model {
static idColumn: string | string[] = "id";
id: number;
created_at: string;
updated_at: string;
static get QueryBuilder() {
return CustomQueryBuilder;
} And you can use it like: const queryResultsInJson = await BaseModel.query().toJson(); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
This might be an issue related to Svelte, but I still think is worth asking the difference, or why a
knex
query result is serializable and anobjection
is not.Example on a
+page.server.ts
in Svelte:The above code fails on Svelte with a
Cannot stringify arbitrary non-POJOs
error. I can circumvent this issue withusers: users.map((u) => u.toJSON()),
but seems inefficient for large resultsets.What is most curious to me, is that a
knex
query works without parsing to JSON:Any advice on how to properly use Objection in Svelte is highly appreciated 🙏
Beta Was this translation helpful? Give feedback.
All reactions