11const fs = require ( "fs" ) ;
2+ const SQLCRUDAdapter = require ( "./sql-crud-adapter" ) ;
3+ const SurveyStorage = require ( "./survey-storage" ) ;
24
35const readFileSync = filename => fs . readFileSync ( filename ) . toString ( "utf8" ) ;
46
@@ -12,127 +14,19 @@ const dbConfig = {
1214 : null
1315} ;
1416
15- var currentId = 1 ;
16- 1717const Pool = require ( 'pg' ) . Pool
1818const pool = new Pool ( dbConfig ) ;
1919
20- function PostgresDBAdapter ( session ) {
21- 22- function getObjectsFromStorage ( tableName , filter , callback ) {
23- filter = filter || [ ] ;
24- let where = "" ;
25- if ( filter . length > 0 ) {
26- where += " WHERE " + filter . map ( fi => "" + fi . name + fi . op + fi . value ) . join ( " AND " ) ;
20+ function PostgresStorage ( session ) {
21+ function queryExecutorFunction ( ) {
22+ if ( ! ! process . env . DATABASE_LOG ) {
23+ console . log ( arguments [ 0 ] ) ;
24+ console . log ( arguments [ 1 ] ) ;
2725 }
28- 29- const command = "SELECT * FROM " + tableName + where ;
30- // console.log(command);
31- pool . query ( command , ( error , results ) => {
32- if ( error ) {
33- throw error ;
34- }
35- callback ( results . rows ) ;
36- } ) ;
37- }
38- 39- function deleteObjectFromStorage ( tableName , idValue , callback ) {
40- pool . query ( "DELETE FROM " + tableName + " WHERE id='" + idValue + "'" , ( error , results ) => {
41- if ( error ) {
42- throw error ;
43- }
44- callback ( results ) ;
45- } ) ;
46- }
47- 48- function createObjectInStorage ( tableName , object , callback ) {
49- const valueNames = [ ] ;
50- const valueIndexes = [ ] ;
51- const values = [ ] ;
52- Object . keys ( object ) . forEach ( ( key , index ) => {
53- if ( object [ key ] !== undefined ) {
54- valueNames . push ( key ) ;
55- valueIndexes . push ( "$" + ( index + 1 ) ) ;
56- values . push ( object [ key ] ) ;
57- }
58- } ) ;
59- const command = "INSERT INTO " + tableName + " (" + valueNames . join ( ", " ) + ") VALUES (" + valueIndexes . join ( ", " ) + ") RETURNING id" ;
60- // console.log(command);
61- // console.log(values);
62- pool . query ( command , values , ( error , results ) => {
63- if ( error ) {
64- throw error ;
65- }
66- console . log ( JSON . stringify ( results ) ) ;
67- callback ( results . rows [ 0 ] . id ) ;
68- } ) ;
26+ return pool . query . apply ( pool , arguments ) ;
6927 }
70- 71- function updateObjectInStorage ( tableName , object , callback ) {
72- const valueNames = [ ] ;
73- const values = [ ] ;
74- Object . keys ( object ) . forEach ( ( key , index ) => {
75- if ( object [ key ] !== undefined ) {
76- valueNames . push ( key + " = $" + ( index + 1 ) ) ;
77- values . push ( object [ key ] ) ;
78- }
79- } ) ;
80- const command = "UPDATE " + tableName + " SET " + valueNames . join ( ", " ) + " WHERE id = '" + object . id + "'" ;
81- // console.log(command);
82- // console.log(values);
83- pool . query ( command , values , ( error , results ) => {
84- if ( error ) {
85- throw error ;
86- }
87- callback ( object ) ;
88- } ) ;
89- }
90- 91- function addSurvey ( name , callback ) {
92- var newObj = {
93- name : name || ( "New Survey" + " " + currentId ++ ) ,
94- json : "{}"
95- } ;
96- createObjectInStorage ( "surveys" , newObj , id => {
97- newObj . id = id ;
98- callback ( newObj ) ;
99- } ) ;
100- }
101- 102- function postResults ( postId , json , callback ) {
103- var newObj = {
104- postid : postId ,
105- json : json
106- } ;
107- console . log ( JSON . stringify ( newObj ) ) ;
108- createObjectInStorage ( "results" , newObj , id => {
109- newObj . id = id ;
110- callback ( newObj ) ;
111- } ) ;
112- }
113- 114- return {
115- addSurvey : addSurvey ,
116- getSurvey : function ( surveyId , callback ) {
117- getObjectsFromStorage ( "surveys" , [ { name : "id" , op : "=" , value : "'" + surveyId + "'" } ] , function ( results ) { callback ( results [ 0 ] ) ; } ) ;
118- } ,
119- storeSurvey : function ( id , name , json , callback ) {
120- updateObjectInStorage ( "surveys" , { id : id , json : json } , function ( results ) { callback ( results ) ; } ) ;
121- } ,
122- getSurveys : function ( callback ) {
123- getObjectsFromStorage ( "surveys" , [ ] , function ( results ) { callback ( results ) ; } ) ;
124- } ,
125- deleteSurvey : function ( surveyId , callback ) {
126- deleteObjectFromStorage ( "surveys" , surveyId , function ( results ) { callback ( results ) ; } ) ;
127- } ,
128- postResults : postResults ,
129- getResults : function ( postId , callback ) {
130- getObjectsFromStorage ( "results" , [ { name : "postid" , op : "=" , value : "'" + postId + "'" } ] , function ( results ) { callback ( { id : postId , data : results . map ( r => r . json ) } ) ; } ) ;
131- } ,
132- changeName : function ( id , name , callback ) {
133- updateObjectInStorage ( "surveys" , { id : id , name : name } , function ( results ) { callback ( results ) ; } ) ;
134- }
135- } ;
28+ const dbQueryAdapter = new SQLCRUDAdapter ( queryExecutorFunction ) ;
29+ return new SurveyStorage ( dbQueryAdapter ) ;
13630}
13731
138- module . exports = PostgresDBAdapter ;
32+ module . exports = PostgresStorage ;
0 commit comments