Skip to content

Commit

Permalink
Update TypeORM integration
Browse files Browse the repository at this point in the history
  • Loading branch information
koistya committed Apr 8, 2021
1 parent 8c69820 commit 0712808
Show file tree
Hide file tree
Showing 19 changed files with 341 additions and 47 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
* @copyright 2016-present Kriasoft (https://git.io/JYNud)
*/

module.exports = {
root: true,

Expand Down
77 changes: 77 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-typescript.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarn/sdks/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript",
"version": "4.2.3-pnpify",
"version": "4.2.4-pnpify",
"main": "./lib/typescript.js",
"type": "commonjs"
}
6 changes: 6 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: "@yarnpkg/plugin-interactive-tools"
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: "@yarnpkg/plugin-typescript"

yarnPath: .yarn/releases/yarn-2.4.1.cjs
12 changes: 2 additions & 10 deletions auth/facebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ const client = new AuthorizationCode({
});

export const redirect: RequestHandler = function (req, res) {
const redirect_uri = env.isProduction
? `${env.APP_ORIGIN}${req.originalUrl}/return`
: `${req.protocol}://${req.get("host")}${req.originalUrl}/return`;

const authorizeUrl = client.authorizeURL({
redirect_uri,
redirect_uri: req.app.locals["redirect_uri"],
scope,
});

Expand All @@ -36,13 +32,9 @@ export const redirect: RequestHandler = function (req, res) {

export const callback: RequestHandler = async function (req, res, next) {
try {
const redirect_uri = env.isProduction
? `${env.APP_ORIGIN}${req.originalUrl}`
: `${req.protocol}://${req.get("host")}${req.originalUrl}`;

const { token } = await client.getToken({
code: req.query.code as string,
redirect_uri,
redirect_uri: req.app.locals["redirect_uri"],
scope,
});

Expand Down
12 changes: 9 additions & 3 deletions auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
*/

import { RequestHandler, Router } from "express";
import env from "../env";
import * as facebook from "./facebook";
import session from "./session";

export const auth = Router();

const noCache: RequestHandler = function (req, res, next) {
const common: RequestHandler = function (req, res, next) {
const [, prefix, provider] = req.path.split("/");
req.app.locals["redirect_uri"] = env.isProduction
? `${env.APP_ORIGIN}/${prefix}/${provider}/return`
: `${req.protocol}://${req.get("host")}/${prefix}/${provider}/return`;

res.setHeader("Cache-Control", "no-store");
next();
};

auth.use(session);

auth.use("/auth/facebook", noCache, facebook.redirect);
auth.use("/auth/facebook/return", noCache, facebook.callback);
auth.get("/auth/facebook", common, facebook.redirect);
auth.get("/auth/facebook/return", common, facebook.callback);

export { session };
12 changes: 10 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
*/

module.exports = {
presets: [["@babel/preset-typescript"]],
plugins: ["@babel/plugin-proposal-class-properties"],
presets: [
["@babel/preset-env", { targets: { node: "14" } }],
["@babel/preset-typescript", { allowDeclareFields: true }],
],

plugins: [
"@babel/plugin-proposal-class-properties",
["@babel/plugin-proposal-decorators", { decoratorsBeforeExport: true }],
],

ignore: [".build/**", ".vscode/**", ".yarn/**", "migrations/**"],
};
26 changes: 22 additions & 4 deletions data/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@
* @copyright 2016-present Kriasoft (https://git.io/JYNud)
*/

import { Column, Entity, PrimaryColumn } from "typeorm";
import {
Column,
CreateDateColumn,
DeleteDateColumn,
Entity,
PrimaryColumn,
UpdateDateColumn,
} from "typeorm";

@Entity()
export class User {
@PrimaryColumn()
@Entity("user")
class User {
@PrimaryColumn({ type: "text" })
id: string;

@Column("citext")
email: string;

@CreateDateColumn({ name: "created_at", type: "datetime" })
createdAt: Date;

@UpdateDateColumn({ name: "updated_at", type: "datetime" })
updatedAt: Date;

@DeleteDateColumn({ name: "deleted_at", type: "datetime" })
deletedAt: Date;
}

export { User };
4 changes: 2 additions & 2 deletions env/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GOOGLE_CLOUD_PROJECT=example-dev
# PostgreSQL
# https://www.postgresql.org/docs/current/static/libpq-envars.html
PGHOST=127.0.0.1
PGPORT=5423
PGPORT=5432
PGUSER=postgres
PGPASSWORD=
PGDATABASE=example_dev
Expand Down Expand Up @@ -40,4 +40,4 @@ FACEBOOK_APP_SECRET=xxxxx
# Email Settings
# https://nodemailer.com/
EMAIL_FROM=[email protected]
EMAIL_PASSWORD=
EMAIL_PASSWORD=xxxxx
4 changes: 2 additions & 2 deletions env/.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GOOGLE_CLOUD_PROJECT=example-dev
# PostgreSQL
# https://www.postgresql.org/docs/current/static/libpq-envars.html
PGHOST=127.0.0.1
PGPORT=5423
PGPORT=5432
PGUSER=postgres
PGPASSWORD=
PGDATABASE=example_local
Expand All @@ -35,4 +35,4 @@ FACEBOOK_APP_SECRET=xxxxx
# Email Settings
# https://nodemailer.com/
EMAIL_FROM=[email protected]
EMAIL_PASSWORD=
EMAIL_PASSWORD=xxxxx
4 changes: 2 additions & 2 deletions env/.env.prod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GOOGLE_CLOUD_PROJECT=example
# PostgreSQL
# https://www.postgresql.org/docs/current/static/libpq-envars.html
PGHOST=127.0.0.1
PGPORT=5423
PGPORT=5432
PGUSER=postgres
PGPASSWORD=
PGDATABASE=example
Expand Down Expand Up @@ -40,4 +40,4 @@ FACEBOOK_APP_SECRET=xxxxx
# Email Settings
# https://nodemailer.com/
EMAIL_FROM=[email protected]
EMAIL_PASSWORD=
EMAIL_PASSWORD=xxxxx
4 changes: 2 additions & 2 deletions env/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GOOGLE_CLOUD_PROJECT=example-test
# PostgreSQL
# https://www.postgresql.org/docs/current/static/libpq-envars.html
PGHOST=127.0.0.1
PGPORT=5423
PGPORT=5432
PGUSER=postgres
PGPASSWORD=
PGDATABASE=example_test
Expand Down Expand Up @@ -40,4 +40,4 @@ FACEBOOK_APP_SECRET=xxxxx
# Email Settings
# https://nodemailer.com/
EMAIL_FROM=[email protected]
EMAIL_PASSWORD=
EMAIL_PASSWORD=xxxxx
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "rollup -c",
"lint": "eslint --cache --report-unused-disable-directives .",
"test": "jest",
"postinstall": "husky install"
"db": "typeorm",
"repl": "node --experimental-repl-await -r ./scripts/repl -i"
},
"dependencies": {
"cookie": "^0.4.1",
Expand All @@ -17,6 +18,7 @@
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"minimist": "^1.2.5",
"pg": "^8.5.1",
"simple-oauth2": "^4.2.0",
"typeorm": "^0.2.32"
},
Expand All @@ -25,8 +27,10 @@
"@babel/core": "^7.13.14",
"@babel/node": "^7.13.13",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-decorators": "^7.13.5",
"@babel/preset-env": "^7.13.12",
"@babel/preset-typescript": "^7.13.0",
"@babel/register": "^7.13.14",
"@jest/types": "^26.6.2",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^18.0.0",
Expand All @@ -37,6 +41,7 @@
"@types/express": "^4.17.11",
"@types/jsonwebtoken": "^8.5.1",
"@types/minimist": "^1.2.1",
"@types/node": "^14.14.37",
"@types/simple-oauth2": "^4.1.0",
"@typescript-eslint/eslint-plugin": "^4.21.0",
"@typescript-eslint/parser": "^4.21.0",
Expand All @@ -47,9 +52,10 @@
"husky": "^6.0.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"reflect-metadata": "^0.1.13",
"rollup": "^2.44.0",
"rollup-plugin-pnp-resolve": "^2.0.0",
"source-map-support": "^0.5.19",
"typescript": "^4.2.3"
"typescript": "^4.2.4"
}
}
7 changes: 5 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* @copyright 2016-present Kriasoft (https://git.io/JYNud)
*/

import { nodeResolve } from "@rollup/plugin-node-resolve";
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import run from "@rollup/plugin-run";
import pkg from "./package.json";

Expand Down Expand Up @@ -36,13 +36,16 @@ const config = {
extensions: [".ts", ".mjs", ".js", ".json", ".node"],
modulesOnly: true,
}),

commonjs(),

json(),

babel({
extensions: [".ts", ".js", ".mjs"],
babelHelpers: "bundled",
include: ["auth/**", "env/**", "functions/**"],
}),

isWatch && run({ execArgv: ["-r", "source-map-support/register"] }),
],

Expand Down
8 changes: 8 additions & 0 deletions scripts/repl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require("reflect-metadata");

const { createConnection } = require("typeorm");
const config = require("../ormconfig");

createConnection(config).then((connection) => {
global.db = connection;
});
2 changes: 1 addition & 1 deletion server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/

import express from "express";
import { auth } from "./auth";
import { api } from "./api";
import { auth } from "./auth";

const app = express();
const port = process.env.PORT ?? 8080;
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// "incremental": true, /* Enable incremental compilation */
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"lib": ["ESNext"], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
Expand Down Expand Up @@ -62,7 +62,7 @@

/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
Expand Down
Loading

0 comments on commit 0712808

Please sign in to comment.