Skip to content

Commit de392a8

Browse files
committed
✨ feat(Model & Document): added class defination and create/data member function to Model and Document class respectively
1 parent 1af7816 commit de392a8

File tree

5 files changed

+113
-7
lines changed

5 files changed

+113
-7
lines changed

src/Document/class.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { firestore as __firestore } from "firebase-admin";
2+
import ObjectSchema from "../SchemaTypes/Object/class";
3+
import { KeyValueStore } from "../SchemaTypes/Object/types/KeyValue";
4+
import { DocErrorTypes as ErrorType } from "./types/error";
5+
import makeError from "../utils/makeError";
6+
7+
class Document<T extends KeyValueStore = any> {
8+
private __docRef: __firestore.DocumentReference<T>;
9+
10+
private __model: string;
11+
12+
private __schema: ObjectSchema<T>;
13+
14+
constructor(
15+
docRef: __firestore.DocumentReference<T>,
16+
model: string,
17+
schema: ObjectSchema<T>
18+
) {
19+
this.__docRef = docRef;
20+
this.__model = model;
21+
this.__schema = schema;
22+
}
23+
24+
get model() {
25+
return this.__model;
26+
}
27+
28+
get schema() {
29+
return this.__schema;
30+
}
31+
32+
public data = async () => {
33+
const docSnap = await this.__docRef.get().catch((err: Error) => {
34+
throw makeError(ErrorType.firestore, err.message);
35+
});
36+
37+
const docData = docSnap.data();
38+
if (typeof docData === "undefined" || !docSnap.exists)
39+
throw makeError(ErrorType.invalid, "Document is empty or non existant");
40+
41+
return docData;
42+
};
43+
}
44+
45+
export default Document;

src/Document/types/error.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enum DocErrorTypes {
2+
invalid = "Document/Invalid",
3+
firestore = "Document/Firestore",
4+
validation = "Document/Validation",
5+
}
6+
7+
export { DocErrorTypes };

src/Model/class.ts

+44-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
11
import { firestore as __firestore } from "firebase-admin";
2-
import { KeyValueStore } from "src/SchemaTypes/Object/types/KeyValue";
2+
import Document from "../Document/class";
3+
import { KeyValueStore } from "../SchemaTypes/Object/types/KeyValue";
34
import ObjectSchema from "../SchemaTypes/Object/class";
5+
import { ModelErrorTypes as ErrorType } from "./types/error";
6+
import makeError from "../utils/makeError";
47

58
class Model<T extends KeyValueStore = any> {
9+
private __name: string;
10+
11+
private __schema: ObjectSchema<T>;
12+
13+
private __db: __firestore.Firestore;
14+
615
constructor(
7-
private __name: string,
8-
private __schema: ObjectSchema<T>,
9-
private __db: __firestore.Firestore
10-
) {}
16+
name: string,
17+
schema: ObjectSchema<T>,
18+
firestore: __firestore.Firestore
19+
) {
20+
this.__name = name;
21+
this.__schema = schema;
22+
this.__db = firestore;
23+
}
24+
25+
get name() {
26+
return this.__name;
27+
}
28+
29+
get schema() {
30+
return this.__schema;
31+
}
32+
33+
public create = async (data: Partial<T>) => {
34+
const { valid, value, errors } = this.__schema.validate(data);
35+
if (!valid) {
36+
throw makeError(ErrorType.validation, errors);
37+
}
38+
39+
const docRef = await this.__db
40+
.collection(this.__name)
41+
.add(value)
42+
.catch((err: Error) => {
43+
throw makeError(ErrorType.firestore, err.message);
44+
});
1145

12-
public print = () => {
13-
console.log(this.__db, this.__name, this.__schema);
46+
return new Document<T>(
47+
docRef as __firestore.DocumentReference<T>,
48+
this.__name,
49+
this.__schema
50+
);
1451
};
1552
}
1653

src/Model/types/error.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
enum ModelErrorTypes {
2+
firestore = "Model/Firestore",
3+
validation = "Model/SchemaValidation",
4+
misc = "Model/Misc",
5+
}
6+
7+
export { ModelErrorTypes };

src/utils/makeError.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const makeError = <T>(type: T, data: string | object) => {
2+
const error = {
3+
type,
4+
data,
5+
};
6+
7+
return new Error(JSON.stringify(error));
8+
};
9+
10+
export default makeError;

0 commit comments

Comments
 (0)