Initial commit

This commit is contained in:
minec 2025-03-31 01:10:49 +02:00
commit e941dc18a4
8 changed files with 3944 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules/
database.sqlite
dist/

3784
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"dependencies": {
"-": "^0.0.1",
"fastify": "^5.2.2",
"graphql": "^16.10.0",
"mercurius": "^16.1.0",
"metadata": "^0.1.0",
"reflect": "^0.1.3",
"reflect-metadata": "^0.2.2",
"sqlite3": "^5.1.7",
"typeorm": "^0.3.21",
"typeorm-fastify-plugin": "^3.0.0"
},
"devDependencies": {
"@types/node": "^22.13.14"
}
}

16
src/db.ts Normal file
View File

@ -0,0 +1,16 @@
import 'reflect-metadata';
import dbConnection from 'typeorm-fastify-plugin';
import { User } from './entity/User';
import { Post } from './entity/Post';
import { FastifyInstance } from 'fastify';
export async function registerDb(fastify: FastifyInstance) {
fastify.register(dbConnection, {
type: 'sqlite',
database: 'database.sqlite',
entities: [User, Post],
synchronize: true,
logging: true,
})
}

14
src/entity/Post.ts Normal file
View File

@ -0,0 +1,14 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { User } from './User';
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@ManyToOne(() => User, (user) => user.posts)
author: User;
}

14
src/entity/User.ts Normal file
View File

@ -0,0 +1,14 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { Post } from './Post';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToMany(() => Post, (post) => post.author)
posts: Post[];
}

82
src/index.ts Normal file
View File

@ -0,0 +1,82 @@
import 'reflect-metadata';
import Fastify from 'fastify';
import mercurius from 'mercurius';
import { registerDb } from './db';
import { User } from './entity/User';
import { Post } from './entity/Post';
const fastify = Fastify( {
logger: true
});
const schema = `
type User {
id: ID!
name: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
author: User!
}
type Query {
user(id: ID!): User
}
type Mutation {
createUser(name: String!): User
createPost(title: String!, userId: ID!): Post
}
`
const resolvers = {
Query: {
user: async (_: unknown, { id }: {id: number} ) => {
const userRepository = fastify.orm.getRepository(User)
return await userRepository.findOne({where: { id}, relations: ['posts']})
}
},
Mutation: {
createUser: async (_: unknown, { name }: {name: string}) => {
const user = new User();
user.name = name;
const userRepository = fastify.orm.getRepository(User)
await userRepository.save(user);
return user;
},
createPost: async (_: unknown, { title, userId }: {title: string, userId: number}) => {
const userRepository = fastify.orm.getRepository(User)
const user = await userRepository.findOne({ where: { id: userId } });
if (!user) {
throw new Error("User not found");
}
const postRepository = fastify.orm.getRepository(Post)
const post = new Post();
post.title = title;
post.author = user;
await postRepository.save(post);
return post;
}
}
}
registerDb(fastify);
fastify.register(mercurius, {
schema,
resolvers,
graphiql: true
})
fastify.listen({ port: 3000}, function (err, address) {
if (err) {
fastify.log.error(err);
process.exit(1);
}
console.log(`Server listening on ${address}`);
})

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"moduleResolution": "node",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false
},
"include": ["src"]
}