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); }
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); }
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); }
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); }
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); }
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; }); }
export const RAMSchema = (schema: Object) => { const result = new mongoose.Schema({ deleteInd: { type: Boolean, default: false }, resourceVersion: { type: String, default: '1' } }, { timestamps: true }); result.add(schema); return result; };
function setUpSchema({schema, virtuals}: MongooseSchema, options?: mongoose.SchemaOptions) { const mongooseSchema = new mongoose.Schema(schema, options); for (const [key, options] of virtuals.entries()) { mongooseSchema.virtual(key, options); } return mongooseSchema; }
export function timestampsPlugin(schema: Schema) { schema.add({ created_at: Date }); schema.add({ updated_at: Date }); schema.pre('save', function(next) { if(! this.created_at) { this.created_at = new Date(); } this.updated_at = new Date(); next(); }) }
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); }