it('untransforms mongodb number types', done => { const input = { long: mongodb.Long.fromNumber(Number.MAX_SAFE_INTEGER), double: new mongodb.Double(Number.MAX_VALUE), }; const output = transform.mongoObjectToParseObject(null, input, { fields: { long: { type: 'Number' }, double: { type: 'Number' }, }, }); expect(output.long).toBe(Number.MAX_SAFE_INTEGER); expect(output.double).toBe(Number.MAX_VALUE); done(); });
describe("Test rule: objectID", () => { it("should validate ObjectID", () => { const rule: RuleObjectID = { type: "objectID", ObjectID }; const check = v.compile({ id: { type: "objectID", ObjectID } }); const message = "The 'id' field must be an valid ObjectID"; expect(check({ id: "5f082780b00cc7401fb8"})).toEqual([{ type: "objectID", field: "id", actual: "5f082780b00cc7401fb8", message }]); expect(check({ id: new ObjectID() })).toEqual(true); const o = { id: "5f082780b00cc7401fb8e8fc" }; expect(check(o)).toEqual(true); expect(o.id).toBe("5f082780b00cc7401fb8e8fc"); }); it("should convert hexString-objectID to ObjectID", () => { const check = v.compile({ id: { type: "objectID", ObjectID, convert: true } }); const oid = new ObjectID(); const o = { id: oid.toHexString() }; expect(check(o)).toEqual(true); expect(o.id).toBeInstanceOf(ObjectID); expect(o.id).toEqual(oid); }); });
const check = v.compile({ id: { type: "objectID", ObjectID, convert: true } }); const oid = new ObjectID(); const o = { id: oid.toHexString() };
delete(collection, id) { return this.connect() .then(db => db.collection(collection).deleteOne({ _id: ObjectId(id) })) .then(() => id); }
// virtual getter for custom _id kittySchema .virtual('id') .get(function() { return MUUID.from(this._id).toString(); }) .set(function(val) { this._id = MUUID.from(val); });
var returnTimestampStr = function (timestamp) { var timestampStr = ""; if (timestamp) { timestampStr = timestamp.toString(); } else { timestampStr = Timestamp(1, new Date().getTime() / 1000).toString(); } return timestampStr; }
async update(collection, id, data) { const db = await this.connect(); const result = await db .collection(collection) .updateOne({ _id: ObjectId(id) }, { $set: data }, { upsert: true }); return result.insertedId; }
const getCampaignById = campaignId => { if (ObjectID.isValid(campaignId)) { return repository.getCampaignById(new ObjectID(campaignId)) } else return Promise.reject(CONSTS.CODE.WRONG_PARAM) }
router.post('/delete', function(req, res, next) { var id = req.body.id; mongo.connect(url, function(err, db) { assert.equal(null, err); db.collection('user-data').deleteOne({"_id": objectId(id)}, function(err, result) { assert.equal(null, err); console.log('Item deleted'); db.close(); }); }); });
async getAll(queryParams, order) { console.log(queryParams); const query = queryParams || {}; if (query && query.userId) { console.log(`query.userID`, query); query.userId = ObjectId(query.userId); } const tasks = await this.mongoDB.getAll(this.collection, query, order); return tasks || []; }
const getCampaignsByZoneId = (zoneId) => { if (ObjectID.isValid(zoneId)) { return repository.getZoneById(new ObjectID(zoneId)) .then(zone => { if (!zone) return Promise.reject(CONSTS.CODE.WRONG_PARAM) return rp({ uri: `http://${process.env.CAMPAIGN_SERVICE_URL}/campaigns?filter={"_id":{"$in":${JSON.stringify(zone.campaigns)}}}`, json: true }) .then(res => { return Promise.resolve(res.data) }) }) } else return Promise.reject(CONSTS.CODE.WRONG_PARAM) }
describe('v1()', () => { it('should generate a valid v1 uuid', function () { const mUUID = MUUID.v1(); assert.equal(validate(mUUID.toString()), true); // ensure generated uuids are always lowercase // as per spec - (see https://www.itu.int/rec/T-REC-X.667-201210-I/en) assert.equal(hasHexUpperCase(mUUID.toString()), false); }); });
find(_id) { if (typeof _id !== 'object') _id = ObjectID(_id); return db.collection('articles2').findOne({ _id }); }
describe('user.generateAuthToken', () => { test('Should return a valid JWT', () => { const payload = { _id: new mongoose.Types.ObjectId().toHexString() }; const user = new User(payload); const token = user.generateAuthToken(); const decoded = jwt.verify(token, config.get('jwtPrivateKey')); expect(decoded).toMatchObject(payload); }); });