2.3.4 • Published 4 years ago
@plt4rm/odm v2.3.4
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).
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
License
2.3.4
4 years ago
2.3.2
4 years ago
2.3.0
4 years ago
2.3.1
4 years ago
2.2.9
4 years ago
2.2.8
4 years ago
2.2.7
4 years ago
2.2.6
4 years ago
2.2.5
4 years ago
2.2.3
4 years ago
2.2.1
4 years ago
2.2.0
4 years ago
2.2.2
4 years ago
2.1.0
4 years ago
2.0.3
4 years ago
2.0.2
4 years ago
2.0.1
4 years ago
2.0.0
4 years ago
2.0.0-alpha-2
4 years ago
2.0.0-alpha
4 years ago
1.9.8
4 years ago
1.9.7
4 years ago
1.9.6
4 years ago
1.9.5
4 years ago
1.9.2
4 years ago
1.9.1
4 years ago
1.9.0
4 years ago
1.8.4
4 years ago
1.8.3
4 years ago
1.8.2
4 years ago
1.8.1
4 years ago
1.7.7
4 years ago
1.8.0
4 years ago
1.7.6
4 years ago
1.7.5
4 years ago
1.7.2
4 years ago
1.7.1
4 years ago
1.7.0
4 years ago
1.6.9
4 years ago