1.0.0 • Published 5 years ago

anysols-model v1.0.0

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
5 years ago

Anysols Model

Wrapper for mongoose with intercepts for operations

Coverage Status

npm install --save anysols-model

Establishing database connection

const {AnysolsModel} = require('anysols-model');

 const anysolsModel = new AnysolsModel();

    const config = {
        "host": "localhost",
        "port": "27017",
        "database": "anysols-model",
        "dialect": "mongodb",
    };

    anysolsModel.connect(config).then(() => {
        console.log('connection success');
        anysolsModel.databaseExists().then(() => {
            console.log('db exists');
            cb(anysolsModel);
        }, () => {
            console.log("db does not exists");
        });
    }, (err) => {
        console.log('connection failed');
    });

Intercepting database operations

// after establishing connection

 anysolsModel.addInterceptor("my-intercept", {
    intercept: (modelName, operation, when, records) => {
        return new Promise((resolve, reject) => {
            if (modelName === 'student') {
                if (operation === 'create') {
                    if (when === "before") {
                        console.log("Student before");
                        if (!Array.isArray(records)) {
                            let record = records;
                            record.set("computed",  record.get("name") + " +++ computed");
                        }
                    } else if (when === "after")
                        console.log("Student after");
                }
            }
            resolve(records);
        });
    }
});

anysolsModel.defineModel({
    name: 'student',
    fields: [{
        name: 'name',
        type: 'string'
    }, {
        name: 'computed',
        type: 'string'
    }]
});

let Student = anysolsModel.model("student");
let s = new Student({});
s.set("name", "John");
s.save().then(function () {
    Student.find().exec().then(function (students) {
        console.log(JSON.stringify(students, null, 4));
    });
});

Define custom field type

// after establishing connection

 anysolsModel.registerFieldDefinition(new FieldDefinition("customType", field => {
     return true
 }, function (field, fieldDefinition) {
     return {
         type: MONGOOSE_TYPES.STRING
     }
 }));

 anysolsModel.defineModel({
     name: 'student',
     fields: [{
         name: 'name',
         type: 'string'
     }, {
         name: 'dob',
         type: 'date'
     }, {
         name: 'custom_field',
         type: 'customType'
     }]
 });

 let Student = anysolsModel.model("student");
 let s = new Student();
 s.set("name", "John");
 s.set("dob", new Date());
 s.set("custom_field", "testing");
 s.save().then(function () {
     console.log("Student created");
     anysolsModel.closeConnection();
 });

Check the examples >> here <<

Code of Conduct

Contributor Covenant

License

Apache License 2.0

1.0.0

5 years ago

1.0.0-rc2

5 years ago

1.0.0-rc

5 years ago

1.0.0-beta-3

5 years ago

1.0.0-beta-2

5 years ago

1.0.0-beta

5 years ago

0.9.0

5 years ago