Questions tagged [objection.js]

An Object Relational Mapper (ORM) for Node.js

Formerly known as moron.js, objection.js is an Promise based ORM built on top of knex.js focusing on the loading and storing of javascript objects into relational databases, specifically the ones supported by knex.js.

It does not currently incorporate a query builder leaving that for knex.js.

Website: https://vincit.github.io/objection.js

Github repo: https://github.com/Vincit/objection.js/

284 questions
12
votes
2 answers

Print complete SQL for all queries made by objection.js

I'm looking for a way to capture the raw SQL for all the queries that the Objection.js library executes with the bindings interpolated into the SQL string. I realize that there's a Knex event handler that I can take advantage of but the second…
Eugene Kim
  • 496
  • 4
  • 17
8
votes
1 answer

How to test objection.js with jest?

Model.knex(knex); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(i18nextMiddleware); I want to test method getUsers of users controller. It get data from usersModel getUsers…
Vladimir Golub
  • 523
  • 1
  • 8
  • 22
7
votes
0 answers

How to run server-only code once on server startup in Next.js?

I am trying to access my User objection.js model from getStaticProps and getStaticPaths. However, I need to run import Knex from "knex"; import knexfile from "./knexfile"; import { Model } from "objection"; function setupDB() { const db =…
Eric
  • 2,635
  • 6
  • 26
  • 66
7
votes
1 answer

Jest Cannot find module 'pg-native' from 'client.js'

My project consists of node (express), knex, objectionjs with graphql. We are using Jest to test calls to graphql which behind the scenes uses objectionjs+knex to perform updates on Postgres db. Any jest tests calling an update to a table that has a…
Tiago Alcobia
  • 99
  • 1
  • 5
6
votes
1 answer

Excluding results from Objection/Knex query based on withGraphFetched results

I have two models in Objection - "brands" and "offers". Brand: const { Model } = require('objection') class Brand extends Model { static get tableName() { return 'brands' } ... static get relationMappings() { const Offer =…
JamesG
  • 2,018
  • 2
  • 28
  • 57
5
votes
1 answer

Vincit/Objection.js returns "cannot read property '$relation' of undefined" when using .withGraphFetched()

I am currently setting up Objection.js with NestJS and am having trouble using any sort of graph fetch or save methods. Relevant models: resource-group.model.ts import { BaseModel } from './base.model'; import { Model } from 'objection'; import {…
TheHiggsBroson
  • 1,410
  • 1
  • 11
  • 19
5
votes
1 answer

How to query for records less than 48 hours in Postgres using knex.js and objection.js?

I'd like to query for all records less than 48 hours of age using a created_at column. In PostgreSQL you can do something like: SELECT * from "media" WHERE updated_at >= now() - '48 hour'::INTERVAL; How do we write this in objection.js/knex.js…
PGT
  • 1,468
  • 20
  • 34
5
votes
5 answers

Batch insert of array data into mysql database using objection.js

I have an array newData = [{ sId: 'XXXXXX', itemlName: 'LSストレッ', iCode: 'XXXXXX', iType: '', skus: 'XXXXXXX', iLevel: 'L2', cCode: '88', cName: 'Other', sCode: '999', sName: 'No Control', pLengthCode: '988', core: 'N', sCode: '1', dCode: 'XX',…
Johncy Binoy
  • 169
  • 3
  • 13
4
votes
1 answer

Make objection model return match the TS interface

I'm working to implement an api using express/objection js and typescript. I took a lot of inspiration from this repo: https://github.com/HappyZombies/brackette-alpha/tree/master/server/src As the creator did, I want to have different interfaces for…
TPCRomain
  • 51
  • 2
4
votes
1 answer

How does Objection.js query builders know when a chained call is supposed to be executed?

As from the example from the docs await User.query() returns a promise with an array of users. await User.query().findById(1) returns a user with the ID of 1. How does the User.query() know when a it needs to be executed or if it is chained. I would…
Ramil Amparo
  • 612
  • 1
  • 9
  • 25
4
votes
1 answer

In Objection.js, what's the benefit of setting up relationMappings?

I'm kind of confused about what relationMappings do inside a Objection.js Model class. I thought once we setup the relationMapping inside a Model, we will get related data in every query. But, it turns out that I still only the Model properties…
Arel Lin
  • 908
  • 2
  • 13
  • 24
4
votes
1 answer

Many-to-Many-to-Many relationship in Objection.js

I have a project where an user can have many platforms. These platforms can have many passwords. Currently I have following database structure: Im trying to use eager loading to get the following object: { "id": 1, "username": "Keith", …
Rashomon
  • 5,962
  • 4
  • 29
  • 67
4
votes
0 answers

Objection insertGraph, insert new and relate or relate to existing rows

So, I'm not super knowledge with MySQL relations, upserting and such. I'm looking for an explanation on how (if?) this is possible to do. [ { scheduledAt: '17:55', league: { name: 'Champions League - Group Stage' } }, { …
tribe
  • 321
  • 2
  • 9
4
votes
2 answers

Objection.js Stubbing Chained "whereIn" methods with Sinon

Trying to stub a chained knex query using Sinon. The query looks like the following const result = await TableModel .query() .whereIn('id', idList) .whereIn('value', valueList); Normally I use a helper function that I created that returns an…
parrott-kevin
  • 118
  • 1
  • 7
4
votes
1 answer

How to 'squash' knex migrations for an Objection/PostgreSQL database?

We have a PostgreSQL database, and have defined JavaScript ORM classes using Objection.js. As our database structure has evolved, we have been defining database migrations using Knex. We would now like to 'squash' the migrations, e.g. into a…
1
2 3
18 19