diff --git a/packages/backend/environments/local.toml b/packages/backend/environments/local.toml index fcc120fe..b1f8d7df 100644 --- a/packages/backend/environments/local.toml +++ b/packages/backend/environments/local.toml @@ -2,3 +2,6 @@ host = "127.0.0.1" port = 8000 gqlPath = "/graphql" + +[database] + dbPath = "db/snowball" diff --git a/packages/backend/src/type.ts b/packages/backend/src/config.ts similarity index 62% rename from packages/backend/src/type.ts rename to packages/backend/src/config.ts index 451445f2..68451e2b 100644 --- a/packages/backend/src/type.ts +++ b/packages/backend/src/config.ts @@ -4,6 +4,11 @@ export interface ServerConfig { gqlPath?: string; } +export interface DatabaseConfig { + dbPath: string; +} + export interface Config { server: ServerConfig; + database: DatabaseConfig; } diff --git a/packages/backend/src/constants.ts b/packages/backend/src/constants.ts new file mode 100644 index 00000000..31b52860 --- /dev/null +++ b/packages/backend/src/constants.ts @@ -0,0 +1,4 @@ +export const DEFAULT_GQL_PATH = '/graphql'; + +// Note: temporary hardcoded user, later to be derived from auth token +export const USER_ID = 1; diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts index 0739a55f..329bbe8f 100644 --- a/packages/backend/src/database.ts +++ b/packages/backend/src/database.ts @@ -2,23 +2,35 @@ import { DataSource } from 'typeorm'; import path from 'path'; import debug from 'debug'; -const log = debug('snowball:server'); +import { User } from './entity/User'; +import { DatabaseConfig } from './config'; -export const initializeDatabase = async ( - database: string = 'db/snowball' -): Promise => { - try { - const AppDataSource = new DataSource({ +const log = debug('snowball:database'); + +export class Database { + private dataSource: DataSource; + + constructor ({ dbPath }: DatabaseConfig) { + this.dataSource = new DataSource({ type: 'better-sqlite3', - database, + database: dbPath, entities: [path.join(__dirname, '/entity/*')], synchronize: true, logging: false }); - - await AppDataSource.initialize(); - log('database initialized'); - } catch (error) { - log(error); } -}; + + async init () : Promise { + await this.dataSource.initialize(); + log('database initialized'); + } + + async getUser (userId: number) : Promise { + const userRepository = this.dataSource.getRepository(User); + const user = await userRepository.findOneBy({ + id: userId + }); + + return user; + } +} diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 7fa58981..c1fef9f3 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -3,22 +3,26 @@ import debug from 'debug'; import fs from 'fs'; import path from 'path'; -import { initializeDatabase } from './database'; +import { Database } from './database'; import { createAndStartServer } from './server'; import { createResolvers } from './resolvers'; import { getConfig } from './utils'; -import { Config } from './type'; +import { Config } from './config'; const log = debug('snowball:server'); const configFilePath = 'environments/local.toml'; export const main = async (): Promise => { // TODO: get config path using cli - const { server } = await getConfig(configFilePath); + const { server, database } = await getConfig(configFilePath); + + const db = new Database(database); + await db.init(); - await initializeDatabase(); const typeDefs = fs.readFileSync(path.join(__dirname, 'schema.gql')).toString(); - await createAndStartServer(typeDefs, createResolvers, server); + const resolvers = await createResolvers(db); + + await createAndStartServer(typeDefs, resolvers, server); }; main() diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index 8a745398..60ec3cfd 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -1,12 +1,16 @@ -const user = { - id: 2 -}; +import { Database } from './database'; -export const createResolvers = async (): Promise => { +export const createResolvers = async (db: Database): Promise => { return { Query: { - // TODO: fetch user data from db - user: () => user + user: ( + _: any, + __: any, + // TODO: add custom type for context + context: any + ) => { + return db.getUser(context.userId); + } } }; }; diff --git a/packages/backend/src/server.ts b/packages/backend/src/server.ts index 24c4e6b5..477a886f 100644 --- a/packages/backend/src/server.ts +++ b/packages/backend/src/server.ts @@ -10,12 +10,11 @@ import { import { TypeSource } from '@graphql-tools/utils'; import { makeExecutableSchema } from '@graphql-tools/schema'; -import { ServerConfig } from './type'; +import { ServerConfig } from './config'; +import { DEFAULT_GQL_PATH, USER_ID } from './constants'; const log = debug('snowball:server'); -const DEFAULT_GQL_PATH = '/graphql'; - export const createAndStartServer = async ( typeDefs: TypeSource, resolvers: any, @@ -31,12 +30,16 @@ export const createAndStartServer = async ( // Create the schema const schema = makeExecutableSchema({ typeDefs, - resolvers: await resolvers() + resolvers }); const server = new ApolloServer({ schema, csrfPrevention: true, + context: () => { + // TODO: Use userId derived from auth token + return { userId: USER_ID }; + }, plugins: [ // Proper shutdown for the HTTP server ApolloServerPluginDrainHttpServer({ httpServer }),