udb 0.4.22

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! main.rs split — scaffold (Phase H).
use super::*;

/// Emit a minimal project scaffold to the current directory (or UDB_INIT_DIR).
pub(crate) fn emit_init_project_scaffold() {
    let dir = env::var("UDB_INIT_DIR").unwrap_or_else(|_| ".".to_string());
    for (rel_path, content) in scaffold_files() {
        let path = format!("{dir}/{rel_path}");
        let parent = std::path::Path::new(&path).parent().unwrap();
        if let Err(e) = fs::create_dir_all(parent) {
            eprintln!("could not create directory {}: {e}", parent.display());
            continue;
        }
        if std::path::Path::new(&path).exists() {
            eprintln!("skipping {path} (already exists)");
            continue;
        }
        match fs::write(&path, content) {
            Ok(()) => eprintln!("created {path}"),
            Err(e) => eprintln!("failed to write {path}: {e}"),
        }
    }
    eprintln!("\nProject scaffold created.");
    eprintln!("{}", migration_to_orm_next_steps());
}

/// Operator next-steps that surface the existing migration pipeline and link it
/// to ORM model generation (master-plan 10.5). The scaffold deliberately does
/// not reimplement migration or codegen — it points at `udb plan` /
/// `udb sync-migrations` (the migration pipeline) and `udb orm scaffold` (which
/// reuses the SDK generation machinery) so a fresh project goes
/// schema → migration → typed models with the shipped commands.
pub(crate) fn migration_to_orm_next_steps() -> String {
    [
        "Next steps (migration → ORM models):",
        "  1. udb plan                              # preview the migration plan from your proto",
        "  2. udb sync-migrations                   # write db_ops/migrations artifacts (proto is source of truth)",
        "  3. udb system-ddl | psql $DATABASE_URL   # apply the schema to the database",
        "  4. udb orm scaffold --lang <lang> [--entity <pkg.Message>]",
        "                                           # generate typed entity/repository models (reuses `udb sdk generate`)",
        "See docs/orm-scaffold.md for the full migrate-plan/apply → model-generation workflow.",
    ]
    .join("\n")
}

/// The scaffold's `(relative_path, file_contents)` pairs. Extracted from the
/// emitter so the generated example clients can be string-/compile-checked in
/// tests and by the CI scaffold-compile gate (see `scripts/check-scaffold-compiles.sh`).
pub(crate) fn scaffold_files() -> Vec<(&'static str, &'static str)> {
    let proto_sample = r#"syntax = "proto3";
package myapp.v1;

import "udb/core/common/v1/db.proto";
import "udb/core/common/v1/security.proto";

message User {
  option (udb.core.common.v1.pg_table) = {
    table_name: "users"
    schema_name: "app"
    is_table: true
    enable_rls: true
  };

  option (udb.core.common.v1.db_table_security) = {
    tenant_column: "tenant_id"
    tenant_isolation_mode: "tenant"
  };

  string id = 1 [(udb.core.common.v1.pg_column) = {
    column_name: "id"
    sql_type: "UUID"
    primary_key: true
    not_null: true
  }];
  string tenant_id = 2 [(udb.core.common.v1.pg_column) = {
    column_name: "tenant_id"
    sql_type: "UUID"
    not_null: true
  }];
  string email = 3 [
    (udb.core.common.v1.pg_column) = {
      column_name: "email"
      sql_type: "TEXT"
      encrypted: true
    },
    (udb.core.common.v1.pii) = true,
    (udb.core.common.v1.log_masked) = true,
    (udb.core.common.v1.data_purpose) = "login"
  ];
  string created_at = 4 [(udb.core.common.v1.pg_column) = {
    column_name: "created_at"
    sql_type: "TIMESTAMPTZ"
    not_null: true
    default_value: "now()"
  }];
}
"#;
    let config_template = r#"# configs/database.yaml — UDB runtime configuration template
# Copy and customize this file. Expand env-vars with ${VAR} syntax.

tier1_postgres:
  primary:
    dsn: "${DATABASE_URL}"
    max_connections: 50

tier2_redis:
  session:
    dsn: "${REDIS_URL}"

tier3_qdrant:
  embeddings:
    url: "${QDRANT_URL}"

tier4_minio:
  artifacts:
    endpoint: "${S3_ENDPOINT}"
    access_key: "${AWS_ACCESS_KEY_ID}"
    secret_key: "${AWS_SECRET_ACCESS_KEY}"
    region: "us-east-1"
"#;
    let docker_compose = r#"# docker-compose.udb.yml — Local UDB development environment
version: "3.8"
services:
  postgres:
    image: postgres:16
    environment:
      POSTGRES_USER: udb
      POSTGRES_PASSWORD: udb
      POSTGRES_DB: udb
    ports: ["5432:5432"]

  redis:
    image: redis:7
    ports: ["6379:6379"]

  qdrant:
    image: qdrant/qdrant:latest
    ports: ["6333:6333"]

  minio:
    image: minio/minio:latest
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    ports: ["9000:9000", "9001:9001"]

  kafka:
    image: confluentinc/cp-kafka:7.6.0
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
    ports: ["9092:9092"]
    depends_on: [zookeeper]

  zookeeper:
    image: confluentinc/cp-zookeeper:7.6.0
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
"#;
    let go_client = r#"// examples/go/client.go — minimal UDB gRPC client (Go)
// go get google.golang.org/grpc github.com/fahara02/udb/sdk/go/gen/udb/entity/v1 github.com/fahara02/udb/sdk/go/gen/udb/services/v1
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"

	entityv1 "github.com/fahara02/udb/sdk/go/gen/udb/entity/v1"
	servicesv1 "github.com/fahara02/udb/sdk/go/gen/udb/services/v1"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	conn, err := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("dial: %v", err)
	}
	defer conn.Close()
	c := servicesv1.NewDataBrokerClient(conn)
	resp, err := c.GetHealthReport(context.Background(), &entityv1.HealthReportRequest{
		Context: &entityv1.RequestContext{Purpose: "health", ServiceIdentity: "example"},
		WithProbes: false,
	})
	if err != nil {
		log.Fatalf("GetHealthReport: %v", err)
	}
	b, _ := json.MarshalIndent(resp, "", "  ")
	fmt.Println(string(b))
}
"#;
    let python_client = r#"# examples/python/client.py — minimal UDB gRPC client (Python)
# pip install grpcio grpcio-tools
import grpc, json, sys
sys.path.insert(0, "gen/python")
from google.protobuf.json_format import MessageToDict
from udb.entity.v1 import types_pb2
from udb.services.v1 import data_broker_pb2_grpc

def main():
    channel = grpc.insecure_channel("localhost:50051")
    stub = data_broker_pb2_grpc.DataBrokerStub(channel)
    resp = stub.GetHealthReport(types_pb2.HealthReportRequest(
        context=types_pb2.RequestContext(purpose="health", service_identity="example"),
        with_probes=False,
    ))
    print(json.dumps(MessageToDict(resp), indent=2))

if __name__ == "__main__":
    main()
"#;
    let typescript_client = r#"// examples/typescript/client.ts — minimal UDB gRPC client (TypeScript)
// npm install @grpc/grpc-js @grpc/proto-loader
import * as grpc from "@grpc/grpc-js";
import * as protoLoader from "@grpc/proto-loader";
import path from "path";

const PROTO_PATH = path.resolve(__dirname, "../../proto/udb/services/v1/data_broker.proto");
const def = protoLoader.loadSync(PROTO_PATH, { keepCase: true, longs: String });
const udbProto = grpc.loadPackageDefinition(def) as any;
const client = new udbProto.udb.services.v1.DataBroker(
  "localhost:50051",
  grpc.credentials.createInsecure()
);

client.GetHealthReport(
  { context: { purpose: "health", service_identity: "example" }, with_probes: false },
  (err: Error | null, resp: unknown) => {
    if (err) { console.error(err); process.exit(1); }
    console.log(JSON.stringify(resp, null, 2));
  }
);
"#;
    let csharp_client = r#"// examples/csharp/Client.cs — minimal UDB gRPC client (C#)
// dotnet add package Grpc.Net.Client Google.Protobuf Grpc.Tools
using Grpc.Net.Client;
using Udb.Entity.V1;
using Udb.Services.V1;

using var channel = GrpcChannel.ForAddress("http://localhost:50051");
var client = new DataBroker.DataBrokerClient(channel);
var resp = await client.GetHealthReportAsync(new HealthReportRequest {
    Context = new RequestContext { Purpose = "health", ServiceIdentity = "example" },
    WithProbes = false
});
Console.WriteLine(resp);
"#;
    let java_client = r#"// examples/java/Client.java — minimal UDB gRPC client (Java)
// mvn dependency: dev.udb:udb-java-client
import com.udb.entity.v1.HealthReportRequest;
import com.udb.entity.v1.RequestContext;
import com.udb.services.v1.DataBrokerGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public final class Client {
  private Client() {}

  public static void main(String[] args) {
    ManagedChannel channel = ManagedChannelBuilder
        .forAddress("localhost", 50051)
        .usePlaintext()
        .build();
    try {
      DataBrokerGrpc.DataBrokerBlockingStub client = DataBrokerGrpc.newBlockingStub(channel);
      HealthReportRequest req = HealthReportRequest.newBuilder()
          .setContext(RequestContext.newBuilder()
              .setPurpose("health")
              .setServiceIdentity("example")
              .build())
          .setWithProbes(false)
          .build();
      System.out.println(client.getHealthReport(req));
    } finally {
      channel.shutdownNow();
    }
  }
}
"#;
    let php_client = r#"<?php
// examples/php/client.php — minimal UDB gRPC client (PHP)
// composer require fahara02/udb-laravel

require __DIR__ . '/vendor/autoload.php';

use Grpc\ChannelCredentials;
use Udb\Entity\V1\HealthReportRequest;
use Udb\Entity\V1\RequestContext;
use Udb\Services\V1\DataBrokerClient;

$client = new DataBrokerClient('localhost:50051', [
    'credentials' => ChannelCredentials::createInsecure(),
]);

$ctx = (new RequestContext())
    ->setPurpose('health')
    ->setServiceIdentity('example');
$req = (new HealthReportRequest())
    ->setContext($ctx)
    ->setWithProbes(false);

[$resp, $status] = $client->GetHealthReport($req)->wait();
if ($status->code !== \Grpc\STATUS_OK) {
    fwrite(STDERR, "GetHealthReport failed: {$status->details}\n");
    exit(1);
}

echo $resp->serializeToJsonString(), PHP_EOL;
"#;
    vec![
        ("proto/app/v1/user.proto", proto_sample),
        ("configs/database.yaml", config_template),
        ("docker-compose.udb.yml", docker_compose),
        ("examples/go/client.go", go_client),
        ("examples/python/client.py", python_client),
        ("examples/typescript/client.ts", typescript_client),
        ("examples/csharp/Client.cs", csharp_client),
        ("examples/java/Client.java", java_client),
        ("examples/php/client.php", php_client),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;

    fn file(rel: &str) -> &'static str {
        scaffold_files()
            .into_iter()
            .find(|(p, _)| *p == rel)
            .unwrap_or_else(|| panic!("scaffold missing {rel}"))
            .1
    }

    #[test]
    fn go_example_uses_the_real_module_path_and_grpc_newclient() {
        let go = file("examples/go/client.go");
        // Real published module path (was the fictional github.com/udb-project/...).
        assert!(
            go.contains("github.com/fahara02/udb/sdk/go/gen/udb/entity/v1"),
            "Go example must import the real entity gen path"
        );
        assert!(
            go.contains("github.com/fahara02/udb/sdk/go/gen/udb/services/v1"),
            "Go example must import the real services gen path"
        );
        // The fictional org must not reappear.
        assert!(
            !go.contains("github.com/udb-project/"),
            "Go example still references the fictional udb-project org"
        );
        // grpc.Dial is deprecated; the example must use grpc.NewClient.
        assert!(
            go.contains("grpc.NewClient("),
            "Go example must use grpc.NewClient"
        );
        assert!(
            !go.contains("grpc.Dial("),
            "Go example must not use the deprecated grpc.Dial"
        );
    }

    #[test]
    fn typescript_example_loads_the_data_broker_proto() {
        let ts = file("examples/typescript/client.ts");
        assert!(
            ts.contains("@grpc/grpc-js"),
            "TS example must import @grpc/grpc-js"
        );
        assert!(
            ts.contains("data_broker.proto"),
            "TS example must load the DataBroker proto"
        );
    }

    #[test]
    fn scaffold_emits_examples_for_all_six_sdks() {
        let files = scaffold_files()
            .into_iter()
            .map(|(path, _)| path)
            .collect::<std::collections::BTreeSet<_>>();
        for expected in [
            "examples/go/client.go",
            "examples/python/client.py",
            "examples/typescript/client.ts",
            "examples/csharp/Client.cs",
            "examples/java/Client.java",
            "examples/php/client.php",
        ] {
            assert!(files.contains(expected), "scaffold missing {expected}");
        }
    }

    #[test]
    fn scaffold_proto_catalogs_with_default_parser_options() {
        let root = std::env::temp_dir().join(format!(
            "udb_scaffold_quickstart_{}_{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("system clock should be after epoch")
                .as_nanos()
        ));
        for (rel, content) in scaffold_files()
            .into_iter()
            .filter(|(rel, _)| rel.ends_with(".proto"))
        {
            let path = root.join(rel);
            std::fs::create_dir_all(path.parent().expect("scaffold proto has parent"))
                .expect("create scaffold proto parent");
            std::fs::write(&path, content).expect("write scaffold proto");
        }

        let schemas = udb::parse_directory(root.join("proto"), &udb::ParserConfig::default())
            .expect("default parser should read scaffold annotations");
        let _ = std::fs::remove_dir_all(&root);

        let user = schemas
            .iter()
            .find(|schema| schema.message_name == "User")
            .expect("scaffold should yield a User schema");
        assert_eq!(user.table_name, "users");
        assert_eq!(user.schema_name, "app");
        assert!(user.is_table);
        assert_eq!(user.columns.len(), 4);
        assert!(
            user.columns
                .iter()
                .any(|column| column.column_name == "email" && column.security.is_pii),
            "scaffold email field should retain scalar security metadata"
        );
    }
}