Fork me on GitHub
#squint
<
2024-04-11
>
tatut14:04:09

any thoughts on using squint as a language inside PostgreSQL with plv8? details in thread

tatut14:04:26

for example, I got the following code

(doseq [a (range 42 420)
        b [10 20 30 40]
        :let [ab (* a b)]]
  (js/plv8.return_next
   {:i ab
    :t (str a " * " b " = " ab)}))
to work as a set returning function

tatut14:04:46

I compiled the code with npx squint compile … + combined it with core.js (removing any export references or squint_core. references from the code… and it worked fine

borkdude15:04:14

for optimized usage you may want to bundle the code with:

npx squint --no-run --show -e '(doseq [i (range 10) j (range 10)] [i j])' | npx esbuild --minify --bundle

👍 1
borkdude15:04:33

This gives something like:

(()=>{function y(e){if(e==null||e instanceof Function)return e;let t=typeof e;return t==="string"?(n,r)=>p(n,e,r):t==="object"?(n,r)=>p(e,n,r):e}globalThis.toFn=y;var _=1,x=2,a=3,g=4,h=5,m=6;function d(e){return e.constructor===Object}function w(e){if(e!=null){if(d(e))return a;if(e instanceof Map)return _;if(e instanceof Set)return h;if(e instanceof c)return g;if(Array.isArray(e))return x;if(e instanceof o)return m;if(e instanceof Object)return a}}function p(e,t,n=void 0){if(e==null)return n;let r;if(d(e))return r=e[t],r===void 0?n:r;switch(w(e)){case h:e.has(t)&&(r=t);break;case _:r=e.get(t);break;case x:r=e[t];break;default:if(e.get instanceof Function)try{r=e.get(t);break}catch{}r=e[t];break}return r!==void 0?r:n}function A(e){return typeof e=="string"||e===null||e===void 0||e instanceof Object&&Symbol.iterator in e}function s(e){return e==null?[]:A(e)?e:Object.entries(e)}var b=Symbol("Iterable");var v=!1;var o=class{constructor(t){this.gen=t,this.usages=0}[Symbol.iterator](){if(this.usages++,this.usages>=2&&v)try{throw new Error}catch(t){console.warn("Re-use of lazy value",t.stack)}return this.gen()}};o.prototype[b]=!0;function M(e){return new o(e)}function l(e,t,n){return M(function*(){let r=e,u=t,i=n;t===void 0&&(r=0,u=e);let f=r||0;for(i=n||1;u===void 0||f<u;)yield f,f+=i})}var c=class extends Array{constructor(...t){super(),this.push(...t)}};var E=Symbol("meta");console.log("dude");for(let e of s(l(10)))(function(){let t=e;for(let n of s(l(10))){let r=n}return null})();dude;})();
which is a standalone version of the whole thing

tatut15:04:08

is it possible to do a toplevel return in squint? the plv8 seems to use that

borkdude15:04:50

can you give an example?

tatut15:04:57

CREATE FUNCTION set_of_records() RETURNS SETOF rec AS
$$
    // plv8.return_next() stores records in an internal tuplestore,
    // and return all of them at the end of function.
    plv8.return_next( { "i": 1, "t": "a" } );
    plv8.return_next( { "i": 2, "t": "b" } );

    // You can also return records with an array of JSON.
    return [ { "i": 3, "t": "c" }, { "i": 4, "t": "d" } ];
$$
LANGUAGE plv8;
it looks like it wraps the code in an implicit js function

borkdude15:04:07

this is probably an esbuild setting, let me check

borkdude15:04:20

squint didn't compile this iife, esbuild did

tatut15:04:14

I mean plv8 seems to wrap the body in a js function so that js return works in it

borkdude15:04:54

doseq returns nil

tatut15:04:19

yes, I tried with for as well

tatut15:04:26

and (into [] (for …

tatut15:04:00

it seems the problem is that it needs an explicit js return statement inside plv8

borkdude15:04:04

perhaps just define a function and then append the function call at the end of your js blob?

borkdude15:04:38

npx squint --show -e '(defn foo [] {:hello true})' | npx esbuild --minify --bundle --format=iife --global-name=my_blob
+ append return my_blob.foo()

👍 1
tatut05:04:05

I guess clj libraries don’t work with squint, thinking of honeysql for example

tatut05:04:03

but as macros work, one could make some query helpers

borkdude06:04:09

Perhaps there’s JS libs that are similar that you could also use

tatut06:04:53

perhaps a pre-built SCI with these libraries included could work better, you could write the proc directly in clojure without transpiling to js…

borkdude07:04:09

could also work with cherry I think