11const { GenericContainer } = require ( "testcontainers" ) ;
22
33//https://www.npmjs.com/package/postgres
4- const postgres = require ( "postgres" ) ;
4+ //const postgres = require("postgres");
5+ //https://node-postgres.com/
6+ const postgres = require ( "pg" ) ;
57
68// docker run -p 5432:5432 -e POSTGRES_PASSWORD=bar -e POSTGRES_USER=foo postgres:11
79
@@ -38,13 +40,23 @@ module.exports ={
3840 console . log ( "connecting " + dbURL ) ;
3941 }
4042
41- connection = postgres ( dbURL , {
42- // host : '', // Postgres ip address[s] or domain name[s]
43- // port : 5432, // Postgres server port[s]
44- database : 'db' , // Name of database to connect to
45- username : 'foo' , // Username of database user
46- password : 'bar' , // Password of database user
47- } )
43+ connection = new postgres . Client ( {
44+ user : 'foo' ,
45+ host : 'localhost' ,
46+ database : 'db' ,
47+ password : 'bar' ,
48+ port : process . env . DB_PORT ,
49+ } ) ;
50+
51+ await connection . connect ( ) ;
52+
53+ // connection = postgres(dbURL, {
54+ // // host : '', // Postgres ip address[s] or domain name[s]
55+ // // port : 5432, // Postgres server port[s]
56+ // database : 'db', // Name of database to connect to
57+ // username : 'foo', // Username of database user
58+ // password : 'bar', // Password of database user
59+ // })
4860
4961 return test_container ;
5062
@@ -59,29 +71,30 @@ module.exports ={
5971 const queryAllTables = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where (TABLE_TYPE=\'TABLE\' OR TABLE_TYPE=\'BASE TABLE\')'
6072 + " AND TABLE_SCHEMA='public' " ;
6173
62- //TODO
63- // return new Promise(resolve => {
64- //
65- // querySql(queryAllTables)
66- // .then(results => {
67- // let truncateAll= '';
68- // let resetSeq = '';
69- // for (const element of results){
70- // truncateAll += `TRUNCATE TABLE ${element.TABLE_NAME};`;
71- // resetSeq += `ALTER TABLE ${element.TABLE_NAME} AUTO_INCREMENT=1;`;
72- // }
73- //
74- // querySql(truncateAll+resetSeq).then(e=>
75- // querySql(enableReferentialIntegrity).then(f=>resolve())
76- // )
77- // })
78-
79- return Promise . resolve ( ) ;
74+ return new Promise ( resolve => {
75+
76+ // connection([queryAllTables])
77+ connection . query ( queryAllTables )
78+ . then ( results => {
79+ let truncateAll = 'TRUNCATE TABLE ' ;
80+ let resetSeq = '' ;
81+ truncateAll += results . rows . map ( _ => _ . table_name ) . join ( "," )
82+ // for (const element of results.rows){
83+ // truncateAll += `${element.table_name},`;
84+ // //TODO
85+ // //resetSeq += `ALTER SEQUENCE ${element.TABLE_NAME} RESTART WITH 1;`;
86+ // }
87+ truncateAll += ";"
88+
89+ //connection([truncateAll+resetSeq]).then(f=>resolve())
90+ connection . query ( truncateAll + resetSeq ) . then ( f => resolve ( ) )
91+ } )
92+ } )
8093 } ,
8194
8295 stopDb : async ( ) => {
8396 if ( connection )
84- connection . end ( ) ;
97+ await connection . end ( ) ;
8598
8699 if ( test_container ) {
87100 await test_container . stop ( ) ;
0 commit comments