@prisma-next/sql-schema-ir 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +147 -0
- package/dist/exports/types.d.ts +114 -0
- package/dist/exports/types.js +1 -0
- package/dist/exports/types.js.map +1 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# @prisma-next/sql-schema-ir
|
|
2
|
+
|
|
3
|
+
SQL Schema Intermediate Representation (IR) types for Prisma Next.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package defines the core types for the SQL Schema IR, a target-agnostic representation of SQL database schemas. This IR is used for schema verification, migration planning, and other tooling operations within the SQL family.
|
|
8
|
+
|
|
9
|
+
## Purpose
|
|
10
|
+
|
|
11
|
+
- **Provide a canonical in-memory representation** of SQL schemas that is independent of specific database implementations
|
|
12
|
+
- **Decouple schema introspection from verification logic** - adapters produce `SqlSchemaIR`, verification logic consumes it
|
|
13
|
+
- **Enable extensible metadata** via annotations for targets and extension packs
|
|
14
|
+
- **Support future migration planning** by providing a structured representation of schema differences
|
|
15
|
+
|
|
16
|
+
## Responsibilities
|
|
17
|
+
|
|
18
|
+
- **Type Definitions**: Provides core types for SQL Schema IR (`SqlSchemaIR`, `SqlTableIR`, `SqlColumnIR`, etc.)
|
|
19
|
+
- **Shared Plane Package**: Located in the shared plane, making it accessible to both migration-plane and runtime-plane packages
|
|
20
|
+
- **Extensibility**: Supports annotations for targets and extension packs to attach metadata without modifying core IR structure
|
|
21
|
+
- **Type Safety**: Provides TypeScript types for schema representation with proper nullability and constraint modeling
|
|
22
|
+
|
|
23
|
+
## Dependencies
|
|
24
|
+
|
|
25
|
+
- **`@prisma-next/contract`**: For `ContractIR` type (used in `SqlContractIR`)
|
|
26
|
+
|
|
27
|
+
**Dependents:**
|
|
28
|
+
- **Migration Plane**:
|
|
29
|
+
- `@prisma-next/core-control-plane` - Core verification logic
|
|
30
|
+
- `@prisma-next/adapter-postgres` - Postgres introspection
|
|
31
|
+
- `@prisma-next/extension-pgvector` - Extension verification hooks
|
|
32
|
+
- **Runtime Plane** (future):
|
|
33
|
+
- Migration planning logic
|
|
34
|
+
- Schema diff utilities
|
|
35
|
+
|
|
36
|
+
## Types
|
|
37
|
+
|
|
38
|
+
### Core Types
|
|
39
|
+
|
|
40
|
+
- **`SqlSchemaIR`** - Complete database schema representation with tables, extensions, and annotations
|
|
41
|
+
- **`SqlTableIR`** - Table representation with columns, constraints, indexes, and annotations
|
|
42
|
+
- **`SqlColumnIR`** - Column representation with `typeId` (codec ID), `nativeType` (DB-specific type), nullability, and annotations
|
|
43
|
+
- **`SqlForeignKeyIR`** - Foreign key constraint representation
|
|
44
|
+
- **`SqlUniqueIR`** - Unique constraint representation
|
|
45
|
+
- **`SqlIndexIR`** - Index representation
|
|
46
|
+
- **`SqlAnnotations`** - Namespaced extensibility metadata (e.g., `{ pg: { version: '15' } }`)
|
|
47
|
+
|
|
48
|
+
### Key Design Decisions
|
|
49
|
+
|
|
50
|
+
1. **`typeId` vs `nativeType`**: Columns include both a codec ID (`typeId`, e.g., `'pg/int4@1'`) and an optional native database type (`nativeType`, e.g., `'integer'`). The codec ID provides target-agnostic type information, while `nativeType` enables verification of extension-specific types (e.g., `'vector'` for pgvector).
|
|
51
|
+
|
|
52
|
+
2. **Annotations**: All IR types support optional `annotations` for extensibility. This allows targets and extensions to attach metadata without modifying the core IR structure.
|
|
53
|
+
|
|
54
|
+
3. **Shared Plane**: This package is in the **shared plane**, meaning it can be safely imported by both migration-plane (verification, migration planning) and runtime-plane code.
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
### Basic Usage
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import type { SqlSchemaIR, SqlTableIR, SqlColumnIR } from '@prisma-next/sql-schema-ir/types';
|
|
62
|
+
|
|
63
|
+
const schemaIR: SqlSchemaIR = {
|
|
64
|
+
tables: {
|
|
65
|
+
user: {
|
|
66
|
+
name: 'user',
|
|
67
|
+
columns: {
|
|
68
|
+
id: {
|
|
69
|
+
name: 'id',
|
|
70
|
+
typeId: 'pg/int4@1',
|
|
71
|
+
nativeType: 'integer',
|
|
72
|
+
nullable: false,
|
|
73
|
+
},
|
|
74
|
+
email: {
|
|
75
|
+
name: 'email',
|
|
76
|
+
typeId: 'pg/text@1',
|
|
77
|
+
nativeType: 'text',
|
|
78
|
+
nullable: false,
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
primaryKey: { columns: ['id'] },
|
|
82
|
+
foreignKeys: [],
|
|
83
|
+
uniques: [{ columns: ['email'] }],
|
|
84
|
+
indexes: [],
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
extensions: ['vector'],
|
|
88
|
+
annotations: {
|
|
89
|
+
pg: {
|
|
90
|
+
version: '15',
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Schema Introspection
|
|
97
|
+
|
|
98
|
+
Adapters produce `SqlSchemaIR` by querying database catalogs:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
// In Postgres adapter
|
|
102
|
+
import postgresAdapter from '@prisma-next/adapter-postgres/control';
|
|
103
|
+
import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
|
|
104
|
+
|
|
105
|
+
const controlAdapter = postgresAdapter.createControlInstance();
|
|
106
|
+
const schemaIR: SqlSchemaIR = await controlAdapter.introspect(driver, contract);
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Schema Verification
|
|
110
|
+
|
|
111
|
+
The core control plane compares contracts against `SqlSchemaIR`:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
// In core-control-plane
|
|
115
|
+
import { verifyDatabaseSchema } from '@prisma-next/core-control-plane/verify-database-schema';
|
|
116
|
+
import type { SqlSchemaIR } from '@prisma-next/sql-schema-ir/types';
|
|
117
|
+
|
|
118
|
+
const result = await verifyDatabaseSchema({
|
|
119
|
+
driver,
|
|
120
|
+
contractIR,
|
|
121
|
+
schemaIR, // Produced by family.introspectSchema
|
|
122
|
+
family,
|
|
123
|
+
target,
|
|
124
|
+
adapter,
|
|
125
|
+
extensions,
|
|
126
|
+
strict: false,
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Architecture
|
|
131
|
+
|
|
132
|
+
### Package Location
|
|
133
|
+
|
|
134
|
+
- **Domain**: `sql`
|
|
135
|
+
- **Layer**: `core`
|
|
136
|
+
- **Plane**: `shared`
|
|
137
|
+
|
|
138
|
+
This package sits at the core layer in the shared plane, making it accessible to both migration-plane (authoring, tooling, targets) and runtime-plane (lanes, runtime, adapters) packages.
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
## Related Documentation
|
|
142
|
+
|
|
143
|
+
- `docs/briefs/11-schema-ir.md` - Schema IR project brief
|
|
144
|
+
- `docs/briefs/SQL Schema IR and Verification.md` - Detailed design document
|
|
145
|
+
- `packages/framework/core-control-plane/src/actions/verify-database-schema.ts` - Core verification action
|
|
146
|
+
- `packages/targets/postgres-adapter/src/exports/introspect.ts` - Postgres introspection implementation
|
|
147
|
+
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQL Schema IR types for target-agnostic schema representation.
|
|
3
|
+
*
|
|
4
|
+
* These types represent the canonical in-memory representation of SQL schemas
|
|
5
|
+
* for the SQL family, used for verification and future migration planning.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Primary key definition matching ContractIR format.
|
|
9
|
+
* Defined here to avoid circular dependency with sql-contract.
|
|
10
|
+
*/
|
|
11
|
+
type PrimaryKey = {
|
|
12
|
+
readonly columns: readonly string[];
|
|
13
|
+
readonly name?: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Namespaced annotations for extensibility.
|
|
17
|
+
* Each namespace (e.g., 'pg', 'pgvector') owns its annotations.
|
|
18
|
+
*/
|
|
19
|
+
type SqlAnnotations = {
|
|
20
|
+
readonly [namespace: string]: unknown;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* SQL column IR representing a column in a table.
|
|
24
|
+
*/
|
|
25
|
+
type SqlColumnIR = {
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly typeId: string;
|
|
28
|
+
readonly nativeType?: string;
|
|
29
|
+
readonly nullable: boolean;
|
|
30
|
+
readonly annotations?: SqlAnnotations;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* SQL foreign key IR.
|
|
34
|
+
*/
|
|
35
|
+
type SqlForeignKeyIR = {
|
|
36
|
+
readonly columns: readonly string[];
|
|
37
|
+
readonly referencedTable: string;
|
|
38
|
+
readonly referencedColumns: readonly string[];
|
|
39
|
+
readonly name?: string;
|
|
40
|
+
readonly annotations?: SqlAnnotations;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* SQL unique constraint IR.
|
|
44
|
+
*/
|
|
45
|
+
type SqlUniqueIR = {
|
|
46
|
+
readonly columns: readonly string[];
|
|
47
|
+
readonly name?: string;
|
|
48
|
+
readonly annotations?: SqlAnnotations;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* SQL index IR.
|
|
52
|
+
*/
|
|
53
|
+
type SqlIndexIR = {
|
|
54
|
+
readonly columns: readonly string[];
|
|
55
|
+
readonly name?: string;
|
|
56
|
+
readonly unique: boolean;
|
|
57
|
+
readonly annotations?: SqlAnnotations;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* SQL table IR representing a table in the schema.
|
|
61
|
+
* Primary key format matches ContractIR for consistency.
|
|
62
|
+
*/
|
|
63
|
+
type SqlTableIR = {
|
|
64
|
+
readonly name: string;
|
|
65
|
+
readonly columns: Record<string, SqlColumnIR>;
|
|
66
|
+
readonly primaryKey?: PrimaryKey;
|
|
67
|
+
readonly foreignKeys: readonly SqlForeignKeyIR[];
|
|
68
|
+
readonly uniques: readonly SqlUniqueIR[];
|
|
69
|
+
readonly indexes: readonly SqlIndexIR[];
|
|
70
|
+
readonly annotations?: SqlAnnotations;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* SQL Schema IR representing the complete database schema.
|
|
74
|
+
* This is the target-agnostic representation used for verification and migration planning.
|
|
75
|
+
*/
|
|
76
|
+
type SqlSchemaIR = {
|
|
77
|
+
readonly tables: Record<string, SqlTableIR>;
|
|
78
|
+
readonly extensions: readonly string[];
|
|
79
|
+
readonly annotations?: SqlAnnotations;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* SQL type metadata for control-plane and execution-plane type availability and mapping.
|
|
83
|
+
* This abstraction provides a read-only view of type information without encode/decode behavior.
|
|
84
|
+
*/
|
|
85
|
+
interface SqlTypeMetadata {
|
|
86
|
+
/**
|
|
87
|
+
* Namespaced type identifier in format 'namespace/name@version'
|
|
88
|
+
* Examples: 'pg/int4@1', 'pg/text@1', 'pg/timestamptz@1'
|
|
89
|
+
*/
|
|
90
|
+
readonly typeId: string;
|
|
91
|
+
/**
|
|
92
|
+
* Contract scalar type IDs that this type can handle.
|
|
93
|
+
* Examples: ['text'], ['int4', 'float8'], ['timestamp', 'timestamptz']
|
|
94
|
+
*/
|
|
95
|
+
readonly targetTypes: readonly string[];
|
|
96
|
+
/**
|
|
97
|
+
* Native database type name (target-specific).
|
|
98
|
+
* Examples: 'integer', 'text', 'character varying', 'timestamp with time zone'
|
|
99
|
+
* This is optional because not all types have a native database representation.
|
|
100
|
+
*/
|
|
101
|
+
readonly nativeType?: string;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Registry interface for SQL type metadata.
|
|
105
|
+
* Provides read-only iteration over type metadata entries.
|
|
106
|
+
*/
|
|
107
|
+
interface SqlTypeMetadataRegistry {
|
|
108
|
+
/**
|
|
109
|
+
* Returns an iterator over all type metadata entries.
|
|
110
|
+
*/
|
|
111
|
+
values(): IterableIterator<SqlTypeMetadata>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type { PrimaryKey, SqlAnnotations, SqlColumnIR, SqlForeignKeyIR, SqlIndexIR, SqlSchemaIR, SqlTableIR, SqlTypeMetadata, SqlTypeMetadataRegistry, SqlUniqueIR };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prisma-next/sql-schema-ir",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "SQL Schema IR types for schema introspection and verification",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@prisma-next/contract": "0.0.1"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@vitest/coverage-v8": "^2.1.1",
|
|
12
|
+
"tsup": "^8.3.0",
|
|
13
|
+
"typescript": "^5.9.3",
|
|
14
|
+
"vitest": "^2.1.1"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
"./types": {
|
|
21
|
+
"types": "./dist/exports/types.d.ts",
|
|
22
|
+
"import": "./dist/exports/types.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsup --config tsup.config.ts",
|
|
27
|
+
"test": "vitest run --passWithNoTests",
|
|
28
|
+
"test:coverage": "vitest run --coverage --passWithNoTests",
|
|
29
|
+
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
30
|
+
"lint": "biome check . --config-path ../../../biome.json --error-on-warnings",
|
|
31
|
+
"clean": "node ../../../../scripts/clean.mjs"
|
|
32
|
+
}
|
|
33
|
+
}
|