Is there any mock(or local) service of aurora serverless Data Api?
Asked Answered
I

2

8

I have replaced the way to connect Database from lambda function.
Previously, the function established a connection to RDS using MySQL module and query via 3306 port.
Currently, I tried to use Data API and I have finished to replace all codes without test.

Now I'm thinking about how to guarantee my codes.

I have a few test codes to connect with local MySQL database running on docker image.
However, Data API cannot connect with local MySQL Database because of no endpoint.

So I want to know is there any service or library to mockup Data API.

This is my docker-compose.yml to run MySQL DB.

services:
  db:
    build: ./docker/mysql
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_DATABASE: $DB
      MYSQL_USER: $USER
      MYSQL_PASSWORD: $PWD
      MYSQL_ROOT_PASSWORD: $ROOT_PWD
    ports:
      - "3306:3306"
    volumes:
      - ./docker/mysql/initdb.d:/docker-entrypoint-initdb.d
      - ./docker/mysql/conf.d:/etc/mysql/conf.d
      - ./docker/log:/var/log/mysql

Previously, the function tried to connect to DB by following code

import * as mysql from 'mysql';
import * as util from 'util';

const connection: mysql.Pool = mysql.createPool({
  connectionLimit: 100,
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  port: Number(process.env.DB_PORT),
  database: process.env.DB_NAME,
  connectTimeout: 60 * 60 * 1000,
  acquireTimeout: 60 * 60 * 1000,
  timeout: 60 * 60 * 1000
});

export async function query(sql: string, param?: Array<any>): Promise<any> {
  const connQueryPromisified = util
    .promisify(connection.query)
    .bind(connection);
  return await connQueryPromisified(sql, param);
}

Currently:

import * as aws from 'aws-sdk';
import {
  ExecuteStatementRequest,
  ExecuteStatementResponse,
  Field,
  SqlParametersList,
  BatchExecuteStatementRequest,
  BatchExecuteStatementResponse,
  SqlParameterSets
} from 'aws-sdk/clients/rdsdataservice';
const RDS = new aws.RDSDataService({ region: 'ap-northeast-1' });
const secretArn = process.env.SECRET_ARN;
const arn = process.env.RESOURCE_ARN;
const dbName = process.env.DATABASE_NAME;

export async function query(
  sql: string,
  param?: SqlParametersList
): Promise<any> {
  const params: ExecuteStatementRequest = {
    secretArn: secretArn,
    resourceArn: arn,
    sql: sql,
    parameters: param,
    database: dbName,
    continueAfterTimeout: true,
    includeResultMetadata: true
  };

  return RDS.executeStatement(params).promise();
}

Does anybody know about the Data API mockup service? or is there any ides to write codes with Data API?

Ironsmith answered 25/8, 2019 at 23:27 Comment(0)
K
8

Have a look at local-data-api, it looks like what you're asking for.

It is a Docker image that hosts a Data API proxy and either a Postgres or MySQL instance; you can find pull it for DockerHub, image info is here: https://hub.docker.com/r/koxudaxi/local-data-api, including instructions how to run it.

Karissakarita answered 31/12, 2019 at 11:41 Comment(1)
Does anyone happen to know of a way to run something similar in-memory? I'd rather not run a whole docker container if not needed.Caryopsis
B
0

Jackal is a thin wrapper around the AWS SDK v2 Aurora Data Client. It includes a mock client implementation which wraps a JDBC DataSource. You could use this in conjunction with an in-memory database like H2 to perform local integration testing.

Bidarka answered 24/9, 2021 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.