Skip to content

Commit

Permalink
Added typescript lib interface + Airbnb Eslint addition and fixes (#64)
Browse files Browse the repository at this point in the history
* Added TS support

* Added eslint

* added knex to prod dependencies instead of dev

* Removed unnecessary function input

* Fixed lint errors

* Fixed error where a bitwise operator was used instead of standard or

* Fixed lint errors in test.js
Returned KnexStore construction inside the lib's init function, because it must be dynamically created with the session object provided by user

* Added testing instructions to README

* Removed node v8 from Travis.yml

* Fixed an ignored minor lint error: Favor global import

* Fixed lint errors in example.js

* Running tests locally with passwords, because both MySQL and Postgres refuse all connections without a password
Travis should still work without db passwords, should just add ON_TRAVIS=1 in Travis's env

* Reverted some of my silly changes in index.js
Fixed lint errors in some of example-postgres.js

* Fixed the rest of the lint errors in example-postgres.js

* Fixed some ignored lint errors

* Eslint ignored index.d.ts

* Fixed bug in index.d.ts where I forgot to add new to an interface

* Added travis env variable that signals to tests that it's in fact running in Travis
  • Loading branch information
omarryhan authored Jun 5, 2020
1 parent f9a4c3a commit 590bf98
Show file tree
Hide file tree
Showing 11 changed files with 1,904 additions and 722 deletions.
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

0 comments on commit 590bf98

Please sign in to comment.