@lyku/lockstep-pg 0.1.1 → 1.3.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 +87 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
# @lyku/lockstep-pg
|
|
2
2
|
|
|
3
|
-
Schema-driven PostgreSQL migration toolkit for [`@lyku/lockstep-core`](../
|
|
3
|
+
Schema-driven PostgreSQL migration toolkit for [`@lyku/lockstep-core`](../lockstep-core) models. Detects drift between your code-defined schemas and a live database, then generates safe (and optionally destructive) SQL migrations.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **Drift detection**
|
|
8
|
-
- **Introspection**
|
|
9
|
-
- **Diff engine**
|
|
10
|
-
- **SQL generation**
|
|
11
|
-
- **Seed data**
|
|
7
|
+
- **Drift detection** — compare `PostgresTableModel` definitions against a live PostgreSQL database
|
|
8
|
+
- **Introspection** — read table structure, indexes, constraints, and foreign keys from any Postgres database
|
|
9
|
+
- **Diff engine** — structural diff between introspected tables and code models, categorized into safe vs. destructive operations
|
|
10
|
+
- **SQL generation** — produces migration SQL from diffs or drift reports, including `CREATE TABLE`, `ALTER COLUMN`, `ADD INDEX`, enum `CHECK` constraint updates, and stock document seeding
|
|
11
|
+
- **Seed data** — generate `INSERT ... ON CONFLICT DO NOTHING` statements for fixture documents defined in table models
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @lyku/lockstep-pg
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Requires `@lyku/lockstep-core` as a peer dependency and `pg` for database connections.
|
|
12
20
|
|
|
13
21
|
## Usage
|
|
14
22
|
|
|
@@ -16,17 +24,21 @@ Schema-driven PostgreSQL migration toolkit for [`@lyku/lockstep-core`](../from-s
|
|
|
16
24
|
|
|
17
25
|
```typescript
|
|
18
26
|
import { detectDrift } from '@lyku/lockstep-pg';
|
|
19
|
-
|
|
27
|
+
|
|
28
|
+
const tables = {
|
|
29
|
+
users: usersModel,
|
|
30
|
+
posts: postsModel,
|
|
31
|
+
// ... your PostgresRecordModel definitions
|
|
32
|
+
};
|
|
20
33
|
|
|
21
34
|
const drifts = await detectDrift(process.env.PG_CONNECTION_STRING, { tables });
|
|
22
|
-
// Returns Drift[]
|
|
35
|
+
// Returns Drift[] — missing tables, extra columns, type mismatches, missing indexes, etc.
|
|
23
36
|
```
|
|
24
37
|
|
|
25
38
|
### Generate a migration
|
|
26
39
|
|
|
27
40
|
```typescript
|
|
28
41
|
import { generateMigration } from '@lyku/lockstep-pg';
|
|
29
|
-
import { tables } from '@lyku/pg-config';
|
|
30
42
|
|
|
31
43
|
const { safe, destructive, driftCount } = await generateMigration(process.env.PG_CONNECTION_STRING, { tables });
|
|
32
44
|
// safe: SQL string wrapped in BEGIN/COMMIT (additive changes)
|
|
@@ -54,10 +66,9 @@ await client.end();
|
|
|
54
66
|
### Diff and generate SQL
|
|
55
67
|
|
|
56
68
|
```typescript
|
|
57
|
-
import { diffDatabase, categorizeOperations } from '@lyku/lockstep-pg';
|
|
58
|
-
import { generateMigrationSql } from '@lyku/lockstep-pg';
|
|
69
|
+
import { diffDatabase, categorizeOperations, generateMigrationSql } from '@lyku/lockstep-pg';
|
|
59
70
|
|
|
60
|
-
const ops = diffDatabase(
|
|
71
|
+
const ops = diffDatabase(introspectedTables, codeDefinedTables);
|
|
61
72
|
const { safe, destructive } = categorizeOperations(ops);
|
|
62
73
|
const sql = generateMigrationSql(ops);
|
|
63
74
|
```
|
|
@@ -66,12 +77,75 @@ const sql = generateMigrationSql(ops);
|
|
|
66
77
|
|
|
67
78
|
```typescript
|
|
68
79
|
import { generateSeedSql } from '@lyku/lockstep-pg';
|
|
69
|
-
import { tables } from '@lyku/pg-config';
|
|
70
80
|
|
|
71
81
|
const sql = generateSeedSql({ tables });
|
|
72
82
|
// INSERT ... ON CONFLICT DO NOTHING for each table's `docs` array
|
|
73
83
|
```
|
|
74
84
|
|
|
85
|
+
## Where it fits
|
|
86
|
+
|
|
87
|
+
<!-- Generated from libs/lockstep-core/diagrams/pipeline.mmd — do not edit manually -->
|
|
88
|
+
|
|
89
|
+
```mermaid
|
|
90
|
+
graph TD
|
|
91
|
+
input-tables["Table Schemas<br/><small>PostgresRecordModel</small>"]
|
|
92
|
+
input-api["API Definitions<br/><small>TsonHandlerModel</small>"]
|
|
93
|
+
|
|
94
|
+
core-jsonmodels["generateJsonModels"]
|
|
95
|
+
core-dbtypes["generateDbTypes"]
|
|
96
|
+
core-apitypes["generateApiTypes"]
|
|
97
|
+
core-validators["buildValidator"]
|
|
98
|
+
|
|
99
|
+
handles-gen["generateHandles"]
|
|
100
|
+
|
|
101
|
+
client-gen["generateTsClient"]
|
|
102
|
+
|
|
103
|
+
pg-drift["detectDrift"]
|
|
104
|
+
pg-migrate["generateMigration"]
|
|
105
|
+
|
|
106
|
+
output-jsonschemas["JSON Schemas +<br/>TypeScript Types"]
|
|
107
|
+
output-kysely["Kysely Database<br/>Interface"]
|
|
108
|
+
output-reqres["Request / Response<br/>TypeScript Types"]
|
|
109
|
+
output-handlers["Handler Factories<br/>+ Validators"]
|
|
110
|
+
output-client["Type-safe<br/>HTTP Client"]
|
|
111
|
+
output-sql["SQL Migrations"]
|
|
112
|
+
|
|
113
|
+
input-tables --> core-jsonmodels
|
|
114
|
+
core-jsonmodels --> output-jsonschemas
|
|
115
|
+
output-jsonschemas --> core-dbtypes
|
|
116
|
+
core-dbtypes --> output-kysely
|
|
117
|
+
|
|
118
|
+
input-api --> core-apitypes
|
|
119
|
+
core-apitypes --> output-reqres
|
|
120
|
+
input-api --> core-validators
|
|
121
|
+
|
|
122
|
+
output-reqres --> handles-gen
|
|
123
|
+
input-api --> handles-gen
|
|
124
|
+
core-validators -.-> handles-gen
|
|
125
|
+
handles-gen --> output-handlers
|
|
126
|
+
|
|
127
|
+
output-reqres --> client-gen
|
|
128
|
+
input-api --> client-gen
|
|
129
|
+
client-gen --> output-client
|
|
130
|
+
|
|
131
|
+
input-tables --> pg-drift
|
|
132
|
+
pg-drift --> pg-migrate
|
|
133
|
+
pg-migrate --> output-sql
|
|
134
|
+
|
|
135
|
+
classDef highlight fill:#4f46e5,stroke:#3730a3,color:#fff,stroke-width:2px
|
|
136
|
+
classDef dimmed fill:#f1f5f9,stroke:#cbd5e1,color:#94a3b8,stroke-width:1px
|
|
137
|
+
classDef inputNode fill:#fef3c7,stroke:#f59e0b,color:#92400e,stroke-width:1px
|
|
138
|
+
classDef outputNode fill:#d1fae5,stroke:#10b981,color:#065f46,stroke-width:1px
|
|
139
|
+
classDef ownedOutput fill:#a5b4fc,stroke:#4f46e5,color:#1e1b4b,stroke-width:2px
|
|
140
|
+
classDef ownedInput fill:#fde68a,stroke:#4f46e5,color:#1e1b4b,stroke-width:2px
|
|
141
|
+
class pg-drift,pg-migrate highlight
|
|
142
|
+
class core-jsonmodels,core-dbtypes,core-apitypes,core-validators,handles-gen,client-gen dimmed
|
|
143
|
+
class input-api inputNode
|
|
144
|
+
class output-jsonschemas,output-kysely,output-reqres,output-handlers,output-client outputNode
|
|
145
|
+
class input-tables ownedInput
|
|
146
|
+
class output-sql ownedOutput
|
|
147
|
+
```
|
|
148
|
+
|
|
75
149
|
## Drift types
|
|
76
150
|
|
|
77
151
|
| Type | Description |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lyku/lockstep-pg",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "Schema-driven PostgreSQL migration toolkit: drift detection, introspection, and SQL generation for @lyku/lockstep-core models",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|