squint

yogthos 2024-11-18T17:34:40.541339Z

has anybody tried using postgres from squint?

borkdude 2024-11-18T17:35:27.008319Z

why would it be different from squint than any other JS language?

yogthos 2024-11-18T17:43:04.143069Z

It shouldn't be, but I'm having trouble translating this bit

const { Pool } = require('pg');
const pool = new Pool({
  user: 'postgres',
  host: 'localhost',
  database: 'postgres',
  password: 'secret',
  port: 5432
});
I tried
(ns foo
  (:require
   ["pg" :as pg :refer [Pool]]))

(def pool
  (Pool.
   {:user "postgres"
    :host "localhost"
    :port 5432
    :database "postgres"
    :password "secret"}))

yogthos 2024-11-18T17:43:54.854259Z

js version is happy, but I get globalThis.foo.Pool is not a constructor in squint nrepl

yogthos 2024-11-18T17:44:14.946629Z

am I just doing something silly here?

yogthos 2024-11-18T17:45:46.679779Z

pg looks like it's an object and I can conosole.log it

yogthos 2024-11-18T17:46:37.142609Z

and it has a Pool: [class BoundPool extends Pool], on it

yogthos 2024-11-18T18:37:23.017439Z

so looking at compiled js, looks like it's require vs import import { Client, Pool } from 'pg';

yogthos 2024-11-18T18:37:48.406409Z

what's the squint synonym for const { Pool } = require('pg');?

yogthos 2024-11-18T18:41:28.750719Z

(ns foo
  (:require
   ["pg" :as pg]))

(def Pool pg.Pool)

compiles to this

import * as pg from 'pg';
var Pool = pg.Pool;
but the pool is nil

borkdude 2024-11-18T18:42:43.416849Z

Try adding $default to the lib string

yogthos 2024-11-18T18:43:33.448159Z

hmm no dice ["pg$default" :as pg :refer [Pool]]

borkdude 2024-11-18T18:43:42.692709Z

Or try console log on pg and check whatever is printed there

yogthos 2024-11-18T18:43:43.540099Z

but there is default in the output

yogthos 2024-11-18T18:44:08.014459Z

this is what I get with pg

[Module: null prototype] {
  default: PG {
    defaults: {
      host: 'localhost',
      user: 'yogthos',
      database: undefined,
      password: null,
      connectionString: undefined,
      port: 5432,
      rows: 0,
      binary: false,
      max: 10,
      idleTimeoutMillis: 30000,
      client_encoding: '',
      ssl: false,
      application_name: undefined,
      fallback_application_name: undefined,
      options: undefined,
      parseInputDatesAsUTC: false,
      statement_timeout: false,
      lock_timeout: false,
      idle_in_transaction_session_timeout: false,
      query_timeout: false,
      connect_timeout: 0,
      keepalives: 1,
      keepalives_idle: 0,
      parseInt8: [Setter]
    },
    Client: [class Client extends EventEmitter] {
      Query: [class Query extends EventEmitter]
    },
    Query: [class Query extends EventEmitter],
    Pool: [class BoundPool extends Pool],
    _pools: [],
    Connection: [class Connection extends EventEmitter],
    types: {
      getTypeParser: [Function: getTypeParser],
      setTypeParser: [Function: setTypeParser],
      arrayParser: [Object],
      builtins: [Object]
    },
    DatabaseError: [class DatabaseError extends Error],
    escapeIdentifier: [Function: escapeIdentifier],
    escapeLiteral: [Function: escapeLiteral]
  }
}

borkdude 2024-11-18T18:46:25.188669Z

I’ll try in a bit

yogthos 2024-11-18T18:46:41.307809Z

thanks

yogthos 2024-11-18T18:47:40.176689Z

I can make a pr with an example once we get it working to document the magic 🙂

borkdude 2024-11-18T20:28:28.331029Z

$ npx squint --show -e '(ns foo (:require ["pg$default" :as pg])) (js/console.log pg.Client)'
import * as squint_core from 'squint-cljs/core.js';
import pg from 'pg';
console.log(pg.Client);

[class Client extends EventEmitter] {
  Query: [class Query extends EventEmitter]
}

borkdude 2024-11-18T20:28:59.213049Z

$ npx squint --show -e '(ns foo (:require ["pg$default" :as pg])) (js/console.log pg.Pool)'
import * as squint_core from 'squint-cljs/core.js';
import pg from 'pg';
console.log(pg.Pool);

[class BoundPool extends Pool]

borkdude 2024-11-18T20:29:30.751469Z

I think you're hitting a bug in squint when doing both $default and :refer

borkdude 2024-11-18T20:30:01.537639Z

since it looks like :refer is still destructuring on the object that wraps default

borkdude 2024-11-18T20:30:17.698169Z

$ npx squint --show -e '(ns foo (:require ["pg$default" :as pg :refer [Pool]])) (js/console.log Pool)'
import * as squint_core from 'squint-cljs/core.js';
import pg from 'pg';
import { Pool } from 'pg';
console.log(Pool);

file:///private/tmp/dude/.tmp4ras1Q/squint.mjs:3
import { Pool } from 'pg';

yogthos 2024-11-18T20:30:20.840259Z

ah yeah makes sense

borkdude 2024-11-18T20:30:26.337609Z

feel free to post an issue about this, will fix soon

👍 1
borkdude 2024-11-18T20:30:37.511859Z

for now just use (def Pool pg.Pool) or so

yogthos 2024-11-18T20:30:53.699319Z

sounds good, I'll add an issue to track it

yogthos 2024-11-18T20:35:51.067329Z

and can confirm stuff is working with pg.Pool

yogthos 2024-11-18T20:37:23.206449Z

@borkdude oh and another quick question, is there a hiccup parser accessible outside #jsx that could be used to return html server side?

borkdude 2024-11-18T20:38:14.222439Z

you can use the #html tag to produce html strings in the same manner as #jsx

yogthos 2024-11-18T20:39:01.292039Z

ah excellent

yogthos 2024-11-18T20:39:05.724009Z

exactly what I was looking for

yogthos 2024-11-18T20:39:30.189649Z

I should have a useful example of a minimal server talking to pg and producing html I can add in a bit here

yogthos 2024-11-18T20:40:02.468979Z

for a bit of context, I'm experimenting with porting aws lambdas I have running on bb to squint to see how resource usage compares

😮 1
borkdude 2024-11-18T21:04:43.779769Z

nice :)

yogthos 2024-11-18T21:06:30.072209Z

so far it's going well, my utter lack of familiarity with the js ecosystem is the main stumbling block 🙂

borkdude 2024-11-18T21:07:03.333779Z

chatgpt to the rescue? ;)

yogthos 2024-11-18T21:07:24.190409Z

I have been leaning on https://www.phind.com/ quite a bit 🙂

yogthos 2024-11-18T21:08:07.981169Z

llms work pretty well for this sort of thing where I know roughly what I'm trying to do, but not the exact syntax

borkdude 2024-11-18T21:09:27.585139Z

yeah indeed. good for rubber ducking

💯 1