Skip to content
This repository was archived by the owner on Oct 30, 2020. It is now read-only.

Commit 107fe2c

Browse files
authored
v1.0.0 (#30)
* version bump: 1.0.0 🎆 * remove express-session as dependency * remove note about code status * mark code blocks in readme as JS for syntax highlighting * update readme on how to pass express-session * remove ESLint comments * require user to pass express-session to documentdb-session * install express-session for testing
1 parent 0bb6658 commit 107fe2c

File tree

5 files changed

+414
-379
lines changed

5 files changed

+414
-379
lines changed

.eslintrc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
"func-names": ["error", "always"],
5050
"func-style": ["warn", "expression", { "allowArrowFunctions": true }],
5151
"generator-star-spacing": ["error", { "before": false, "after": true }],
52-
"global-require": "warn",
5352
"guard-for-in": "warn",
5453
"handle-callback-err": "error",
5554
"id-length": "off",
@@ -80,7 +79,7 @@
8079
"max-lines": "off",
8180
"max-nested-callbacks": ["warn", 2],
8281
"max-params": ["warn", 4],
83-
"max-statements": ["warn", 10],
82+
"max-statements": ["off"],
8483
"max-statements-per-line": ["warn", { "max": 2 }],
8584
"multiline-ternary": "off",
8685
"new-cap": ["error", {
@@ -150,7 +149,7 @@
150149
"no-lone-blocks": "error",
151150
"no-lonely-if": "warn",
152151
"no-loop-func": "error",
153-
"no-magic-numbers": "warn",
152+
"no-magic-numbers": "off",
154153
"no-mixed-operators": ["warn", { "allowSamePrecedence": true }],
155154
"no-mixed-requires": ["error", { "allowCall": true }],
156155
"no-mixed-spaces-and-tabs": "error",

README.md

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,22 @@ Used in conjunction with the [`express-session`](https://www.npmjs.com/package/e
2929

3030
* Provides an optional `.initialize()` method if you'd like to initialize your database, collection, and stored procedures before making your first request, allowing you to check for errors before making other requests (otherwise the database will be initialized on the first request).
3131

32-
## Status
33-
Code is stable and passes all tests, and been tested in a production environment. The version will be incremented to `v1.0.0` once others have successfully implemented it in production.
34-
3532
## Install
36-
```
33+
```bash
3734
npm install --save documentdb-session
3835
```
3936

4037
## Typical Usage
41-
```
42-
const DocumentDBStore = require('documentdb-session');
38+
```js
39+
const DocumentDBSession = require('documentdb-session');
4340
const express = require('express');
4441
const session = require('express-session');
4542

43+
// pass the express-session object to documentdb-session
44+
const DocumentDBStore = DocumentDBStore(session);
45+
// you could also do this when you require the module if you'd prefer, like so:
46+
// const DocumentDBStore = require('documentdb-session')(session);
47+
4648
const config = {
4749
host: `https://mydbaccount.documents.azure.com:443/`,
4850
key: '8idtLLsiRJsKvgHLi...vgOJ9YXTTYK61LX15pobbmQ==',
@@ -68,8 +70,8 @@ Option | Default | Description
6870
`collection` | `"sessions"` | The ID of the collection where the session data should be stored. If the collection does not exist, it will be created when the session store initializes. The collection may contain other data, or it may be a dedicated collection just for sessions.
6971
`database` | `"sessionstore"` | The ID of the database where the session data should be stored. If the database does not exist, it will be creaed when the session store initializes.
7072
`discriminator` | `{ type: "session" }` | By default, `documentdb-session` sets a `"type"` attribute on each session document with a value of `"session"`, to distinguish session documents from other documents in the collection. If you would like a different attribute or value to be used to discriminate session documents from other documents, enter that as an attribute-value pair in an object here, e.g. `{ site: "mysite.com" }` or `{ _type: "session" }`.
71-
`host` (required) | none | The URL / hostname of your DocumentDB database account, usually of the form `https://mydbaccount.documents.azure.com:443/`. You can also provide this in an environment variable, (`DOCUMENTDB_URL`) instead.
72-
`key` (required) | none | The primary key for your DocumentDB account. A primary key is required because `documentdb-session` may create a new database for your account, if none exists. You can also provide this in an environment variable (`DOCUMENTDB_KEY`) instead.
73+
`host` (required) | none | The URL / hostname of your DocumentDB database account, usually of the form `https://mydbaccount.documents.azure.com:443/`. You can also provide this in an environment variable, (`DOCUMENTDB_URL`) instead.
74+
`key` (required) | none | The primary key for your DocumentDB account. A primary key is required because `documentdb-session` may create a new database for your account, if none exists. You can also provide this in an environment variable (`DOCUMENTDB_KEY`) instead.
7375
`ttl` | none | The TTL (time-to-live or expiration time) for your sessions, in seconds. After this time has elapsed since the last change to the session data, the session will be deleted. A session's TTL is extended each time session data is changed, restarting the timer. See more on [**Configuring TTL**](https://github.com/dwhieb/documentdb-session#configuring-ttl-time-to-live-or-expiration-time) below. *Enabling TTL is strongly recommended.*
7476

7577
**NB:** If you'd like to more fully customize the settings for the collection where your session data is stored (e.g. the connection policy and consistency level), you can create the collection in advance, and simply provide the ID of that collection in the `collection` config parameter. `documentdb-session` will then use that collection's settings.
@@ -98,7 +100,7 @@ To enable TTL without a default expiration, either call the `.replaceCollection(
98100
### Working with Partitioned Collections
99101
If you use partitioned collections, you can enable partitioning in the same way that you would enable it using the [DocumentDB Node.js SDK](https://github.com/Azure/azure-documentdb-node). The session store instance exposes the DocumentDB SDK's [DocumentClient](http://azure.github.io/azure-documentdb-node/DocumentClient.html) object (as `.client`), which you can use to register your partition resolver. A short example is below, and you can see a more complete example of using a partition resolver [here](https://github.com/Azure/azure-documentdb-node/blob/master/samples/Partitioning/app.js).
100102

101-
```
103+
```js
102104
const documentdb = require('documentdb');
103105
const DocumentDBStore = require('documentdb-session');
104106

@@ -122,42 +124,59 @@ store.client.partitionResolvers[databaseLink] = resolver;
122124
## API
123125
`documentdb-session` follows the [specification for session stores](https://github.com/expressjs/session#session-store-implementation) given by `express-session`. It includes all required, recommended, and optional methods, as well as a few extra convenience methods.
124126

127+
### DocumentDBSession
128+
The `DocumentDBSession` method is exposed by requiring `documentdb-session`. Calling this function and passing it the `express-session` instance will return the [DocumentDBStore constructor](https://github.com/dwhieb/documentdb-session#documentdbstore). Example use:
129+
130+
```js
131+
const session = require('express-session');
132+
const DocumentDBStore = require('documentdb-session')(session);
133+
const store = new DocumentDBStore({ /* config options */ });
134+
```
135+
125136
### DocumentDBStore
126-
The `DocumentDBStore` object exposed by `documentdb-session` is used to create a new instance of a session store. This may then be passed to `express-session`. See the [Typical Usage](https://github.com/dwhieb/documentdb-session#typical-usage) above for an example.
137+
The `DocumentDBStore` constructor exposed by `documentdb-session` is used to create a new instance of a session `store` object. Calling this function and passing it an object with config options will return the new DocumentDB store object. This may then be passed to `express-session`. See the [Typical Usage](https://github.com/dwhieb/documentdb-session#typical-usage) and [DocumentDBSession](https://github.com/dwhieb/documentdb-session#documentdbsession) sections above for examples.
127138

128-
### .client
139+
### store.client
129140
The DocumentDB DocumentClient object from the Node.js SDK (complete documentation [here](http://azure.github.io/azure-documentdb-node/DocumentClient.html)). This provides complete access to the DocumentDB API and all its methods, and can be used to customize collection settings, or make other database calls independent of storing session data.
130141

131-
### .all(cb)
142+
### store.all(cb)
132143
Retrieves all sessions from the collection, by filtering on the session discriminator (usually `"type": "session"`).
133144

134-
`callback(err, sessions = [])`
145+
```js
146+
callback(err, sessions = [])
147+
```
135148

136-
### .clear(cb)
149+
### store.clear(cb)
137150
Deletes all sessions from the collection. Callback is fired once the collection is cleared of all sessions. Other documents in the collection that are not sessions are not affected. This operation uses a stored procedure called `clear`, which is uploaded to the collection on initialization.
138151

139-
`callback(err)`
152+
```js
153+
callback(err)
154+
```
140155

141-
### .destroy(sid, cb)
156+
### store.destroy(sid, cb)
142157
Deletes a session with the given session ID (`sid`). Callback is fired once the document is deleted.
143158

144-
`callback(err)`
159+
```js
160+
callback(err)
161+
```
145162

146-
### .get(sid, cb)
163+
### store.get(sid, cb)
147164
Retrieves a session from the collection using the given session ID (`sid`). The session is returned as an object, and includes its administrative database properties (e.g. `_RID`, `_ETAG`). If the session is not found, the `session` object will be set to `null`.
148165

149-
`callback(err, session)`
166+
```js
167+
callback(err, session)
168+
```
150169

151-
### .initialize(cb)
170+
### store.initialize(cb)
152171
Normally, `documentdb-session` will check for a DocumentDB database and collection the first time a request to the database is made, and will create them if they do not exist. It will also upload a few stored procedures to the collection. If you would like to initialize the database before you start making database calls, you can do so by calling `.initialize()`. This is useful if you want your application to check for database configuration errors before attempting to write sessions, and for testing.
153172

154-
`callback(err)`
173+
```js
174+
callback(err)
175+
```
155176

156177
**Example**
157178

158-
```
159-
...
160-
179+
```js
161180
const app = express();
162181
const store = new DocumentDBStore(config);
163182

@@ -169,20 +188,26 @@ store.initialize((err, db) => {
169188
});
170189
```
171190

172-
### .length(cb)
191+
### store.length(cb)
173192
Retrieves a count of the number of sessions in the collection, filtering on the session discriminator (usually `"type": "session"`). This operation uses a stored procedure called `length`, which is uploaded to the collection on initialization.
174193

175-
`callback(err, len)`
194+
```js
195+
callback(err, len)
196+
```
176197

177-
### .set(sid, session, cb)
198+
### store.set(sid, session, cb)
178199
Upserts the session into the collection given a session ID (`sid`) and session object (`session`). The callback fires once the session has been added to the collection.
179200

180-
`callback(err)`
201+
```js
202+
callback(err)
203+
```
181204

182-
### .touch(sid, session, cb)
205+
### store.touch(sid, session, cb)
183206
Resets the TTL (time-to-live) for the session (see the [`ttl` config option](https://github.com/dwhieb/documentdb-session#config-options) above). The callback fires onces the document has been updated. This operation works by updating the `lastActive` field on the document.
184207

185-
`callback(err)`
208+
```js
209+
callback(err)
210+
```
186211

187212

188213
## Tests

0 commit comments

Comments
 (0)