當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Schema.method方法代碼示例

本文整理匯總了TypeScript中mongoose.Schema.method方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Schema.method方法的具體用法?TypeScript Schema.method怎麽用?TypeScript Schema.method使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mongoose.Schema的用法示例。


在下文中一共展示了Schema.method方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: configureStoryModel

export function configureStoryModel(mongoose: Mongoose) {
  var storySchema = new Schema({
    title: String,
    description: String,
    project: {type: String, ref: 'Project'},
    sprint: {type: String, ref: 'Sprint'}
  });

  storySchema.method('replaceAttributes', function(attributes) {
    this.title = attributes.title;
    this.description = attributes.description;
    this.project = MongooseUtils.id(attributes.project);
    this.sprint = MongooseUtils.id(attributes.sprint);
  });

  storySchema.method('updateAttributes', function(attributes) {
    if(attributes.title) {
      this.title = attributes.title;
    }

    if(attributes.description) {
      this.description = attributes.description;
    }
  });

  storySchema.plugin(timestampsPlugin);
  storySchema.plugin(promisifyPlugin);

  StoryModel = mongoose.model<IStoryDocument>('Story', storySchema); 
}
開發者ID:dghijben,項目名稱:dashboard,代碼行數:30,代碼來源:StoryModel.ts

示例2: promisifyPlugin

export function promisifyPlugin(schema: Schema) {
  schema.method('promiseToSave', function(next) {
    let promise = new Promise();

    this.save(function(err, document) {
      if(err) {
        promise.reject(err);
      } else {
        promise.fulfill(document);
      }
    });

    return promise;
  });
  schema.method('promiseToRemove', function(next) {
    let promise = new Promise();

    this.remove(function(err) {
      if(err) {
        promise.reject(err);
      } else {
        promise.fulfill(true);
      }
    });

    return promise;
  });
}
開發者ID:kaka3004,項目名稱:cms,代碼行數:28,代碼來源:promisify-plugin.ts

示例3: configureStoryModel

export function configureStoryModel(mongoose: Mongoose) {
  var storySchema = new Schema({
    title: String,
    description: String
  });

  storySchema.method('replaceAttributes', function(attributes) {
    this.title = attributes.title;
    this.description = attributes.description;
  });

  storySchema.method('updateAttributes', function(attributes) {
    if(attributes.title) {
      this.title = attributes.title;
    }

    if(attributes.description) {
      this.description = attributes.description;
    }
  });

  storySchema.plugin(timestampsPlugin);
  storySchema.plugin(promisifyPlugin);

  StoryModel = mongoose.model<IStoryDocument>('Story', storySchema); 
}
開發者ID:kareem3d,項目名稱:redux-typescript-nodejs,代碼行數:26,代碼來源:story-model.ts

示例4: configureUserModel

export function configureUserModel(mongoose: Mongoose) {
  var userSchema = new Schema({
    firstName: String,
    lastName: String,
    email: String,
    hashedPassword: String,

    type: Number
  });

  userSchema.plugin(timestampsPlugin);
  userSchema.plugin(promisifyPlugin);

  userSchema.method('replaceAttributes', function(attributes) {
    throw new Error("Not implemented");
  });

  userSchema.method('updateAttributes', function(attributes) {
    throw new Error("Not implemented");
  });

  userSchema.method('hashPassword', function(password) {
    this.hashedPassword = md5(password);
  });

  userSchema.method('checkPassword', function(password) {
    return this.hashedPassword === md5(password);
  });

  const md5 = (password) => crypto.createHash('md5').update('"'+password+'"').digest('hex');

  UserModel = mongoose.model<IUserDocument>('User', userSchema); 
}
開發者ID:dghijben,項目名稱:dashboard,代碼行數:33,代碼來源:UserModel.ts

示例5: configureTaskModel

export function configureTaskModel(mongoose: Mongoose) {
  var taskSchema = new Schema({
    title: String,
    description: String,
    assigned_to: {type: String, ref: 'User'}
  });

  taskSchema.method('replaceAttributes', function(attributes) {
    this.title = attributes.title;
    this.description = attributes.description;
    this.assigned_to = MongooseUtils.id(attributes.assigned_to);
  });

  taskSchema.method('updateAttributes', function(attributes) {
    if(attributes.title) {
      this.title = attributes.title;
    }
    if(attributes.description) {
      this.description = attributes.description;
    }
    if(attributes.assigned_to) {
      this.assigned_to = attributes.assigned_to;
    }
  });

  taskSchema.plugin(timestampsPlugin);
  taskSchema.plugin(promisifyPlugin);

  TaskModel = mongoose.model<ITaskDocument>('Task', taskSchema); 
}
開發者ID:dghijben,項目名稱:dashboard,代碼行數:30,代碼來源:TaskModel.ts

示例6: configureProjectModel

export function configureProjectModel(mongoose: Mongoose) {
  var projectSchema = new Schema({
    title: String,
    description: String
  });

  projectSchema.method('replaceAttributes', function(attributes) {
    this.title = attributes.title;
    this.description = attributes.description;
  });

  projectSchema.method('updateAttributes', function(attributes) {
    if(attributes.title) {
      this.title = attributes.title;
    }
    if(attributes.description) {
      this.description = attributes.description;
    }
  });

  projectSchema.plugin(timestampsPlugin);
  projectSchema.plugin(promisifyPlugin);

  ProjectModel = mongoose.model<IProjectDocument>('Project', projectSchema); 
}
開發者ID:dghijben,項目名稱:dashboard,代碼行數:25,代碼來源:ProjectModel.ts

示例7: configureSprintModel

export function configureSprintModel(mongoose: Mongoose) {
  var sprintSchema = new Schema({
    from_date: Date,
    to_date: Date,
    project: {type: String, ref: 'Project'}
  });

  sprintSchema.method('replaceAttributes', function(attributes) {
    this.from_date = attributes.from_date;
    this.to_date = attributes.to_date;
    this.project = MongooseUtils.id(attributes.project);
  });

  sprintSchema.method('updateAttributes', function(attributes) {
    if(attributes.from_date) {
      this.from_date = attributes.from_date;
    }
    if(attributes.to_date) {
      this.to_date = attributes.to_date;
    }
    if(attributes.project) {
      this.project = MongooseUtils.id(attributes.project);
    }
  });

  sprintSchema.plugin(timestampsPlugin);
  sprintSchema.plugin(promisifyPlugin);

  SprintModel = mongoose.model<ISprintDocument>('Sprint', sprintSchema); 
}
開發者ID:dghijben,項目名稱:dashboard,代碼行數:30,代碼來源:SprintModel.ts

示例8: configureCategoryModel

export function configureCategoryModel(mongoose: Mongoose) {
  let categoryImageSchema = {
    src: String,
    width: Number,
    height: Number,
    kind: {type: String, enum: ['cover', 'main']},
  };

  let categorySchema = new Schema({
    title: String,
    slug: String,
    status: String,
    description: String,
    images: [categoryImageSchema]
  });

  categorySchema.method('replaceAttributes', function(attributes) {
    this.title = attributes.title;
    this.slug = attributes.slug;
    this.status = attributes.status;
    this.description = attributes.description;
    this.images = attributes.images;
  });

  categorySchema.method('updateAttributes', function(attributes) {
    
    if(attributes.title) {
      this.title = attributes.title;
    }
    
    if(attributes.slug) {
      this.slug = attributes.slug;
    }
    
    if(attributes.status) {
      this.status = attributes.status;
    }
    
    if(attributes.description) {
      this.description = attributes.description;
    }
    
    if(attributes.cover_image) {
      this.cover_image = this.cover_image.concat(attributes.cover_image);
    }
  });

  categorySchema.plugin(timestampsPlugin);
  categorySchema.plugin(promisifyPlugin);

  CategoryModel = mongoose.model<ICategoryDocument>('Category', categorySchema); 
}
開發者ID:kaka3004,項目名稱:cms,代碼行數:52,代碼來源:category-model.ts

示例9: configureShopModel

export function configureShopModel(mongoose: Mongoose) {
  let addressSchema = {
    address: String,
    address2: String,
    city:String,
    state: String,
    country: String,
    country_code: String,
    zip: String
  };

  let shopSchema = new Schema({
    name: String,
    published: Boolean,
    config: {
      address: addressSchema,
      currency_code: String
    },
    creator_id: {type: Schema.Types.ObjectId, ref: 'User', required: true}
  });

  shopSchema.method('replaceAttributes', function(attributes) {
    this.published = attributes.published;
    this.config = attributes.config;
  });

  shopSchema.method('updateAttributes', function(attributes) {
    if(attributes.published) {
      this.published = attributes.published;
    }
    
    if(attributes.config) {
      this.config = attributes.config;
    }
  });

  shopSchema.method('setCreator', function(creator) {
    this.creator_id = getDocumentId(creator);
  });

  shopSchema.plugin(timestampsPlugin);
  shopSchema.plugin(promisifyPlugin);

  ShopModel = mongoose.model<IShopDocument>('Shop', shopSchema); 
}
開發者ID:kaka3004,項目名稱:cms,代碼行數:45,代碼來源:shop-model.ts

示例10: slugPlugin

export function slugPlugin(schema: Schema, options) {

  schema.add({ slug: String });

  schema.pre('validate', function(next) {
    if(! this.slug ) {
      this.makeSlug();
    }
    next();
  });

  schema.path('slug').validate(function(value, respond) {
    var query = this.constructor.count();

    if(this.id) {
      query.where('_id').ne(this.id);
    }

    // Make sure the slug is unique in this shop
    if(options && options.uniqueShop) {
      query.where('shop_id', getDocumentId(this.shop_id));
    }

    // Make sure it's unique for parent (used in categories)
    if(options && options.uniqueParent) {
      query.where('parent_id', getDocumentId(this.parent_id));
    }

    // Make sure it's unique for this user
    if(options && options.uniqueUser) {
      query.where('user_id', getDocumentId(this.user_id));
    }

    query.where('slug', this.slug);

    return query.exec().then(function(count) {
      if(count > 0) {
        respond(false);
      } else {
        respond(true);
      }
    }, function() {
      respond(false);
    });

  }, 'Slug must be unique');


  schema.method('makeSlug', function() {
    this.slug = slugify(this.title);
  })
}
開發者ID:kaka3004,項目名稱:cms,代碼行數:52,代碼來源:slug-plugin.ts


注:本文中的mongoose.Schema.method方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。