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

Commit fed74bd

Browse files
author
Luciano Nooijen
committed
Work in progress on NPM module
1 parent c2da3c3 commit fed74bd

File tree

7 files changed

+131
-34
lines changed

7 files changed

+131
-34
lines changed

README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,54 @@ For contributing to the development, fork the [GitHub repository](https://github
3535
To use the NodeJS Blog module, first, import the package
3636

3737
```js
38-
const NodeBlog = require('nodejs-blog');
38+
const nodeBlog = require('nodejs-blog');
39+
const { authors, auth, users, articles } = require('nodejs-blog');
3940
```
4041

41-
or using ES6 modules
42+
to start using the package, create a new instance of the NodeBlog class
4243

4344
```js
44-
import NodeBlog from 'nodejs-blog';
45+
const client = 'YOUR_DB_CLIENT'; // for more info, see https://knexjs.org/
46+
const host = 'YOUR_DB_HOST';
47+
const database = 'YOUR_DB_NAME';
48+
const user = 'YOUR_DB_USER';
49+
const pass = 'YOUR_DB_PASS';
50+
const debug = true || false;
51+
52+
const blog = nodeBlog(client, host, user, database, password, debug);
4553
```
4654

47-
to start using the package, create a new instance of the NodeBlog class
55+
Then you can use the imported functions as you wish, for example:
56+
57+
```js
58+
const posts = await articles.list(blog);
59+
```
60+
61+
Just send the `blog` instance as the first argument and the rest of the arguments second.
62+
63+
The available methods are:
4864

4965
```js
50-
const nodeBlogConfig = {
51-
client: 'YOUR_DB_CLIENT', // for more info, see https://knexjs.org/
52-
host: 'YOUR_DB_HOST',
53-
database: 'YOUR_DB_NAME',
54-
user: 'YOUR_DB_USER',
55-
pass: 'YOUR_DB_PASS',
56-
debug: true || false,
57-
};
58-
const nodeBlog = new NodeBlog(nodeBlogConfig);
66+
authors.list(blog)
67+
authors.get(blog, id)
68+
authors.add(blog, authorObject)
69+
authors.modify(blog, id, modifiedData)
70+
authors.delete(blog, id)
71+
72+
auth.authenticate(blog, username, password) // Returns JWT
73+
auth.validate(blog, username, password) // Valid if data object is returned
74+
75+
users.list(blog)
76+
users.get(blog, id)
77+
users.add(blog, userObject)
78+
users.modify(blog, id, modifiedData)
79+
users.delete(blog, id)
80+
81+
articles.list(blog)
82+
articles.get(blog, id)
83+
articles.add(blog, articleObject)
84+
articles.modify(blog, id, modifiedData)
85+
articles.delete(blog, id)
5986
```
6087

6188
We recommend creating a single file that will create the NodeBlog instance, and `export` this instance, and `import` in all other files where you want to use NodeJS Blog.

controllers/controllers.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
const Authors = require('./authors');
22
const Users = require('./users');
33
// const Categories = require('./categories');
4-
// const Posts = require('./posts');
4+
const Auth = require('./auth');
5+
const Articles = require('./articles');
56

67
module.exports = {
78
Authors,
9+
Auth,
810
Users,
11+
Articles,
912
};

src/create-node-blog.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* eslint-disable object-curly-newline */ // TODO: Write docs!!!!!!
2+
3+
const getKnexInstance = require('knex');
4+
const generateKnexfile = require('../database/generate-knexfile');
5+
6+
const { Authors, Auth, Users, Articles } = require('../controllers');
7+
8+
const authors = {
9+
list: Authors.listAuthors,
10+
get: Authors.getAuthor,
11+
add: Authors.addAuthor,
12+
modify: Authors.modifyAuthor,
13+
delete: Authors.deleteAuthor,
14+
};
15+
16+
const auth = {
17+
authenticate: Auth.authenticateUser,
18+
validate: Auth.validateToken,
19+
};
20+
21+
const users = {
22+
list: Users.listUsers,
23+
get: Users.listUsers,
24+
add: Users.addUser,
25+
modify: Users.modifyUser,
26+
delete: Users.deleteUser,
27+
};
28+
29+
const articles = {
30+
list: Articles.listArticles,
31+
get: Articles.getArticle,
32+
add: Articles.addArticle,
33+
// Modify is not yet available
34+
delete: Articles.deleteArticle,
35+
};
36+
37+
// eslint-disable-next-line max-len, prettier/prettier
38+
const createNodeBlogInstance = (client, host, database, user, password, debug) => {
39+
const knexConfig = { client, host, database, user, password, debug };
40+
const knexfile = generateKnexfile(knexConfig);
41+
const knex = getKnexInstance(knexfile);
42+
knex.migrate.latest();
43+
return knex;
44+
};
45+
46+
module.exports = createNodeBlogInstance;
47+
module.exports.authors = authors;
48+
module.exports.auth = auth;
49+
module.exports.users = users;
50+
module.exports.articles = articles;

src/nodeblog-class.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/nodejs-blog.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
const getNodeBlogInstance = require('./nodeblog-class');
1+
const createNodeBlog = require('./create-node-blog');
22

3-
module.exports = getNodeBlogInstance;
3+
module.exports = createNodeBlog;

tests/config/blog-instance.ts

Whitespace-only changes.

tests/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const nodeBlog = require('../');
2+
const { authors, auth, users, articles } = require('../');
3+
const { Authors } = require('../controllers');
4+
5+
const client = process.env.DB_CLIENT_TEST;
6+
const host = process.env.DB_HOST_TEST;
7+
const user = process.env.DB_USER_TEST;
8+
const database = process.env.DB_NAME_TEST;
9+
const password = process.env.DB_PASS_TEST;
10+
const debug = process.env.KNEX_DEBUG;
11+
12+
const nodeBlogArguments = { client, host, user, database, password, debug };
13+
const blog = nodeBlog(client, host, user, database, password, debug);
14+
15+
describe('NodeBlog NPM module', () => {
16+
test('NodeBlog to create a knex instance', () => {
17+
expect(typeof blog).toBe('function');
18+
});
19+
test('Blog authors should work', async () => {
20+
expect.assertions(3);
21+
expect(typeof await authors.list(blog)).toBe('array');
22+
expect(await authors.list(blog)).toEqual(Authors.list(blog));
23+
expect(typeof await authors.get(blog, 1)).toBe('object');
24+
});
25+
test('Blog users should work', async () => {
26+
expect.assertions(2);
27+
expect(typeof await users.list(blog)).toBe('array');
28+
expect(typeof await users.get(blog, 1)).toBe('object');
29+
});
30+
test('Blog articles should work', async () => {
31+
expect.assertions(2);
32+
expect(typeof await articles.list(blog)).toBe('array');
33+
expect(typeof await articles.get(blog, 1)).toBe('object');
34+
});
35+
});

0 commit comments

Comments
 (0)