From a088b67ccb5f8a8f7ed9b9ebb8c232073f679ea3 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 22 Nov 2016 14:05:47 -0500 Subject: [PATCH] add interfaces for schema definitions to enable intellisense --- mongoose/mongoose-tests.ts | 44 ++++++++++- mongoose/mongoose.d.ts | 157 ++++++++++++++++++++++++++++++++++++- 2 files changed, 198 insertions(+), 3 deletions(-) diff --git a/mongoose/mongoose-tests.ts b/mongoose/mongoose-tests.ts index 25a0ba2677..8e4fa4a1ef 100644 --- a/mongoose/mongoose-tests.ts +++ b/mongoose/mongoose-tests.ts @@ -301,6 +301,48 @@ new mongoose.Schema({ favs: Number } }); +new mongoose.Schema({ name: { type: String, index: true }}); +new mongoose.Schema({ loc: { type: [Number], index: 'hashed' }}); +new mongoose.Schema({ loc: { type: [Number], index: '2d', sparse: true }}); +new mongoose.Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }}}); +new mongoose.Schema({ date: { type: Date, index: { unique: true, expires: '1d' }}}); +new mongoose.Schema({ born: { type: Date, required: '{PATH} is required!' }}); +new mongoose.Schema({ born: { type: Date, required: function() { + return this.age >= 18; +}}}); +new mongoose.Schema({ state: { type: String, enum: ['opening', 'open', 'closing', 'closed'] }}); +new mongoose.Schema({ state: { type: String, enum: { + values: ['opening', 'open', 'closing', 'closed'], + message: 'enum validator failed for path `{PATH}` with value `{VALUE}`' +}}}); +new mongoose.Schema({ name: { type: String, match: /^a/ }}); +new mongoose.Schema({ name: { type: String, match: [ + /\.html$/, "That file doesn't end in .html ({VALUE})" +]}}); +new mongoose.Schema({ + createdAt: {type: Date, expires: 60 * 60 * 24} +}); +new mongoose.Schema({ createdAt: { type: Date, expires: '1.5h' }}); +new mongoose.Schema({ d: { type: Date, max: new Date('2014-01-01') }}); +new mongoose.Schema({ d: { type: Date, max: [ + new Date('2014-01-01'), + 'The value of path `{PATH}` ({VALUE}) exceeds the limit ({MAX}).' +]}}); +new mongoose.Schema({d: {type: Date, min: [ + new Date('1970-01-01'), + 'The value of path `{PATH}` ({VALUE}) is beneath the limit ({MIN}).' +]}}); +new mongoose.Schema({ + integerOnly: { + type: Number, + get: v => Math.round(v), + set: v => Math.round(v) + } +}); +new mongoose.Schema({ name: { type: String, validate: [ + { validator: () => {return true}, msg: 'uh oh' }, + { validator: () => {return true}, msg: 'failed' } +]}}); animalSchema.statics.findByName = function(name: any, cb: any) { return this.find({ name: new RegExp(name, 'i') }, cb); }; @@ -311,7 +353,7 @@ animalSchema.virtual('name.full').get(function () { return this.name.first + ' ' + this.name.last; }); new mongoose.Schema({ - child: new mongoose.Schema({ name: 'string' }) + child: new mongoose.Schema({ name: String }) }); new mongoose.Schema({ eggs: { diff --git a/mongoose/mongoose.d.ts b/mongoose/mongoose.d.ts index 96c03c438a..1b1cba3e8c 100644 --- a/mongoose/mongoose.d.ts +++ b/mongoose/mongoose.d.ts @@ -531,10 +531,10 @@ declare module "mongoose" { * the child schema first before passing it into its parent. * @event init Emitted after the schema is compiled into a Model. */ - constructor(definition?: Object, options?: SchemaOptions); + constructor(definition?: SchemaDefinition, options?: SchemaOptions); /** Adds key path / schema type pairs to this schema. */ - add(obj: Object, prefix?: string): void; + add(obj: SchemaDefinition, prefix?: string): void; /** * Iterates the schemas paths similar to Array.forEach. @@ -716,6 +716,159 @@ declare module "mongoose" { timestamps?: Object; } + /* + * Intellisense for Schema definitions + */ + interface SchemaDefinition { + [path: string]: SchemaTypeOpts; + } + + /* + * The standard options available when configuring a schema type: + * new Schema({ + * name: { + * type: String, + * required: true, + * ... + * } + * }); + * References: + * - http://mongoosejs.com/docs/schematypes.html + * - http://mongoosejs.com/docs/api.html#schema_Schema.Types + */ + interface SchemaTypeOpts { + /* Common Options for all schema types */ + type?: T; + + /** Sets a default value for this SchemaType. */ + default?: SchemaTypeOpts.DefaultFn | T; + + /** + * Getters allow you to transform the representation of the data as it travels + * from the raw mongodb document to the value that you see. + */ + get?: (value: T, schematype?: this) => T; + + /** Declares the index options for this schematype. */ + index?: SchemaTypeOpts.IndexOpts | boolean | string; + + /** + * Adds a required validator to this SchemaType. The validator gets added + * to the front of this SchemaType's validators array using unshift(). + */ + required?: SchemaTypeOpts.RequiredFn | + boolean | [boolean, string] | + string | [string, string]; + + /** + * Sets default select() behavior for this path. + * Set to true if this path should always be included in the results, false + * if it should be excluded by default. This setting can be overridden at + * the query level. + */ + select?: boolean; + + /** + * Setters allow you to transform the data before it gets to the raw mongodb + * document and is set as a value on an actual key. + */ + set?: (value: T, schematype?: this) => T; + + /** Declares a sparse index. */ + sparse?: boolean; + + /** Declares a full text index. */ + text?: boolean; + + /** + * Adds validator(s) for this document path. + * Validators always receive the value to validate as their first argument + * and must return Boolean. Returning false means validation failed. + */ + validate?: RegExp | [RegExp, string] | + SchemaTypeOpts.ValidateFn | [SchemaTypeOpts.ValidateFn, string] | + SchemaTypeOpts.ValidateOpts | SchemaTypeOpts.ValidateOpts[]; + + /** Declares an unique index. */ + unique?: boolean; + + + /* Options for specific schema types (String, Number, Date, etc.) */ + /** String only - Adds an enum validator */ + enum?: T[] | SchemaTypeOpts.EnumOpts; + /** String only - Adds a lowercase setter. */ + lowercase?: boolean; + /** String only - Sets a regexp validator. */ + match?: RegExp | [RegExp, string]; + /** String only - Sets a maximum length validator. */ + maxlength?: number | [number, string]; + /** String only - Sets a minimum length validator. */ + minlength?: number | [number, string]; + /** String only - Adds a trim setter. */ + trim?: boolean; + /** String only - Adds an uppercase setter. */ + uppercase?: boolean; + + /** + * Date, Number only - Sets a minimum number validator. + * Sets a minimum date validator. + */ + min?: number | [number, string] | + Date | [Date, string]; + + /** + * Date, Number only - Sets a maximum number validator. + * Sets a maximum date validator. + */ + max?: number | [number, string] | + Date | [Date, string]; + + /** + * Date only - Declares a TTL index (rounded to the nearest second) + * for Date types only. + */ + expires?: number | string; + + /** ObjectId only - Adds an auto-generated ObjectId default if turnOn is true. */ + auto?: boolean; + + [other: string]: any; + } + + // Interfaces specific to schema type options should be scoped in this namespace + namespace SchemaTypeOpts { + interface DefaultFn { + (...args: any[]): T; + } + + interface RequiredFn { + (required: boolean, message?: string): T; + } + + interface ValidateFn { + (obj: RegExp | Function, message?: string, type?: string): T; + } + + interface ValidateOpts { + validator?: RegExp | Function, + msg?: string, + type?: string + } + + interface EnumOpts { + values?: T[]; + message?: string; + } + + interface IndexOpts { + background?: boolean, + expires?: number | string + sparse?: boolean, + type?: string, + unique?: boolean, + } + } + /* * section document.js * http://mongoosejs.com/docs/api.html#document-js