Skip to content

Commit

Permalink
Now session stores token
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaCrafter committed Jul 28, 2019
1 parent d8a137d commit 37981ca
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 45 deletions.
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Options:
| Field | Default | Description |
|-----------------------|---------------------|----------------------------------------------|
| `db` | Optional | Object |
| `db[driverName]` | | Name of [database driver](#plugins) |
| `db[driverName]` | | Code of [database driver](#DB-module) |
| `db[driverName].name` | Required | Database name |
| `db[driverName].port` | Defined by plugin | Database port |
| `db[driverName].user` | Optional | Database username |
Expand All @@ -70,6 +70,7 @@ Options:
| | | |
| `plugins` | `[]` | Array of plugin packages names |
| `devMode` | `false` | DEPRECATED, use CLI instead |
| `ignore` | `[]` | Excluded directories in development mode |
| `origin` | `Origin` header | Accept requests only from this origin |
| `port` | `8081` | API listing port |
| `ws_timeout` | `60` | WebSocket request waiting timeout in seconds |
Expand Down Expand Up @@ -101,20 +102,33 @@ Example:

---

<tag id="plugins" />
## DB module

## Database driver and plugins
```ts
require('dc-api-core/DB'): {
[string: code]: (config?: string) => DBDriver
}
```

Drivers can be defines by plugins, that named like `dc-api-pluginName`.
For example, official plugin `dc-api-mysql`.
* `code` - Registered code of database driver. For example `dc-api-mongo` registers `mongo`.
* `config` - Configuration name after dot in `config.json`. Ex. `mongo('dev')` points to `db['mongo.dev']`.
* `DBDriver` - Mongoose-like object (not always, defined by plugin)

## Plugins

For first, install plugin package via `npm` or `yarn`.
Arter this add name of plugin package to `plugins` array in `config.json`.
After this add name of plugin package to `plugins` array in `config.json`.

Example `config.json`:

Database plugins registers their middleware in `this.db` array (controller function scope),
but other plugins can define this anywhere.
```js
{
// ...
plugins: ["dc-api-mongo"]
}
```

If you want create your own plugin, read [plugin developing documentation](docs/Plugins.md)
If you want create your own plugin, read [plugin development documentation](docs/Plugins.md)

---

Expand Down
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (process.argv.indexOf('--dev') != -1) {
nodemon({
ext: 'js json',
script: __dirname + '/index.js',
args: process.argv.slice(2),
ignore: config.ignore
});

Expand Down
35 changes: 2 additions & 33 deletions docs/CodeStyling.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,8 @@ Main rules:
1) Use only PascalCase for naming.
2) File name = Class name plus `.js` extension

<!-- TODO: check this
Note:
HTTP requests url part will be transformed from kebab-case to PascalCase,
but you shouldn't worry about that if you using my API class.

## Database using

Bad way:

```js
const DB = require('dc-api-core/DB');
const db = DB.mongo;
```

Yes, you can use `DB` for storing drivers wrapper, also you can use `db`
for storing driver's instance, but not both in one file!

---------------------------------------------------------------------------

Better:

```js
const { mongo: db } = require('dc-api-core/DB');
```

Yes, this better, because it declarates that in this file will be used only
`mongo` as database (`db`). And there are no case-sensitive variables.

I think this is good compromise between useability and readability.

---------------------------------------------------------------------------

Very good way:

```js
const { mongo } = require('dc-api-core/DB');
```
-->
8 changes: 6 additions & 2 deletions docs/DBPlugin.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Database plugins

Coming soon, but if you want do this now, you can try to understand [dc-api-mysql] and [dc-api-mongo] plugins
Coming soon, but if you want do this now, you can try to understand [dc-api-mongo] plugin

[dc-api-mysql]: https://github.com/DimaCrafter/dc-api-mysql
<!-- [dc-api-mysql]: https://github.com/DimaCrafter/dc-api-mysql -->
[dc-api-mongo]: https://github.com/DimaCrafter/dc-api-mongo

## Sketch of documentation
Expand All @@ -28,9 +28,13 @@ class MyCoolDB extends EventEmitter {
this.emit('no-model', name);
return;
}

// Mongoose-like model
return NativeDB.getModelFromSchema(schemaRaw);
}
}

// register(pluginType, pluginClass, ...advanced)
// advanced[0] - code of database driver
module.exports = core => core.register('db', MyCoolDB, 'mycooldb');
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dc-api-core",
"version": "0.2.1-3",
"version": "0.2.1-4",
"author": "DimaCrafter",
"homepage": "https://github.com/DimaCrafter/dc-api-core",
"bugs": "https://github.com/DimaCrafter/dc-api-core/issues",
Expand Down
2 changes: 2 additions & 0 deletions session.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module.exports = (token, onToken) => {
expiresIn: config.session.ttl
}, (err, token) => {
if (err) return reject('Can`t sign session');
session.token = token;
session.save();
onToken(token);
resolve(proxify(session));
});
Expand Down

0 comments on commit 37981ca

Please sign in to comment.