aws-sdk#RDSDataService TypeScript Examples

The following examples show how to use aws-sdk#RDSDataService. 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: RDSData.ts    From rds-data with MIT License 6 votes vote down vote up
private getConnection(): RDSDataService {
    if (!this.rds) {
      this.rds = new RDSDataService({
        apiVersion: '2018-08-01',
        region: this.config.region,
        // apply extra options
        // these override apiVersion and region
        ...this.config.rdsConfig,
      });
    }
    return this.rds;
  }
Example #2
Source File: RDSData.ts    From rds-data with MIT License 6 votes vote down vote up
public async query(sql: string, params?: RDSDataParameters, transactionId?: string): Promise<RDSDataResponse> {
    const parameters = RDSData.formatParameters(params);
    return new Promise((resolve, reject) => {
      let queryParameters: RDSDataService.Types.ExecuteStatementRequest = {
        secretArn: this.config.secretArn,
        resourceArn: this.config.resourceArn,
        database: this.config.database,
        includeResultMetadata: true,
        parameters,
        sql,
      };
      if (transactionId) {
        queryParameters = { ...queryParameters, transactionId };
      }

      this.getConnection()
        .executeStatement(queryParameters)
        .promise()
        .then((response) => {
          const result = RDSData.resultFormat(response);
          resolve(result);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }
Example #3
Source File: RDSData.ts    From rds-data with MIT License 5 votes vote down vote up
private static resultFormat(response: RDSDataService.Types.ExecuteStatementResponse): RDSDataResponse {
    const insertId =
      response.generatedFields && response.generatedFields.length > 0 ? response.generatedFields[0].longValue : 0;
    const columns: RDSDataColumn[] = [];
    const data: { [key: string]: RDSDataResponseValue }[] = [];
    const numberOfRecordsUpdated = response.numberOfRecordsUpdated ?? 0;

    if (response && response.columnMetadata && response.records) {
      response.columnMetadata.map((column) => {
        return columns.push({
          name: column.label || '',
          tableName: column.tableName || '',
          type: column.typeName?.toUpperCase() || '',
        });
      });

      response.records.forEach((record) => {
        const row: RDSDataRow = {};
        for (let c = 0; c < record.length; c += 1) {
          /* tslint:disable:no-string-literal */
          const isNull = record[c].isNull ?? false;
          const v: RDSDataResponseValue = { isNull };
          switch (columns[c].type) {
            case 'BINARY':
              v.buffer = isNull ? undefined : Buffer.from((record[c].blobValue || '').toString());
              v.string = isNull ? undefined : v.buffer?.toString('base64');
              break;
            case 'BOOL':
            case 'BIT':
              v.boolean = isNull ? undefined : record[c].booleanValue;
              v.number = v.boolean ? 1 : 0;
              break;
            case 'TIMESTAMP':
            case 'DATETIME':
            case 'DATE':
              v.date = isNull ? undefined : new Date(record[c].stringValue ?? '');
              v.string = isNull ? undefined : record[c].stringValue;
              v.number = v.date ? v.date.getTime() : 0;
              break;
            case 'INTEGER':
            case 'INTEGER UNSIGNED':
            case 'INT':
            case 'INT4':
            case 'INT8':
            case 'INT UNSIGNED':
            case 'BIGINT':
            case 'BIGINT UNSIGNED':
            case 'SERIAL':
              v.number = isNull ? undefined : record[c].longValue;
              break;
            case 'UUID':
            case 'TEXT':
            case 'CHAR':
            case 'BPCHAR':
            case 'VARCHAR':
              v.string = isNull ? undefined : record[c].stringValue;
              break;
            default:
              throw new Error(`Missing type: ${columns[c].type}`);
          }
          /* tslint:enable:no-string-literal */
          row[columns[c].name] = v;
        }
        data.push(row);
      });
    }

    return { data, columns, numberOfRecordsUpdated, insertId };
  }
Example #4
Source File: RDSData.ts    From rds-data with MIT License 5 votes vote down vote up
private rds: RDSDataService | null = null;