oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.89k stars 94 forks source link

Make it possible to registerFunction for a version of `generate_series` #383

Open marcus13371337 opened 4 months ago

marcus13371337 commented 4 months ago

Consider the following query:

SELECT * FROM generate_series(1, 5)
 generate_series
-----------------
               1
               2
               3
               4
               5
(5 rows)

I'm having a hard time registering a function that could handle this scenario. What I've tried:

 db.public.registerFunction({
    name: "generate_series",
    args: [DataType.integer, DataType.integer],
    returns: DataType.integer, // Should be typed to array somehow I guess?
    implementation: (from: any, to: any) => {
      const start = parseInt(from);
      const stop = parseInt(to);

      const result = [];
      for (let i = start; i <= stop; i++) {
        result.push(i);
      }

      return result;
    },
  });

Is this possible already? Or are there any missing functionalities in the library to get this to work?

mattiasahlsen commented 4 months ago

This would be a really nice feature, I need this as well