Skip to main content
NetScriptdocs Search docs ⌘K v0.0.9
Browse documentation

Workspace data

In this chapter you give the workspace a second, isolated database with its own Prisma schema, migration history and generated client, separate from the primary Postgres that holds the auth tables. One netscript db add scaffolds the datasource and joins it to the Aspire graph; after one migrate-and-generate pass you have a typed Prisma client scoped to that datasource.

  1. 1 · Scaffold
  2. 2 · Auth
  3. 3 · Workspace data
  4. 4 · Provision job
  5. 5 · Route authz
  6. 6 · Deploy

What you will build

A second Postgres datasource named workspace, scaffolded by netscript db add, with Member and Workspace models, its own migration and its own typed Prisma client. At the end you import that client from database/workspace/schema/.generated/client.server.ts and query workspace records, independently of the primary database and the KV-backed auth sessions.

Before you begin

You need the auth layer from chapter 2 and aspire start running. The second database runs as its own container in the Aspire graph, so Docker must be up too:

Shell
# In my-workspace/, with `aspire start` up in another terminal
docker info                # Docker engine is running

Step 1 — Scaffold the second database

From the workspace root, run netscript db add <engine>. The --name flag sets the config key the datasource is registered under. Use workspace so it doesn't collide with the primary postgres:

Shell
# Add a second Postgres for workspace records. Registered under
# NetScript.Databases.workspace, scaffolded into database/workspace/.
netscript db add postgres --name workspace

db add does four things in one pass:

  • scaffolds a workspace at database/workspace/ with its own schema/schema.prisma, prisma.config.ts and scripts/;
  • registers the datasource in appsettings.json under NetScript.Databases.workspace;
  • adds it as a project member;
  • regenerates the Aspire config so the new container joins the resource graph.

It does not run a migration or generate a client: schema.prisma ships a starter schema and there is no .generated/ directory yet. You run the migration yourself in Step 4.

Step 2 — Restart Aspire so the container joins the graph

db add regenerated the Aspire config, so restart the AppHost to provision the new container:

Shell
cd aspire
aspire start

Open the dashboard at the URL Aspire prints and confirm the new workspace resource goes green next to postgres and redis.

Step 3 — Define the workspace models

Edit the second datasource's schema. A Workspace row is a team; a Member row links a signed-in user, by their auth subject, to a workspace:

database/workspace/schema/schema.prismaPrisma
// database/workspace/schema/schema.prisma — add these models
model Workspace {
  id        String   @id @default(cuid())
  name      String
  createdAt DateTime @default(now())
  members   Member[]
}

model Member {
  id          String    @id @default(cuid())
  // The auth Principal.subject from chapter 2 — the stable user identifier.
  subject     String
  role        String    @default("member")
  workspace   Workspace @relation(fields: [workspaceId], references: [id])
  workspaceId String
  createdAt   DateTime  @default(now())

  @@unique([workspaceId, subject])
}

The subject column links back to auth: it stores the Principal.subject the auth backend resolves for a signed-in user. A member is "this auth identity, in this workspace".

Step 4 — Migrate and generate the second datasource

Every netscript db operation takes a --db <target> flag, where the target is a config key, a database name or all. Point each command at the workspace datasource, from the workspace root, with aspire start up:

Shell
1 · initShell
# Create + apply the first migration for the SECOND datasource only.
netscript db init --db workspace --name init

2 · generateShell
# Generate the Deno-runtime Prisma client + zod schemas into
# database/workspace/schema/.generated for the workspace datasource.
netscript db generate --db workspace

3 · statusShell
# Confirm the workspace datasource is migrated and in sync.
netscript db status --db workspace

Each datasource keeps its own migration history and generated client. Migrating --db workspace never touches the primary Postgres or the KV-backed auth sessions. Each database migration targets its selected datasource.

Step 5 — Query the workspace client

After db generate --db workspace, the second datasource has its own typed client. Import it like the primary client, from the new path. It is an independent PrismaClient, typed from the Workspace and Member models:

services/workspace/src/db.tsTypeScript
// services/workspace/src/db.ts
// The SECOND datasource generates its OWN client. Import it from its path.
import { PrismaClient as WorkspacePrisma } from '../../database/workspace/schema/.generated/client.server.ts';

export const workspaceDb = new WorkspacePrisma();

// Fully typed off the workspace schema — separate from the primary client.
const teams = await workspaceDb.workspace.findMany({ take: 20 });
console.log(teams.length);

Extend — app-level org scoping

The model above is single-tenant: every Member belongs to a Workspace, but nothing models an organization that owns many workspaces. NetScript doesn't ship orgs, tenants or RBAC roles. For multi-tenant scoping, you add it in your own schema and your own queries.

Verify your progress

Confirm the second datasource is migrated and its client generated:

Shell
netscript db status --db workspace
ls database/workspace/schema/.generated/client.server.ts

db status --db workspace reports the datasource migrated and in sync, and the generated client file exists.

  • [ ] netscript db add postgres --name workspace scaffolded database/workspace/.
  • [ ] The workspace resource is green in the Aspire dashboard.
  • [ ] database/workspace/schema/schema.prisma defines Workspace and Member.
  • [ ] netscript db status --db workspace reports migrated and in sync.
  • [ ] The generated client exists at database/workspace/schema/.generated/client.server.ts.

What you built

A second, isolated Postgres datasource with Workspace and Member models, its own migration history and its own typed client, separate from auth and the primary database. Org scoping stays app-level. Next, you add members to a team with a background job that runs off the request path.

Search all documentation

Type a term to search the documentation.