Skip to content

Commit 39abec2

Browse files
committed
Init project.
0 parents  commit 39abec2

File tree

12 files changed

+313
-0
lines changed

12 files changed

+313
-0
lines changed

.gitignore

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Created by https://www.gitignore.io/api/node
2+
# Edit at https://www.gitignore.io/?templates=node
3+
4+
lib
5+
package-lock.json
6+
__snapshots__
7+
8+
### Node ###
9+
# Logs
10+
logs
11+
*.log
12+
npm-debug.log*
13+
yarn-debug.log*
14+
yarn-error.log*
15+
lerna-debug.log*
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
19+
20+
# Runtime data
21+
pids
22+
*.pid
23+
*.seed
24+
*.pid.lock
25+
26+
# Directory for instrumented libs generated by jscoverage/JSCover
27+
lib-cov
28+
29+
# Coverage directory used by tools like istanbul
30+
coverage
31+
32+
# nyc test coverage
33+
.nyc_output
34+
35+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
36+
.grunt
37+
38+
# Bower dependency directory (https://bower.io/)
39+
bower_components
40+
41+
# node-waf configuration
42+
.lock-wscript
43+
44+
# Compiled binary addons (https://nodejs.org/api/addons.html)
45+
build/Release
46+
47+
# Dependency directories
48+
node_modules/
49+
jspm_packages/
50+
51+
# TypeScript v1 declaration files
52+
typings/
53+
54+
# Optional npm cache directory
55+
.npm
56+
57+
# Optional eslint cache
58+
.eslintcache
59+
60+
# Optional REPL history
61+
.node_repl_history
62+
63+
# Output of 'npm pack'
64+
*.tgz
65+
66+
# Yarn Integrity file
67+
.yarn-integrity
68+
69+
# dotenv environment variables file
70+
.env
71+
.env.test
72+
73+
# parcel-bundler cache (https://parceljs.org/)
74+
.cache
75+
76+
# next.js build output
77+
.next
78+
79+
# nuxt.js build output
80+
.nuxt
81+
82+
# vuepress build output
83+
.vuepress/dist
84+
85+
# Serverless directories
86+
.serverless/
87+
88+
# FuseBox cache
89+
.fusebox/
90+
91+
# DynamoDB Local files
92+
.dynamodb/
93+
94+
# End of https://www.gitignore.io/api/node
95+
96+
.DS_Store
97+
.cache
98+
.vscode
99+
.idea
100+
.env
101+
102+
*.bak
103+
*.tem
104+
*.temp
105+
#.swp
106+
*.*~
107+
~*.*
108+
109+
# IDEA
110+
*.iml
111+
*.ipr
112+
*.iws
113+
.idea/

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Basic Example
2+
---
3+
4+
```bash
5+
npm run watch # Listen compile .ts files.
6+
npm run build # compile .ts files.
7+
8+
npm run start
9+
```

ormconfig.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"type": "mysql",
3+
"host": "127.0.0.1",
4+
"port": 3306,
5+
"username": "root",
6+
"password": "W.cjiang1314",
7+
"database": "test",
8+
"entities": [
9+
"lib/entity/*.js"
10+
],
11+
"subscribers": [
12+
"lib/subscriber/*.js"
13+
],
14+
"migrations": [
15+
"lib/migration/*.js"
16+
],
17+
"cli": {
18+
"entitiesDir": "lib/entity",
19+
"migrationsDir": "src/migration",
20+
"subscribersDir": "lib/subscriber"
21+
},
22+
"logging": true,
23+
"synchronize": true
24+
}

package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "express-typeorm",
3+
"version": "1.0.0",
4+
"description": "Express TypeORM Example.",
5+
"scripts": {
6+
"prepare": "npm run build",
7+
"init": "typeorm migration:create -n Init",
8+
"run": "typeorm migration:run",
9+
"start": "node lib/app.js",
10+
"watch": "npm run watch:ts",
11+
"watch:ts": "tsbb watch & npm run build:types -- --watch",
12+
"build": "npm run build:ts && npm run build:types",
13+
"build:ts": "tsbb build",
14+
"build:types": "tsbb types",
15+
"test": "tsbb test",
16+
"coverage": "tsbb test --coverage"
17+
},
18+
"repository": {
19+
"type": "git",
20+
"url": "https://github.com/jaywcjlove/tsbb/tree/master/example/basic"
21+
},
22+
"jest": {},
23+
"keywords": [],
24+
"license": "MIT",
25+
"devDependencies": {
26+
"@types/fs-extra": "^8.0.1",
27+
"tsbb": "^1.4.0"
28+
},
29+
"author": "",
30+
"dependencies": {
31+
"@types/body-parser": "^1.17.1",
32+
"@types/express": "^4.17.2",
33+
"@types/node": "^13.1.2",
34+
"body-parser": "^1.19.0",
35+
"express": "^4.17.1",
36+
"fs-extra": "^8.1.0",
37+
"mysql": "^2.17.1",
38+
"typeorm": "^0.2.22"
39+
}
40+
}

src/app.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import express from 'express';
2+
import { Request, Response } from 'express';
3+
import * as bodyParser from 'body-parser';
4+
import { createConnection } from 'typeorm';
5+
import AppRoutes from './routes';
6+
import getConf from './utils/getORMConf';
7+
8+
(async () => {
9+
try {
10+
/**
11+
* Init DB
12+
*/
13+
const conf = await getConf();
14+
const conn = await createConnection({
15+
name: 'InitDB',
16+
type: conf.type,
17+
host: conf.host,
18+
port: conf.port,
19+
username: conf.username,
20+
password: conf.password,
21+
});
22+
const queryRunner = await conn.createQueryRunner();
23+
await queryRunner.createDatabase(conf.database, true);
24+
await conn.close();
25+
26+
await createConnection();
27+
// create express app
28+
const app = express();
29+
app.use(bodyParser.json());
30+
31+
// register all application routes
32+
AppRoutes.forEach(route => {
33+
(app as any)[route.method](route.path, (request: Request, response: Response, next: Function) => {
34+
route.action(request, response)
35+
.then(() => next)
36+
.catch(err => next(err));
37+
});
38+
});
39+
40+
// start express server
41+
app.listen(3001);
42+
console.log('Express application is up and running on port 3000');
43+
} catch (error) {
44+
console.log('TypeORM connection error: ', error);
45+
}
46+
})();

src/controller/login.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Request, Response } from 'express';
2+
import { getManager } from 'typeorm';
3+
import { User } from '../entity/User';
4+
5+
export async function login(request: Request, response: Response) {
6+
const repository = getManager().getRepository(User);
7+
const posts = await repository.find();
8+
response.send(posts);
9+
}

src/entity/User.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
2+
3+
@Entity()
4+
export class User {
5+
@PrimaryGeneratedColumn()
6+
id: number;
7+
8+
@Column({ type: 'varchar' })
9+
username: string;
10+
11+
@Column({ type: 'varchar' })
12+
email: string;
13+
14+
@Column({ type: 'varchar' })
15+
password: string;
16+
}

src/migration/.gitkeep

Whitespace-only changes.

src/routes.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { login } from './controller/login';
2+
3+
4+
5+
export default [
6+
{
7+
path: '/api/login',
8+
method: 'post',
9+
action: login,
10+
},
11+
];

src/subscriber/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)