
Security News
New CVE Forecasting Tool Predicts 47,000 Disclosures in 2025
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
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.
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.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.