
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
@hostaworld/active-record-node
Advanced tools
A Simple Active Record Style ORM for Node.
i.e. Implmention details will be covered and only user data is visible from console.log.
const s = new CSearcher("people"); //--- query people table
s["firstName"]="Tom"; //-- fetch all records firstName column = "Tom"
s["lastName"]="Green"; //-- And lastName column = "Green"
const rows = await s.fetchResult(); //--- fetch all records match the conditions
console.log(s);
console.log(rows);
The followings will ne shown in console:
{ firstName: { '=': 'Tom' }, lastName: { '=': 'Green' } }
[ { id: 3, firstName: 'Tom', lastName: 'Green' } ]
const car = new CActiveRecord("cars"); //--- create a new record on `car` table
car["make"]="ford"; //--- set make column to "ford"
car["model"]="falcon";
for(let f in rows[0]){
console.log(f); //--- output: make, model
}
If a table has columns with the following special names:
You can set the column value using set
or get
method:
car.set("set","abc"); //--- set `set` column of `car` table to "abc"
car.get("get"); //-- get `get` column of `car` table
Create test table people
in test
database:
CREATE TABLE `test`.`people` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR(255) NOT NULL DEFAULT '' ,
`lastName` VARCHAR(255) NOT NULL DEFAULT '' ,
PRIMARY KEY (`id`)
) ENGINE = MyISAM;
Create index.js:
const mysql = require("mysql2/promise"); //--- load Mysql2 lib;
const {CSearcher, CActiveRecord,setDB} = require("@hostaworld/active-record-node");
CSearcher.setDefaultConfig({
debug:true // --- debug mode will print SQL in console
});
CActiveRecord.setDefaultConfig({
debug:true // --- debug mode will print SQL in console
});
//-- Create mysql connection
const db = mysql.createPool({
connectionLimit: 1,
host: "192.168.1.166",
user: "root",
password: "",
database: "test"
});
setDB(db); //-- @hostaworld/active-record-node will use this db connection
(async ()=>{
try{
let s, rows;
//-- create new record for table people
const record = new CActiveRecord("people");
record["firstName"] = "Jim";
record["lastName"] = "Will";
const newId = await record.commit();
console.log(newId); //--- output newly generated auto increment primary key
s = new CSearcher("people"); //--- query people table
s["id"]["<="] = 100; //-- fetch all records id <= 100
rows = await s.fetchResult(1); //--- fetch first record id = 1
console.log(rows);
rows[0]["firstName"] = "Tom"; //--- update firstName column of the row
rows[0]["lastName"] = "Green"; //--- update lastName column of the row
await rows[0].commit();
s = new CSearcher("people"); //--- query people table
s["firstName"]="Tom"; //-- fetch all records firstName column = "Tom"
s["lastName"]="Green"; //-- And lastName column = "Green"
rows = await s.fetchResult(); //--- fetch all records match the conditions
console.log(rows);
await rows[0].delete(); //--- delete this record
}catch(e){
console.log(e);
}
process.exit();
})();
@hostaworld/active-record-node
requires node v7.6.0 or higher for ES2015 and async function support.
npm install --save @hostaworld/active-record-node
You also need mysql2:
npm install --save mysql2
If you're not using node v7.6+, we recommend setting up babel with babel-preset-env:
npm install babel-register babel-preset-env --save
Setup babel-register in your entry file:
require('babel-register');
And have your .babelrc setup:
{
"presets": [
["env", {
"targets": {
"node": true
}
}]
]
}
This class creates the data access mapping to a table row in database.
Import class
import { CActiveRecord } from "@hostaworld/active-record-node";
or
const { CActiveRecord } = require("@hostaworld/active-record-node");
set
, get
etc. See in top Features section.await
expression to pause the operation until the Promise is fulfilled or rejected.insert
operation, the promise will be resolved to the newly generated ID for an auto increment primary key. Otherwise, the promise will be resolved to undefined
.await
expression to pause the operation until the Promise is fulfilled or rejected.The following control properties can be used to control how CSearcher fetch result from database:
debug
: set this property to true will cause CSearcher output all SQL queries to terminal (through console.log
)This class creates a query interface mapping to a table row in database.
Set value of a property of initiated CSearcher object will create equals
query condition.
e.g.
const s = new CSearcher("table");
s["columnName"]=value;
Will generate query condition columnName=value
.
You also can create the similar query condition by:
const s = new CSearcher("table");
s["columnName"]["="]=value;
the ["="]
can be replaced with other operators (e.g. ["operator"]
). Supported operators are:
=
: equals!=
: not equals>
: larger than>=
: larger than or equal to<
: lower than<=
: lower than or equal toLIKE
: string column matching certain partternNOT LIKE
: string column matching certain partternIN
: Column value is included in the list. value provided must be an arrayNOT IN
: Column value is not included in the list. value provided must be an arrayIS NOT NULL
: You can set any value when set this operator. The value you set will be ignored. e.g. s["column"]["IS NOT NULL"]=1;
will generate query condition column IS NOT NULL
.IS NULL
: You can set any value when set this operator. The value you set will be ignored. e.g. s["column"]["IS NULL"]=1;
will generate query condition column IS NULL
.For usage, please see Getting Started section above.
Import class
import { CSearcher } from "@hostaworld/active-record-node";
or
const { CSearcher } = require("@hostaworld/active-record-node");
The following control properties can be used to control how CSearcher fetch result from database:
orderBy
: set order of returned records.
s.orderBy = 'columnA ASC';
groupBy
: group result by certian column
s.groupBy = 'columnA,columnB';
debug
: set this property to true will cause CSearcher output all SQL queries to terminal (through console.log
)Ultility function to set db connection to be used by CActiveRecord & CSearcher
Parameter db
can be connection
or pool
of mysql2 or mysql libs (or any objects support query & execute method)
Examples:
const mysql = require("mysql2/promise"); //--- load Mysql2 lib;
const {setDB} = require("@hostaworld/active-record-node");
//-- Create mysql connection
const db = await mysql.createConnection({
host: "192.168.1.166",
user: "root",
password: "",
database: "test"
});
setDB(db); //-- @hostaworld/active-record-node will use this db connection
or
const mysql = require("mysql2/promise"); //--- load Mysql2 lib;
const {setDB} = require("@hostaworld/active-record-node");
//-- Create mysql connection
const db = mysql.createPool({
connectionLimit: 1,
host: "192.168.1.166",
user: "root",
password: "",
database: "test"
});
setDB(db); //-- @hostaworld/active-record-node will use this db connection
FAQs
A Simple Active Record Style ORM for Node
We found that @hostaworld/active-record-node 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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.