diff --git a/.vscode/settings.json b/.vscode/settings.json index 9d807ae4..e3b6a49d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,5 +6,13 @@ "typescript", "typescriptreact" ], - "typescript.tsdk": "./app/node_modules/typescript/lib" + "typescript.tsdk": "./app/node_modules/typescript/lib", + "sqltools.connections": [ + { + "previewLimit": 50, + "driver": "SQLite", + "name": "Local SQLite", + "database": "./db/db.sqlite3" + } + ] } diff --git a/ormconfig.js b/ormconfig.js index 0746ba20..75883d12 100644 --- a/ormconfig.js +++ b/ormconfig.js @@ -3,11 +3,11 @@ const devConfig = { database: 'db/db.sqlite3', synchronize: true, logging: true, - entities: ['src/entity/**/*.ts'], - migrations: ['src/migration/**/*.ts'], + entities: ['server/entity/**/*.ts'], + migrations: ['server/migration/**/*.ts'], cli: { - entitiesDir: 'src/entity', - migrationsDir: 'src/migration', + entitiesDir: 'server/entity', + migrationsDir: 'server/migration', }, }; @@ -16,12 +16,12 @@ const prodConfig = { database: 'db/db.sqlite3', synchronize: false, logging: false, - entities: ['dist/entity/**/*.js'], - migrations: ['dist/migration/**/*.js'], + entities: ['dist/server/entity/**/*.js'], + migrations: ['dist/server/migration/**/*.js'], migrationsRun: true, cli: { - entitiesDir: 'dist/entity', - migrationsDir: 'dist/migration', + entitiesDir: 'dist/server/entity', + migrationsDir: 'dist/server/migration', }, }; diff --git a/package.json b/package.json index 3ccf2f2a..3c0356a1 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "version": "0.1.0", "private": true, "scripts": { - "dev": "nodemon -e ts -x ts-node --project tsconfig.server.json server/index.ts", - "build:server": "tsc --project tsconfig.server.json", + "dev": "nodemon -e ts -x ts-node --project server/tsconfig.json server/index.ts", + "build:server": "tsc --project server/tsconfig.json", "build:next": "next build", "build": "yarn build:next && yarn build:server", "start": "NODE_ENV=production node dist/server/index.js" @@ -14,6 +14,7 @@ "next": "9.5.2", "react": "16.13.1", "react-dom": "16.13.1", + "reflect-metadata": "^0.1.13", "sqlite3": "^5.0.0", "typeorm": "^0.2.25" }, diff --git a/server/entity/User.ts b/server/entity/User.ts new file mode 100644 index 00000000..a91122bc --- /dev/null +++ b/server/entity/User.ts @@ -0,0 +1,29 @@ +import { + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm'; + +@Entity() +export class User { + @PrimaryGeneratedColumn() + public id: number; + + @Column({ unique: true }) + public email: string; + + @Column({ nullable: true }) + public plexToken: string; + + @CreateDateColumn() + public createdAt: Date; + + @UpdateDateColumn() + public updatedAt: Date; + + constructor(init?: Partial) { + Object.assign(this, init); + } +} diff --git a/server/index.ts b/server/index.ts index c087da69..1a3266ae 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,6 +1,7 @@ import express from 'express'; import next from 'next'; import { createConnection } from 'typeorm'; +import routes from './routes'; const dev = process.env.NODE_ENV !== 'production'; const app = next({ dev }); @@ -12,9 +13,7 @@ app .prepare() .then(() => { const server = express(); - server.get('/api', (req, res) => { - res.json({ worked: true }); - }); + server.use('/api', routes); server.get('*', (req, res) => handle(req, res)); const port = Number(process.env.PORT) || 3000; diff --git a/server/routes/index.ts b/server/routes/index.ts new file mode 100644 index 00000000..e30c59d1 --- /dev/null +++ b/server/routes/index.ts @@ -0,0 +1,15 @@ +import { Router } from 'express'; +import user from './user'; + +const router = Router(); + +router.use('/user', user); + +router.get('/', (req, res) => { + return res.status(200).json({ + api: 'Overseerr API', + version: '1.0', + }); +}); + +export default router; diff --git a/server/routes/user.ts b/server/routes/user.ts new file mode 100644 index 00000000..e108727e --- /dev/null +++ b/server/routes/user.ts @@ -0,0 +1,15 @@ +import { Router } from 'express'; +import { getRepository } from 'typeorm'; +import { User } from '../entity/User'; + +const router = Router(); + +router.get('/', async (req, res) => { + const userRepository = getRepository(User); + + const users = await userRepository.find(); + + return res.status(200).json(users); +}); + +export default router; diff --git a/server/tsconfig.json b/server/tsconfig.json new file mode 100644 index 00000000..e27ab81e --- /dev/null +++ b/server/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "../dist", + "noEmit": false, + "strictPropertyInitialization": false, + "experimentalDecorators": true, + "emitDecoratorMetadata": true + } +} diff --git a/tsconfig.server.json b/tsconfig.server.json deleted file mode 100644 index e15d4a9a..00000000 --- a/tsconfig.server.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "module": "commonjs", - "outDir": "dist", - "noEmit": false - } -}