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

Commit

Permalink
Work in progress on NPM module
Browse files Browse the repository at this point in the history
  • Loading branch information
Luciano Nooijen committed Dec 6, 2018
1 parent c2da3c3 commit fed74bd
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 34 deletions.
53 changes: 40 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,54 @@ For contributing to the development, fork the [GitHub repository](https://github
To use the NodeJS Blog module, first, import the package

```js
const NodeBlog = require('nodejs-blog');
const nodeBlog = require('nodejs-blog');
const { authors, auth, users, articles } = require('nodejs-blog');
```

or using ES6 modules
to start using the package, create a new instance of the NodeBlog class

```js
import NodeBlog from 'nodejs-blog';
const client = 'YOUR_DB_CLIENT'; // for more info, see https://knexjs.org/
const host = 'YOUR_DB_HOST';
const database = 'YOUR_DB_NAME';
const user = 'YOUR_DB_USER';
const pass = 'YOUR_DB_PASS';
const debug = true || false;

const blog = nodeBlog(client, host, user, database, password, debug);
```

to start using the package, create a new instance of the NodeBlog class
Then you can use the imported functions as you wish, for example:

```js
const posts = await articles.list(blog);
```

Just send the `blog` instance as the first argument and the rest of the arguments second.

The available methods are:

```js
const nodeBlogConfig = {
client: 'YOUR_DB_CLIENT', // for more info, see https://knexjs.org/
host: 'YOUR_DB_HOST',
database: 'YOUR_DB_NAME',
user: 'YOUR_DB_USER',
pass: 'YOUR_DB_PASS',
debug: true || false,
};
const nodeBlog = new NodeBlog(nodeBlogConfig);
authors.list(blog)
authors.get(blog, id)
authors.add(blog, authorObject)
authors.modify(blog, id, modifiedData)
authors.delete(blog, id)

auth.authenticate(blog, username, password) // Returns JWT
auth.validate(blog, username, password) // Valid if data object is returned

users.list(blog)
users.get(blog, id)
users.add(blog, userObject)
users.modify(blog, id, modifiedData)
users.delete(blog, id)

articles.list(blog)
articles.get(blog, id)
articles.add(blog, articleObject)
articles.modify(blog, id, modifiedData)
articles.delete(blog, id)
```

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.
Expand Down
5 changes: 4 additions & 1 deletion controllers/controllers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const Authors = require('./authors');
const Users = require('./users');
// const Categories = require('./categories');
// const Posts = require('./posts');
const Auth = require('./auth');
const Articles = require('./articles');

module.exports = {
Authors,
Auth,
Users,
Articles,
};
50 changes: 50 additions & 0 deletions src/create-node-blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* eslint-disable object-curly-newline */ // TODO: Write docs!!!!!!

const getKnexInstance = require('knex');
const generateKnexfile = require('../database/generate-knexfile');

const { Authors, Auth, Users, Articles } = require('../controllers');

const authors = {
list: Authors.listAuthors,
get: Authors.getAuthor,
add: Authors.addAuthor,
modify: Authors.modifyAuthor,
delete: Authors.deleteAuthor,
};

const auth = {
authenticate: Auth.authenticateUser,
validate: Auth.validateToken,
};

const users = {
list: Users.listUsers,
get: Users.listUsers,
add: Users.addUser,
modify: Users.modifyUser,
delete: Users.deleteUser,
};

const articles = {
list: Articles.listArticles,
get: Articles.getArticle,
add: Articles.addArticle,
// Modify is not yet available
delete: Articles.deleteArticle,
};

// eslint-disable-next-line max-len, prettier/prettier
const createNodeBlogInstance = (client, host, database, user, password, debug) => {
const knexConfig = { client, host, database, user, password, debug };
const knexfile = generateKnexfile(knexConfig);
const knex = getKnexInstance(knexfile);
knex.migrate.latest();
return knex;
};

module.exports = createNodeBlogInstance;
module.exports.authors = authors;
module.exports.auth = auth;
module.exports.users = users;
module.exports.articles = articles;
18 changes: 0 additions & 18 deletions src/nodeblog-class.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/nodejs-blog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const getNodeBlogInstance = require('./nodeblog-class');
const createNodeBlog = require('./create-node-blog');

module.exports = getNodeBlogInstance;
module.exports = createNodeBlog;
Empty file added tests/config/blog-instance.ts
Empty file.
35 changes: 35 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const nodeBlog = require('../');
const { authors, auth, users, articles } = require('../');
const { Authors } = require('../controllers');

const client = process.env.DB_CLIENT_TEST;
const host = process.env.DB_HOST_TEST;
const user = process.env.DB_USER_TEST;
const database = process.env.DB_NAME_TEST;
const password = process.env.DB_PASS_TEST;
const debug = process.env.KNEX_DEBUG;

const nodeBlogArguments = { client, host, user, database, password, debug };
const blog = nodeBlog(client, host, user, database, password, debug);

describe('NodeBlog NPM module', () => {
test('NodeBlog to create a knex instance', () => {
expect(typeof blog).toBe('function');
});
test('Blog authors should work', async () => {
expect.assertions(3);
expect(typeof await authors.list(blog)).toBe('array');
expect(await authors.list(blog)).toEqual(Authors.list(blog));
expect(typeof await authors.get(blog, 1)).toBe('object');
});
test('Blog users should work', async () => {
expect.assertions(2);
expect(typeof await users.list(blog)).toBe('array');
expect(typeof await users.get(blog, 1)).toBe('object');
});
test('Blog articles should work', async () => {
expect.assertions(2);
expect(typeof await articles.list(blog)).toBe('array');
expect(typeof await articles.get(blog, 1)).toBe('object');
});
});

0 comments on commit fed74bd

Please sign in to comment.