Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generator support #165

Open
Markyiptw opened this issue Jun 13, 2024 · 0 comments
Open

generator support #165

Markyiptw opened this issue Jun 13, 2024 · 0 comments

Comments

@Markyiptw
Copy link

First of all, I want to say thanks because I am looking for a library that allows me to write migrations in .js file, and this seems to be the only one available and actively maintained. In fact one of the example from Readme illustrated my use case (pulling data from external sources, something that's not possible by writing .sql).

However, I am concern about large query. What if my data source has hundreds or even thousands of rows? While I can do something like

import axios from "axios";

const largeNumberOfRows = 5000;

module.exports.generateSql = async () => {
  let query = '';
  for (let i = 0; i < largeNumberOfRows; i++) {
    const response = await axios({
      method: "get",
      url: `https://api.example.org/person/${i}`,
    });
    query += `INSERT INTO person (name, age) VALUES ('${response.data.name}', ${response.data.age});`
  }
  return query;
};

I am worried about the string being too big and causing OOM, so I would like to be able to execute query in each loop instead of concatenating the string. Generator / Async generator seems to be a good api for such use case.

import axios from "axios";

const largeNumberOfRows = 5000;

module.exports.generateSql = async function* () {
  for (let i = 0; i < largeNumberOfRows; i++) {
    const response = await axios({
      method: "get",
      url: `https://api.example.org/person/${i}`,
    });
    yield `INSERT INTO person (name, age) VALUES ('${response.data.name}', ${response.data.age});`
  }
};

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant