Skip to content

Commit

Permalink
Merge pull request #36 from kleydon/dev-4
Browse files Browse the repository at this point in the history
expires -> expiresAt; min express-session and prisma versions
  • Loading branch information
William Sedlacek authored Feb 15, 2021
2 parents e3cc4c3 + e736dac commit 239e026
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 126 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ dist

# Testing
coverage
prisma/dev.db
prisma/dev.db
prisma/migrations/**
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ Must be one of the following:
- **build**: Changes that affect the build system or external dependencies
- **ci**: Changes to our CI configuration files and scripts
- **docs**: Documentation only changes
- **style**: Changes that do not affect the meaning of the code
- **feat**: A new feature
- **fix**: A bug fix
- **perf**: A code change that improves performance
- **refactor**: A code change that neither fixes a bug nor adds a feature
- **test**: Adding missing tests or correcting existing tests
- **chore**: Changes to the build process or auxiliary tools and libraries

### Commit Message Body

Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ model Session {
id String @id
sid String @unique
data String
expires DateTime
expiresAt DateTime
}
```

Expand All @@ -112,7 +112,7 @@ If you are using [@nexus/schema](https://www.npmjs.com/package/@nexus/schema) yo
t.model.id();
t.model.sid();
t.model.data();
t.model.expires();
t.model.expiresAt();
}
}
...
Expand All @@ -137,6 +137,11 @@ Prisma `String` properties are mapped to `VARCHAR(191)` by default. Session data

If you are using a version of Prisma that supports [migrating with native types](https://github.com/prisma/prisma/issues/4330) you can use a type annotation in your `schema.prisma` file instead of manually modifying your data column.

## Migrating from versions following `2.0.0`

Following version `2.0.0`, the `Session` `expires` field was renamed to `expiresAt` to match Prisma's naming convention for date fields.


## Migrating from versions prior to `1.0.0`

In `1.0.0` the public API of this library was reworked. Previously the default export that was a
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@
"type-fest": "^0.20.2"
},
"peerDependencies": {
"@prisma/client": "*",
"express-session": "*"
"@prisma/client": ">=2.16.1",
"express-session": ">=1.17.3"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@prisma/cli": "^2.14.0",
"@prisma/client": "^2.14.0",
"@prisma/cli": "^2.16.1",
"@prisma/client": "^2.16.1",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/express": "^4.17.9",
"@types/express": "^4.17.11",
"@types/express-session": "^1.17.3",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.20",
"@types/node": "^14.14.28",
"@types/supertest": "^2.0.10",
"commitizen": "^4.2.2",
"commitizen": "^4.2.3",
"cz-conventional-changelog": "3.3.0",
"express": "^4.17.1",
"express-session": "^1.17.1",
Expand All @@ -72,11 +72,11 @@
"prettier": "^2.2.1",
"pretty-quick": "^3.1.0",
"semantic-release": "^17.3.1",
"supertest": "^6.0.1",
"ts-jest": "^26.4.4",
"supertest": "^6.1.3",
"ts-jest": "^26.5.1",
"tslint": "^6.1.3",
"tslint-config-suiyobi": "^0.4.8",
"typescript": "^4.1.3"
"typescript": "^4.1.5"
},
"config": {
"commitizen": {
Expand Down
10 changes: 0 additions & 10 deletions prisma/migrations/20210110233720_/migration.sql

This file was deleted.

2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ model Session {
id String @id
sid String @unique
data String
expires DateTime
expiresAt DateTime
}
6 changes: 3 additions & 3 deletions src/@types/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface IPrismaSession {
data: string | null;
expires: Date;
expiresAt: Date;
id: string;
sid: string;
}
Expand All @@ -11,7 +11,7 @@ interface ICreatePrismaSession extends IPrismaSession {

interface IFindUniqueArgs {
select?: {
expires?: boolean;
expiresAt?: boolean;
sid?: boolean;
};
where: {
Expand All @@ -22,7 +22,7 @@ interface IFindUniqueArgs {
interface IFindManyArgs {
select?: {
data?: boolean;
expires?: boolean;
expiresAt?: boolean;
sid?: boolean;
};
where?: {
Expand Down
16 changes: 8 additions & 8 deletions src/lib/prisma-session-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ describe('PrismaSessionStore', () => {
});

findManyMock.mockResolvedValueOnce([
{ sid: 'sid-0', expires: createExpiration(-1, { rounding: 100 }) },
{ sid: 'sid-1', expires: createExpiration(500, { rounding: 100 }) },
{ sid: 'sid-0', expiresAt: createExpiration(-1, { rounding: 100 }) },
{ sid: 'sid-1', expiresAt: createExpiration(500, { rounding: 100 }) },
]);
deleteMock.mockResolvedValue(undefined);

Expand Down Expand Up @@ -729,7 +729,7 @@ describe('PrismaSessionStore', () => {
});
deleteMock.mockResolvedValue(undefined);
findManyMock.mockResolvedValue([
{ expires: createExpiration(-1, { rounding: 100 }) },
{ expiresAt: createExpiration(-1, { rounding: 100 }) },
]);

await sleep(10);
Expand All @@ -745,7 +745,7 @@ describe('PrismaSessionStore', () => {
});

findManyMock.mockResolvedValue([
{ expires: createExpiration(-1, { rounding: 100 }) },
{ expiresAt: createExpiration(-1, { rounding: 100 }) },
]);

await sleep(10);
Expand All @@ -761,7 +761,7 @@ describe('PrismaSessionStore', () => {

expect(createMock).toHaveBeenCalledWith({
data: expect.objectContaining({
expires: createExpiration(500, { rounding: 100 }),
expiresAt: createExpiration(500, { rounding: 100 }),
}),
});
});
Expand All @@ -776,7 +776,7 @@ describe('PrismaSessionStore', () => {

expect(createMock).toHaveBeenCalledWith({
data: expect.objectContaining({
expires: createExpiration(500, { rounding: 100 }),
expiresAt: createExpiration(500, { rounding: 100 }),
}),
});
});
Expand All @@ -797,7 +797,7 @@ describe('PrismaSessionStore', () => {

expect(createMock).toHaveBeenCalledWith({
data: expect.objectContaining({
expires: createExpiration(100, { rounding: 100 }),
expiresAt: createExpiration(100, { rounding: 100 }),
}),
});
});
Expand All @@ -809,7 +809,7 @@ describe('PrismaSessionStore', () => {

expect(createMock).toHaveBeenCalledWith({
data: expect.objectContaining({
expires: createExpiration(500, { rounding: 100 }),
expiresAt: createExpiration(500, { rounding: 100 }),
}),
});
});
Expand Down
18 changes: 11 additions & 7 deletions src/lib/prisma-session-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,17 @@ export class PrismaSessionStore extends Store {
this.logger.log('Checking for any expired sessions...');
const sessions = await this.prisma.session.findMany({
select: {
expires: true,
expiresAt: true,
sid: true,
},
});

for (const session of sessions) {
const now = new Date();
const remainingSec = (session.expires.valueOf() - now.valueOf()) / 1000;
const remainingSec = (session.expiresAt.valueOf() - now.valueOf()) / 1000;
this.logger.log(`session:${session.sid} expires in ${remainingSec}sec`);

if (now.valueOf() >= session.expires.valueOf()) {
if (now.valueOf() >= session.expiresAt.valueOf()) {
this.logger.log(`Deleting session with sid: ${session.sid}`);
await this.prisma.session.delete({
where: { sid: session.sid },
Expand All @@ -350,7 +350,9 @@ export class PrismaSessionStore extends Store {
if (!(await this.validateConnection())) return callback?.();

const ttl = getTTL(this.options, session, sid);
const expires = createExpiration(ttl, { rounding: this.options.roundTTL });
const expiresAt = createExpiration(ttl, {
rounding: this.options.roundTTL,
});

let sessionString;
try {
Expand All @@ -370,7 +372,7 @@ export class PrismaSessionStore extends Store {

const data = {
sid,
expires,
expiresAt,
data: sessionString,
id: this.dbRecordIdIsSessionId ? sid : this.dbRecordIdFunction(sid),
};
Expand Down Expand Up @@ -434,7 +436,9 @@ export class PrismaSessionStore extends Store {
if (!(await this.validateConnection())) return callback?.();

const ttl = getTTL(this.options, session, sid);
const expires = createExpiration(ttl, { rounding: this.options.roundTTL });
const expiresAt = createExpiration(ttl, {
rounding: this.options.roundTTL,
});

try {
const existingSession = await this.prisma.session.findUnique({
Expand All @@ -450,7 +454,7 @@ export class PrismaSessionStore extends Store {
await this.prisma.session.update({
where: { sid: existingSession.sid },
data: {
expires,
expiresAt,
data: this.serializer.stringify(existingSessionData),
},
});
Expand Down
Loading

0 comments on commit 239e026

Please sign in to comment.