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 | import mongoose from 'mongoose'; import { Thingy } from '../../models/thingy'; import { ThingyRepository } from '../../interfaces/thingyRepository'; const thingySchema = new mongoose.Schema({ macAddress: { type: String, required: true }, temperature: { type: Number, required: false }, humidity: { type: Number, required: false }, co2: { type: Number, required: false }, pm_1: { type: Number, required: false }, pm_2_5: { type: Number, required: false }, pm_10: { type: Number, required: false }, timestamp: { type: Date, required: false }, }, { versionKey: false } // Disable the __v field); ); const ThingyModel = mongoose.model('Thingy', thingySchema, 'thingys'); export class MongooseThingyRepository implements ThingyRepository { private toDomain(doc: any): Thingy { return { _id: doc._id.toString(), macAddress: doc.macAddress, temperature: doc.temperature, humidity: doc.humidity, co2: doc.co2, pm_1: doc.pm_1, pm_2_5: doc.pm_2_5, pm_10: doc.pm_10, timestamp: doc.timestamp, }; } async filterThingys(filters: any): Promise<Thingy[]> { const query: any = {}; Iif (filters.timestamp) { Iif (filters.timestamp.$gte) query.timestamp = { ...query.timestamp, $gte: filters.timestamp.$gte }; Iif (filters.timestamp.$lte) query.timestamp = { ...query.timestamp, $lte: filters.timestamp.$lte }; } Iif (filters.macAddress) query.macAddress = filters.macAddress; const limit = filters.limit ? parseInt(filters.limit, 10) : 100; const docs = await ThingyModel.find(query).limit(limit).sort({ timestamp: -1 }); return docs.map((doc) => this.toDomain(doc)); } async findByMacAddress(macAddress: string, limit = 100): Promise<Thingy[]> { const docs = await ThingyModel.find({ macAddress }) .sort({ timestamp: -1 }) .limit(limit); return docs.map((doc) => this.toDomain(doc)); } async create(thingy: Thingy): Promise<Thingy> { const newThingy = new ThingyModel(thingy); const savedThingy = await newThingy.save(); return this.toDomain(savedThingy); } async deleteById(id: string): Promise<boolean> { console.log('query', { _id: new mongoose.Types.ObjectId(id) }); // Just log const result = await ThingyModel.deleteOne({ _id: new mongoose.Types.ObjectId(id) }); // Use raw string return result.deletedCount > 0; } } |