[mongodb] Checked TSchema on insert and update documents (#29245)

* Schema definition on insertOne, insertMany and update definitions. Added types as OptionalTSchema and UpdateQuery.

* added user on definitions by

* added missing properties and url docs

* added missing properties and url docs with test

* added missing properties and url docs with test

* changed OptionalTSchema to Partial, modified logic on update funcs, this will be more strict
This commit is contained in:
hector7
2018-10-01 21:12:14 -07:00
committed by Wesley Wigham
parent 8c197683d5
commit e15cb60ee0
2 changed files with 54 additions and 38 deletions
+50 -36
View File
@@ -17,6 +17,7 @@
// Dominik Heigl <https://github.com/various89>
// Angela-1 <https://github.com/angela-1>
// Mikael Lirbank <https://github.com/lirbank>
// Hector Ribes <https://github.com/hector7>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
@@ -642,14 +643,14 @@ export interface Collection<TSchema = Default> {
* @deprecated Use countDocuments or estimatedDocumentCount
*/
count(callback: MongoCallback<number>): void;
count(query: Object, callback: MongoCallback<number>): void;
count(query?: Object, options?: MongoCountPreferences): Promise<number>;
count(query: Object, options: MongoCountPreferences, callback: MongoCallback<number>): void;
count(query: FilterQuery<TSchema>, callback: MongoCallback<number>): void;
count(query?: FilterQuery<TSchema>, options?: MongoCountPreferences): Promise<number>;
count(query: FilterQuery<TSchema>, options: MongoCountPreferences, callback: MongoCallback<number>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments */
countDocuments(callback: MongoCallback<number>): void;
countDocuments(query: Object, callback: MongoCallback<number>): void;
countDocuments(query?: Object, options?: MongoCountPreferences): Promise<number>;
countDocuments(query: Object, options: MongoCountPreferences, callback: MongoCallback<number>): void;
countDocuments(query: FilterQuery<TSchema>, callback: MongoCallback<number>): void;
countDocuments(query?: FilterQuery<TSchema>, options?: MongoCountPreferences): Promise<number>;
countDocuments(query: FilterQuery<TSchema>, options: MongoCountPreferences, callback: MongoCallback<number>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#createIndex */
createIndex(fieldOrSpec: string | any, callback: MongoCallback<string>): void;
createIndex(fieldOrSpec: string | any, options?: IndexOptions): Promise<string>;
@@ -667,9 +668,9 @@ export interface Collection<TSchema = Default> {
deleteOne(filter: FilterQuery<TSchema>, options?: CommonOptions & { bypassDocumentValidation?: boolean }): Promise<DeleteWriteOpResultObject>;
deleteOne(filter: FilterQuery<TSchema>, options: CommonOptions & { bypassDocumentValidation?: boolean }, callback: MongoCallback<DeleteWriteOpResultObject>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#distinct */
distinct(key: string, query: Object, callback: MongoCallback<any>): void;
distinct(key: string, query: Object, options?: { readPreference?: ReadPreference | string, maxTimeMS?: number, session?: ClientSession }): Promise<any>;
distinct(key: string, query: Object, options: { readPreference?: ReadPreference | string, maxTimeMS?: number, session?: ClientSession }, callback: MongoCallback<any>): void;
distinct(key: string, query: FilterQuery<TSchema>, callback: MongoCallback<any>): void;
distinct(key: string, query: FilterQuery<TSchema>, options?: { readPreference?: ReadPreference | string, maxTimeMS?: number, session?: ClientSession }): Promise<any>;
distinct(key: string, query: FilterQuery<TSchema>, options: { readPreference?: ReadPreference | string, maxTimeMS?: number, session?: ClientSession }, callback: MongoCallback<any>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#drop */
drop(options?: { session: ClientSession }): Promise<any>;
drop(callback: MongoCallback<any>): void;
@@ -684,9 +685,9 @@ export interface Collection<TSchema = Default> {
dropIndexes(options: { session?: ClientSession, maxTimeMS?: number }, callback: MongoCallback<any>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#estimatedDocumentCount */
estimatedDocumentCount(callback: MongoCallback<number>): void;
estimatedDocumentCount(query: Object, callback: MongoCallback<number>): void;
estimatedDocumentCount(query?: Object, options?: MongoCountPreferences): Promise<number>;
estimatedDocumentCount(query: Object, options: MongoCountPreferences, callback: MongoCallback<number>): void;
estimatedDocumentCount(query: FilterQuery<TSchema>, callback: MongoCallback<number>): void;
estimatedDocumentCount(query?: FilterQuery<TSchema>, options?: MongoCountPreferences): Promise<number>;
estimatedDocumentCount(query: FilterQuery<TSchema>, options: MongoCountPreferences, callback: MongoCallback<number>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#find */
find<T = TSchema>(query?: FilterQuery<TSchema>): Cursor<T>;
/** @deprecated */
@@ -737,19 +738,19 @@ export interface Collection<TSchema = Default> {
initializeUnorderedBulkOp(options?: CommonOptions): UnorderedBulkOperation;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertOne */
/** @deprecated Use insertOne, insertMany or bulkWrite */
insert(docs: Object, callback: MongoCallback<InsertOneWriteOpResult>): void;
insert(docs: TSchema, callback: MongoCallback<InsertOneWriteOpResult>): void;
/** @deprecated Use insertOne, insertMany or bulkWrite */
insert(docs: Object, options?: CollectionInsertOneOptions): Promise<InsertOneWriteOpResult>;
insert(docs: TSchema, options?: CollectionInsertOneOptions): Promise<InsertOneWriteOpResult>;
/** @deprecated Use insertOne, insertMany or bulkWrite */
insert(docs: Object, options: CollectionInsertOneOptions, callback: MongoCallback<InsertOneWriteOpResult>): void;
insert(docs: TSchema, options: CollectionInsertOneOptions, callback: MongoCallback<InsertOneWriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertMany */
insertMany(docs: Object[], callback: MongoCallback<InsertWriteOpResult>): void;
insertMany(docs: Object[], options?: CollectionInsertManyOptions): Promise<InsertWriteOpResult>;
insertMany(docs: Object[], options: CollectionInsertManyOptions, callback: MongoCallback<InsertWriteOpResult>): void;
insertMany(docs: TSchema[], callback: MongoCallback<InsertWriteOpResult>): void;
insertMany(docs: TSchema[], options?: CollectionInsertManyOptions): Promise<InsertWriteOpResult>;
insertMany(docs: TSchema[], options: CollectionInsertManyOptions, callback: MongoCallback<InsertWriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#insertOne */
insertOne(docs: Object, callback: MongoCallback<InsertOneWriteOpResult>): void;
insertOne(docs: Object, options?: CollectionInsertOneOptions): Promise<InsertOneWriteOpResult>;
insertOne(docs: Object, options: CollectionInsertOneOptions, callback: MongoCallback<InsertOneWriteOpResult>): void;
insertOne(docs: TSchema, callback: MongoCallback<InsertOneWriteOpResult>): void;
insertOne(docs: TSchema, options?: CollectionInsertOneOptions): Promise<InsertOneWriteOpResult>;
insertOne(docs: TSchema, options: CollectionInsertOneOptions, callback: MongoCallback<InsertOneWriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#isCapped */
isCapped(options?: { session: ClientSession }): Promise<any>;
isCapped(callback: MongoCallback<any>): void;
@@ -784,39 +785,52 @@ export interface Collection<TSchema = Default> {
rename(newName: string, options?: { dropTarget?: boolean, session?: ClientSession }): Promise<Collection<TSchema>>;
rename(newName: string, options: { dropTarget?: boolean, session?: ClientSession }, callback: MongoCallback<Collection<TSchema>>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#replaceOne */
replaceOne(filter: FilterQuery<TSchema>, doc: Object, callback: MongoCallback<ReplaceWriteOpResult>): void;
replaceOne(filter: FilterQuery<TSchema>, doc: Object, options?: ReplaceOneOptions): Promise<ReplaceWriteOpResult>;
replaceOne(filter: FilterQuery<TSchema>, doc: Object, options: ReplaceOneOptions, callback: MongoCallback<ReplaceWriteOpResult>): void;
replaceOne(filter: FilterQuery<TSchema>, doc: TSchema, callback: MongoCallback<ReplaceWriteOpResult>): void;
replaceOne(filter: FilterQuery<TSchema>, doc: TSchema, options?: ReplaceOneOptions): Promise<ReplaceWriteOpResult>;
replaceOne(filter: FilterQuery<TSchema>, doc: TSchema, options: ReplaceOneOptions, callback: MongoCallback<ReplaceWriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#save */
/** @deprecated Use insertOne, insertMany, updateOne or updateMany */
save(doc: Object, callback: MongoCallback<WriteOpResult>): void;
save(doc: TSchema, callback: MongoCallback<WriteOpResult>): void;
/** @deprecated Use insertOne, insertMany, updateOne or updateMany */
save(doc: Object, options?: CommonOptions): Promise<WriteOpResult>;
save(doc: TSchema, options?: CommonOptions): Promise<WriteOpResult>;
/** @deprecated Use insertOne, insertMany, updateOne or updateMany */
save(doc: Object, options: CommonOptions, callback: MongoCallback<WriteOpResult>): void;
save(doc: TSchema, options: CommonOptions, callback: MongoCallback<WriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#stats */
stats(callback: MongoCallback<CollStats>): void;
stats(options?: { scale: number, session?: ClientSession }): Promise<CollStats>;
stats(options: { scale: number, session?: ClientSession }, callback: MongoCallback<CollStats>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#update */
/** @deprecated use updateOne, updateMany or bulkWrite */
update(filter: FilterQuery<TSchema>, update: Object, callback: MongoCallback<WriteOpResult>): void;
update(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, callback: MongoCallback<WriteOpResult>): void;
/** @deprecated use updateOne, updateMany or bulkWrite */
update(filter: FilterQuery<TSchema>, update: Object, options?: ReplaceOneOptions & { multi?: boolean }): Promise<WriteOpResult>;
update(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, options?: ReplaceOneOptions & { multi?: boolean }): Promise<WriteOpResult>;
/** @deprecated use updateOne, updateMany or bulkWrite */
update(filter: FilterQuery<TSchema>, update: Object, options: ReplaceOneOptions & { multi?: boolean }, callback: MongoCallback<WriteOpResult>): void;
update(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, options: ReplaceOneOptions & { multi?: boolean }, callback: MongoCallback<WriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateMany */
updateMany(filter: FilterQuery<TSchema>, update: Object, callback: MongoCallback<UpdateWriteOpResult>): void;
updateMany(filter: FilterQuery<TSchema>, update: Object, options?: CommonOptions & { upsert?: boolean }): Promise<UpdateWriteOpResult>;
updateMany(filter: FilterQuery<TSchema>, update: Object, options: CommonOptions & { upsert?: boolean }, callback: MongoCallback<UpdateWriteOpResult>): void;
updateMany(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, callback: MongoCallback<UpdateWriteOpResult>): void;
updateMany(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, options?: CommonOptions & { upsert?: boolean }): Promise<UpdateWriteOpResult>;
updateMany(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, options: CommonOptions & { upsert?: boolean }, callback: MongoCallback<UpdateWriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateOne */
updateOne(filter: FilterQuery<TSchema>, update: Object, callback: MongoCallback<UpdateWriteOpResult>): void;
updateOne(filter: FilterQuery<TSchema>, update: Object, options?: ReplaceOneOptions): Promise<UpdateWriteOpResult>;
updateOne(filter: FilterQuery<TSchema>, update: Object, options: ReplaceOneOptions, callback: MongoCallback<UpdateWriteOpResult>): void;
updateOne(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, callback: MongoCallback<UpdateWriteOpResult>): void;
updateOne(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, options?: ReplaceOneOptions): Promise<UpdateWriteOpResult>;
updateOne(filter: FilterQuery<TSchema>, update: UpdateQuery<TSchema> | TSchema, options: ReplaceOneOptions, callback: MongoCallback<UpdateWriteOpResult>): void;
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#watch */
watch(pipeline?: Object[], options?: ChangeStreamOptions & { startAtClusterTime?: Timestamp, session?: ClientSession }): ChangeStream;
}
/** https://docs.mongodb.com/manual/reference/operator/update */
export type UpdateQuery<T> = {
$inc: { [P in keyof T]?: number } | { [key: string]: number },
$min: { [P in keyof T]?: number } | { [key: string]: number },
$max: { [P in keyof T]?: number } | { [key: string]: number },
$mul: { [P in keyof T]?: number } | { [key: string]: number },
$set: Partial<T> | { [key: string]: any },
$setOnInsert: Partial<T> | { [key: string]: any },
$unset: { [P in keyof T]?: '' } | { [key: string]: '' },
$rename: { [key: string]: keyof T } | { [key: string]: string },
$currentDate: { [P in keyof T]?: (true | { $type: 'timestamp' }) } | { [key: string]: (true | { $type: 'timestamp' }) },
};
export type FilterQuery<T> = {
[P in keyof T]?: T[P] | {
$eq?: T[P];
+4 -2
View File
@@ -36,6 +36,7 @@ let options: mongodb.MongoClientOptions = {
MongoClient.connect(connectionString, options, function (err: mongodb.MongoError, client: mongodb.MongoClient) {
if (err) throw err;
const db = client.db('test');
var collection = db.collection('test_insert');
collection.insertOne({ a: 2 }, function (err: mongodb.MongoError, docs: any) {
@@ -156,10 +157,11 @@ MongoClient.connect(connectionString, options, function (err: mongodb.MongoError
{
type TestCollection = {
stringField: string;
numberField: number;
numberField?: number;
};
let testCollection = db.collection<TestCollection>('testCollection');
testCollection.insertOne({stringField:'hola'})
testCollection.insertMany([{stringField:'hola'},{stringField:'hola', numberField: 1}])
testCollection.find({
numberField: {
$and: [{ $gt: 0, $lt: 100 }]