Skip to content

Commit 6f2b8fb

Browse files
committed
Typeorm setup
1 parent 25b5340 commit 6f2b8fb

File tree

7 files changed

+198
-145
lines changed

7 files changed

+198
-145
lines changed

config/modules.js

Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
'use strict';
1+
"use strict";
22

3-
const fs = require('fs');
4-
const path = require('path');
5-
const paths = require('./paths');
6-
const chalk = require('react-dev-utils/chalk');
7-
const resolve = require('resolve');
3+
const fs = require("fs");
4+
const path = require("path");
5+
const paths = require("./paths");
6+
const chalk = require("react-dev-utils/chalk");
7+
const resolve = require("resolve");
88

99
/**
1010
* Get additional module paths based on the baseUrl of a compilerOptions object.
1111
*
1212
* @param {Object} options
1313
*/
1414
function getAdditionalModulePaths(options = {}) {
15-
const baseUrl = options.baseUrl;
16-
17-
if (!baseUrl) {
18-
return '';
19-
}
20-
21-
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
22-
23-
// We don't need to do anything if `baseUrl` is set to `node_modules`. This is
24-
// the default behavior.
25-
if (path.relative(paths.appNodeModules, baseUrlResolved) === '') {
26-
return null;
27-
}
28-
29-
// Allow the user set the `baseUrl` to `appSrc`.
30-
if (path.relative(paths.appSrc, baseUrlResolved) === '') {
31-
return [paths.appSrc];
32-
}
33-
34-
// If the path is equal to the root directory we ignore it here.
35-
// We don't want to allow importing from the root directly as source files are
36-
// not transpiled outside of `src`. We do allow importing them with the
37-
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
38-
// an alias.
39-
if (path.relative(paths.appPath, baseUrlResolved) === '') {
40-
return null;
41-
}
42-
43-
// Otherwise, throw an error.
44-
throw new Error(
45-
chalk.red.bold(
46-
"Your project's `baseUrl` can only be set to `src` or `node_modules`." +
47-
' Create React App does not support other values at this time.'
48-
)
49-
);
15+
const baseUrl = options.baseUrl;
16+
17+
if (!baseUrl) {
18+
return "";
19+
}
20+
21+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
22+
23+
// We don't need to do anything if `baseUrl` is set to `node_modules`. This is
24+
// the default behavior.
25+
if (path.relative(paths.appNodeModules, baseUrlResolved) === "") {
26+
return null;
27+
}
28+
29+
// Allow the user set the `baseUrl` to `appSrc`.
30+
if (path.relative(paths.appSrc, baseUrlResolved) === "") {
31+
return [paths.appSrc];
32+
}
33+
34+
// If the path is equal to the root directory we ignore it here.
35+
// We don't want to allow importing from the root directly as source files are
36+
// not transpiled outside of `src`. We do allow importing them with the
37+
// absolute path (e.g. `src/Components/Button.js`) but we set that up with
38+
// an alias.
39+
if (path.relative(paths.appPath, baseUrlResolved) === "") {
40+
return null;
41+
}
42+
43+
// Otherwise, throw an error.
44+
throw new Error(
45+
chalk.red.bold(
46+
"Your project's `baseUrl` can only be set to `src` or `node_modules`." +
47+
" Create React App does not support other values at this time."
48+
)
49+
);
5050
}
5151

5252
/**
@@ -55,19 +55,19 @@ function getAdditionalModulePaths(options = {}) {
5555
* @param {*} options
5656
*/
5757
function getWebpackAliases(options = {}) {
58-
const baseUrl = options.baseUrl;
58+
const baseUrl = options.baseUrl;
5959

60-
if (!baseUrl) {
61-
return {};
62-
}
60+
if (!baseUrl) {
61+
return {};
62+
}
6363

64-
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
64+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
6565

66-
if (path.relative(paths.appPath, baseUrlResolved) === '') {
67-
return {
68-
src: paths.appSrc,
69-
};
70-
}
66+
if (path.relative(paths.appPath, baseUrlResolved) === "") {
67+
return {
68+
src: paths.appSrc,
69+
};
70+
}
7171
}
7272

7373
/**
@@ -76,59 +76,59 @@ function getWebpackAliases(options = {}) {
7676
* @param {*} options
7777
*/
7878
function getJestAliases(options = {}) {
79-
const baseUrl = options.baseUrl;
79+
const baseUrl = options.baseUrl;
8080

81-
if (!baseUrl) {
82-
return {};
83-
}
81+
if (!baseUrl) {
82+
return {};
83+
}
8484

85-
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
85+
const baseUrlResolved = path.resolve(paths.appPath, baseUrl);
8686

87-
if (path.relative(paths.appPath, baseUrlResolved) === '') {
88-
return {
89-
'^src/(.*)$': '<rootDir>/src/$1',
90-
};
91-
}
87+
if (path.relative(paths.appPath, baseUrlResolved) === "") {
88+
return {
89+
"^src/(.*)$": "<rootDir>/src/$1",
90+
};
91+
}
9292
}
9393

9494
function getModules() {
95-
// Check if TypeScript is setup
96-
const hasTsConfig = fs.existsSync(paths.appTsConfig);
97-
const hasJsConfig = fs.existsSync(paths.appJsConfig);
95+
// Check if TypeScript is setup
96+
const hasTsConfig = fs.existsSync(paths.appTsConfig);
97+
const hasJsConfig = fs.existsSync(paths.appJsConfig);
98+
99+
if (hasTsConfig && hasJsConfig) {
100+
throw new Error(
101+
"You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file."
102+
);
103+
}
104+
105+
let config;
106+
107+
// If there's a tsconfig.json we assume it's a
108+
// TypeScript project and set up the config
109+
// based on tsconfig.json
110+
if (hasTsConfig) {
111+
const ts = require(resolve.sync("typescript", {
112+
basedir: paths.appNodeModules,
113+
}));
114+
config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
115+
// Otherwise we'll check if there is jsconfig.json
116+
// for non TS projects.
117+
} else if (hasJsConfig) {
118+
config = require(paths.appJsConfig);
119+
}
120+
121+
config = config || {};
122+
const options = config.compilerOptions || {};
123+
124+
const additionalModulePaths = getAdditionalModulePaths(options);
98125

99-
if (hasTsConfig && hasJsConfig) {
100-
throw new Error(
101-
'You have both a tsconfig.json and a jsconfig.json. If you are using TypeScript please remove your jsconfig.json file.'
102-
);
103-
}
104-
105-
let config;
106-
107-
// If there's a tsconfig.json we assume it's a
108-
// TypeScript project and set up the config
109-
// based on tsconfig.json
110-
if (hasTsConfig) {
111-
const ts = require(resolve.sync('typescript', {
112-
basedir: paths.appNodeModules,
113-
}));
114-
config = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile).config;
115-
// Otherwise we'll check if there is jsconfig.json
116-
// for non TS projects.
117-
} else if (hasJsConfig) {
118-
config = require(paths.appJsConfig);
119-
}
120-
121-
config = config || {};
122-
const options = config.compilerOptions || {};
123-
124-
const additionalModulePaths = getAdditionalModulePaths(options);
125-
126-
return {
127-
additionalModulePaths: additionalModulePaths,
128-
webpackAliases: getWebpackAliases(options),
129-
jestAliases: getJestAliases(options),
130-
hasTsConfig,
131-
};
126+
return {
127+
additionalModulePaths: additionalModulePaths,
128+
webpackAliases: getWebpackAliases(options),
129+
jestAliases: getJestAliases(options),
130+
hasTsConfig,
131+
};
132132
}
133133

134134
module.exports = getModules();

config/paths.js

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
'use strict';
1+
"use strict";
22

3-
const path = require('path');
4-
const fs = require('fs');
5-
const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
3+
const path = require("path");
4+
const fs = require("fs");
5+
const getPublicUrlOrPath = require("react-dev-utils/getPublicUrlOrPath");
66

77
// Make sure any symlinks in the project folder are resolved:
88
// https://github.com/facebook/create-react-app/issues/637
99
const appDirectory = fs.realpathSync(process.cwd());
10-
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
10+
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
1111

1212
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
1313
// "public path" at which the app is served.
@@ -16,58 +16,56 @@ const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
1616
// We can't use a relative path in HTML because we don't want to load something
1717
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
1818
const publicUrlOrPath = getPublicUrlOrPath(
19-
process.env.NODE_ENV === 'development',
20-
require(resolveApp('package.json')).homepage,
21-
process.env.PUBLIC_URL
19+
process.env.NODE_ENV === "development",
20+
require(resolveApp("package.json")).homepage,
21+
process.env.PUBLIC_URL
2222
);
2323

2424
const moduleFileExtensions = [
25-
'web.mjs',
26-
'mjs',
27-
'web.js',
28-
'js',
29-
'web.ts',
30-
'ts',
31-
'web.tsx',
32-
'tsx',
33-
'json',
34-
'web.jsx',
35-
'jsx',
25+
"web.mjs",
26+
"mjs",
27+
"web.js",
28+
"js",
29+
"web.ts",
30+
"ts",
31+
"web.tsx",
32+
"tsx",
33+
"json",
34+
"web.jsx",
35+
"jsx",
3636
];
3737

3838
// Resolve file paths in the same order as webpack
3939
const resolveModule = (resolveFn, filePath) => {
40-
const extension = moduleFileExtensions.find(extension =>
41-
fs.existsSync(resolveFn(`${filePath}.${extension}`))
42-
);
40+
const extension = moduleFileExtensions.find((extension) =>
41+
fs.existsSync(resolveFn(`${filePath}.${extension}`))
42+
);
4343

44-
if (extension) {
45-
return resolveFn(`${filePath}.${extension}`);
46-
}
44+
if (extension) {
45+
return resolveFn(`${filePath}.${extension}`);
46+
}
4747

48-
return resolveFn(`${filePath}.js`);
48+
return resolveFn(`${filePath}.js`);
4949
};
5050

5151
// config after eject: we're in ./config/
5252
module.exports = {
53-
dotenv: resolveApp('.env'),
54-
appPath: resolveApp('.'),
55-
appBuild: resolveApp('build'),
56-
appPublic: resolveApp('public'),
57-
appHtml: resolveApp('public/index.html'),
58-
appIndexJs: resolveModule(resolveApp, 'src/client/index'),
59-
appPackageJson: resolveApp('package.json'),
60-
appSrc: resolveApp('src'),
61-
appTsConfig: resolveApp('tsconfig.json'),
62-
appJsConfig: resolveApp('jsconfig.json'),
63-
yarnLockFile: resolveApp('yarn.lock'),
64-
testsSetup: resolveModule(resolveApp, 'src/setupTests'),
65-
proxySetup: resolveApp('src/setupProxy.js'),
66-
appNodeModules: resolveApp('node_modules'),
67-
swSrc: resolveModule(resolveApp, 'src/service-worker'),
68-
publicUrlOrPath,
53+
dotenv: resolveApp(".env"),
54+
appPath: resolveApp("."),
55+
appBuild: resolveApp("build/client"),
56+
appPublic: resolveApp("public"),
57+
appHtml: resolveApp("public/index.html"),
58+
appIndexJs: resolveModule(resolveApp, "src/client/index"),
59+
appPackageJson: resolveApp("package.json"),
60+
appSrc: resolveApp("src"),
61+
appTsConfig: resolveApp("tsconfig.json"),
62+
appJsConfig: resolveApp("jsconfig.json"),
63+
yarnLockFile: resolveApp("yarn.lock"),
64+
testsSetup: resolveModule(resolveApp, "src/setupTests"),
65+
proxySetup: resolveApp("src/setupProxy.js"),
66+
appNodeModules: resolveApp("node_modules"),
67+
swSrc: resolveModule(resolveApp, "src/service-worker"),
68+
publicUrlOrPath,
6969
};
7070

71-
72-
7371
module.exports.moduleFileExtensions = moduleFileExtensions;

ormconfig.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const path = require("path");
2+
3+
const dotenv = require("dotenv");
4+
5+
dotenv.config();
6+
7+
module.exports = {
8+
type: "postgres",
9+
host: process.env.DB_HOST,
10+
username: process.env.DB_USER,
11+
password: process.env.DB_PASSWORD,
12+
port: parseInt(process.env.DB_PORT),
13+
database: process.env.DB_NAME,
14+
entities: [path.join(__dirname, "src/server/entities/*.ts")],
15+
migrations: [path.join(__dirname, "src/server/migrations/*.ts")],
16+
cli: {
17+
migrationsDir: "src/server/migrations",
18+
},
19+
};

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"proxy": "http://localhost:5000",
4545
"scripts": {
4646
"client-dev": "node scripts/start.js",
47-
"server-dev": "ts-node-dev src/server/server.ts",
47+
"server-dev": "ts-node-dev src/server/server.ts --watch src/server/**/*.ts",
4848
"client-build": "node scripts/build.js",
4949
"server-build": "webpack --config ./config/webpackServer.config.js",
5050
"build": "yarn client-build && yarn server-build",
@@ -53,7 +53,9 @@
5353
"prettier-check": "prettier --check src/",
5454
"eslint": "eslint --fix --ext .tsx,ts,jsx,js src/",
5555
"eslint-check": "eslint --ext .tsx,ts,jsx,js src/ --max-warnings=0",
56-
"generate": "graphql-codegen"
56+
"generate-graphql": "graphql-codegen",
57+
"generate-migration": "ts-node ./node_modules/typeorm/cli.js migration:generate -n Migration",
58+
"run-migration": "ts-node ./node_modules/typeorm/cli.js migration:run"
5759
},
5860
"eslintConfig": {
5961
"extends": [

src/server/entities/Hello.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
2+
import { Field, ObjectType } from "type-graphql";
3+
4+
@ObjectType()
5+
@Entity()
6+
export class Hello extends BaseEntity {
7+
@PrimaryGeneratedColumn()
8+
id: string;
9+
10+
@Field()
11+
@Column({ length: 256 })
12+
hello: string;
13+
}

0 commit comments

Comments
 (0)