rngo

Tutorial

Non-Trivial Schemas

Add the following DDL file at db/3-posts.sql:

CREATE TABLE posts (
    id bigserial PRIMARY KEY,
    author_id bigint REFERENCES users(id) NOT NULL,
    title varchar(512) NOT NULL,
    body text NOT NULL,
    created_at timestamptz NOT NULL DEFAULT now(),
    published_at timestamptz
);

First run docker compose down -v and docker compose up -d to pick up the posts table.

Then run rngo infer again - you'll see a corresponding posts stream in .rngo/config.yml:

streams:
  ...
  posts:
    systems:
      db: {}
    schema:
      type: object
      properties:
        id:
          type: integer
          rngo:
            value: (streams.posts.last.id ?? 0) + 1
        author_id:
          type: integer
          rngo:
            value: streams.users.random.id
        title:
          type: string
        body:
          type: string
        created_at:
          type: string
          format: date-time
        published_at:
          type: string
          format: date-time
      required:
        - id
        - author_id
        - title
        - body
        - published_at

Much of what we configured manually for the users stream is added automatically to the posts stream by rngo infer.

Notably, the author_id column references the users stream due to the foreign key constraint in Postgres. In the expression, stream.users references the collection of users as it exists at that point in the simulation. As a result, the simulation will never produce a post that was created before its author.

Previous
Scenarios