mirror of
https://github.com/FranP-code/spend-ia.git
synced 2025-10-13 00:14:09 +00:00
feat: mongoose integration
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
1
packages/server/schemas/index.ts
Normal file
1
packages/server/schemas/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './user';
|
||||
17
packages/server/schemas/user.ts
Normal file
17
packages/server/schemas/user.ts
Normal 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);
|
||||
1
packages/server/types/index.ts
Normal file
1
packages/server/types/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './user';
|
||||
3
packages/server/types/primitives.ts
Normal file
3
packages/server/types/primitives.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { type z } from 'zod';
|
||||
|
||||
export type StringInput = string | z.ZodString | { type: StringConstructor; [key: string]: any };
|
||||
7
packages/server/types/user.ts
Normal file
7
packages/server/types/user.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { type StringInput } from './primitives';
|
||||
|
||||
export interface UserType {
|
||||
email: StringInput;
|
||||
name: StringInput;
|
||||
password: StringInput;
|
||||
}
|
||||
Reference in New Issue
Block a user