Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/pgls_schema_cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ version = "0.0.0"
default = ["db"]
# Enable serde serialization/deserialization for all types (needed for WASM)
serde = []
# Enable schemars JsonSchema derive for schema generation/codegen
schema = ["dep:schemars"]
# Enable database connectivity (sqlx, async-std, tokio, futures-util)
# Disable this feature for WASM builds
db = ["dep:sqlx", "dep:async-std", "dep:futures-util", "dep:tokio"]
Expand All @@ -24,6 +26,7 @@ async-std = { version = "1.12.0", optional = true }
futures-util = { version = "0.3.31", optional = true }
pgls_console.workspace = true
pgls_diagnostics.workspace = true
schemars = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
sqlx = { workspace = true, optional = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/pgls_schema_cache/src/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum ColumnClassKind {
OrdinaryTable,
View,
Expand Down Expand Up @@ -40,6 +41,7 @@ impl From<char> for ColumnClassKind {
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Column {
pub name: String,

Expand Down Expand Up @@ -69,6 +71,7 @@ pub struct Column {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct ForeignKeyReference {
pub schema: Option<String>,
pub table: String,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::PgPool;
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Extension {
pub name: String,
pub schema: Option<String>,
Expand Down
5 changes: 5 additions & 0 deletions crates/pgls_schema_cache/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use sqlx::PgPool;
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum ProcKind {
#[default]
Function,
Expand Down Expand Up @@ -35,6 +36,7 @@ impl From<i8> for ProcKind {

/// `Behavior` describes the characteristics of the function. Is it deterministic? Does it changed due to side effects, and if so, when?
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum Behavior {
/// The function is a pure function (same input leads to same output.)
Immutable,
Expand Down Expand Up @@ -62,6 +64,7 @@ impl From<Option<String>> for Behavior {
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct FunctionArg {
/// `in`, `out`, or `inout`.
pub mode: String,
Expand All @@ -75,6 +78,7 @@ pub struct FunctionArg {
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct FunctionArgs {
pub args: Vec<FunctionArg>,
}
Expand All @@ -88,6 +92,7 @@ impl From<Option<JsonValue>> for FunctionArgs {
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Function {
/// The Id (`oid`).
pub id: i64,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::schema_cache::SchemaCacheItem;
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Index {
pub id: i64,
pub schema: String,
Expand Down
2 changes: 2 additions & 0 deletions crates/pgls_schema_cache/src/policies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum PolicyCommand {
Select,
Insert,
Expand Down Expand Up @@ -58,6 +59,7 @@ impl From<PolicyQueried> for Policy {
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Policy {
pub name: String,
pub table_name: String,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Role {
pub name: String,
pub is_super_user: bool,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/schema_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::versions::Version;
use crate::{Extension, Role, Trigger};

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(default)]
pub struct SchemaCache {
pub schemas: Vec<Schema>,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::PgPool;
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Schema {
pub id: i64,
pub name: String,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::schema_cache::SchemaCacheItem;
use serde::{Deserialize, Serialize};

#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Sequence {
pub id: i64,
pub schema: String,
Expand Down
3 changes: 3 additions & 0 deletions crates/pgls_schema_cache/src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::PgPool;
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum ReplicaIdentity {
#[default]
Default,
Expand All @@ -27,6 +28,7 @@ impl From<String> for ReplicaIdentity {
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum TableKind {
#[default]
Ordinary,
Expand Down Expand Up @@ -55,6 +57,7 @@ impl From<i8> for TableKind {
}

#[derive(Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Table {
pub id: i64,
pub schema: String,
Expand Down
4 changes: 4 additions & 0 deletions crates/pgls_schema_cache/src/triggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::schema_cache::SchemaCacheItem;
use strum::{EnumIter, IntoEnumIterator};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum TriggerAffected {
Row,
Statement,
Expand All @@ -22,6 +23,7 @@ impl From<i16> for TriggerAffected {
}

#[derive(Debug, PartialEq, Eq, EnumIter, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum TriggerEvent {
Insert,
Delete,
Expand Down Expand Up @@ -51,6 +53,7 @@ impl From<i16> for TriggerEvents {
}

#[derive(Debug, PartialEq, Eq, EnumIter, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum TriggerTiming {
Before,
After,
Expand Down Expand Up @@ -92,6 +95,7 @@ pub struct TriggerQueried {
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Trigger {
pub name: String,
pub table_name: String,
Expand Down
4 changes: 4 additions & 0 deletions crates/pgls_schema_cache/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use sqlx::PgPool;
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct TypeAttributes {
pub attrs: Vec<PostgresTypeAttribute>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PostgresTypeAttribute {
pub name: String,
pub type_id: i64,
Expand All @@ -26,6 +28,7 @@ impl From<Option<JsonValue>> for TypeAttributes {
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Enums {
pub values: Vec<String>,
}
Expand All @@ -39,6 +42,7 @@ impl From<Option<JsonValue>> for Enums {
}

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct PostgresType {
pub id: i64,
pub name: String,
Expand Down
1 change: 1 addition & 0 deletions crates/pgls_schema_cache/src/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::PgPool;
use crate::schema_cache::SchemaCacheItem;

#[derive(Debug, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Version {
pub version: Option<String>,
pub version_num: Option<i64>,
Expand Down
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gen-lint:
cargo run -p xtask_codegen -- analyser
cargo run -p xtask_codegen -- configuration
cargo run -p xtask_codegen -- bindings
cargo run -p xtask_codegen -- schema-types
cargo run -p xtask_codegen -- splinter
cargo run -p xtask_codegen -- pglinter
cargo run -p rules_check
Expand Down Expand Up @@ -113,6 +114,7 @@ ready:
cargo run -p xtask_codegen -- configuration
cargo run -p docs_codegen
cargo run -p xtask_codegen -- bindings
cargo run -p xtask_codegen -- schema-types
cargo sqlx prepare --workspace
just format # format after codegen, so we don't have staged changes before lint-fix
just lint-fix
Expand Down
9 changes: 6 additions & 3 deletions packages/@postgres-language-server/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,23 @@ const hover = workspace.hover("/query.sql", 14); // position over "users"

### With Schema

For schema-aware completions and hover, provide your database schema:
For schema-aware completions and hover, provide your database schema.
TypeScript types exported as `SchemaCache`:

```typescript
import type { SchemaCache } from '@postgres-language-server/wasm';

const workspace = await createWorkspace();

// Set schema (matches pgls_schema_cache format)
// Set schema
workspace.setSchema(JSON.stringify({
schemas: [{ id: 1, name: 'public', owner: 'postgres', ... }],
tables: [{ id: 1, schema: 'public', name: 'users', ... }],
columns: [{ name: 'id', table_name: 'users', type_name: 'integer', ... }],
functions: [],
types: [],
// ...
}));
} satisfies SchemaCache));

workspace.insertFile('/query.sql', 'SELECT * FROM ');
const completions = workspace.complete('/query.sql', 14);
Expand Down
5 changes: 3 additions & 2 deletions packages/@postgres-language-server/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
"scripts": {
"build": "bun run build:wasm && bun run build:ts",
"build:wasm": "../../../crates/pgls_wasm/build-wasm.sh --release && mkdir -p wasm && cp ../../../crates/pgls_wasm/dist/pgls.js ../../../crates/pgls_wasm/dist/pgls.wasm ./wasm/",
"build:ts": "tsc -p tsconfig.build.json && cp wasm/pgls.wasm wasm/pgls.js dist/",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir dist",
"gen:schema-types": "cargo run -p xtask_codegen -- schema-types",
"build:ts": "bun run gen:schema-types && tsc -p tsconfig.build.json && cp src/schema-cache.d.ts dist/schema-cache.d.ts && cp wasm/pgls.wasm wasm/pgls.js dist/",
"build:types": "bun run gen:schema-types && tsc --declaration --emitDeclarationOnly --outDir dist && cp src/schema-cache.d.ts dist/schema-cache.d.ts",
"test": "bun test",
"test:e2e": "playwright test",
"test:e2e:ui": "playwright test --ui",
Expand Down
Loading
Loading