Socket
Socket
Sign inDemoInstall

@plt4rm/odm

Package Overview
Dependencies
103
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @plt4rm/odm

PLT4RM's ODM (Object Document Mapper) is built for NodeJS and provides transparent persistence for JavaScript objects to MongoDB database


Version published
Maintainers
1
Created

Readme

Source

plt4rm's ODM

plt4rm's ODM (Object Document Mapper) is built for NodeJS and provides transparent persistence for JavaScript objects to MongoDB database.

Supports schemas with multi-level inheritance. Also supports interception on operations (create, read, update and delete).

Build Coverage Status CodeFactor

npm install --save @plt4rm/odm

Establishing database connection

const {ODM} = require("@plt4rm/odm");

const odm = new ODM();
 
const config = {
    "host": "localhost",
    "port": "27017",
    "database": "collection-service",
    "dialect": "mongodb",
};

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

Intercepting database operations

// after establishing connection

odm.addInterceptor({

    getName: function () {
        return "my-intercept";
    },

    intercept: (collectionName, operation, when, payload) => {
        return new Promise((resolve, reject) => {
            if (collectionName === 'student') {
                if (operation === 'CREATE') {
                    console.log("[collectionName=" + collectionName + ", operation=" + operation + ", when=" + when + "]");
                    if (when === "BEFORE") {
                        for (let record of payload.records) {
                            console.log("computed field updated for :: " + record.get('name'));
                            record.set("computed", record.get("name") + " +++ computed");
                        }
                    }
                }
                if (operation === 'READ') {
                    console.log("[collectionName=" + collectionName + ", operation=" + operation + ", when=" + when + "]");
                    if (when === "AFTER") {
                        for (let record of payload.records)
                            console.log(JSON.stringify(record.toObject(), null, 4));
                    }
                }
            }
            resolve(payload);
        });
    }
});

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

let studentCollection = odm.collection("student");
let s = studentCollection.createNewRecord();
s.set("name", "John " + new Date().toISOString());
s.insert().then(function () {
    studentCollection.find().toArray().then(function (students) {
        odm.closeConnection();
    });
});

Define custom field type

// after establishing connection

odm.addFieldType({

 getDataType: function () {
     return new StringDataType()
 },

 getType: function () {
     return "email"
 },

 async validateValue(schema: Schema, field: Field, record: any, context: any) {
     const pattern = "(.+)@(.+){2,}\\.(.+){2,}";
     if (!new RegExp(pattern).test(record[field.getName()]))
         throw new Error("Not a valid email");
 },

 validateDefinition: function (fieldDefinition: any) {
     return !!fieldDefinition.name
 },

 getValueIntercept(schema: Schema, field: Field, record: any, context: any): any {
     return record[field.getName()];
 },

 setValueIntercept(schema: Schema, field: Field, newValue: any, record: any, context: any): any {
     return newValue;
 },
 
 setODM() {}

});

odm.defineCollection({
    name: 'student',
    fields: [{
        name: 'name',
        type: 'string'
    }, {
        name: 'email',
        type: 'email'
    }, {
        name: 'dob',
        type: 'date'
    }]
});

let studentCollection = odm.collection("student");
let s = studentCollection.createNewRecord();
s.set("name", "John");
s.set("email", "test@example.com");
s.set("dob", new Date());
s.insert().then(function () {
    console.log("Student created");
    odm.closeConnection();
}, (err) => {
    console.log(err);
    odm.closeConnection();
});

Check the examples >> here <<

Code of Conduct

Contributor Covenant

License

Apache License 2.0

Keywords

FAQs

Last updated on 28 Oct 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc