kleydon / prisma-session-store

Express session store for Prisma
MIT License
116 stars 18 forks source link

Initial setup issue #49

Closed mjknight50 closed 2 years ago

mjknight50 commented 2 years ago

I've followed the instructions, but I keep getting this error: Screenshot from 2021-06-11 14-31-09

Argument of type 'PrismaClient<{ errorFormat: "minimal"; }, never, false>' is not assignable to parameter of type 'IPrisma'. The types of 'session.create' are incompatible between these types. Type '(args: SelectSubset<T, SessionCreateArgs>) => CheckSelect<T, PrismaSessionClient, PrismaSessionClient<...>>' is not assignable to type '(args: ICreateArgs) => Promise'. Types of parameters 'args' and 'args' are incompatible. Type 'ICreateArgs' is not assignable to type '{ select?: SessionSelect; data: (Without<SessionCreateInput, SessionUncheckedCreateInput> & SessionUncheckedCreateInput) | (Without<...> & SessionCreateInput); }'. Types of property 'data' are incompatible. Type 'ICreatePrismaSession' is not assignable to type '(Without<SessionCreateInput, SessionUncheckedCreateInput> & SessionUncheckedCreateInput) | (Without<...> & SessionCreateInput)'. Type 'ICreatePrismaSession' is not assignable to type 'Without<SessionUncheckedCreateInput, SessionCreateInput> & SessionCreateInput'. Type 'ICreatePrismaSession' is not assignable to type 'Without<SessionUncheckedCreateInput, SessionCreateInput>'. Types of property 'id' are incompatible. Type 'string' is not assignable to type 'never'.

My page imports look like this:

import expressSession from 'express-session'; import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient({ errorFormat: 'minimal' }); import { PrismaSessionStore } from '@quixo3/prisma-session-store';

kleydon commented 2 years ago

Hi @mjknight50,

I've just tried the following minimal server, which I think incorporates the same imports and app.use expression from your notes above:

import express from 'express';
import expressSession from 'express-session';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({ errorFormat: 'minimal' });
import { PrismaSessionStore } from '@quixo3/prisma-session-store';

// rest of the code remains same
const app = express();

app.use(
  expressSession({
    cookie: {
      maxAge: 7 * 24 * 60 * 60 * 100,
    },
    secret: 'a santa at nasa',
    store: new PrismaSessionStore(
        prisma, 
        {
          checkPeriod: 2* 60 * 1000,
          dbRecordIdIsSessionId: true,
          dbRecordIdFunction: undefined, 
        }
    ),
  })
)

const PORT = 8000;
app.get('/', (req, res) => res.send('Express + TypeScript Server'));
app.listen(PORT, () => {
  console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
});

This seems to work for me. At least, I'm not encountering any warning / error re: the prisma parameter. If you use this code sample, do you still encounter the same issue?

mjknight50 commented 2 years ago

Hi @kleydon ! Thanks for taking the time to help me out...

Yes, I get that same error copying your sample code.

I am on: "prisma": "^2.24.1", "@prisma/client": "^2.24.1", "@quixo3/prisma-session-store": "^3.0.1", "express": "^4.17.1", "express-session": "^1.17.2",

Here is my tsconfig in case that helps too:

{ "compilerOptions": { "declaration": false, "noUnusedLocals": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "lib": ["es2020", "dom"], "removeComments": false, "sourceMap": true, "module": "commonjs", "esModuleInterop": true, "moduleResolution": "node", "outDir": "../dist/", "target": "es2020", "allowJs": false, "typeRoots": ["../node_modules/@types"], "baseUrl": "." } }

kleydon commented 2 years ago

@mjknight50 hmmm...

The module versions I've used for testing on my end are slightly different, but I'd be surprised if they account for the problem.

Here are all the files for my test case (below). You'll still need to run npm/yarn install, for all the node modules. Perhaps you can use this as a base, and progressively, slowly add in changes from your test, until the problem reveals itself? I'd love to know what's going on, if you do find something amiss.

test-server.zip

Another approach might be to create a project/branch that reproduces what you are seeing, so that we're all looking at the same thing.

Best,

-K

mjknight50 commented 2 years ago

@kleydon Using your sample code, without changing anything, I get a slightly different error:

{
    "resource": "/home/knight/Desktop/test-server/test-server/index.ts",
    "owner": "typescript",
    "code": "2345",
    "severity": 8,
    "message": "Argument of type 'PrismaClient<{ errorFormat: \"minimal\"; }, never, false>' is not assignable to parameter of type 'IPrisma'.\n  Property 'session' is missing in type 'PrismaClient<{ errorFormat: \"minimal\"; }, never, false>' but required in type 'IPrisma'.",
    "source": "ts",
    "startLineNumber": 18,
    "startColumn": 9,
    "endLineNumber": 18,
    "endColumn": 15,
    "relatedInformation": [
        {
            "startLineNumber": 44,
            "startColumn": 5,
            "endLineNumber": 44,
            "endColumn": 12,
            "message": "'session' is declared here.",
            "resource": "/home/knight/Desktop/test-server/test-server/node_modules/@quixo3/prisma-session-store/dist/@types/prisma.d.ts"
        }
    ]
}

Here is some more info:

❯ npm -v 7.13.0 ❯ node -v v14.15.1

I tried with both yarn and npm because sometimes we've seen differences there.

kleydon commented 2 years ago

@mjknight50 Not sure what's going on there.

Two thoughts: 1) Would it be possible for you to create a branch that includes everything needed to try to reproduce the problem? 2) Curious what happens when you try to run the above code sample within a docker environment, further isolating it from your local machine environment.

-K

sayertindall commented 2 years ago

I'm also running into this error...

kleydon commented 2 years ago

@sayerrrr, care to post a reproduction branch, to help diagnose?

mjknight50 commented 2 years ago

@kleydon Hopefully this helps..

❯ npm -v 7.13.0 ❯ node -v v14.15.1

reproduction.zip

erfanasbari commented 2 years ago

@mjknight50 This is happening because you did not run prisma generate after editing schema.prisma and the @prisma/client types does not include Session data model.

First add Session data model to schema.prisma (Codes are in README.md) Then run prisma generate or npx prisma generate and you will be good to go.

Don't forget to close the issue if your problem got fixed.

kleydon commented 2 years ago

@erfanasbari, thanks for your intuition.

@mjknight50, @sayerrrr: Could this explain what you are experiencing?

If not, let me know, and I'll test out the reproduction.zip file above.

-K

mjknight50 commented 2 years ago

@erfanasbari Thanks for the help and idea, but that isn't it. Same error message as originally reported.

kleydon commented 2 years ago

@mjknight50 , @sayerrrr ,

When I...

  1. Download reproduction.zip, posted above
  2. Decompress
  3. Enter the reproduction directory
  4. Run npm install
  5. Open reproduction/server/app.ts in VS Code locally (i.e not "reaching into" any docker container)
  6. Look at line 48: store: new PrismaSessionStore(prisma, {

The word "prisma" does not, in my environment, have the squiggly underline depicted in the image at the top of this thread. So it seems that, so far, I'm not able to reproduce the problem you are seeing with this set of files.

In case its useful, here is my environment: Mac OS 10.14 VS Code v1.57.0 npm v.6.14.7 node v14.9.0

Could it be that you have globally-installed npm packages (for prisma, or other dependencies) that are over-riding the local ones? (I don't know if this is even possible, but it was the only new thought I had...)

If the steps I've listed above don't seem like the right ones to do, in attempting to reproduce this issue, please let this forum know.

-K

mjknight50 commented 2 years ago

I've spent all day today chasing this and I can't find the pattern. I'm going to move on to something else but I wish you the best of luck with what I think is a nice idea.

kleydon commented 2 years ago

Fair enough. Sorry it seems to have wasted a bit of your time, but perhaps its the sort of thing that becomes clear (or disappears) when one stop fixating on it? Not sure; hopefully time will tell. At any rate, good luck with your project! -K

On Jun 21, 2021, at 11:05 AM, Matthew Knight @.***> wrote:

 I've spent all day today chasing this and I can't find the pattern. I'm going to move on to something else but I wish you the best of luck with what I think is a nice idea.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

rikschennink commented 2 years ago

fwiw I had a smilar error, caused by adjusting the session model to

id Int @id instead of id String @id

kleydon commented 2 years ago

Thanks for the tip, @rikschennink.

tobiasostman commented 2 years ago

Ran into this issue myself. After changing my npx prisma generate output from the default one everything started to work. image image this fixed it for me

kleydon commented 2 years ago

@tobiasostman - thanks for posting this. Not sure why an explicit, custom output location for the prisma client would make the difference - but glad it has! (Maybe the symptoms described above are general; maybe they happen whenever the prisma client fails to successfully generate? Just a theory.)

alaeddine119 commented 2 years ago

I encounter the same error first time. The code at the README.md didn't work. I guess the problem might be that instead of giving prisma from @prisma/client to PrismaSessionStore, we might want to create an instance of new PrismaClient() from @prisma/client.

Which means instead of

import * as expressSession from 'express-session';
import { PrismaSessionStore } from '@quixo3/prisma-session-store';
import { prisma } from '@prisma/client';

try

import * as expressSession from 'express-session';
import { PrismaSessionStore } from '@quixo3/prisma-session-store';
import { PrismaClient } from '@prisma/client';

and instead of

app.use(
  expressSession({
    cookie: {
     maxAge: 7 * 24 * 60 * 60 * 1000 // ms
    },
    secret: 'a santa at nasa',
    resave: true,
    saveUninitialized: true,
    store: new PrismaSessionStore(
      prisma,
      {
        checkPeriod: 2 * 60 * 1000,  //ms
        dbRecordIdIsSessionId: true,
        dbRecordIdFunction: undefined,
      }
    )
  })
);

try

app.use(
    expressSession({
      cookie: {
        maxAge: 7 * 24 * 60 * 60 * 1000, // ms
      },
      secret: 'a santa at nasa',
      resave: true,
      saveUninitialized: true,
      store: new PrismaSessionStore(new PrismaClient(), {
        checkPeriod: 2 * 60 * 1000, //ms
        dbRecordIdIsSessionId: true,
        dbRecordIdFunction: undefined,
      }),
    }),
  );

Hope it works for you !

kleydon commented 2 years ago

Hi @alaeddine119,

Thanks for the feedback.

The approach you recommend - passing a new PrismaClient() into the store - seems like a good one, but it remains puzzling to me why passing in an existing prismaClient instance (e.g. prisma from the read-me) would cause problems... I don't understand this.

If anyone else is encountering the same behavior, please let us know.

hbriese commented 2 years ago

Same issue. Passing in a new PrismaClient() instance works :shrug:

Using the imported client gives

Type 'typeof import(".../node_modules/.prisma/client/index").prisma' is not assignable to type 'Record<"session", { create(args: ICreateArgs): Promise<IPrismaSession>; delete(args: IDeleteArgs): Promise<IPrismaSession>; deleteMany(args?: unknown): Promise<...>; findMany(args?: IFindManyArgs | undefined): Promise<...>; findUnique(args: IFindUniqueArgs): Promise<...>; update(args: IUpdateArgs): Promise<...>; }>'

Typescript 4.6.3

kleydon commented 2 years ago

Thanks for the help @mjknight50 / @alaeddine119 / @hbriese / @rikschennink ; finally fixed the docs on importing PrismaClient, in PR #86 (of prisma-session-store)