5.1.0 • Published 12 months ago
@kiza/utils v5.1.0
Utils
Mostly mysql utilities for Node.js
npm install @kiza/utils
Setup connection string
Run once on startup:
import { poolProvider } from "@kiza/utils";
import mysql from 'mysql2/promise';
import config from "config";
await poolProvider.setPool(() => mysql.createPool(config.database));
Example of config:
{
"host": "localhost",
"user": "root",
"password": "password",
"database": "mydb",
"dateStrings": true,
"port": 3306,
"charset": "utf8mb4"
}
Use mysqlBase
See example in testApp.js
Create your repositories like:
import { mysqlBase } from "@kiza/utils";
export default class extends mysqlBase {
static getJobs(siteId) {
return this.all("SELECT * FROM job WHERE siteId = ? ORDER BY posted DESC", [siteId]);
}
}
Transactions:
import { mysqlBase } from "@kiza/utils";
export default class extends mysqlBase {
static addJobs(job, job2) {
return this.inTransacation(async con => {
await this.insert("INSERT INTO job SET ?", job, con);
await this.insert("INSERT INTO job SET ?", job2, con);
});
}
}
Publish package
Note to self
npm publish --access public
Change log
v5
- Remove dep on mysql2 package, pool should be provided by consumer. This lets the consumer manage the mysql version
- Remove guid util. node:crypto should be used instead
v4
- BIT maps to boolean automatically
v3
- moved es6 imports