7

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 = Knex(knexfile[process.env.NODE_ENV]);
  Model.knex(db);
}

on server startup.

When I do

  const users = await User.query();

inside getStaticPaths, I get Error: no database connection available for a query. You need to bind the model class or the query to a knex instance. error.

I tried running setupDB() inside _app.js with typeof window === "undefined" check, but just importing Knex and objection in _app.js throws an error.

Where should I run this? I really really want to avoid extending server.js.

(I should add there was a discussion on next.js github, but not sure if there's a way to do it specific to Objection.js, or any recent development.

Eric
  • 2,635
  • 6
  • 26
  • 66

0 Answers0