Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add models and migrations #1

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

10 changes: 10 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"airbnb-base",
"plugin:jest/recommended"
],
"plugins": ["jest"],
"env": {
"jest/globals": true
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ dist

# TernJS port file
.tern-port

# Zone Identifiers
*.Identifier
**/.Identifier
67 changes: 67 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
.DS_Store

# Built assets
build/**/*
!build/assets/.gitkeep

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

.idea
.envrc
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"trailingComma" : "all"
}
8 changes: 8 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const path = require('path');

module.exports = {
config: path.resolve('src', 'config', 'database.js'),
'migrations-path': path.resolve('src', 'migrations'),
'models-path': path.resolve('src', 'models'),
'seeders-path': path.resolve('src', 'seeds'),
};
5 changes: 5 additions & 0 deletions .yo-rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"@iic2513/generator-template": {
"projectName": "test-app"
}
}
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: yarn start
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Gud Reads
Código de ejemplo en el contexto de las cápsulas desarrolladas para demostrar usos básicos del template del curso.
22 changes: 22 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = function babelConfig(api) {
const babelEnv = api.env();
const presets = [
['@babel/env', {
targets: {
ie: '9',
browsers: ['>1%', 'last 3 versions'],
},
modules: babelEnv === 'test' ? 'auto' : false,
useBuiltIns: 'usage',
corejs: 3,
}],
['@babel/preset-react', {
development: babelEnv === 'development',
}],
];

const plugins = [
'react-hot-loader/babel',
];
return { presets, plugins };
};
20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint no-console: "off" */

const app = require('./src/app');
const db = require('./src/models');

const PORT = process.env.PORT || 3000;

db.sequelize
.authenticate()
.then(() => {
console.log('Connection to the database has been established successfully.');
app.listen(PORT, (err) => {
if (err) {
return console.error('Failed', err);
}
console.log(`Listening on port ${PORT}`);
return app;
});
})
.catch((err) => console.error('Unable to connect to the database:', err));
3 changes: 3 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ignore": ["src/assets", "build"]
}
72 changes: 72 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "test-app",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"lint": "eslint ./src index.js",
"lint-fix": "eslint --fix ./src index.js",
"build-assets": "yarn run clean-assets && NODE_ENV=production webpack -p",
"clean-assets": "rm -rf build/assets",
"heroku-postbuild": "yarn run build-assets && sequelize db:migrate",
"test": "jest"
},
"devDependencies": {
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"babel-jest": "^26.3.0",
"babel-loader": "^8.1.0",
"css-loader": "^4.3.0",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^24.0.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.1.2",
"file-loader": "^6.1.0",
"jest": "^26.4.2",
"koa-webpack": "^6.0.0",
"mini-css-extract-plugin": "^0.11.1",
"node-sass": "^4.14.1",
"nodemon": "^2.0.4",
"react-test-renderer": "^16.13.1",
"sass-loader": "^10.0.2",
"style-loader": "^1.2.1",
"supertest": "^4.0.2",
"webpack": "^4.44.1",
"webpack-command": "^0.5.0",
"webpack-manifest-plugin": "^2.2.0"
},
"dependencies": {
"core-js": "^3.6.5",
"dotenv": "^8.2.0",
"koa": "^2.13.0",
"koa-body": "^4.2.0",
"koa-ejs": "^4.3.0",
"koa-flash-message": "^0.1.6",
"koa-logger": "^3.2.1",
"koa-override-method": "^1.0.0",
"koa-router": "^9.4.0",
"koa-session": "^6.0.0",
"koa-static": "^5.0.0",
"nodemailer": "^6.4.11",
"pg": "^8.3.3",
"pg-hstore": "^2.3.3",
"prop-types": "^15.7.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-hot-loader": "^4.12.21",
"sequelize": "^6.3.5",
"sequelize-cli": "^6.2.0"
},
"engines": {
"node": "10.x || 12.x",
"yarn": "^1.22.0"
}
}
89 changes: 89 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const path = require('path');
const Koa = require('koa');
const koaBody = require('koa-body');
const koaLogger = require('koa-logger');
const koaFlashMessage = require('koa-flash-message').default;
const koaStatic = require('koa-static');
const render = require('koa-ejs');
const session = require('koa-session');
const override = require('koa-override-method');
const assets = require('./assets');
const mailer = require('./mailers');
const routes = require('./routes');
const orm = require('./models');

// App constructor
const app = new Koa();

const developmentMode = app.env === 'development';
const testMode = app.env === 'test';

app.keys = [
'these secret keys are used to sign HTTP cookies',
'to make sure only this app can generate a valid one',
'and thus preventing someone just writing a cookie',
'saying he is logged in when it\'s really not',
];

// expose ORM through context's prototype
app.context.orm = orm;

/**
* Middlewares
*/

// expose running mode in ctx.state
app.use((ctx, next) => {
ctx.state.env = ctx.app.env;
return next();
});

// log requests
if (!testMode) {
app.use(koaLogger());
}

// webpack middleware for dev mode only
if (developmentMode) {
// eslint-disable-next-line import/no-extraneous-dependencies, global-require
const koaWebpack = require('koa-webpack');
koaWebpack()
.then((middleware) => app.use(middleware))
.catch(console.error); // eslint-disable-line no-console
}

app.use(koaStatic(path.join(__dirname, '..', 'build'), {}));

// expose a session hash to store information across requests from same client
app.use(session({
maxAge: 14 * 24 * 60 * 60 * 1000, // 2 weeks
}, app));

// flash messages support
app.use(koaFlashMessage);

// parse request body
app.use(koaBody({
multipart: true,
keepExtensions: true,
}));

app.use((ctx, next) => {
ctx.request.method = override.call(ctx, ctx.request.body.fields || ctx.request.body);
return next();
});

// Configure EJS views
app.use(assets(developmentMode));
render(app, {
root: path.join(__dirname, 'views'),
viewExt: 'html.ejs',
cache: !developmentMode,
});

mailer(app);

// Routing middleware
app.use(routes.routes());

module.exports = app;
18 changes: 18 additions & 0 deletions src/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let manifest;

try {
// eslint-disable-next-line global-require,import/no-unresolved
manifest = require('../build/assets/manifest.json');
} catch (err) {
// noop
}

module.exports = function assetsBuilder(developmentMode) {
function assetPath(path) {
return (!developmentMode && manifest && manifest[path]) || `/assets/${path}`;
}
return function assets(ctx, next) {
ctx.state.assetPath = assetPath;
return next();
};
};
Binary file added src/assets/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/assets/js/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "airbnb",
"env": {
"browser": true
}
}
9 changes: 9 additions & 0 deletions src/assets/js/app.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';

const reactAppContainer = document.getElementById('react-app');

if (reactAppContainer) {
ReactDOM.render(<App />, reactAppContainer);
}
1 change: 1 addition & 0 deletions src/assets/js/assets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '../images/logo.png';
8 changes: 8 additions & 0 deletions src/assets/js/components/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react';
import { hot } from 'react-hot-loader';

function App() {
return <div>Hello React World!</div>;
}

export default hot(module)(App);
Loading