本文整理匯總了TypeScript中mongoose.Schema.set方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Schema.set方法的具體用法?TypeScript Schema.set怎麽用?TypeScript Schema.set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mongoose.Schema
的用法示例。
在下文中一共展示了Schema.set方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: Schema
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import {Schema, Model, model, Document} from 'mongoose';
import {AUTO_INDEX} from '../../../../shared/shared';
const userSchema: Schema = new Schema({
name: {type: String, index: true},
pass: String,
email: {type: String, index: true},
roles: [String],
});
// automat. Validierung der Indexe beim 1. Zugriff
userSchema.set('autoIndex', AUTO_INDEX);
// fuer ein Document (-Objekt) die Methode toJSON bereitstellen
userSchema.set('toJSON', {getters: true, virtuals: false});
// Ein Model ist ein uebersetztes Schema und stellt die CRUD-Operationen fuer
// die Dokumente bereit, d.h. das Pattern "Active Record" wird realisiert.
export const User: Model<Document> = model('User', userSchema);
示例2: require
/// <reference path="../definitions/references.d.ts"/>
module Genovesa.Models {
var mongoose = require('mongoose');
/**
* Represents a Township.
*/
var townshipSchema = new mongoose.Schema({
// Postal code of this Township.
postalCode: { type: Number, required: true, unique: true },
// Name of this Township.
name: { type: String, required: true },
// Department this Township belongs to.
department: { type: String, required: true }
});
// Townships are never updated, versioning is not needed.
townshipSchema.set('versionKey', false);
// Export the model.
export var Township = mongoose.model('Township', townshipSchema);
}
示例3: Schema
// Der Default-Name der Collection ist der Plural zum Namen des Models (s.u.),
// d.h. die Collection haette den Namen "Buchs".
const videoSchema: Schema = new Schema(
{
titel: {type: String, index: true},
erscheinungsdatum: Date,
beschreibung: String,
altersbeschränkung: Number,
viedopfad: String,
genere: [Schema.Types.Mixed],
kanal: [Schema.Types.Mixed]
},
{collection: 'videos'});
// automat. Validierung der Indexe beim 1. Zugriff
videoSchema.set('autoIndex', autoIndex);
// fuer ein Document (-Objekt) die Methode toJSON bereitstellen
videoSchema.set('toJSON', {getters: true, virtuals: false});
const MODEL_NAME: string = 'Video';
// Methoden zum Schema hinzufuegen, damit sie spaeter beim Model (s.u.)
// verfuegbar sind, was aber bei buch.check() zu eines TS-Syntaxfehler fuehrt:
// buchSchema.methods.check = function(): any { ... }
export function validateVideo(video: any): any {
'use strict';
let invalid: boolean = false;
let err: any = {};
示例4: require
/// <reference path="../definitions/references.d.ts"/>
module Genovesa.Models {
var mongoose = require('mongoose');
// TODO: doc
var documentTypeSchema = new mongoose.Schema({
/**
* Type of document.
* For example: "birth", "death", ...
*/
type: { type: String, required: true, unique: true },
/**
* Localized name of the document type.
* For example: "naissance", "décès", ...
*/
name: { type: String, required: true },
});
// DocumentTypes are never updated, versioning is not needed.
documentTypeSchema.set('versionKey', false);
export var DocumentType = mongoose.model('DocumentType', documentTypeSchema);
}
示例5: function
m2: cb
});
schema.path('a', mongoose.Schema.Types.Buffer).path('a');
schema.pathType('m1').toLowerCase();
schema.plugin(function (schema, opts) {
schema.get('path');
opts.hasOwnProperty('');
}).plugin(cb, {opts: true});
schema.post('post', function (doc) {}).post('post', function (doc, next) {
next(new Error());
});
schema.queue('m1', [1, 2, 3]).queue('m2', [[]]);
schema.remove('path');
schema.remove(['path1', 'path2', 'path3']);
schema.requiredPaths(true)[0].toLowerCase();
schema.set('key', 999).set('key');
schema.static('static', cb).static({
s1: cb,
s2: cb
});
schema.virtual('virt', {}).applyGetters({}, {});
schema.virtualpath('path').applyGetters({}, {});
/* static properties */
mongoose.Schema.indexTypes[0].toLowerCase();
mongoose.Schema.reserved.hasOwnProperty('');
/* inherited properties */
schema.addListener('e', cb);
/* practical examples */
var animalSchema = new mongoose.Schema({
name: String,
type: String
示例6: next
next(error);
}
});
userSchema.method.annotationGroups = function (callback) {
this.model('AnnotationGroup').find({ user_id: this._id }, callback);
};
/**
* Removes the sensitive information from the user object.
*
* @author Gabriel Malkas
*/
userSchema.set('toJSON', {
transform: function (doc, ret, options) {
delete ret.passwordHash;
delete ret.passwordSalt;
}
});
/**
* Tries to authentify the given user.
*
* @param [String] email
* @param [String] password
* @param [Function] callback
* @author Gabriel Malkas
*/
userSchema.statics.authenticate = function (email, password, callback) {
this.findOne({ email: email }, null, function (err, user) {
if (err) {
callback(err);
示例7: function
userSchema.pre('save', function (next) {
if (!this.isModified('password')) {return next(); }
bcrypt.genSalt(10, function(err, salt) {
if (err) {return next(err); }
bcrypt.hash(this.password, salt, function(error, hash) {
if (error) {return next(error); }
this.password = hash;
next();
});
});
});
userSchema.methods.comparePassword = function(candidatePassword, callback) {
bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
if (err) {return callback(err); }
callback(null, isMatch);
});
};
// Omit the password when returning a user
userSchema.set('toJSON', {
transform: function(doc, ret, options) {
delete ret.password;
return ret;
}
});
const User = mongoose.model('User', userSchema);
export default User;
示例8: Number
required: `Product Id can't be blank.`
},
userId: {
type: String,
required: `User id can't be blank.`
},
cost: {
type: Number,
required: `Cost can't be blank.`
}
})
// Duplicate the ID field.
ratingSchema.virtual('id').get(function() {
return this._id
})
// Ensure virtual fields are serialised.
ratingSchema.set('toJSON', {
virtuals: true
})
ratingSchema.path('productId').required(true, `Prodcut id can't be blank.`)
ratingSchema.path('userId').required(true, `User id can't be blank.`)
ratingSchema.path('cost').validate(function(price) {
return Number(price).toString() === price.toString()
}, `Cost must be a float number.`)
const Rating = mongoose.model('Rating', ratingSchema)
export default Rating
示例9: Number
},
price: {
type: Number,
required: `Price can't be blank.`
},
imageUrl: {
type: String,
required: `Image can't be blank.`
}
})
// Duplicate the ID field.
productSchema.virtual('id').get(function() {
return this._id
})
// Ensure virtual fields are serialised.
productSchema.set('toJSON', {
virtuals: true
})
productSchema.path('name').required(true, `Product name can't be blank.`)
productSchema.path('price').required(true, `Price can't be blank.`)
productSchema.path('price').validate(function(price) {
return Number(price).toString() === price.toString()
}, `Price must be a float number.`)
productSchema.path('imageUrl').required(true, `Image can't be blank.`)
const Product = mongoose.model('Product', productSchema)
export default Product
示例10: function
}
}, {
toObject: {
virtuals: true
},
toJSON: {
virtuals: true
}
})
/**
* Removes password from return JSON
*/
PlatformSchema.set('toJSON', {
transform: function(doc: any, ret: any, opt: any) {
delete ret.password
return ret
}
})
/**
* Activities handled | responded by user
*/
PlatformSchema.virtual('activities', {
ref: 'Activity',
localField: '_id',
foreignField: 'handler'
})
/**
* Hash incoming password with salt and
* compare it to stored password hash