Skip to content

Commit d0f1ef4

Browse files
committed
feat: add several user database read and write function
Added readById, editById, create, upsertById function
1 parent 28440e0 commit d0f1ef4

File tree

4 files changed

+90
-25
lines changed

4 files changed

+90
-25
lines changed

_module/Database/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
import { writeById } from "./user";
1+
import { createNew, readById, editById, upsertById } from "./user";
22

3+
import { UserWrite } from "_types/database/user";
34

4-
export default class UserDB {
5-
static async writeById(id: string, dataToSave: any) {
6-
return await writeById(id, dataToSave);
7-
}
5+
6+
export class UserDB {
7+
static read = {
8+
readById: async (id: string) => {
9+
return await readById(id);
10+
}
11+
};
12+
13+
static write = {
14+
editById: async (id: string, dataToSave: UserWrite) => {
15+
return await editById(id, dataToSave);
16+
},
17+
create: async (dataToSave: UserWrite) => {
18+
return await createNew(dataToSave);
19+
},
20+
upsertById: async (id: string, dataToSave: UserWrite) => {
21+
return await upsertById(id, dataToSave);
22+
}
23+
};
824
}

_module/Database/user/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { writeById } from "./write";
1+
import { readById } from "./read";
2+
import { createNew, editById, upsertById } from "./write";
23

34

4-
export { writeById };
5+
export { editById, createNew, readById, upsertById };

_module/Database/user/read.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { User, UserSchema } from "_types/database/user";
2+
import { CustomError } from "_module/CustomError";
23
import profile from "_module/Database/_schema/user";
34

45

@@ -13,7 +14,9 @@ async function readData(key: string, value: any) {
1314
})) as unknown as UserSchema;
1415

1516
if (!data) {
16-
throw new Error("User not found");
17+
new CustomError(
18+
`Error while reading data with key '${key}' and value '${value}'`,
19+
).default();
1720
}
1821

1922
return {
@@ -22,6 +25,10 @@ async function readData(key: string, value: any) {
2225
} as User;
2326
}
2427
catch (error: any) {
25-
throw new Error(error.message);
28+
console.error(error);
29+
30+
new CustomError(
31+
`Error while reading data with key '${key}' and value '${value}'`,
32+
).func(readData);
2633
}
2734
}

_module/Database/user/write.ts

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
import { User, UserSchema, UserWrite } from "_types/database/user";
2+
import { CustomError } from "_module/CustomError";
23
import profile from "_module/Database/_schema/user";
34

45

5-
export function writeById(id: string, dataToSave: UserWrite) {
6-
return writeData("_id", id, dataToSave);
6+
export function editById(id: string, dataToSave: UserWrite) {
7+
return editData("_id", id, dataToSave);
78
}
89

9-
async function writeData(key: string, value: any, dataToSave: UserWrite) {
10-
try {
11-
const data = await profile.findOne({ [key]: value });
12-
const code = data ? 1 : 0;
10+
export async function createNew(dataToSave: UserWrite) {
11+
return createData(dataToSave);
12+
}
1313

14-
let newData: UserSchema;
15-
if (code) {
16-
newData = await profile.findOneAndUpdate({ [key]: value }, dataToSave, {
14+
export async function upsertById(id: string, dataToSave: UserWrite) {
15+
return upsertData("_id", id, dataToSave);
16+
}
17+
18+
async function editData(key: string, value: any, dataToSave: UserWrite) {
19+
try {
20+
const newData = (await profile.findOneAndUpdate(
21+
{ [key]: value },
22+
dataToSave,
23+
{
1724
new: true
18-
});
19-
}
20-
else {
21-
newData = (await profile.create(dataToSave)) as unknown as UserSchema;
22-
}
25+
},
26+
)) as unknown as UserSchema;
2327

2428
return {
2529
...newData,
2630
id: newData._id
2731
} as User;
2832
}
2933
catch (error) {
30-
throw new Error(error.message);
34+
new CustomError(
35+
`Error while editing data with key '${key}' and value '${value}'`,
36+
).func(editData);
3137
}
3238
}
3339

@@ -41,6 +47,41 @@ async function createData(dataToSave: UserWrite) {
4147
} as User;
4248
}
4349
catch (error) {
44-
throw new Error(error.message);
50+
console.error(error);
51+
52+
new CustomError(
53+
`Error while creating new user with data:\n${JSON.stringify(
54+
dataToSave,
55+
null,
56+
2,
57+
)}`,
58+
).func(createData);
59+
}
60+
}
61+
62+
async function upsertData(key: string, value: any, dataToSave: UserWrite) {
63+
try {
64+
const newData = (await profile.findOneAndUpdate(
65+
{ [key]: value },
66+
dataToSave,
67+
{
68+
new: true,
69+
upsert: true
70+
},
71+
)) as unknown as UserSchema;
72+
73+
return {
74+
...newData,
75+
id: newData._id
76+
} as User;
77+
}
78+
catch (error) {
79+
throw new Error(
80+
`Error while upserting new user with key '${key}', value '${value}' and data:\n${JSON.stringify(
81+
dataToSave,
82+
null,
83+
2,
84+
)}`,
85+
);
4586
}
4687
}

0 commit comments

Comments
 (0)