electric-sql/electric

Idea: lib which converts SQL queries to TS Shape Definitions

Fechada

#2.392 aberto em 28 de fev. de 2025

 (1 comentário) (1 reação) (0 responsável)Elixir (329 forks)batch import
feature requestgood first issuehelp wanted

Métricas do repositório

Stars
 (10.155 estrelas)
Métricas de merge de PR
 (Mesclagem média 3d 16h) (149 fundiu PRs em 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.

Guia do colaborador