
Research
/Security News
Toptal’s GitHub Organization Hijacked: 10 Malicious Packages Published
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
node-model.js
Advanced tools
model.js is a simple ORM (Object Relational Mapper) for use with Node.js and MySQL.
Create a model object by using the model()
function. It takes the following arguments:
id
It is assumed all tables have a primary key named id
of type INT with auto increment.
var User = model('users', ['name', 'email']);
Use the mysql
module to connect to the database.
var mysql = require('mysql');
var db = mysql.createConnection({
host: 'localhost',
user: 'username',
password: 'password',
database: 'database'
});
db.connect();
Use the save()
function to save a row into the table. If the row has not been saved before, it will be assigned an id
automatically.
var john = new User(db, {name: 'John', email: 'john@example.com'});
john.save(function () {
console.log(john.id);
});
Use the get()
function to retrieve a row from the table. Currently you can only retrieve rows by their id
.
var user_id = 1;
var theUser = new User(db, {id: user_id});
theUser.get(function () {
console.log('Your user name: ', theUser.name);
});
Convert a row into an object literal using the data()
function.
var userData = theUser.data(); // {id: 1, name: 'John', email: 'john@example.com'}
Use the setData()
function to update a row's data.
theUser.setData({name: 'Michael', email: 'michael@example.com'});
theUser.save();
Or use a field's property to update a single field.
theUser.name = 'Michael';
theUser.save();
Use the remove()
function to delete a row from a table.
var theUser = new User(db, {id: 1});
theUser.remove();
Use the count()
function to count rows in a table by any equality condition.
User.count(db, {name: 'John'}, function (count) {
console.log('Number of users named John: ', count);
});
Define the relationship between model objects using the model.oneToMany()
function. It takes the following arguments:
parentModel
- The 'One' in the relationshipchildModel
- The 'Many' in the relationshipparentName
- The method name to access the parent from a childchildrenName
- The method name to access the list of children from the parentchildModelField
(optional) - The field name in the child corresponding to the parent id. If not specified, it is assumed to be parentName
+ _id
var User = model('users', ['name', 'email']);
var Post = model('posts', ['user_id', 'message']);
model.oneToMany(User, Post, 'user', 'posts'); // childModelField becomes user_id
Currently One To Many relationships are the only type of relationship implemented in model.js.
var john = new User(db, {id: 1});
john.get(function () {
var firstPost = new Post(db, {user_id: john.id, message: 'Hello'});
firstPost.save();
});
Get all of a user's posts:
var john = new User(db, {id: 1});
john.get(function () {
john.posts(function (posts) {
for (var i = 0, l = posts.length; i < l; i++) {
console.log(john.name + ' says: ' + posts[i].message);
}
});
});
Or get the user of a post:
var post = new Post(db, {id: 1});
post.get(function () {
post.user(function (user) {
console.log(user.name + ' says: ' + post.message);
});
});
FAQs
A simple ORM for use with Node.js and MySQL.
The npm package node-model.js receives a total of 4 weekly downloads. As such, node-model.js popularity was classified as not popular.
We found that node-model.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.
Research
/Security News
Socket researchers investigate 4 malicious npm and PyPI packages with 56,000+ downloads that install surveillance malware.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.