Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typescript lib interface + Airbnb Eslint addition and fixes #64

Merged
merged 18 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/**/*.d.ts
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
env: {
commonjs: true,
es6: true,
node: true,
},
extends: [
'airbnb-base',
],
};
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: node_js
node_js:
- "8"
- "10"
- "12"

env:
- CXX=g++-4.8
- IN_TRAVIS=1

before_script:
- psql -c 'create database travis_ci_test;' -U postgres
Expand Down
62 changes: 62 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,65 @@ If a knex instance is not provided, this module will attempt to create a sqlite3
[gitter-channel-url]: https://gitter.im/llambda/connect-session-knex
[express-session-url]: https://github.com/expressjs/session
[io-url]: https://iojs.org

## Testing

Install Postgresql
Instructions for Ubuntu after intalling the db:

```bash
sudo -u postgres psql
```

```sql
CREATE DATABASE travis_ci_test OWNER postgres;
```

```sql
GRANT all privileges ON DATABASE travis_ci_test TO postgres;
```

```sql
ALTER USER postgres WITH PASSWORD 'postgres';
```

```sql
\q
```

Install Mysql
Instructions for Ubuntu after installing the db:

```bash
sudo mysql -u root
```

```sql
create user 'travis' identified by 'travis';
```

```sql
ALTER USER 'travis'@'localhost' IDENTIFIED BY 'travis';
```

```sql
create database travis_ci_test;
```

```sql
grant all on travis_ci_test.* to 'travis';
```

```sql
\q
```

```bash
sudo service mysql restart
```

Make sure both the MySQL and Postgres services are running

```bash
npm run test
```
48 changes: 26 additions & 22 deletions example-postgres.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
const express = require("express");
/* eslint-disable import/no-unresolved */
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */

const express = require('express');

const app = express();

const session = require("express-session");
const KnexSessionStore = require("connect-session-knex")(session);
const session = require('express-session');
const KnexSessionStore = require('connect-session-knex')(session);

const Knex = require('knex');

const Knex = require("knex");
const knex = Knex({
client: "pg",
client: 'pg',
connection: {
host: "127.0.0.1",
user: "postgres",
password: "",
database: "travis_ci_test"
}
host: '127.0.0.1',
user: 'postgres',
password: '',
database: 'travis_ci_test',
},
});

const store = new KnexSessionStore({
knex: knex,
tablename: "sessions" // optional. Defaults to 'sessions'
knex,
tablename: 'sessions', // optional. Defaults to 'sessions'
});

app.use(
session({
secret: "keyboard cat",
secret: 'keyboard cat',
cookie: {
maxAge: 10000 // ten seconds, for testing
maxAge: 10000, // ten seconds, for testing
},
store: store
})
store,
}),
);

var count = 0;

app.use("/", function(req, res, next) {
var n = req.session.views || 0;
req.session.views = ++n;
res.end(n + " views");
app.use('/', (req, res) => {
const n = req.session.views || 0;
req.session.views = n + 1;
res.end(`${n} views`);
});

app.listen(3000);
43 changes: 23 additions & 20 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
const express = require("express"); // Express 4
const app = express();
/* eslint-disable import/no-unresolved */
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-console */

const express = require('express');

const app = express(); // Express 4
const session = require('express-session');
const KnexSessionStore = require('connect-session-knex')(session);

const session = require("express-session");
const KnexSessionStore = require("connect-session-knex")(session);
const store = new KnexSessionStore(/* options here */); // defaults to a sqlite3 database

app.use(
session({
secret: "keyboard cat",
secret: 'keyboard cat',
cookie: {
maxAge: 30000 // 30 seconds for testing
maxAge: 30000, // 30 seconds for testing
},
store: store
})
store,
}),
);

var count = 0;

app.use("/", function(req, res, next) {
var n = req.session.views || 0;
req.session.views = ++n;
res.end(n + " views");
app.use('/', (req, res) => {
const n = req.session.views || 0;
req.session.views = n + 1;
res.end(`${n} views`);
});

app.listen(3000);

setInterval(function() {
store.length().then(function(length) {
console.log("There are " + JSON.stringify(length) + " sessions");
setInterval(() => {
store.length().then((length) => {
console.log(`There are ${JSON.stringify(length)} sessions`);
});
}, 2000);

setInterval(function() {
store.clear().then(function(length) {
console.log("Cleared " + JSON.stringify(length) + " sessions");
setInterval(() => {
store.clear().then((length) => {
console.log(`Cleared ${JSON.stringify(length)} sessions`);
});
}, 30000);
18 changes: 18 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Knex from 'knex';
import expressSession, { Store } from 'express-session';

declare module 'connect-session-knex' {
type ConfigType = {
tablename?: string;
sidfieldname?: string;
knex?: Knex;
createTable?: boolean;
clearInterval?: number;
};

interface StoreFactory {
new (configs?: ConfigType): Store;
}

export default function initFunction(session: typeof expressSession): StoreFactory;
}
Loading