1

I'm using NextAuth to create a web service and connected it to a MongoDB database. I'm now able to log in with Google Auth and the records are saving correctly to my database. I now need to add new models to my database and add to them via API routes.

I followed these instructions to add a custom model Test.

Test.js

export default class Test {
  constructor(name) {
    if (name) {
      this.name = name;
    }
  }
}

export const TestSchema = {
  name: 'Test',
  target: Test,
  columns: {
    id: {
      // This property has `objectId: true` instead of `type: int` in MongoDB
      primary: true,
      type: 'int',
      generated: true,
    },
    name: {
      type: 'varchar',
      nullable: true,
    },
    createdAt: {
      type: 'timestamp',
      createDate: true,
    },
    updatedAt: {
      type: 'timestamp',
      updateDate: true,
    },
  },
};

index.js

import Test, { TestSchema } from './Test';

export default {
  Test: {
    model: Test,
    schema: TestSchema,
  },
};

The I updated [...nextauth.js] to add Test to my adapter

adapter: Adapters.TypeORM.Adapter(
  process.env.DATABASE_URL,
  {
    models: {
      Test: Models.Test,
    },
  }
)

At this point I'm not seeing any errors but I'm not sure it's added correctly. So I want to try to create a test Test document. However, I can't find any documentation on how to do this. I need to have access to the database connection but I've tried logging everything imported from 'next-auth/adapters', 'next-auth/client', 'next-auth', req, etc. and can't find anywhere that this connection is stored. I'd think I need to call the getAdapter() function stored in the TypeORM Adapter class but I can't seem to access it. I also tried accessing any of the built in methods like setUser and can't get those either. So I can't find any way to manually modify database records.

4

0 回答 0