@builderbot/database-json 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 ADDED
@@ -0,0 +1,11 @@
1
+ # `database-json`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const databaseJson = require('database-json');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var path = require('path');
5
+
6
+ class JsonFileDB {
7
+ constructor(options = {
8
+ filename: 'de',
9
+ }) {
10
+ this.listHistory = [];
11
+ this.options = { filename: 'db.json' };
12
+ this.options = { ...this.options, ...options };
13
+ this.pathFile = path.join(process.cwd(), this.options.filename);
14
+ this.init().then();
15
+ }
16
+ /**
17
+ * Revisamos si existe o no el archivo JSON
18
+ */
19
+ async init() {
20
+ if (fs.existsSync(this.pathFile)) {
21
+ return;
22
+ }
23
+ try {
24
+ const parseData = JSON.stringify([], null, 2);
25
+ await fs.promises.writeFile(this.pathFile, parseData, 'utf-8');
26
+ }
27
+ catch (e) {
28
+ throw new Error(e.message);
29
+ }
30
+ }
31
+ /**
32
+ * Validar JSON
33
+ * @param raw
34
+ */
35
+ validateJson(raw) {
36
+ try {
37
+ return JSON.parse(raw);
38
+ }
39
+ catch (e) {
40
+ return {};
41
+ }
42
+ }
43
+ /**
44
+ * Leer archivo y parsear
45
+ */
46
+ async readFileAndParse() {
47
+ const data = await fs.promises.readFile(this.pathFile, 'utf-8');
48
+ const parseData = this.validateJson(data);
49
+ return parseData;
50
+ }
51
+ /**
52
+ * Buscar el último mensaje por número
53
+ * @param from
54
+ */
55
+ async getPrevByNumber(from) {
56
+ const history = await this.readFileAndParse();
57
+ if (!history.length) {
58
+ return undefined;
59
+ }
60
+ const result = history
61
+ .slice()
62
+ .reverse()
63
+ .filter((i) => !!i.keyword);
64
+ return result.find((a) => a.from === from);
65
+ }
66
+ /**
67
+ * Guardar dato
68
+ * @param ctx
69
+ */
70
+ async save(ctx) {
71
+ this.listHistory.push(ctx);
72
+ const parseData = JSON.stringify(this.listHistory, null, 2);
73
+ await fs.promises.writeFile(this.pathFile, parseData, 'utf-8');
74
+ }
75
+ }
76
+
77
+ exports.JsonFileDB = JsonFileDB;
@@ -0,0 +1,32 @@
1
+ import { HistoryEntry, JsonFileAdapterOptions } from './types';
2
+ declare class JsonFileDB {
3
+ private pathFile;
4
+ listHistory: HistoryEntry[];
5
+ private options;
6
+ constructor(options?: JsonFileAdapterOptions);
7
+ /**
8
+ * Revisamos si existe o no el archivo JSON
9
+ */
10
+ private init;
11
+ /**
12
+ * Validar JSON
13
+ * @param raw
14
+ */
15
+ private validateJson;
16
+ /**
17
+ * Leer archivo y parsear
18
+ */
19
+ private readFileAndParse;
20
+ /**
21
+ * Buscar el último mensaje por número
22
+ * @param from
23
+ */
24
+ getPrevByNumber(from: string): Promise<HistoryEntry | undefined>;
25
+ /**
26
+ * Guardar dato
27
+ * @param ctx
28
+ */
29
+ save(ctx: HistoryEntry): Promise<void>;
30
+ }
31
+ export { JsonFileDB };
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAA;AAE9D,cAAM,UAAU;IACZ,OAAO,CAAC,QAAQ,CAAQ;IACxB,WAAW,EAAE,YAAY,EAAE,CAAK;IAChC,OAAO,CAAC,OAAO,CAAkD;gBAG7D,OAAO,GAAE,sBAER;IAOL;;OAEG;YACW,IAAI;IAYlB;;;OAGG;IACH,OAAO,CAAC,YAAY;IAQpB;;OAEG;YACW,gBAAgB;IAM9B;;;OAGG;IACG,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC;IAatE;;;OAGG;IACG,IAAI,CAAC,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;CAK/C;AAED,OAAO,EAAE,UAAU,EAAE,CAAA"}
@@ -0,0 +1,12 @@
1
+ export interface JsonFileAdapterOptions {
2
+ filename: string;
3
+ }
4
+ export interface HistoryEntry {
5
+ ref: string;
6
+ keyword: string;
7
+ answer: any;
8
+ refSerialize: string;
9
+ from: string;
10
+ options: any;
11
+ }
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,sBAAsB;IACnC,QAAQ,EAAE,MAAM,CAAA;CACnB;AAED,MAAM,WAAW,YAAY;IACzB,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,GAAG,CAAA;IACX,YAAY,EAAE,MAAM,CAAA;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,GAAG,CAAA;CACf"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@builderbot/database-json",
3
+ "version": "0.1.3-alpha.22",
4
+ "description": "Esto es el conector a json",
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
+ "devDependencies": {
33
+ "@rollup/plugin-commonjs": "^25.0.7",
34
+ "@rollup/plugin-node-resolve": "^15.2.3",
35
+ "@rollup/plugin-terser": "^0.4.4",
36
+ "@types/node": "^20.11.0",
37
+ "kleur": "^4.1.5",
38
+ "rimraf": "^3.0.2",
39
+ "rollup-plugin-typescript2": "^0.36.0",
40
+ "tslib": "^2.6.2",
41
+ "tsm": "^2.3.0"
42
+ },
43
+ "gitHead": "0fb50a130573d7a1f6b1cd7fcfa0fdf1b8f9d578"
44
+ }