Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | import { dbClientPromise } from "@/lib/db";
import { insertOne } from "@/lib/dbHelpers/insertOne";
import { AudioBook } from "@/schemas/AudioBook";
import { NoThrow } from "@/utils/NoThrow";
import { Filter, MongoError, ObjectId, UpdateFilter } from "mongodb";
export type AudioBookDocumentType = AudioBook & {
createdAt: Date;
updatedAt: Date;
lastAccessedAt: Date;
status: "ACTIVE" | "ARCHIVED" | "DELETED";
};
export type AudioBookSummaryDocumentType = {
id: ObjectId;
name: string;
plot: string;
genre: string[];
numCharacters: number;
numEpisodes: number;
numScenes: number;
numDialogues: number;
createdAt: Date;
updatedAt: Date;
lastAccessedAt: Date;
status: "ACTIVE" | "ARCHIVED" | "DELETED";
};
export const AudioBookRepositoryPromise = (async function () {
const dbClientResult = await dbClientPromise;
if (dbClientResult.isErr()) {
return dbClientResult;
}
const dbClient = dbClientResult.value;
const db = dbClient.db("core");
const collection = db.collection<AudioBookDocumentType>("AudioBook");
return NoThrow.success({
async findAllSummaries() {
try {
const result = await collection
.aggregate<AudioBookSummaryDocumentType>([
{
$project: {
id: "$_id",
_id: 0,
name: 1,
plot: 1,
genre: 1,
numCharacters: { $size: "$characters" },
numEpisodes: { $size: "$episodes" },
numScenes: { $size: "$scenes" },
numDialouges: { $size: "$dialogues" },
createdAt: 1,
updatedAt: 1,
lastAccessedAt: 1,
status: 1,
},
},
])
.toArray();
return NoThrow.success(result);
} catch (error) {
if (error instanceof MongoError) {
return NoThrow.error(error);
}
if (error instanceof Error) {
return NoThrow.error(error);
}
return NoThrow.error(new Error("Unknown error"));
}
},
async findOneByID(id: ObjectId) {
try {
const result = await collection.findOne({ _id: id, status: "ACTIVE" });
if (!result) {
return NoThrow.error(new Error("Data not found"));
}
return NoThrow.success(result);
} catch (error) {
if (error instanceof MongoError) {
return NoThrow.error(error);
}
if (error instanceof Error) {
return NoThrow.error(error);
}
return NoThrow.error(new Error("Unknown error"));
}
},
async insertOne(document: AudioBookDocumentType) {
return insertOne(document, collection);
},
async replaceOneByID(id: ObjectId, document: AudioBookDocumentType) {
try {
const result = await collection.replaceOne({ _id: id }, document);
/* if (!result.acknowledged) {
return NoThrow.error(new Error("Data not found"));
} */
return NoThrow.success(result);
} catch (error) {
if (error instanceof MongoError) {
return NoThrow.error(error);
}
if (error instanceof Error) {
return NoThrow.error(error);
}
return NoThrow.error(new Error("Unknown error"));
}
},
async updateOne(filter: Filter<AudioBookDocumentType>, updateFilter: UpdateFilter<AudioBookDocumentType>) {
try {
const result = await collection.updateOne(filter, updateFilter);
/* if (!result.acknowledged) {
return NoThrow.error(new Error(""))
} */
return NoThrow.success(result);
} catch (error) {
if (error instanceof MongoError) {
return NoThrow.error(error);
}
if (error instanceof Error) {
return NoThrow.error(error);
}
return NoThrow.error(new Error("Unknown error"));
}
},
});
})();
|