electric-sql/electric

Idea: lib which converts SQL queries to TS Shape Definitions

Chiusa

#2392 aperta il 28 feb 2025

 (1 commento) (1 reazione) (0 assegnatari)Elixir (329 fork)batch import
feature requestgood first issuehelp wanted

Metriche repository

Star
 (10.155 stelle)
Metriche merge PR
 (Merge medio 3g 16h) (149 PR mergiate in 30 g)

Descrizione

Tweet: https://x.com/harrysolovay/status/1895146186916450808

Quick and dirty Claude prototype: https://claude.site/artifacts/eff6626c-ef98-4873-a7b0-8636463797c7

The idea is that:

// userShape
select * from users;

// latestProjectsShape
select id, title, status, description, created_at, updated_at 
from projects 
where created_at > now() - interval '1 month';

Becomes:

/**
 * Shape function for userShape
 * Original SQL: select * from users;
 * @param url Base URL for the ShapeStream
 * @returns ShapeStreamOptions
 */
export const userShape = (url: string): ShapeStreamOptions => { 
  return { 
    url, 
    params: { 
      table: 'users'
    },
    subscribe: true
  }
}
/**
 * Shape function for latestProjectsShape
 * Original SQL: select id, title, status, description, created_at, updated_at  from projects  where created_at > now() - interval '1 month';
 * @param url Base URL for the ShapeStream
 * @returns ShapeStreamOptions
 */
export const latestProjectsShape = (url: string): ShapeStreamOptions => { 
  return { 
    url, 
    params: { 
      table: 'projects',
      where: 'created_at > now() - interval \'1 month\'',
      columns: ['id', 'title', 'status', 'description', 'created_at', 'updated_at']
    },
    subscribe: true
  }
}

It could also be used as a TS lib in a build script so you loop over a bunch of e.g. Drizzle or Prisma queries (which are setup to output the raw SQL).

We could package this as a lib & CLI that people could use in their projects.

Guida contributor