start DI
All checks were successful
All checks were successful
This commit is contained in:
parent
da035bf5c4
commit
7377bd3538
14 changed files with 76 additions and 4 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
11
migrations/20240703103050_accounts.up.sql
Normal file
11
migrations/20240703103050_accounts.up.sql
Normal file
|
@ -0,0 +1,11 @@
|
|||
CREATE TABLE IF NOT EXISTS public.accounts
|
||||
(
|
||||
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
|
||||
username text NOT NULL,
|
||||
password text NOT NULL,
|
||||
role_id smallint NOT NULL DEFAULT '1'::smallint,
|
||||
created_at timestamp without time zone NOT NULL DEFAULT now(),
|
||||
updated_at timestamp without time zone NOT NULL DEFAULT now(),
|
||||
CONSTRAINT accounts_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT accounts_username_key UNIQUE (username)
|
||||
);
|
15
migrations/20240703103105_movies.up.sql
Normal file
15
migrations/20240703103105_movies.up.sql
Normal file
|
@ -0,0 +1,15 @@
|
|||
-- Add migration script here
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.movies
|
||||
(
|
||||
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
|
||||
title text NOT NULL,
|
||||
overview text NOT NULL,
|
||||
poster_path text NOT NULL,
|
||||
backdrop_path text,
|
||||
release_date date NOT NULL,
|
||||
tmdb_id integer NOT NULL,
|
||||
created_at timestamp without time zone NOT NULL DEFAULT now(),
|
||||
updated_at timestamp without time zone NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (id)
|
||||
);
|
|
@ -16,7 +16,10 @@
|
|||
"typescript": "^5.6.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"fastify": "^5.2.0",
|
||||
"hono": "^4.6.15",
|
||||
"pg": "^8.13.1"
|
||||
"inversify": "^6.2.1",
|
||||
"pg": "^8.13.1",
|
||||
"reflect-metadata": "^0.2.2"
|
||||
}
|
||||
}
|
||||
|
|
4
src/accounts/AccountsController.ts
Normal file
4
src/accounts/AccountsController.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
import { injectable } from "inversify";
|
||||
|
||||
@injectable()
|
||||
export default class AccountsController {}
|
7
src/accounts/AccountsRouter.ts
Normal file
7
src/accounts/AccountsRouter.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
import type { FastifyInstance, FastifyPluginCallback } from "fastify";
|
||||
|
||||
const accountsRouter: FastifyPluginCallback = (fastify, opts, done) => {
|
||||
done();
|
||||
};
|
||||
|
||||
export default accountsRouter;
|
0
src/accounts/repository/AccountsRepository.ts
Normal file
0
src/accounts/repository/AccountsRepository.ts
Normal file
0
src/accounts/repository/IAccountsRepository.ts
Normal file
0
src/accounts/repository/IAccountsRepository.ts
Normal file
0
src/accounts/service/AccountsService.ts
Normal file
0
src/accounts/service/AccountsService.ts
Normal file
0
src/accounts/service/IAccountsService.ts
Normal file
0
src/accounts/service/IAccountsService.ts
Normal file
17
src/app.ts
17
src/app.ts
|
@ -0,0 +1,17 @@
|
|||
import Fastify from "fastify";
|
||||
import APIError from "./core/errors/APIError";
|
||||
|
||||
import accountsRouter from "./accounts/AccountsRouter";
|
||||
|
||||
const app = Fastify();
|
||||
|
||||
app.register(accountsRouter, { prefix: "/accounts" });
|
||||
|
||||
app.setErrorHandler((err, request, reply) => {
|
||||
const apiError =
|
||||
err instanceof APIError
|
||||
? err
|
||||
: new APIError("Internal Server Error", 500, err);
|
||||
|
||||
reply.status(apiError.statusCode).send(apiError.toStruct());
|
||||
});
|
|
@ -1,4 +1,5 @@
|
|||
import { Client, Pool, type PoolClient, type QueryResult } from "pg";
|
||||
import { injectable } from "inversify";
|
||||
import { Pool, type PoolClient, type QueryResult } from "pg";
|
||||
import type { SqlParams } from "./IDatabase";
|
||||
import type IDatabase from "./IDatabase";
|
||||
|
||||
|
@ -10,6 +11,7 @@ export interface PgConnectionOptions {
|
|||
password: string;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
export default class PgDatabase implements IDatabase {
|
||||
private pool: Pool;
|
||||
|
||||
|
|
|
@ -1,21 +1,31 @@
|
|||
export interface APIErrorStruct {
|
||||
msg: string;
|
||||
statusCode: number;
|
||||
cause?: string;
|
||||
}
|
||||
|
||||
export default class APIError extends Error {
|
||||
public statusCode: number;
|
||||
|
||||
constructor(msg?: string, statusCode?: number, options?: ErrorOptions) {
|
||||
public cause?: Error;
|
||||
|
||||
constructor(
|
||||
msg?: string,
|
||||
statusCode?: number,
|
||||
cause?: Error,
|
||||
options?: ErrorOptions,
|
||||
) {
|
||||
super(msg, options);
|
||||
|
||||
this.statusCode = statusCode || 500;
|
||||
this.cause = cause;
|
||||
}
|
||||
|
||||
toStruct(): APIErrorStruct {
|
||||
return {
|
||||
msg: this.message,
|
||||
statusCode: this.statusCode,
|
||||
cause: this.cause?.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,9 @@
|
|||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
"noPropertyAccessFromIndexSignature": false,
|
||||
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue