Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
egg-yoursql
Advanced tools
Aliyun rds client(support mysql portocal) for egg framework
$ npm i egg-mysql --save
MySQL Plugin for egg, support egg application access to MySQL database.
This plugin based on ali-rds, if you want to know specific usage, you should refer to the document of ali-rds.
Change ${app_root}/config/plugin.js
to enable MySQL plugin:
exports.mysql = {
enable: true,
package: 'egg-mysql',
};
Configure database information in ${app_root}/config/config.default.js
:
exports.mysql = {
// database configuration
client: {
// host
host: 'mysql.com',
// port
port: '3306',
// username
user: 'test_user',
// password
password: 'test_password',
// database
database: 'test',
},
// load into app, default is open
app: true,
// load into agent, default is close
agent: false,
};
Usage:
app.mysql.query(sql, values); // you can access to simple database instance by using app.mysql.
exports.mysql = {
clients: {
// clientId, access the client instance by app.mysql.get('clientId')
db1: {
// host
host: 'mysql.com',
// port
port: '3306',
// username
user: 'test_user',
// password
password: 'test_password',
// database
database: 'test',
},
// ...
},
// default configuration for all databases
default: {
},
// load into app, default is open
app: true,
// load into agent, default is close
agent: false,
};
Usage:
const client1 = app.mysql.get('db1');
client1.query(sql, values);
const client2 = app.mysql.get('db2');
client2.query(sql, values);
// insert
const result = yield app.mysql.insert('posts', { title: 'Hello World' });
const insertSuccess = result.affectedRows === 1;
// get
const post = yield app.mysql.get('posts', { id: 12 });
// query
const results = yield app.mysql.select('posts',{
where: { status: 'draft' },
orders: [['created_at','desc'], ['id','desc']],
limit: 10,
offset: 0
});
// update by primary key ID, and refresh
const row = {
id: 123,
name: 'fengmk2',
otherField: 'other field value',
modifiedAt: app.mysql.literals.now, // `now()` on db server
};
const result = yield app.mysql.update('posts', row);
const updateSuccess = result.affectedRows === 1;
const result = yield app.mysql.delete('table-name', {
name: 'fengmk2'
});
beginTransaction
, commit
or rollback
can be completely under control by developerconst conn = yield app.mysql.beginTransaction();
try {
yield conn.insert(table, row1);
yield conn.update(table, row2);
yield conn.commit();
} catch (err) {
// error, rollback
yield conn.rollback(); // rollback call won't throw err
throw err;
}
*beginTransactionScope(scope, ctx)
scope
: A generatorFunction which will execute all sqls of this transaction.ctx
: The context object of current request, it will ensures that even in the case of a nested transaction, there is only one active transaction in a request at the same time.const result = yield app.mysql.beginTransactionScope(function* (conn) {
// don't commit or rollback by yourself
yield conn.insert(table, row1);
yield conn.update(table, row2);
return { success: true };
}, ctx); // ctx is the context of current request, access by `this.ctx`.
// if error throw on scope, will auto rollback
const results = yield app.mysql.query('update posts set hits = (hits + ?) where id = ?', [1, postId]);
If you want to call literals or functions in mysql , you can use Literal
.
app.mysql.literals.now
.yield app.mysql.insert(table, {
create_time: app.mysql.literals.now
});
// INSERT INTO `$table`(`create_time`) VALUES(NOW())
The following demo showed how to call CONCAT(s1, ...sn)
funtion in mysql to do string splicing.
const Literal = app.mysql.literals.Literal;
const first = 'James';
const last = 'Bond';
yield app.mysql.insert(table, {
id: 123,
fullname: new Literal(`CONCAT("${first}", "${last}"`),
});
// INSERT INTO `$table`(`id`, `fullname`) VALUES(123, CONCAT("James", "Bond"))
Please open an issue here.
FAQs
MySQL plugin for egg
The npm package egg-yoursql receives a total of 1 weekly downloads. As such, egg-yoursql popularity was classified as not popular.
We found that egg-yoursql 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.