@builderbot/database-mysql 0.1.3-alpha.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/dist/index.cjs +87 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/mysqlAdapter.d.ts +23 -0
- package/dist/mysqlAdapter.d.ts.map +1 -0
- package/dist/types.d.ts +18 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +49 -0
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var mysql = require('mysql2');
|
|
4
|
+
|
|
5
|
+
class MysqlAdapter {
|
|
6
|
+
constructor(_credentials) {
|
|
7
|
+
this.listHistory = [];
|
|
8
|
+
this.credentials = { host: null, user: null, database: null, password: null };
|
|
9
|
+
this.getPrevByNumber = async (from) => {
|
|
10
|
+
// TODO:pendiente valida _closing, lanza error
|
|
11
|
+
// if (this.db._closing) await this.init()
|
|
12
|
+
return await new Promise((resolve, reject) => {
|
|
13
|
+
const sql = `SELECT * FROM history WHERE phone='${from}' ORDER BY id DESC`;
|
|
14
|
+
this.db.query(sql, (error, rows) => {
|
|
15
|
+
if (error) {
|
|
16
|
+
reject(error);
|
|
17
|
+
}
|
|
18
|
+
if (rows.length) {
|
|
19
|
+
const [row] = rows;
|
|
20
|
+
row.options = JSON.parse(row.options);
|
|
21
|
+
resolve(row);
|
|
22
|
+
}
|
|
23
|
+
if (!rows.length) {
|
|
24
|
+
resolve(null);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
this.save = async (ctx) => {
|
|
30
|
+
const values = [
|
|
31
|
+
[ctx.ref, ctx.keyword, ctx.answer, ctx.refSerialize, ctx.from, JSON.stringify(ctx.options), null],
|
|
32
|
+
];
|
|
33
|
+
const sql = 'INSERT INTO history (ref, keyword, answer, refSerialize, phone, options, created_at) values ?';
|
|
34
|
+
this.db.query(sql, [values], (err) => {
|
|
35
|
+
if (err)
|
|
36
|
+
throw err;
|
|
37
|
+
console.log('Guardado en DB...', values);
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
this.createTable = () => new Promise((resolve) => {
|
|
41
|
+
const tableName = 'history';
|
|
42
|
+
const sql = `CREATE TABLE ${tableName}
|
|
43
|
+
(id INT AUTO_INCREMENT PRIMARY KEY,
|
|
44
|
+
ref varchar(255) NOT NULL,
|
|
45
|
+
keyword varchar(255) NULL,
|
|
46
|
+
answer longtext NOT NULL,
|
|
47
|
+
refSerialize varchar(255) NOT NULL,
|
|
48
|
+
phone varchar(255) NOT NULL,
|
|
49
|
+
options longtext NOT NULL,
|
|
50
|
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)
|
|
51
|
+
CHARACTER SET utf8mb4 COLLATE utf8mb4_General_ci`;
|
|
52
|
+
this.db.query(sql, (err) => {
|
|
53
|
+
if (err)
|
|
54
|
+
throw err;
|
|
55
|
+
console.log(`Tabla ${tableName} creada correctamente `);
|
|
56
|
+
resolve(true);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
this.checkTableExists = () => new Promise((resolve) => {
|
|
60
|
+
const sql = "SHOW TABLES LIKE 'history'";
|
|
61
|
+
this.db.query(sql, (err, rows) => {
|
|
62
|
+
if (err)
|
|
63
|
+
throw err;
|
|
64
|
+
if (!rows.length) {
|
|
65
|
+
this.createTable();
|
|
66
|
+
}
|
|
67
|
+
resolve(!!rows.length);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
this.credentials = _credentials;
|
|
71
|
+
this.init().then();
|
|
72
|
+
}
|
|
73
|
+
async init() {
|
|
74
|
+
this.db = mysql.createConnection(this.credentials);
|
|
75
|
+
await this.db.connect(async (error) => {
|
|
76
|
+
if (!error) {
|
|
77
|
+
console.log(`Solicitud de conexión a base de datos exitosa`);
|
|
78
|
+
await this.checkTableExists();
|
|
79
|
+
}
|
|
80
|
+
if (error) {
|
|
81
|
+
console.log(`Solicitud de conexión fallida ${error.stack}`);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
exports.MysqlAdapter = MysqlAdapter;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Connection } from 'mysql2';
|
|
2
|
+
|
|
3
|
+
import { HistoryRow, MysqlAdapterCredentials } from './types';
|
|
4
|
+
declare class MysqlAdapter {
|
|
5
|
+
db: Connection;
|
|
6
|
+
listHistory: any[];
|
|
7
|
+
credentials: MysqlAdapterCredentials;
|
|
8
|
+
constructor(_credentials: MysqlAdapterCredentials);
|
|
9
|
+
init(): Promise<void>;
|
|
10
|
+
getPrevByNumber: (from: any) => Promise<HistoryRow | null>;
|
|
11
|
+
save: (ctx: {
|
|
12
|
+
ref: string;
|
|
13
|
+
keyword: string;
|
|
14
|
+
answer: any;
|
|
15
|
+
refSerialize: string;
|
|
16
|
+
from: string;
|
|
17
|
+
options: any;
|
|
18
|
+
}) => Promise<void>;
|
|
19
|
+
createTable: () => Promise<boolean>;
|
|
20
|
+
checkTableExists: () => Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export { MysqlAdapter };
|
|
23
|
+
//# sourceMappingURL=mysqlAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mysqlAdapter.d.ts","sourceRoot":"","sources":["../src/mysqlAdapter.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,UAAU,EAA2B,MAAM,QAAQ,CAAA;AAEnE,OAAO,EAAE,UAAU,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAA;AAE7D,cAAM,YAAY;IACd,EAAE,EAAE,UAAU,CAAA;IACd,WAAW,QAAK;IAChB,WAAW,EAAE,uBAAuB,CAA6D;gBAErF,YAAY,EAAE,uBAAuB;IAK3C,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAe3B,eAAe,SAAgB,GAAG,KAAG,QAAQ,UAAU,GAAG,IAAI,CAAC,CAoB9D;IAED,IAAI,QAAe;QACf,GAAG,EAAE,MAAM,CAAA;QACX,OAAO,EAAE,MAAM,CAAA;QACf,MAAM,EAAE,GAAG,CAAA;QACX,YAAY,EAAE,MAAM,CAAA;QACpB,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,GAAG,CAAA;KACf,KAAG,QAAQ,IAAI,CAAC,CAUhB;IAED,WAAW,QAAO,QAAQ,OAAO,CAAC,CAoB5B;IAEN,gBAAgB,QAAO,QAAQ,OAAO,CAAC,CAYjC;CACT;AAED,OAAO,EAAE,YAAY,EAAE,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RowDataPacket } from 'mysql2';
|
|
2
|
+
export interface MysqlAdapterCredentials {
|
|
3
|
+
host: string;
|
|
4
|
+
user: string;
|
|
5
|
+
database: string;
|
|
6
|
+
password: string;
|
|
7
|
+
}
|
|
8
|
+
export interface HistoryRow extends RowDataPacket {
|
|
9
|
+
id: number;
|
|
10
|
+
ref: string;
|
|
11
|
+
keyword: string | null;
|
|
12
|
+
answer: string;
|
|
13
|
+
refSerialize: string;
|
|
14
|
+
phone: string;
|
|
15
|
+
options: string;
|
|
16
|
+
created_at: Date;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AAEtC,MAAM,WAAW,uBAAuB;IACpC,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,UAAW,SAAQ,aAAa;IAC7C,EAAE,EAAE,MAAM,CAAA;IACV,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;IACtB,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,EAAE,IAAI,CAAA;CACnB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@builderbot/database-mysql",
|
|
3
|
+
"version": "0.1.3-alpha.22",
|
|
4
|
+
"description": "Esto es el conector a Mysql",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "Leifer Mendez <leifer33@gmail.com>",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"main": "dist/index.cjs",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"directories": {
|
|
12
|
+
"src": "src",
|
|
13
|
+
"test": "__tests__"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"./dist/"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/codigoencasa/bot-whatsapp.git"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "rimraf dist && rollup --config",
|
|
24
|
+
"test": " npx uvu -r tsm ./__tests__ .test.ts",
|
|
25
|
+
"test:debug": " npx tsm --inspect-brk ../../node_modules/uvu/bin.js ./__tests__ .test.ts",
|
|
26
|
+
"test:coverage": "npx c8 npm run test"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/codigoencasa/bot-whatsapp/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://github.com/codigoencasa/bot-whatsapp#readme",
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"mysql2": "^2.3.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
37
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
38
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
39
|
+
"@types/node": "^20.11.0",
|
|
40
|
+
"@types/sinon": "^17.0.3",
|
|
41
|
+
"kleur": "^4.1.5",
|
|
42
|
+
"rimraf": "^3.0.2",
|
|
43
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
44
|
+
"sinon": "^17.0.1",
|
|
45
|
+
"tslib": "^2.6.2",
|
|
46
|
+
"tsm": "^2.3.0"
|
|
47
|
+
},
|
|
48
|
+
"gitHead": "0fb50a130573d7a1f6b1cd7fcfa0fdf1b8f9d578"
|
|
49
|
+
}
|