-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persistance migrated to SQLite3 / TypeORM
- Loading branch information
App Generator
committed
Jul 20, 2021
1 parent
eaa01de
commit 19eabff
Showing
25 changed files
with
811 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
NODE_ENV=DEV | ||
PORT=5000 | ||
SQLITE_PATH=./database.db | ||
SECRET="SuperS3cret_4277m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module.exports = { | ||
env: { | ||
es2021: true, | ||
node: true, | ||
}, | ||
extends: ['airbnb-typescript/base', 'plugin:@typescript-eslint/recommended'], | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
project: './tsconfig.json', | ||
}, | ||
rules: { | ||
'@typescript-eslint/explicit-module-boundary-types': 'off', | ||
'no-param-reassign': 'off', | ||
'no-underscore-dangle': 'off', | ||
'consistent-return': 'off', | ||
'no-console': 'off', | ||
'import/prefer-default-export': 'off', | ||
'@typescript-eslint/no-non-null-assertion': 'off', | ||
'class-methods-use-this': 'off', | ||
'@typescript-eslint/naming-convention': 'off' | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Declare files that will always have LF line endings on checkout. | ||
|
||
*.html text eol=lf | ||
*.scss text eol=lf | ||
*.sass text eol=lf | ||
*.css text eol=lf | ||
*.js text eol=lf | ||
*.json text eol=lf | ||
*.svg text eol=lf | ||
*.yml text eol=lf | ||
*.yaml text eol=lf | ||
*.md text eol=lf | ||
|
||
.babelrc text eol=lf | ||
.gitignore text eol=lf | ||
.gitattributes text eol=lf | ||
|
||
LICENSE text eol=lf | ||
|
||
# Denote all files that are truly binary and should not be modified. | ||
|
||
*.png binary | ||
*.jpg binary | ||
*.jpeg binary | ||
*.gif binary | ||
*.bmp binary | ||
*.ai binary | ||
*.psd binary | ||
*.pdf binary | ||
|
||
*.otf binary | ||
*.eot binary | ||
*.ttf binary | ||
*.woff binary | ||
*.woff2 binary | ||
*.zip binary | ||
*.rar binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
node_modules | ||
build | ||
|
||
# Logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
|
||
# Nodejs API Server | ||
|
||
Express / Nodejs Starter with JWT authentication, and **SQLite** persistance - Provided by **AppSeed** [App Generator](https://appseed.us/app-generator). | ||
Authentication Flow uses [json web tokens](https://jwt.io) via Passport library - `passport-jwt` startegy. | ||
|
||
<br /> | ||
|
||
> Features: | ||
- Simple, intuitive codebase - can be extended with ease. | ||
- Typescript | ||
- **Stack**: NodeJS / Express / SQLite / TypeORM | ||
- Auth: Passport / `passport-jwt` strategy | ||
- [API Interface Descriptor](https://github.com/app-generator/api-server-nodejs/blob/master/media/api.postman_collection.json): POSTMAN Collection | ||
|
||
<br /> | ||
|
||
> Support: | ||
- Github (issues tracker), Email: **support @ appseed.us** | ||
- **Discord**: [LIVE Support](https://discord.gg/fZC6hup) (registered AppSeed Users) | ||
|
||
<br /> | ||
|
||
![Nodejs API Server - Open-source Nodejs Starter provided by AppSeed.](https://user-images.githubusercontent.com/51070104/124414813-142aa180-dd5c-11eb-9279-6b082dadc51a.png) | ||
|
||
<br /> | ||
|
||
## Requirements | ||
|
||
- [Node.js](https://nodejs.org/) >= 12.x | ||
- [SQLite](https://www.sqlite.org/index.html) | ||
|
||
<br /> | ||
|
||
## How to use the code | ||
|
||
**Clone the sources** | ||
|
||
```bash | ||
$ git clone https://github.com/app-generator/api-server-nodejs.git | ||
$ cd api-server-nodejs | ||
``` | ||
|
||
**Install dependencies** via NPM or Yarn | ||
|
||
```bash | ||
$ npm i | ||
// OR | ||
$ yarn | ||
``` | ||
|
||
**Run the SQLite migration** | ||
|
||
``` | ||
$ yarn typeorm migration:run | ||
``` | ||
|
||
**Start the API server** - development mode | ||
|
||
```bash | ||
$ npm dev | ||
// OR | ||
$ yarn dev | ||
``` | ||
|
||
**Production Build** - files generated in `build` directory | ||
|
||
```bash | ||
$ npm build | ||
// OR | ||
$ yarn build | ||
``` | ||
|
||
**Start the API server** - for production (files served from `build/index.js`) | ||
|
||
```bash | ||
$ npm start | ||
// OR | ||
$ yarn start | ||
``` | ||
|
||
The API server will start using the `PORT` specified in `.env` file (default 5000) | ||
|
||
<br /> | ||
|
||
## Codebase Structure | ||
|
||
```bash | ||
< ROOT / src > | ||
| | ||
|-- config/ | ||
| |-- config.ts # Configuration | ||
| |-- passport.ts # Define Passport Strategy | ||
| | ||
|-- migration/ | ||
| |-- some_migration.ts # database migrations | ||
| | ||
|-- models/ | ||
| |-- activeSession.ts # Sessions Model (Typeorm) | ||
| |-- user.ts # User Model (Typeorm) | ||
| | ||
|-- routes/ | ||
| |-- users.ts # Define Users API Routes | ||
| | ||
| | ||
|-- index.js # API Entry Point | ||
|-- .env # Specify the ENV variables | ||
| | ||
|-- ************************************************************************ | ||
``` | ||
|
||
<br /> | ||
|
||
## SQLite Path | ||
|
||
The SQLite Path is set in `.env`, as `SQLITE_PATH` | ||
|
||
## Database migration | ||
|
||
##### generate migration: | ||
|
||
yarn typeorm migration:generate -n your_migration_name | ||
|
||
##### run migration: | ||
|
||
yarn typeorm migration:run | ||
|
||
<br /> | ||
|
||
## API | ||
|
||
For a fast set up, use this POSTMAN file: [api_sample](https://github.com/app-generator/api-server-nodejs-pro/blob/master/media/api.postman_collection.json) | ||
|
||
> **Register** - `api/users/register` | ||
``` | ||
POST api/users/register | ||
Content-Type: application/json | ||
{ | ||
"username":"test", | ||
"password":"pass", | ||
"email":"[email protected]" | ||
} | ||
``` | ||
|
||
<br /> | ||
|
||
> **Login** - `api/users/login` | ||
``` | ||
POST /api/users/login | ||
Content-Type: application/json | ||
{ | ||
"password":"pass", | ||
"email":"[email protected]" | ||
} | ||
``` | ||
|
||
<br /> | ||
|
||
> **Logout** - `api/users/logout` | ||
``` | ||
POST api/users/logout | ||
Content-Type: application/json | ||
authorization: JWT_TOKEN (returned by Login request) | ||
{ | ||
"token":"JWT_TOKEN" | ||
} | ||
``` | ||
|
||
<br /> | ||
|
||
## Credits | ||
|
||
This software is provided by the core AppSeed team with an inspiration from other great NodeJS starters: | ||
|
||
- Initial verison - coded by [Teo Deleanu](https://www.linkedin.com/in/teodeleanu/) | ||
- [Hackathon Starter](https://github.com/sahat/hackathon-starter) - A truly amazing boilerplate for Node.js apps | ||
- [Node Server Boilerplate](https://github.com/hagopj13/node-express-boilerplate) - just another cool starter | ||
- [React NodeJS Argon](https://github.com/creativetimofficial/argon-dashboard-react-nodejs) - released by **Creative-Tim** and [ProjectData](https://projectdata.dev/) | ||
|
||
<br /> | ||
|
||
--- | ||
Nodejs API Server - provided by AppSeed [App Generator](https://appseed.us) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
transform: { | ||
'^.+\\.ts?$': 'ts-jest', | ||
}, | ||
setupFiles: ['dotenv/config'], | ||
testEnvironment: 'node', | ||
coveragePathIgnorePatterns: ['node_modules'], | ||
coverageReporters: ['text', 'lcov', 'clover', 'html'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[ | ||
{ | ||
"name": "default", | ||
"type": "sqlite", | ||
"database": "./database.db", | ||
"autoSchemaSync": true, | ||
"entities": [ | ||
"src/models/**/*{.js,.ts}" | ||
], | ||
"migrations": ["src/migrations/**/*{.js,.ts}"], | ||
"cli": { | ||
"migrationsDir": "src/migrations/" | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "api-server-nodejs", | ||
"version": "1.0.0", | ||
"description": "RESTful back-end for Node.js and Express.js", | ||
"license": "Commercial", | ||
"author": "AppSeed.us", | ||
"main": "src/index", | ||
"scripts": { | ||
"start": "pm2 start production.config.json", | ||
"start-no-daemon": "pm2 start production.config.json --no-daemon", | ||
"start-node": "node build/index.js", | ||
"dev": "ts-node-dev src/index.ts", | ||
"test": "jest -i --colors --verbose --detectOpenHandles", | ||
"lint": "eslint src --ext .ts", | ||
"build": "tsc -p tsconfig.build.json", | ||
"typecheck": "tsc --noEmit", | ||
"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js" | ||
}, | ||
"dependencies": { | ||
"bcrypt": "5.0.1", | ||
"compression": "^1.7.4", | ||
"cors": "^2.8.5", | ||
"cross-env": "^7.0.3", | ||
"dotenv": "^10.0.0", | ||
"express": "^4.17.1", | ||
"express-passport-logout": "^0.1.0", | ||
"joi": "^17.4.1", | ||
"jsonwebtoken": "^8.5.1", | ||
"passport": "^0.4.1", | ||
"passport-jwt": "^4.0.0", | ||
"sqlite3": "5.0.2", | ||
"pm2": "^5.1.0", | ||
"typeorm": "0.2.34" | ||
}, | ||
"devDependencies": { | ||
"@types/bcrypt": "5.0.0", | ||
"@types/compression": "^1.7.1", | ||
"@types/cors": "^2.8.11", | ||
"@types/express": "^4.17.13", | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16.3.3", | ||
"@types/passport-jwt": "^3.0.6", | ||
"@types/sqlite3": "3.1.7", | ||
"@types/supertest": "^2.0.11", | ||
"@typescript-eslint/eslint-plugin": "^4.28.3", | ||
"@typescript-eslint/parser": "^4.28.3", | ||
"eslint": "^7.31.0", | ||
"eslint-config-airbnb-typescript": "^12.3.1", | ||
"eslint-plugin-import": "^2.23.4", | ||
"jest": "^27.0.6", | ||
"supertest": "^6.1.3", | ||
"ts-jest": "^27.0.3", | ||
"ts-node": "10.1.0", | ||
"ts-node-dev": "1.1.8", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
Oops, something went wrong.