@xata.io/pgroll
Advanced tools
Comparing version 0.0.0-alpha.v99c72e6d79a14270b66801754b2518dbbc4ef9d9 to 0.0.0-alpha.vb3a627396b7cf45bf4a240cebb72f4dada0b0cb5
# @xata.io/pgroll | ||
## 0.0.0-alpha.v99c72e6d79a14270b66801754b2518dbbc4ef9d9 | ||
## 0.0.0-alpha.vb3a627396b7cf45bf4a240cebb72f4dada0b0cb5 | ||
### Minor Changes | ||
### Patch Changes | ||
- [#1250](https://github.com/xataio/client-ts/pull/1250) [`5c7f7ec`](https://github.com/xataio/client-ts/commit/5c7f7ec8f85428c8c5de3cc6e49d95d261b340f0) Thanks [@SferaDev](https://github.com/SferaDev)! - Initial release | ||
- Force canary build | ||
## 0.4.4 | ||
### Patch Changes | ||
- [#1250](https://github.com/xataio/client-ts/pull/1250) [`9a6af72`](https://github.com/xataio/client-ts/commit/9a6af72ba4dd7880e8196a0a57d4133930957add) Thanks [@SferaDev](https://github.com/SferaDev)! - Add new package for pgroll migrations |
{ | ||
"name": "@xata.io/pgroll", | ||
"version": "0.0.0-alpha.v99c72e6d79a14270b66801754b2518dbbc4ef9d9", | ||
"version": "0.0.0-alpha.vb3a627396b7cf45bf4a240cebb72f4dada0b0cb5", | ||
"description": "Migration tool for PostgreSQL", | ||
@@ -29,7 +29,7 @@ "type": "module", | ||
"zod": "^3.22.4", | ||
"zod-to-json-schema": "^3.21.4" | ||
"zod-to-json-schema": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"ts-morph": "^21.0.1", | ||
"tsx": "^4.1.2" | ||
"tsx": "^4.7.0" | ||
}, | ||
@@ -36,0 +36,0 @@ "scripts": { |
@@ -7,3 +7,3 @@ import fs from 'fs/promises'; | ||
type Def = | ||
type Definition = | ||
| { type: 'string' | 'boolean' | 'number'; description?: string } | ||
@@ -13,3 +13,4 @@ | { $ref: string; description?: string } | ||
type: 'object'; | ||
properties: Record<string, Def>; | ||
properties: Record<string, Definition>; | ||
oneOf?: unknown[]; | ||
required?: string[]; | ||
@@ -19,6 +20,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 +36,5 @@ z.object({ | ||
type: z.literal('object'), | ||
properties: z.record(DefSchema), | ||
properties: z.record(DefinitionSchema), | ||
// TODO: Add full support for oneOf | ||
oneOf: z.array(z.any()).optional(), | ||
required: z.array(z.string()).optional(), | ||
@@ -43,7 +46,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 +61,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 +107,3 @@ return definition.$ref.replace(/^#\/\$defs\//, '') + 'Definition'; | ||
function getDependencies(definition: Def): string[] { | ||
function getDependencies(definition: Definition): string[] { | ||
if ('$ref' in definition) { | ||
@@ -127,6 +130,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 +143,3 @@ if (visited.has(name)) return; | ||
// Visit dependencies before adding the current node | ||
for (const dep of getDependencies(node[1])) { | ||
@@ -147,2 +152,3 @@ visit(dep); | ||
// Visit all nodes in the graph | ||
for (const [name] of nodes) { | ||
@@ -156,11 +162,18 @@ 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 url = process.env.PGROLL_JSON_SCHEMA_URL ?? PGROLL_JSON_SCHEMA_URL; | ||
const response = await fetch(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 }); | ||
const schemaFile = project.createSourceFile('schema.ts', '', { overwrite: true }); | ||
const typesFile = project.createSourceFile('types.ts', '', { overwrite: true }); | ||
file.addImportDeclaration({ moduleSpecifier: 'zod', namedImports: ['z'] }); | ||
// Write the JSON schema to a file | ||
schemaFile.addStatements(`export const schema = ${JSON.stringify(response, null, 2)} as const;`); | ||
// Add import statements | ||
typesFile.addImportDeclaration({ moduleSpecifier: 'zod', namedImports: ['z'] }); | ||
// Topologically sort the schema definitions | ||
const statements = topologicalSort(Object.entries(schema.$defs)).map(([name, definition]) => [ | ||
@@ -171,5 +184,8 @@ name, | ||
// Generate TypeScript code for each definition | ||
for (const [name, statement] of statements) { | ||
file.addTypeAlias({ name, type: `z.infer<typeof ${name}Definition>`, isExported: true }); | ||
file.addVariableStatement({ | ||
// Add a type alias for the Zod type | ||
typesFile.addTypeAlias({ name, type: `z.infer<typeof ${name}Definition>`, isExported: true }); | ||
// Add a variable statement for the Zod schema | ||
typesFile.addVariableStatement({ | ||
declarationKind: VariableDeclarationKind.Const, | ||
@@ -181,3 +197,4 @@ declarations: [{ name: `${name}Definition`, initializer: statement }], | ||
file.addTypeAlias({ | ||
// Add a type alias for the OperationType | ||
typesFile.addTypeAlias({ | ||
name: 'OperationType', | ||
@@ -188,4 +205,5 @@ type: `(typeof operationTypes)[number]`, | ||
// 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)); | ||
file.addVariableStatement({ | ||
typesFile.addVariableStatement({ | ||
declarationKind: VariableDeclarationKind.Const, | ||
@@ -201,5 +219,7 @@ declarations: [ | ||
await fs.writeFile('src/types.ts', prettier.format(file.getFullText(), { parser: 'typescript' })); | ||
// Write the generated TypeScript code to a file | ||
await fs.writeFile('src/schema.ts', prettier.format(schemaFile.getFullText(), { parser: 'typescript' })); | ||
await fs.writeFile('src/types.ts', prettier.format(typesFile.getFullText(), { parser: 'typescript' })); | ||
} | ||
main(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
228616
4784
Updatedzod-to-json-schema@^3.22.4