@azera/model
Mongoose model helper library
Last updated 2 years ago by mdzzohrabi .
MIT · Original npm · Tarball · package.json
$ cnpm install @azera/model 
SYNC missed versions from official npm registry.

Azera model

Specially designed for TypeScript. required to enable emitDecoratorMetadata in your tsconfig.json.

Create model :

interface Link {
    url: string;
}

class Question {
    @Field() question: string;
}

@Schema({ collection: 'questions' }) // Optional decorator
class QuestionRepo extends Model<Question> {}

class Exam {
    @Field() @Required() @Default("Sample exam") name: string;
    @Field() createdOn: Date;
    @CollectionOf(Question) questions: Question[];
    @Field() links: Link[];
}

@Schema({ collection: 'exams' }) // Optional decorator
class ExamRepo extends Model<Exam> {

    get hasQuestions() {
        return this.questions.length > 0;
    }

    static findByQuestion(q: Question) {
        return this.find( /* Condition logic */ );
    }

}

// Generate mongoose model
const ExamModel = toModel(Exam, ExamRepo);
const QuestionModel = toModel(Question, QuestionRepo);

// Sample
let q = new Question();
ExamModel.findByQustion(q);

// Sample
let exam = ExamModel.findById(/* Any Id */);
console.log( exam.hasQuestions ? 'Has question' : 'No questions' );

Current Tags

  • 1.0.8                                ...           latest (2 years ago)

9 Versions

  • 1.0.8                                ...           2 years ago
  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           3 years ago
  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 6
Last Day 0
Last Week 2
Last Month 3
Dependencies (9)
Dev Dependencies (5)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |