express#query TypeScript Examples

The following examples show how to use express#query. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: controllers.ts    From StratosDB with MIT License 4 votes vote down vote up
stratosController: controllers = {
  /**
   * CONTROLLER: CREATESCHEMA
   * WILL ENTER THE SCHEMA INTO THE CONNECTED DATABASE THAT WAS
   * INPUTTED BY THE USER FROM THE FRONTEND
   */
  createSchema: (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ) => {
    // DESTRUCTURING SCHEMAENTRY FROM REQ.BODY
    const { schemaEntry } = req.body;
    db.query(schemaEntry)
      .then(() => {
        return next();
      })
      .catch((error: string) => {
        console.log(
          'Error in Controllers > createSchema > db.query > SCHEMA: ',
          error
        );
      });
  },

  /**
   * CONTROLLER: QUERYTABLE
   * WILL RETURN THE DATA REQUESTED BY THE USER
   * INPUTTED QUERY STRING FROM FRONTEND
   */
  queryTable: (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ) => {
    // DESTRUCTURING QUERYENTRY FROM REQ.BODY
    const { queryEntry } = req.body;
    db.query(queryEntry)
      .then((results: any) => {
        res.locals.queryResult = results.rows;
        return next();
      })
      .catch((error: string) => {
        console.log(
          'Error in Controller > queryTable > db.query > QUERY RESULT: ',
          error
        );
      });
  },

  /**
   * CONTROLLER: RUNTEST
   * WILL RETURN THE DATA STATISTICS FROM THE RAN QUERY STRING
   */
  runTest: (
    req: express.Request,
    res: express.Response,
    next: express.NextFunction
  ) => {
    // DESTRUCTURING QUERYENTRY FROM REQ.BODY
    const { queryEntry } = req.body;
    // OBJECT DECLARATION THAT WILL HOLD OUR RESULTS FROM OUR DB.QUERY
    const newData: dataType = {
      queryData: '',
      queryStatistics: '',
    };
    // VARIABLE STORING THE NEW.DATA.QUERYSTATISTICS[0] (WHICH IS AN OBJECT) PROPERTY NAME BECAUSE OF TS COMPILE ERROR
    const queryPlan: any = 'QUERY PLAN';
    // RUNNING EXPLAIN BY PASSING IN A CONCANTENATED STRING OF EXPLAIN... AND THE SCHEMAENTRY QUERY STRING
    db.query('EXPLAIN (FORMAT JSON, ANALYZE) ' + queryEntry)
      .then((queryStatistics: any) => {
        // RE-ASSIGNING OUR NEWDATA.QUERYSTATISTICS TO OUR RETURNED DATA ANALYTICS
        newData.queryStatistics = queryStatistics.rows[0][queryPlan];
        res.locals.explain = newData.queryStatistics;
        return next();
      })
      .catch((error: string) => {
        console.log(
          'Error in Controllers > runTest > db.query > EXPLAIN: ',
          error
        );
      });
  },
}