electric-sql/electric

Idea: lib which converts SQL queries to TS Shape Definitions

Closed

#2,392 opened on Feb 28, 2025

 (1 comment) (1 reaction) (0 assignees)Elixir (329 forks)batch import
feature requestgood first issuehelp wanted

Repository metrics

Stars
 (10,155 stars)
PR merge metrics
 (Avg merge 3d 16h) (149 merged PRs in 30d)

Description

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.

Contributor guide