@builderbot/database-postgres 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 +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/postgresAdapter.d.ts +16 -0
- package/dist/postgresAdapter.d.ts.map +1 -0
- package/dist/types.d.ts +24 -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 @@
|
|
|
1
|
+
"use strict";var t=require("pg");exports.PostgreSQLAdapter=class{constructor(t){this.listHistory=[],this.credentials={host:"localhost",user:"",database:"",password:null,port:5432},this.credentials=t,this.init().then()}async init(){try{const n=new t.Pool(this.credentials),o=await n.connect();return this.db=o,console.log("🆗 Conexión Correcta DB"),this.checkTableExistsAndSP(),!0}catch(t){throw console.log("Error",t),t}}async getPrevByNumber(t){try{const n=(await this.db.query("SELECT * FROM public.history WHERE phone = $1 ORDER BY created_at DESC LIMIT 1",[t])).rows[0];return n&&(n.refSerialize=n.refserialize,delete n.refserialize),n}catch(t){throw console.error("Error al obtener la entrada anterior por número:",t),t}}async save(t){const n=[t.ref,t.keyword,t.answer,t.refSerialize,t.phone,JSON.stringify(t.options)];try{await this.db.query("SELECT save_or_update_history_and_contact($1, $2, $3, $4, $5, $6)",n),console.log("🆗 Historico creado con exito")}catch(t){throw console.error("Error al registrar la entrada del historial:",t),t}this.listHistory.push(t)}async getContact(t){const n=t.phone;try{return(await this.db.query("SELECT * FROM public.contact WHERE phone = $1 LIMIT 1",[n])).rows[0]}catch(t){throw console.error("Error al obtener contacto por número:",t.mesage),t}}async saveContact(t){const n=await this.getContact(t);let o={};o="a"===(t?.action??"a")?{...n?.values,...t?.values??{}}:t?.values??{};const e=[t.from,JSON.stringify(o)];try{await this.db.query("SELECT save_or_update_contact($1, $2)",e),console.log("🆗 Contacto guardado o actualizado con éxito")}catch(t){throw console.error("🚫 Error al guardar o actualizar contacto:",t),t}}async checkTableExistsAndSP(){try{await this.db.query("\n CREATE TABLE IF NOT EXISTS contact (\n id SERIAL PRIMARY KEY,\n phone VARCHAR(255) NOT NULL,\n created_at TIMESTAMP DEFAULT current_timestamp,\n updated_in TIMESTAMP,\n last_interaction TIMESTAMP,\n values JSONB\n )"),console.log("🆗 Tabla contact existe o fue creada con éxito")}catch(t){throw console.error("🚫 Error al crear la tabla contact:",t),t}try{await this.db.query("\n CREATE TABLE IF NOT EXISTS history (\n id SERIAL PRIMARY KEY,\n ref VARCHAR(255) NOT NULL,\n keyword VARCHAR(255),\n answer TEXT NOT NULL,\n refSerialize TEXT NOT NULL,\n phone VARCHAR(255) NOT NULL,\n options JSONB,\n created_at TIMESTAMP DEFAULT current_timestamp,\n updated_in TIMESTAMP,\n contact_id INTEGER REFERENCES contact(id)\n )"),console.log("🆗 Tabla history existe o fue creada con éxito")}catch(t){throw console.error("🚫 Error al crear la tabla de history:",t),t}await this.createSP()}async createSP(){try{await this.db.query('\n CREATE OR REPLACE FUNCTION save_or_update_contact(\n in_phone VARCHAR(255),\n in_values JSONB\n )\n RETURNS VOID AS\n $$\n DECLARE\n contact_cursor refcursor := \'cur_contact\';\n contact_id INT;\n BEGIN\n SELECT id INTO contact_id FROM contact WHERE phone = in_phone;\n \n IF contact_id IS NULL THEN\n INSERT INTO contact (phone, "values")\n VALUES (in_phone, in_values);\n ELSE\n UPDATE contact SET "values" = in_values, updated_in = current_timestamp\n WHERE id = contact_id;\n END IF;\n END;\n $$ LANGUAGE plpgsql;'),console.log("🆗 Procedimiento almacenado de contacto existe o fue creada con éxito")}catch(t){throw console.error("🚫 Error al crear el procedimiento almacenado de contacto:",t),t}try{await this.db.query("\n CREATE OR REPLACE FUNCTION save_or_update_history_and_contact(\n in_ref VARCHAR(255),\n in_keyword VARCHAR(255),\n in_answer TEXT,\n in_refserialize TEXT,\n in_phone VARCHAR(255),\n in_options JSONB\n )\n RETURNS VOID AS\n $$\n DECLARE\n _contact_id INT;\n BEGIN\n SELECT id INTO _contact_id FROM contact WHERE phone = in_phone;\n \n IF _contact_id IS NULL THEN\n INSERT INTO contact (phone)\n VALUES (in_phone)\n RETURNING id INTO _contact_id;\n ELSE\n UPDATE contact SET last_interaction = current_timestamp WHERE id = _contact_id;\n END IF;\n \n INSERT INTO history (ref, keyword, answer, refserialize, phone, options, contact_id, created_at)\n VALUES (in_ref, in_keyword, in_answer, in_refserialize, in_phone, in_options, _contact_id, current_timestamp);\n \n END;\n $$ LANGUAGE plpgsql;"),console.log("🆗 Procedimiento almacenado de historico existe o fue creada con éxito")}catch(t){throw console.error("🚫 Error al crear el procedimiento almacenado de historico:",t),t}}};
|
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,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Contact, Credential, HistoryEntry } from './types';
|
|
2
|
+
declare class PostgreSQLAdapter {
|
|
3
|
+
db: any;
|
|
4
|
+
listHistory: HistoryEntry[];
|
|
5
|
+
credentials: Credential;
|
|
6
|
+
constructor(_credentials: Credential);
|
|
7
|
+
init(): Promise<boolean | undefined>;
|
|
8
|
+
getPrevByNumber(from: string): Promise<HistoryEntry | undefined>;
|
|
9
|
+
save(ctx: HistoryEntry): Promise<void>;
|
|
10
|
+
getContact(ctx: HistoryEntry): Promise<Contact | undefined>;
|
|
11
|
+
saveContact(ctx: any): Promise<void>;
|
|
12
|
+
checkTableExistsAndSP(): Promise<void>;
|
|
13
|
+
createSP(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export { PostgreSQLAdapter };
|
|
16
|
+
//# sourceMappingURL=postgresAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgresAdapter.d.ts","sourceRoot":"","sources":["../src/postgresAdapter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAE3D,cAAM,iBAAiB;IACnB,EAAE,EAAE,GAAG,CAAA;IACP,WAAW,EAAE,YAAY,EAAE,CAAK;IAChC,WAAW,EAAE,UAAU,CAA4E;gBAEvF,YAAY,EAAE,UAAU;IAK9B,IAAI,IAAI,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAcpC,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,SAAS,CAAC;IAkBhE,IAAI,CAAC,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IActC,UAAU,CAAC,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC;IAY3D,WAAW,CAAC,GAAG,KAAA,GAAG,OAAO,CAAC,IAAI,CAAC;IAuB/B,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;IA0CtC,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;CAsElC;AAED,OAAO,EAAE,iBAAiB,EAAE,CAAA"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type HistoryEntry = {
|
|
2
|
+
ref: string;
|
|
3
|
+
keyword?: string;
|
|
4
|
+
answer: string;
|
|
5
|
+
refSerialize: string;
|
|
6
|
+
phone: string;
|
|
7
|
+
options?: Record<string, any>;
|
|
8
|
+
};
|
|
9
|
+
export interface Credential {
|
|
10
|
+
host: string;
|
|
11
|
+
user: string;
|
|
12
|
+
database: string;
|
|
13
|
+
password: any;
|
|
14
|
+
port: number;
|
|
15
|
+
}
|
|
16
|
+
export interface Contact {
|
|
17
|
+
id: number;
|
|
18
|
+
phone: string;
|
|
19
|
+
created_at: string;
|
|
20
|
+
updated_in?: string | null;
|
|
21
|
+
last_interaction?: string | null;
|
|
22
|
+
values: Record<string, any>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG;IACvB,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAChC,CAAA;AAED,MAAM,WAAW,UAAU;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,GAAG,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,OAAO;IACpB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,EAAE,MAAM,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAC1B,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC9B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@builderbot/database-postgres",
|
|
3
|
+
"version": "0.1.3-alpha.22",
|
|
4
|
+
"description": "> TODO: description",
|
|
5
|
+
"author": "vicente1992 <vic_ortiz20@hotmail.es>",
|
|
6
|
+
"homepage": "https://github.com/codigoencasa/bot-whatsapp#readme",
|
|
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
|
+
"dependencies": {
|
|
32
|
+
"pg": "^8.11.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
36
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
37
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
38
|
+
"@types/node": "^20.11.0",
|
|
39
|
+
"@types/pg": "^8.10.9",
|
|
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
|
+
}
|