
Product
Introducing Manifest Alerts
Socket now detects supply chain risks in project manifests, starting with missing lockfiles that can make dependency installs non-reproducible.
@prisma-next/sql-contract
Advanced tools
SQL contract types, validators, and IR factories for Prisma Next
SQL contract types, validators, and IR factories for Prisma Next.
This package provides TypeScript type definitions, Arktype validators, and factory functions for constructing SQL contract structures. It is located in the shared plane, making it available to both migration-plane (authoring, emitter) and runtime-plane (lanes, runtime) packages.
SqlContract, SqlStorage, StorageTable, SqlModelStorage, SqlModelFieldStorage, ForeignKeysConfig) that extend framework-level contract typescontractSerializer SPI consumes for runtime-safe contract validationEach StorageColumn in SQL contracts includes both:
nativeType (required): Native database type identifier (e.g., 'int4', 'text', 'vector') - used for database structure verification and migration planningcodecId (required): Codec identifier (e.g., 'pg/int4@1', 'pg/text@1', 'pg/vector@1') - used for query builders and runtime codecsnullable (required): Whether the column is nullabledefault (optional): Uses the shared ColumnDefault type from @prisma-next/contract for db-agnostic defaults (literal or function). Client-generated defaults live in execution.mutations.defaults.Both nativeType and codecId are required to ensure contracts are consumable by both the application (via codec IDs) and the database (via native types). See docs/briefs/Sql-Contract-Native-and-Codec-Types.md for details.
SqlContract, SqlStorage, StorageTable, SqlModelStorage, SqlModelFieldStorage, ForeignKeysConfig, and related typesImport SQL contract types:
import type {
SqlContract,
SqlStorage,
StorageTable,
SqlModelStorage,
ForeignKeysConfig,
} from '@prisma-next/sql-contract/types';
SqlContract includes an optional foreignKeys field of type ForeignKeysConfig that controls whether the planner emits foreign key constraints and their backing indexes:
type ForeignKeysConfig = {
readonly constraints: boolean; // Emit FOREIGN KEY constraints
readonly indexes: boolean; // Emit FK-backing indexes
};
When omitted, defaults to { constraints: true, indexes: true }. See ADR 161 for design rationale.
ForeignKey supports optional onDelete and onUpdate fields of type ReferentialAction:
type ReferentialAction = 'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault';
type ForeignKey = {
readonly columns: readonly string[];
readonly references: ForeignKeyReferences;
readonly name?: string;
readonly onDelete?: ReferentialAction;
readonly onUpdate?: ReferentialAction;
};
When omitted, the database applies its default behavior (Postgres: NO ACTION). See ADR 166 for design rationale.
The fk() factory accepts referential actions via an options object:
import { fk } from '@prisma-next/sql-contract/factories';
// Simple FK (no referential actions)
const simple = fk(['userId'], 'user', ['id']);
// FK with onDelete cascade
const cascading = fk(['userId'], 'user', ['id'], { onDelete: 'cascade' });
// FK with name and both actions
const named = fk(['userId'], 'user', ['id'], {
name: 'post_userId_fkey',
onDelete: 'cascade',
onUpdate: 'noAction',
});
Semantic validation: validateStorageSemantics() rejects setNull when the FK column is NOT NULL (the database would fail at runtime).
Validate contract structures using Arktype validators:
import { validateSqlContractFully, validateStorage, validateModel } from '@prisma-next/sql-contract/validators';
// Validate a complete contract
const contract = validateSqlContractFully<Contract>(contractJson);
// Validate storage structure
const storage = validateStorage(storageJson);
// Validate model structure
const model = validateModel(modelJson);
Validate JSON-emitted contracts with mapping + logic checks via the
target descriptor's contractSerializer SPI:
import postgresTarget from '@prisma-next/target-postgres/control';
const contract = postgresTarget.contractSerializer.deserializeContract(contractJson);
deserializeContract parses the on-disk envelope, hydrates the SQL
Contract IR class hierarchy (SqlStorage → StorageTable → StorageColumn
/ PrimaryKey / …), and validates model-to-storage cross-references in
one pass. End-user app code typically calls the canonical façade instead
(e.g. postgres<Contract>({ contractJson, … })), which threads the same
SPI internally.
Use factory functions to construct contract IR structures in tests:
import { col, table, storage, model, contract, pk, unique, index, fk } from '@prisma-next/sql-contract/factories';
// Create a column (nativeType, codecId, nullable)
const idColumn = col('int4', 'pg/int4@1', false);
// Create a table
const userTable = table(
{
id: col('int4', 'pg/int4@1'),
email: col('text', 'pg/text@1'),
},
{
pk: pk('id'),
uniques: [unique('email')],
indexes: [index('email')],
}
);
// Create storage
const s = storage({ user: userTable });
// Create a model
const userModel = model('user', {
id: { column: 'id' },
email: { column: 'email' },
});
// Create a complete contract
const c = contract({
target: 'postgres',
storageHash: 'sha256:abc123',
storage: s,
models: { User: userModel },
});
./types: TypeScript type definitions./validators: Arktype validators for structural validation./factories: Factory functions for constructing contract IR./pack-types: Shared extension/pack typing helpersflowchart TD
subgraph "SQL Contract Package (Shared Plane)"
TYPES[Type Definitions]
VALIDATORS[Validators]
FACTORIES[IR Factories]
end
subgraph "Migration Plane"
AUTHORING[Authoring]
EMITTER[Emitter]
end
subgraph "Runtime Plane"
LANES[Lanes]
RUNTIME[Runtime]
end
TYPES --> AUTHORING
TYPES --> EMITTER
TYPES --> LANES
TYPES --> RUNTIME
VALIDATORS --> AUTHORING
VALIDATORS --> EMITTER
VALIDATORS --> RUNTIME
FACTORIES --> AUTHORING
FACTORIES --> EMITTER
@prisma-next/contract: Framework-level contract types (ContractBase)arktype: Runtime validation libraryDependents:
@prisma-next/sql-contract-ts: Uses SQL contract types and validators for authoring@prisma-next/sql-contract-emitter: Uses SQL contract types for emission@prisma-next/sql-query: Uses SQL contract types for query building@prisma-next/sql-runtime: Uses SQL contract types for runtime execution@prisma-next/sql-lane: Uses SQL contract types for lane operations@prisma-next/contract: Framework-level contract types (ContractBase)@prisma-next/sql-contract-ts: SQL contract authoring surface (uses this package)@prisma-next/emitter: Contract emission engine (uses validators)FAQs
SQL contract types, validators, and IR factories for Prisma Next
The npm package @prisma-next/sql-contract receives a total of 18,376 weekly downloads. As such, @prisma-next/sql-contract popularity was classified as popular.
We found that @prisma-next/sql-contract demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now detects supply chain risks in project manifests, starting with missing lockfiles that can make dependency installs non-reproducible.

Research
/Security News
The trojanized extensions use TinyGo-compiled WebAssembly and Solana transaction memos to resolve command-and-control infrastructure.

Security News
Anthropic says the directive cited national security concerns over a narrow jailbreak, but offered no specific technical details.