@xata.io/pgroll
Advanced tools
Comparing version 0.0.0-alpha.v99c72e6d79a14270b66801754b2518dbbc4ef9d9 to 0.0.0-alpha.vf0d55c85ef5c649e4f49105bc342dc7e70d49ca9
# @xata.io/pgroll | ||
## 0.0.0-alpha.v99c72e6d79a14270b66801754b2518dbbc4ef9d9 | ||
## 0.0.0-alpha.vf0d55c85ef5c649e4f49105bc342dc7e70d49ca9 | ||
@@ -5,0 +5,0 @@ ### Minor Changes |
{ | ||
"name": "@xata.io/pgroll", | ||
"version": "0.0.0-alpha.v99c72e6d79a14270b66801754b2518dbbc4ef9d9", | ||
"version": "0.0.0-alpha.vf0d55c85ef5c649e4f49105bc342dc7e70d49ca9", | ||
"description": "Migration tool for PostgreSQL", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -7,3 +7,3 @@ import fs from 'fs/promises'; | ||
type Def = | ||
type Definition = | ||
| { type: 'string' | 'boolean' | 'number'; description?: string } | ||
@@ -13,3 +13,3 @@ | { $ref: string; description?: string } | ||
type: 'object'; | ||
properties: Record<string, Def>; | ||
properties: Record<string, Definition>; | ||
required?: string[]; | ||
@@ -19,6 +19,6 @@ description?: string; | ||
} | ||
| { type: 'array'; items: Def | Def[]; description?: string } | ||
| { anyOf: Def[] }; | ||
| { type: 'array'; items: Definition | Definition[]; description?: string } | ||
| { anyOf: Definition[] }; | ||
const DefSchema: z.ZodSchema<Def> = z.lazy(() => | ||
const DefinitionSchema: z.ZodSchema<Definition> = z.lazy(() => | ||
z.union([ | ||
@@ -35,3 +35,3 @@ z.object({ | ||
type: z.literal('object'), | ||
properties: z.record(DefSchema), | ||
properties: z.record(DefinitionSchema), | ||
required: z.array(z.string()).optional(), | ||
@@ -43,7 +43,7 @@ description: z.string().optional(), | ||
type: z.literal('array'), | ||
items: z.union([DefSchema, z.array(DefSchema)]), | ||
items: z.union([DefinitionSchema, z.array(DefinitionSchema)]), | ||
description: z.string().optional() | ||
}), | ||
z.object({ | ||
anyOf: z.array(DefSchema) | ||
anyOf: z.array(DefinitionSchema) | ||
}) | ||
@@ -58,6 +58,6 @@ ]) | ||
description: z.string(), | ||
$defs: z.record(DefSchema) | ||
$defs: z.record(DefinitionSchema) | ||
}); | ||
function buildZodSchema(definition: Def): string { | ||
function buildZodSchema(definition: Definition): string { | ||
if ('$ref' in definition) { | ||
@@ -104,3 +104,3 @@ return definition.$ref.replace(/^#\/\$defs\//, '') + 'Definition'; | ||
function getDependencies(definition: Def): string[] { | ||
function getDependencies(definition: Definition): string[] { | ||
if ('$ref' in definition) { | ||
@@ -127,6 +127,7 @@ return [definition.$ref.replace(/^#\/\$defs\//, '')]; | ||
function topologicalSort(nodes: [string, Def][]): [string, Def][] { | ||
const sorted: [string, Def][] = []; | ||
function topologicalSort(nodes: [string, Definition][]): [string, Definition][] { | ||
const sorted: [string, Definition][] = []; | ||
const visited = new Set<string>(); | ||
// Recursive function to visit nodes in a topological order | ||
function visit(name: string) { | ||
@@ -139,2 +140,3 @@ if (visited.has(name)) return; | ||
// Visit dependencies before adding the current node | ||
for (const dep of getDependencies(node[1])) { | ||
@@ -147,2 +149,3 @@ visit(dep); | ||
// Visit all nodes in the graph | ||
for (const [name] of nodes) { | ||
@@ -156,11 +159,13 @@ visit(name); | ||
async function main() { | ||
// Fetch the schema from the URL and write it to a file. | ||
const response = await fetch(PGROLL_JSON_SCHEMA_URL).then((response) => response.json()); | ||
const schema = JSONSchema.parse(response); | ||
// Create a TypeScript project | ||
const project = new Project({ compilerOptions: { target: ScriptTarget.ESNext } }); | ||
const file = project.createSourceFile('types.ts', '', { overwrite: true }); | ||
// Add import statements | ||
file.addImportDeclaration({ moduleSpecifier: 'zod', namedImports: ['z'] }); | ||
// Topologically sort the schema definitions | ||
const statements = topologicalSort(Object.entries(schema.$defs)).map(([name, definition]) => [ | ||
@@ -171,4 +176,7 @@ name, | ||
// Generate TypeScript code for each definition | ||
for (const [name, statement] of statements) { | ||
// Add a type alias for the Zod type | ||
file.addTypeAlias({ name, type: `z.infer<typeof ${name}Definition>`, isExported: true }); | ||
// Add a variable statement for the Zod schema | ||
file.addVariableStatement({ | ||
@@ -181,2 +189,3 @@ declarationKind: VariableDeclarationKind.Const, | ||
// Add a type alias for the OperationType | ||
file.addTypeAlias({ | ||
@@ -188,2 +197,3 @@ name: 'OperationType', | ||
// Extract operation types from the schema and add a variable statement | ||
const operationTypes = (schema.$defs['PgRollOperation'] as any).anyOf.flatMap((def) => Object.keys(def.properties)); | ||
@@ -201,2 +211,3 @@ file.addVariableStatement({ | ||
// Write the generated TypeScript code to a file | ||
await fs.writeFile('src/types.ts', prettier.format(file.getFullText(), { parser: 'typescript' })); | ||
@@ -203,0 +214,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
116239
3052