@builderbot/database-postgres 1.2.2-alpha.4 → 1.2.3-alpha.0
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/dist/index.cjs +1 -1
- package/dist/postgresAdapter.d.ts +0 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("@builderbot/bot"),n=require("pg");class
|
|
1
|
+
"use strict";var t=require("@builderbot/bot"),n=require("pg");class r extends t.MemoryDB{constructor(t){super(),this.listHistory=[],this.credentials={host:"localhost",user:"",database:"",password:null,port:5432},this.credentials=t,this.init().then()}async init(){try{const t=new n.Pool(this.credentials),r=await t.connect();return this.db=r,console.log("🆗 Successful DB Connection"),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 getting previous entry by number:",t),t}}async save(t){const n=[t.ref,t.keyword,t.answer,t.refSerialize,t.from,JSON.stringify(t.options)];try{await this.db.query("SELECT save_or_update_history_and_contact($1, $2, $3, $4, $5, $6)",n)}catch(t){throw console.error("Error registering history entry:",t),t}this.listHistory.push(t)}async getContact(t){const n=t.from;try{return(await this.db.query("SELECT * FROM public.contact WHERE phone = $1 LIMIT 1",[n])).rows[0]}catch(t){throw console.error("Error getting contact by number:",t.message),t}}async saveContact(t){const n=await this.getContact(t);let r={};r="a"===(t?.action??"a")?{...n?.values,...t?.values??{}}:t?.values??{};const e=[t.from,JSON.stringify(r)];try{await this.db.query("SELECT save_or_update_contact($1, $2)",e)}catch(t){throw console.error("🚫 Error saving or updating contact:",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) DEFAULT NULL,\n created_at TIMESTAMP DEFAULT current_timestamp,\n updated_in TIMESTAMP,\n last_interaction TIMESTAMP,\n values JSONB\n )")}catch(t){throw console.error("🚫 Error creating the contact table:",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) DEFAULT NULL,\n options JSONB,\n created_at TIMESTAMP DEFAULT current_timestamp,\n updated_in TIMESTAMP,\n contact_id INTEGER REFERENCES contact(id)\n )")}catch(t){throw console.error("🚫 Error creating the history table:",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;')}catch(t){throw console.error("🚫 Error creating the stored procedure for contact:",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;")}catch(t){throw console.error("🚫 Error creating the stored procedure for history:",t),t}}}exports.PostgreSQLAdapter=r;
|
package/dist/types.d.ts
CHANGED
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
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,IAAI,EAAE,MAAM,CAAA;IACZ,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
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@builderbot/database-postgres",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3-alpha.0",
|
|
4
4
|
"description": "> TODO: description",
|
|
5
5
|
"author": "vicente1992 <vic_ortiz20@hotmail.es>",
|
|
6
6
|
"homepage": "https://github.com/codigoencasa/bot-whatsapp#readme",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"url": "https://github.com/codigoencasa/bot-whatsapp/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@builderbot/bot": "^1.2.
|
|
32
|
+
"@builderbot/bot": "^1.2.3-alpha.0",
|
|
33
33
|
"pg": "^8.11.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
@@ -46,5 +46,5 @@
|
|
|
46
46
|
"tslib": "^2.6.2",
|
|
47
47
|
"tsm": "^2.3.0"
|
|
48
48
|
},
|
|
49
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "0f6e93213ffdcab4bfbd1c9d7842a5b1a209de69"
|
|
50
50
|
}
|