GoogleSQL compiler for @effect/sql with Cloud Spanner support.
- π― Type-safe GoogleSQL query compilation
- βοΈ Cloud Spanner type hints for parameters
- π Proper escaping with backticks
- β‘ Built for production use with Effect.ts
pnpm add effect-sql-googlesql @effect/sql effectThis package is typically consumed via effect-sql-spanner, not used directly. It provides the GoogleSQL compiler that translates Effect SQL statements to Spanner-compatible GoogleSQL.
GoogleSQL/Spanner requires explicit type hints for parameters in some contexts:
import * as Compiler from "effect-sql-googlesql/Compiler"
import * as Statement from "@effect/sql/Statement"
// Use paramOf for typed parameters
const jsonData = { user: "alice", role: "admin" }
const fragment = Compiler.paramOf(Compiler.types.json(), jsonData)
// Available types:
Compiler.types.string()
Compiler.types.int64()
Compiler.types.float64()
Compiler.types.bool()
Compiler.types.json()
Compiler.types.bytes()
Compiler.types.timestamp()
Compiler.types.date()
Compiler.types.array(Compiler.types.string()) // Array typesimport { makeCompiler } from "effect-sql-googlesql/Compiler"
const compiler = makeCompiler({
// Optional: transform column names (e.g. camelCase β snake_case)
transformQueryNames: (name) => name
})
// Compile statements manually (rare - usually handled by @effect/sql)
const statement = Statement.make`SELECT * FROM users WHERE id = ${userId}`
const [sql, params] = compiler.compile(statement)import * as Layer from "effect-sql-googlesql/Layer"
import * as Sql from "@effect/sql"
import * as Effect from "effect/Effect"
// Provide compiler via layer
const GoogleSqlCompilerLayer = Layer.layer({
transformQueryNames: (name) => name
})
const program = Effect.gen(function* () {
const sql = yield* Sql.client.Client
// Compiler is used automatically by @effect/sql
const users = yield* sql`SELECT * FROM users WHERE active = ${true}`
return users
})import { nullOf, types } from "effect-sql-googlesql/Compiler"
// Create typed null values
const fragment = Statement.make`INSERT INTO users (data) VALUES (${nullOf(types.json())})`Creates a GoogleSQL compiler instance.
Parameters:
options.transformQueryNames?: (value: string) => string- Optional transformer for identifiers
Returns: Statement.Compiler
Creates a typed parameter for GoogleSQL with explicit Spanner type hints.
Parameters:
type- Spanner type fromtypesobject or string type codevalue- The parameter value
Returns: Statement.Fragment
Creates a typed null parameter.
Parameters:
type- Spanner type fromtypesobject or string type code
Returns: Statement.Fragment
Spanner data type constructors:
types.string() // STRING
types.int64() // INT64
types.float64() // FLOAT64
types.bool() // BOOL
types.json() // JSON
types.bytes() // BYTES
types.timestamp() // TIMESTAMP
types.date() // DATE
types.array(elementType) // ARRAY<T>Creates an Effect Layer that provides the GoogleSQL compiler.
Parameters:
options- Same asmakeCompiler()
Returns: Layer.Layer<never, never, typeof GoogleSqlCompiler>
Default compiler instance (no name transformation).
Type: Statement.Compiler
- Takes Effect SQL statements (using
@effect/sql/Statement) - Compiles to GoogleSQL dialect:
- Escapes identifiers with backticks:
`table_name` - Converts placeholders to
@param1,@param2, etc. - Handles RETURNING β THEN RETURN conversion
- Escapes identifiers with backticks:
- Attaches Spanner type hints as metadata on parameters
- Returns
[sql: string, params: Primitive[]]tuple - Type metadata retrieved via
getParamTypeMetadata(params)
Key differences handled by this compiler:
| Feature | PostgreSQL | GoogleSQL |
|---|---|---|
| Identifier escaping | "identifier" |
`identifier` |
| Parameter placeholders | $1, $2 |
@param1, @param2 |
| RETURNING clause | RETURNING |
THEN RETURN |
| Type hints | Optional | Required in some contexts |
| Array syntax | ARRAY[1,2,3] |
[1,2,3] |
This package is designed to be consumed via effect-sql-spanner, which provides a complete Spanner client with automatic GoogleSQL compilation:
import * as SpannerClient from "effect-sql-spanner/Client"
import * as Effect from "effect/Effect"
const program = Effect.gen(function* () {
const sql = yield* SpannerClient.ClientTag
// GoogleSQL compilation happens automatically
const users = yield* sql`SELECT * FROM users`
return users
})Apache-2.0
Ryan Hunter (@artimath)