Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
npm install orm@2.0.0-alpha4
Despite the alpha tag, this is the recommended version for new applications.
This is a node.js object relational mapping module.
Here is an example on how to use it:
var orm = require('orm');
orm.connect("mysql://username:password@host/database", function (err, db) {
if (err) throw err;
var Person = db.define('person', {
name : String,
surname : String,
age : Number,
male : Boolean,
continent : [ 'Europe', 'America', 'Asia', 'Africa', 'Australia', 'Antartica' ], // ENUM type
photo : Buffer, // BLOB/BINARY
data : Object // JSON encoded
});
Person.find({ surname: "Doe" }, function (err, people) {
// SQL: "SELECT * FROM person WHERE surname = 'Doe'"
console.log("People found: %d", people.length);
console.log("First person: %s", people[0].name);
});
});
A Model is a structure binded to one or more tables, depending on the associations. The model name is assumed to be the table name. After defining a model you can use it to manipulate the table.
After defining a Model you can get a specific element or find one or more based on some conditions.
Person.find({ name: "John", surname: "Doe" }, 3, function (err, people) {
// finds people with name='John' AND surname='Doe' and returns the first 3
});
Or if you know the ID of the item (called Instance):
Person.get(123, function (err, person) {
// finds person with id = 123
});
If you need to sort the results because you're limiting or just because you want them sorted do:
Person.find({ surname: "Doe" }, "name", function (err, people) {
// finds people with surname='Doe' and returns sorted by name ascending
});
Person.find({ surname: "Doe" }, [ "name", "Z" ], function (err, people) {
// finds people with surname='Doe' and returns sorted by name descending
// ('Z' means DESC; 'A' means ASC - default)
});
There are more options that you can pass to find something. These options are passed in a second object:
Person.find({ surname: "Doe" }, { offset: 2 }, function (err, people) {
// finds people with surname='Doe', skips the first 2 and returns the others
});
The order of the parameters is not fixed. You can pass the callback first if you like or mix the other paramenters. The only parameter that needs to be in order is when you pass 2 objects. The first one is for conditions (although it can be empty) and the second one is for options.
An association is a relation between one or more tables.
If you have a relation of 1 to 0 or 1 to 1, you should use hasOne
association. This assumes a column in the model that has the id of the other end of the relation.
var Person = db.define('person', {
name : String
});
var Animal = db.define('animal', {
name : String
});
Animal.hasOne("owner", Person); // assumes column 'owner_id' in 'animal' table
// get animal with id = 123
Animal.get(123, function (err, Foo) {
// Foo is the animal model instance, if found
Foo.getOwner(function (err, John) {
// if Foo animal has really an owner, John points to it
});
});
For relations of 1 to many you have to use hasMany
associations. This assumes another table that has 2 columns, one for each table in the association.
var Person = db.define('person', {
name : String
});
Person.hasMany("friends"); // omitting the other Model, it will assume self model
Person.get(123, function (err, John) {
Person.getFriends(function (err, friends) {
// assumes table person_friends with columns person_id and friends_id
});
});
FAQs
NodeJS Object-relational mapping
The npm package orm receives a total of 639 weekly downloads. As such, orm popularity was classified as not popular.
We found that orm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.