From 0fc2f541816034fe4ab1ccacae1d27b46ef5bc90 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Sat, 28 Oct 2023 17:12:23 -0300 Subject: [PATCH] feat: criacao dos arquivos de database e conexao com knex --- src/database/BaseDatabase.ts | 18 ++++++++++++++++++ src/database/PostDatabase.ts | 6 ++++++ src/database/UserDatabase.ts | 6 ++++++ src/index.ts | 23 +---------------------- 4 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 src/database/BaseDatabase.ts create mode 100644 src/database/PostDatabase.ts create mode 100644 src/database/UserDatabase.ts diff --git a/src/database/BaseDatabase.ts b/src/database/BaseDatabase.ts new file mode 100644 index 0000000..e91267e --- /dev/null +++ b/src/database/BaseDatabase.ts @@ -0,0 +1,18 @@ +import {knex} from 'knex' + +export abstract class BaseDatabase { + protected static connection = knex({ + client: "sqlite3", + connection: { + filename: "./src/database/poo.db", + }, + useNullAsDefault: true, + pool: { + min: 0, + max: 1, + afterCreate: (conn: any, cb: any) => { + conn.run("PRAGMA foreign_keys = ON", cb) + } + } + }) +} \ No newline at end of file diff --git a/src/database/PostDatabase.ts b/src/database/PostDatabase.ts new file mode 100644 index 0000000..dd01616 --- /dev/null +++ b/src/database/PostDatabase.ts @@ -0,0 +1,6 @@ +import { BaseDatabase } from "./BaseDatabase"; + +export class PostDatabase extends BaseDatabase { + public static TABLE_POSTS = 'posts'; + +} \ No newline at end of file diff --git a/src/database/UserDatabase.ts b/src/database/UserDatabase.ts new file mode 100644 index 0000000..2ddd635 --- /dev/null +++ b/src/database/UserDatabase.ts @@ -0,0 +1,6 @@ +import { BaseDatabase } from "./BaseDatabase"; + +export class UserDatabase extends BaseDatabase { + public static TABLE_USERS = 'users' + +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 111786c..558986d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,25 +14,4 @@ app.listen('3003', () => { // postman test app.get('/ping', (req: Request, res: Response) => { res.status(200).send('pong') -}) - -const post = new Post( - 'u001', - 'u003', - 'Coe irmao', - 5, - 0, - new Date().toISOString(), - new Date().toISOString() -) -console.log(post) - -const user = new User( - 'u003', - 'fulanoi', - 'fulano@email', - 'fluano123', - 'friend', - new Date().toISOString() -) -console.log(user) \ No newline at end of file +}) \ No newline at end of file