Comparing version 0.2.10 to 0.2.11
@@ -29,3 +29,3 @@ import * as MongoDb from 'mongodb'; | ||
*/ | ||
save(document: TDocument): Promise<UpdateResult | InsertResult<TDocument>>; | ||
save(document: TDocument | Object): Promise<UpdateResult | InsertResult<TDocument>>; | ||
/** | ||
@@ -190,3 +190,3 @@ * @param pipeline | ||
*/ | ||
insertOne(doc: TDocument, options?: MongoDb.CollectionInsertOneOptions): Promise<InsertResult<TDocument>>; | ||
insertOne(doc: Object | TDocument, options?: MongoDb.CollectionInsertOneOptions): Promise<InsertResult<TDocument>>; | ||
/** | ||
@@ -254,3 +254,4 @@ * @TODO | ||
updateOne(filter: Object | SchemaDocument, update: Object, options?: Object): Promise<UpdateResult>; | ||
private createUpdateSchema(document); | ||
} | ||
export { Collection }; |
@@ -10,2 +10,3 @@ "use strict"; | ||
}; | ||
const assert_1 = require('assert'); | ||
const document_1 = require("./document"); | ||
@@ -62,2 +63,5 @@ const cursor_1 = require("./cursor"); | ||
} | ||
else if (typeof query === 'string') { | ||
return { _id: mongodb_1.ObjectID.createFromHexString(query) }; | ||
} | ||
return new query_1.Query(this.construct, query).format(); | ||
@@ -77,4 +81,10 @@ } | ||
save(document) { | ||
if (document._id) { | ||
return this.updateOne(document, document.toObject()); | ||
assert_1.ok(document && typeof document === 'object', 'Collection.save(document) require an object or an instance of SchemaDocument.'); | ||
if (Object.prototype.hasOwnProperty.call(document, '_id') && document['_id']) { | ||
if (document instanceof document_1.SchemaDocument) { | ||
return this.updateOne(document._id, document.toObject()); | ||
} | ||
else { | ||
return this.updateOne(document['_id'], document); | ||
} | ||
} | ||
@@ -319,5 +329,9 @@ return this.insertOne(document); | ||
return this.queue((collection) => __awaiter(this, void 0, void 0, function* () { | ||
const listener = doc.getEventListener(); | ||
let document = doc; | ||
if (false === doc instanceof document_1.SchemaDocument) { | ||
document = this.factory(doc); | ||
} | ||
const listener = document.getEventListener(); | ||
listener.emit(Events.beforeInsert); | ||
const insertResult = new helpers_1.InsertResult(yield collection.insertOne(doc.toObject(), options), doc); | ||
const insertResult = new helpers_1.InsertResult(yield collection.insertOne(document.toObject(), options), document); | ||
listener.emit(Events.afterInsert); | ||
@@ -408,11 +422,24 @@ return insertResult; | ||
listener.emit(Events.beforeUpdate); | ||
const updateResult = new helpers_1.UpdateResult(yield collection.updateOne(this.filter(filter), update, options)); | ||
const updateResult = new helpers_1.UpdateResult(yield collection.updateOne(this.filter(filter), this.createUpdateSchema(update), options)); | ||
listener.emit(Events.afterUpdate); | ||
return updateResult; | ||
} | ||
return new helpers_1.UpdateResult(yield collection.updateOne(this.normalizeQuery(filter), update, options)); | ||
return new helpers_1.UpdateResult(yield collection.updateOne(this.filter(filter), this.createUpdateSchema(update), options)); | ||
})); | ||
} | ||
createUpdateSchema(document) { | ||
if (document instanceof document_1.SchemaDocument) { | ||
return this.createUpdateSchema(document.toObject()); | ||
} | ||
if (false === Object.prototype.hasOwnProperty.call(document, '_id')) { | ||
return document; | ||
} | ||
else { | ||
return Object.assign({}, Object.keys(document) | ||
.filter(key => key !== '_id') | ||
.map(key => ({ [key]: document[key] }))); | ||
} | ||
} | ||
} | ||
exports.Collection = Collection; | ||
//# sourceMappingURL=collection.js.map |
@@ -178,2 +178,28 @@ "use strict"; | ||
})); | ||
wrap_1.default('Collection.updateOne()', (t) => __awaiter(this, void 0, void 0, function* () { | ||
const collection = connect_1.default().get(TestCollection_1.TestCollection); | ||
const { ref } = yield collection.insertOne({ name: 'foo' }); | ||
ref.number = 98; | ||
const res1 = yield collection.updateOne(ref, ref); | ||
t.ok(res1.modified === 1, 'collection.updateOne(ref, ref) should be ok'); | ||
ref.number = 99; | ||
const res2 = yield collection.updateOne(ref._id, ref); | ||
t.ok(res2.modified === 1, 'collection.updateOne(ref._id, ref) should be ok'); | ||
ref.number = 100; | ||
const res3 = yield collection.updateOne({ _id: ref._id.toString() }, ref); | ||
t.ok(res3.modified === 1, 'collection.updateOne(ref._id.toString(), ref) should be ok'); | ||
return (yield collection.connection).disconnect(); | ||
})); | ||
wrap_1.default('Collection.save()', (t) => __awaiter(this, void 0, void 0, function* () { | ||
const collection = connect_1.default().get(TestCollection_1.TestCollection); | ||
const { ref } = yield collection.insertOne({ name: 'foo' }); | ||
ref.number = 100; | ||
const res1 = yield collection.save(ref); | ||
t.ok(res1 instanceof helpers_1.UpdateResult && res1.modified === 1, 'collection.save(ref) should be ok'); | ||
const res2 = yield collection.save({ name: 'bar' }); | ||
t.ok(res2 instanceof helpers_1.InsertResult && res2.insertedId, 'collection.save({name: bar}) should be ok'); | ||
const res3 = yield collection.save(collection.factory({ name: 'bar' })); | ||
t.ok(res3 instanceof helpers_1.InsertResult && res3.insertedId, 'collection.save(collection.factory({name: bar})) should be ok'); | ||
return (yield collection.connection).disconnect(); | ||
})); | ||
//# sourceMappingURL=collection.spec.js.map |
"use strict"; | ||
const MongoDb = require('mongodb'); | ||
class UpdateResult { | ||
@@ -8,3 +7,5 @@ constructor({ matchedCount, modifiedCount, upsertedCount, upsertedId }) { | ||
this.upserted = upsertedCount; | ||
this.upsertedId = new MongoDb.ObjectID(upsertedId._id.toString()); | ||
if (upsertedId) { | ||
this.upsertedId = upsertedId._id; | ||
} | ||
} | ||
@@ -15,3 +16,3 @@ } | ||
constructor({ insertedId }, document) { | ||
this.insertedId = new MongoDb.ObjectID(insertedId.toString()); | ||
this.insertedId = insertedId; | ||
this.ref = document; | ||
@@ -18,0 +19,0 @@ this.ref[Symbol.for('id')] = this.insertedId; |
{ | ||
"name": "mongot", | ||
"version": "0.2.10", | ||
"version": "0.2.11", | ||
"description": "A lightweight typed MongoDb library for TypeScript.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136054
2007