feat: mongoose integration

This commit is contained in:
2023-07-04 23:06:32 -03:00
parent da3e6cb101
commit 6971eeda3c
9 changed files with 62 additions and 7 deletions

View File

@@ -19,7 +19,7 @@ export const SpendScreen = (): JSX.Element => {
]);
useEffect(() => {
trpc.userCreate
.mutate({ name: 'ABC' })
.mutate({ email: 'abdfasfasdfasdc@gmail.com', name: 'ABC', password: '23eqwrqwerqwe4' })
.then(() => {
trpc.userList
.query()

View File

@@ -1,21 +1,26 @@
import { z } from 'zod';
import { createHTTPServer } from '@trpc/server/adapters/standalone';
import cors from 'cors';
import { db } from './db';
import dotenv from 'dotenv';
import dbConnection from './db';
import { publicProcedure, router } from './trpc';
import { User, UserSchema } from './schemas';
dotenv.config();
console.log(process.env);
const appRouter = router({
userById: publicProcedure.input(z.string()).query(async (opts) => {
const { input } = opts;
const user = await db.user.findById(input);
const user = await User.findById(input);
return user;
}),
userCreate: publicProcedure.input(z.object({ name: z.string() })).mutation(async ({ input }) => {
const user = await db.user.create(input);
userCreate: publicProcedure.input(UserSchema).mutation(async ({ input }) => {
const user = await User.create(input);
return user;
}),
userList: publicProcedure.query(async () => {
const users = await db.user.findMany();
const users = await User.find();
return users;
}),
});
@@ -27,4 +32,10 @@ const server = createHTTPServer({
router: appRouter,
});
server.listen(3000);
dbConnection()
.then(() => {
server.listen(3000);
})
.catch((e) => {
console.log(e);
});

View File

@@ -1,3 +1,5 @@
import mongoose from 'mongoose';
export interface User {
id: string;
name: string;
@@ -16,3 +18,14 @@ export const db = {
findMany: async () => users,
},
};
export default async (): Promise<void> => {
await mongoose
.connect(process.env.MONGO_URI as string)
.then(() => {
console.log('Connected to MongoDB');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
});
};

View File

@@ -15,6 +15,8 @@
"dependencies": {
"@types/cors": "^2.8.13",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"mongoose": "^7.3.1",
"ts-node": "^10.9.1"
}
}

View File

@@ -0,0 +1 @@
export * from './user';

View File

@@ -0,0 +1,17 @@
import { Schema, model } from 'mongoose';
import { z } from 'zod';
import { type UserType } from '../types';
export const UserSchema = z.object({
email: z.string().email(),
name: z.string(),
password: z.string().min(8),
} satisfies UserType);
const schema = new Schema({
email: { required: true, type: String, unique: true },
name: { required: true, type: String },
password: { required: true, type: String },
} satisfies UserType);
export const User = model('User', schema);

View File

@@ -0,0 +1 @@
export * from './user';

View File

@@ -0,0 +1,3 @@
import { type z } from 'zod';
export type StringInput = string | z.ZodString | { type: StringConstructor; [key: string]: any };

View File

@@ -0,0 +1,7 @@
import { type StringInput } from './primitives';
export interface UserType {
email: StringInput;
name: StringInput;
password: StringInput;
}