
Security News
NVD Quietly Sweeps 100K+ CVEs Into a “Deferred” Black Hole
NVD now marks all pre-2018 CVEs as "Deferred," signaling it will no longer enrich older vulnerabilities, further eroding trust in its data.
Aliyun rds client(support mysql protocol) for egg framework
npm i egg-mysql --save
MySQL Plugin for egg, support egg application access to MySQL database.
This plugin based on @eggjs/rds, if you want to know specific usage, you should refer to the document of @eggjs/rds.
Change ${app_root}/config/plugin.ts
to enable MySQL plugin:
export default {
mysql: {
enable: true,
package: 'egg-mysql',
},
}
Configure database information in ${app_root}/config/config.default.ts
:
export default {
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:
await app.mysql.query(sql, values); // you can access to simple database instance by using app.mysql.
export default {
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.mysqls.get('db1');
await client1.query(sql, values);
const client2 = app.mysqls.get('db2');
await client2.query(sql, values);
// insert
const result = await app.mysql.insert('posts', { title: 'Hello World' });
const insertSuccess = result.affectedRows === 1;
// get
const post = await app.mysql.get('posts', { id: 12 });
// query
const results = await 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 = await app.mysql.update('posts', row);
const updateSuccess = result.affectedRows === 1;
const result = await app.mysql.delete('table-name', {
name: 'fengmk2',
});
beginTransaction
, commit
or rollback
can be completely under control by developerconst conn = await app.mysql.beginTransaction();
try {
await conn.insert(table, row1);
await conn.update(table, row2);
await conn.commit();
} catch (err) {
// error, rollback
await conn.rollback(); // rollback call won't throw err
throw err;
}
async 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 = await app.mysql.beginTransactionScope(async (conn) => {
// don't commit or rollback by yourself
await conn.insert(table, row1);
await 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 = await 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
.await 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)
function in mysql to do string splicing.
const Literal = app.mysql.literals.Literal;
const first = 'James';
const last = 'Bond';
await 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.
Made with contributors-img.
5.0.0 (2025-03-08)
closes https://github.com/eggjs/egg-mysql/issues/31
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->FAQs
MySQL plugin for egg
The npm package egg-mysql receives a total of 5,690 weekly downloads. As such, egg-mysql popularity was classified as popular.
We found that egg-mysql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 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
NVD now marks all pre-2018 CVEs as "Deferred," signaling it will no longer enrich older vulnerabilities, further eroding trust in its data.
Research
Security News
Lazarus-linked threat actors expand their npm malware campaign with new RAT loaders, hex obfuscation, and over 5,600 downloads across 11 packages.
Security News
Safari 18.4 adds support for Iterator Helpers and two other TC39 JavaScript features, bringing full cross-browser coverage to key parts of the ECMAScript spec.