mirror of
https://github.com/FranP-code/spend-ia.git
synced 2025-10-13 00:14:09 +00:00
feat: added seed
This commit is contained in:
@@ -5,11 +5,13 @@ import dotenv from 'dotenv';
|
||||
import dbConnection from './db';
|
||||
import { publicProcedure, router } from './trpc';
|
||||
import { User, UserSchema } from './schemas';
|
||||
import { type UserType } from './types';
|
||||
import seed from './seed';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const appRouter = router({
|
||||
userById: publicProcedure.input(z.string()).query(async (opts) => {
|
||||
userById: publicProcedure.input(z.string()).query(async (opts): Promise<UserType | null> => {
|
||||
const { input } = opts;
|
||||
const user = await User.findById(input);
|
||||
return user;
|
||||
@@ -18,7 +20,7 @@ const appRouter = router({
|
||||
const user = await User.create(input);
|
||||
return user;
|
||||
}),
|
||||
userList: publicProcedure.query(async () => {
|
||||
userList: publicProcedure.query(async (): Promise<UserType[]> => {
|
||||
const users = await User.find();
|
||||
return users;
|
||||
}),
|
||||
@@ -34,6 +36,7 @@ const server = createHTTPServer({
|
||||
dbConnection()
|
||||
.then(() => {
|
||||
server.listen(3000);
|
||||
void seed();
|
||||
})
|
||||
.catch((e) => {
|
||||
// eslint-disable-next-line no-console
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"watch": "**/*",
|
||||
"ext": "ts, json",
|
||||
"exec": "ts-node app.ts"
|
||||
"exec": "ts-node --require ts-node/register ./app.ts"
|
||||
}
|
||||
|
||||
@@ -3,11 +3,14 @@
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"type": "module",
|
||||
"exports": "./dist/index.js",
|
||||
"devDependencies": {
|
||||
"@faker-js/faker": "^8.0.2",
|
||||
"nodemon": "^2.0.22"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "nodemon app.ts",
|
||||
"dev": "nodemon --watch './**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' app.ts",
|
||||
"build": "tsc --build"
|
||||
},
|
||||
"author": "",
|
||||
|
||||
46
packages/server/seed.ts
Normal file
46
packages/server/seed.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { getRandomItem } from 'utils';
|
||||
import { Currency, Spending, SpendingCategory, User } from './schemas';
|
||||
|
||||
export default async (): Promise<void> => {
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
return;
|
||||
}
|
||||
void import('@faker-js/faker').then(async ({ faker }) => {
|
||||
const EMAIL = 'abc1234@gmail.com';
|
||||
if (await User.exists({ email: EMAIL })) {
|
||||
return;
|
||||
}
|
||||
const user = await User.create({
|
||||
email: EMAIL,
|
||||
name: faker.person.fullName(),
|
||||
password: '123456',
|
||||
});
|
||||
const spendingCategories = await SpendingCategory.insertMany(
|
||||
[
|
||||
{ backgroundColor: 'rgb(99, 128, 255)', label: 'invest' },
|
||||
{ backgroundColor: 'rgb(54, 162, 235)', label: 'school' },
|
||||
{ backgroundColor: 'rgb(145, 86, 255)', label: 'party' },
|
||||
].map(({ backgroundColor, label }) => ({ backgroundColor, label, userId: user.id })),
|
||||
);
|
||||
const currencies = await Currency.insertMany([
|
||||
{
|
||||
label: 'USD',
|
||||
},
|
||||
{
|
||||
label: 'EUR',
|
||||
},
|
||||
{
|
||||
label: 'ARS',
|
||||
},
|
||||
]);
|
||||
await Spending.insertMany(
|
||||
[...Array(10)].map(() => ({
|
||||
amount: Math.random() * 10000,
|
||||
currencyId: getRandomItem(currencies).id,
|
||||
date: faker.date.past(),
|
||||
spendingCategoryId: getRandomItem(spendingCategories).id,
|
||||
userId: user.id,
|
||||
})),
|
||||
);
|
||||
});
|
||||
};
|
||||
@@ -26,7 +26,8 @@
|
||||
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
||||
|
||||
/* Modules */
|
||||
"module": "commonjs" /* Specify what module code is generated. */,
|
||||
"module": "ES2020" /* Specify what module code is generated. */,
|
||||
"moduleResolution": "Node",
|
||||
"rootDir": "./" /* Specify the root folder within your source files. */,
|
||||
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
|
||||
Reference in New Issue
Block a user