Skip to content
This repository was archived by the owner on Dec 14, 2022. It is now read-only.

Commit 867ff6a

Browse files
committed
perf: converted async/await to promises
1 parent 920c7a4 commit 867ff6a

File tree

2 files changed

+69
-40
lines changed

2 files changed

+69
-40
lines changed

src/Collection.ts

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
101101
*
102102
* @returns The entity.
103103
*/
104-
public async get(id: string): Promise<T | null> {
105-
const snapshot = await this._native.doc(id).get();
106-
if (snapshot.exists) {
107-
return FirestoreSerializer.deserialize(snapshot, this._Entity, this);
108-
}
109-
return null;
104+
public get(id: string): Promise<T | null> {
105+
return new Promise((resolve): void => {
106+
this._native.doc(id).get().then((snapshot): void => {
107+
if (snapshot.exists) {
108+
resolve(FirestoreSerializer.deserialize(snapshot, this._Entity, this));
109+
}
110+
return resolve(null);
111+
});
112+
});
110113
}
111114

112115
/**
@@ -115,13 +118,18 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
115118
*
116119
* @returns The updated entity.
117120
*/
118-
public async update(entity: T): Promise<T | null> {
119-
if (!entity.id) {
120-
throw new Error(`An ID must be provided when updating ${entity.constructor.name}`);
121-
}
122-
const { id, ...data } = FirestoreSerializer.serialize(entity, WriteTypes.Update);
123-
await this._native.doc(id).update(data);
124-
return this.get(entity.id);
121+
public update(entity: T): Promise<T | null> {
122+
return new Promise((resolve): void => {
123+
if (!entity.id) {
124+
throw new Error(`An ID must be provided when updating ${entity.constructor.name}`);
125+
}
126+
const { id, ...data } = FirestoreSerializer.serialize(entity, WriteTypes.Update);
127+
this._native.doc(id).update(data).then((): void => {
128+
this.get(entity.id).then((updatedEntity): void => {
129+
resolve(updatedEntity);
130+
});
131+
});
132+
});
125133
};
126134

127135
/**
@@ -130,15 +138,23 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
130138
*
131139
* @returns The created entity.
132140
*/
133-
public async create(entity: T): Promise<T | null> {
134-
const { id, ...data } = FirestoreSerializer.serialize(entity, WriteTypes.Create);
135-
if (id) {
136-
await this._native.doc(id).set(data);
137-
return this.get(id);
138-
} else {
139-
const result = await this._native.add(data);
140-
return this.get(result.id);
141-
}
141+
public create(entity: T): Promise<T | null> {
142+
return new Promise((resolve): void => {
143+
const { id, ...data } = FirestoreSerializer.serialize(entity, WriteTypes.Create);
144+
if (id) {
145+
this._native.doc(id).set(data).then((): void => {
146+
this.get(id).then((snapshot): void => {
147+
resolve(snapshot);
148+
});
149+
});
150+
} else {
151+
this._native.add(data).then((result): void => {
152+
this.get(result.id).then((snapshot): void => {
153+
resolve(snapshot);
154+
});
155+
});
156+
}
157+
});
142158
};
143159

144160
/**
@@ -147,25 +163,33 @@ class Collection <T extends Entity, P extends Entity> implements ICollection<T,
147163
*
148164
* @returns A list of entities matching the criteria.
149165
*/
150-
public async find(query? : ICollectionQuery<T>): Promise<T[]> {
151-
let querySnapshot: firestore.QuerySnapshot;
152-
if (query) {
153-
const fields = getRepository(this._Entity.prototype.constructor.name).fields;
154-
querySnapshot = await QueryBuilder.query(this, fields, query).get();
155-
} else {
156-
querySnapshot = await this._native.get();
157-
}
158-
return querySnapshot.docs.map((snapshot): T => {
159-
return FirestoreSerializer.deserialize(snapshot, this._Entity, this) as T;
166+
public find(query? : ICollectionQuery<T>): Promise<T[]> {
167+
return new Promise((resolve): void => {
168+
let querySnapshotPromise: Promise<firestore.QuerySnapshot>;
169+
if (query) {
170+
const fields = getRepository(this._Entity.prototype.constructor.name).fields;
171+
querySnapshotPromise = QueryBuilder.query(this, fields, query).get();
172+
} else {
173+
querySnapshotPromise = this._native.get();
174+
}
175+
querySnapshotPromise.then((querySnapshot): void => {
176+
resolve(querySnapshot.docs.map((snapshot): T => {
177+
return FirestoreSerializer.deserialize(snapshot, this._Entity, this) as T;
178+
}));
179+
});
160180
});
161181
}
162182

163183
/**
164184
* Removes a document from the collection.
165185
* @param id The document ID to remove.
166186
*/
167-
public async remove(id: string): Promise<void> {
168-
await this._native.doc(id).delete();
187+
public remove(id: string): Promise<void> {
188+
return new Promise((resolve): void => {
189+
this._native.doc(id).delete().then((): void => {
190+
resolve();
191+
});
192+
});
169193
}
170194
}
171195

src/fields/DocumentRef.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,17 @@ class DocumentRef <T extends Entity> implements IDocumentRef<T> {
9292
/**
9393
* Gets the data from the document reference.
9494
*/
95-
public async get(): Promise<T> {
96-
if (this._cachedDocument === null) {
97-
const snapshot = await this._native.get();
98-
this._cachedDocument = FirestoreSerializer.deserialize(snapshot, this._model, this.parent);
99-
}
100-
return this._cachedDocument;
95+
public get(): Promise<T> {
96+
return new Promise((resolve): void => {
97+
if (this._cachedDocument === null) {
98+
this._native.get().then((snapshot): void => {
99+
this._cachedDocument = FirestoreSerializer.deserialize(snapshot, this._model, this.parent);
100+
resolve(this._cachedDocument);
101+
});
102+
} else {
103+
resolve(this._cachedDocument);
104+
}
105+
});
101106
}
102107

103108
/**

0 commit comments

Comments
 (0)