Added typings for the mongoose-sequence plugin (#11201)

This commit is contained in:
linusbrolin
2016-09-14 14:46:59 +02:00
committed by Masahiro Wakame
parent 4280cc2e71
commit c404aa6f7b
2 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
/// <reference path="./mongoose-sequence.d.ts" />
/// <reference path="../mongoose/mongoose.d.ts" />
/// <reference path="../express/express.d.ts" />
/**
* Based on the examples on: https://github.com/ramiel/mongoose-sequence
* Created by Linus Brolin <https://github.com/linusbrolin/>.
*/
import { SequenceDocument, SequenceOptions, SequenceSchema, Document, Schema, Model, model } from 'mongoose';
import * as mongooseSequence from 'mongoose-sequence';
//#region Test Models
interface User extends SequenceDocument {
name: string;
country: string;
city: string;
inhabitant_number: number;
}
const UserSchema: SequenceSchema = new Schema({
name: String,
country: String,
city: String,
inhabitant_number: Number
});
let seqOpts: SequenceOptions = { id: 'inhabitant_seq', inc_field: 'inhabitant_number', reference_fields: ['country', 'city'] };
UserSchema.plugin(mongooseSequence, seqOpts);
let UserModel: Model<User> = model<User>('User', UserSchema);
//#endregion
//#region Test Sequence
let user: User = new UserModel({
name: 'Patrice',
country: 'France',
city: 'Paris'
});
user.save();
console.log(user.inhabitant_number);
user.setNext('inhabitant_seq', function(err: any, user: User) {
if (err) {
console.log(err);
return;
}
if (user) {
console.log(user.inhabitant_number);
}
});
//#endregion

View File

@@ -0,0 +1,36 @@
// Type definitions for mongoose-sequence 3.0.2
// Project: https://github.com/ramiel/mongoose-sequence
// Definitions by: Linus Brolin <https://github.com/linusbrolin/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../mongoose/mongoose.d.ts" />
declare module 'mongoose' {
export interface SequenceOptions {
inc_field: string; // The name of the field to increment. Mandatory, default is _id
id?: string; // Id of the sequence. Is mandatory only for scoped sequences but its use is strongly encouraged.
reference_fields?: Array<string>; // The field to reference for a scoped counter. Optional
disable_hooks?: boolean; // If true, the counter will not be incremented on saving a new document. Default to false
collection_name?: string; // By default the collection name to mantain the status of the counters is counters. You can override it using this option
}
export interface SequenceDocument extends Document {
setNext(sequenceId: string, callback: (err: any, res: SequenceDocument) => void): void;
}
export interface SequenceSchema extends Schema {
plugin(
plugin: (schema: SequenceSchema, options: SequenceOptions) => void,
options: SequenceOptions
): this;
// overload for the default mongoose plugin function
plugin(plugin: (schema: Schema, options?: Object) => void, opts?: Object): this;
}
}
declare module 'mongoose-sequence' {
import mongoose = require('mongoose');
var _: (schema: mongoose.Schema, options?: Object) => void;
export = _;
}