@azera/model
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

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' );

Readme

Keywords

none

Package Sidebar

Install

npm i @azera/model

Weekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

45.6 kB

Total Files

29

Last publish

Collaborators

  • mdzzohrabi