Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@analtools/jsonormalize",
"version": "0.0.14",
"version": "0.0.15",
"description": "JSONormalize — Transform any JSON into a relational database schema. Automatically normalizes nested structures, detects relationships, and generates SQLite migrations. Perfect for rapid prototyping, data migrations, and structured data workflows.",
"keywords": [
"json-normalize",
Expand Down
9 changes: 9 additions & 0 deletions src/commands/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const description = `JSONormalize — Transform any JSON into a relational database schema. Automatically normalizes nested structures, detects relationships, and generates SQLite migrations. Perfect for rapid prototyping, data migrations, and structured data workflows.`;

export const requiredArgs = {
jsonPath: {
name: "<json-path>",
Expand Down Expand Up @@ -25,6 +27,13 @@ export const optionalArgs = Object.fromEntries(
};
};

export const options = {
schema: {
name: "--schema <schema>",
description: "The name of the database schema",
},
};

export const commands = {
sql: {
name: (prefix: string) => `${prefix}:sql`,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/postgres/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { setup } from "./setup";
export { sql } from "./sql";
export * from "./setup";
export * from "./sql";
29 changes: 25 additions & 4 deletions src/commands/postgres/setup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ClientConfig } from "pg";

import { setupTables } from "../../postgres";
import { commands, optionalArgs, requiredArgs } from "../constants";
import { commands, optionalArgs, options, requiredArgs } from "../constants";
import { prepare } from "../prepare";
import { program } from "../program";

Expand All @@ -10,6 +10,7 @@ program
.description(commands.setup.description)
.argument(requiredArgs.jsonPath.name, requiredArgs.jsonPath.description)
.argument(optionalArgs.dbPath.name, optionalArgs.dbPath.description)
.option(options.schema.name, options.schema.description)
.option("--user <user>", "default process.env.PGUSER || process.env.USER")
.option("--password <password>", "default process.env.PGPASSWORD")
.option("--host <host>", "default process.env.PGHOST")
Expand Down Expand Up @@ -63,7 +64,9 @@ program
"--options <options>",
"command-line options to be sent to the server",
)
.action(setup);
.action((...args: Parameters<typeof parseArgs>) =>
setup(...parseArgs(...args)),
);

function getNumberOption(value: string | undefined): number | undefined {
return value === undefined ? undefined : Number(value);
Expand All @@ -77,7 +80,7 @@ function getSslOption(value: any): ClientConfig["ssl"] {
}
}

export async function setup(
function parseArgs(
jsonPath: string,
dbPath: string | undefined,
options: {
Expand All @@ -98,8 +101,11 @@ export async function setup(
clientEncoding?: string;
fallbackApplicationName?: string;
options?: string;
schema?: string;
} = {},
) {
): Parameters<typeof setup> {
const schemaName = options.schema;

const config: ClientConfig | undefined =
dbPath === undefined && Object.keys(options).length
? {
Expand Down Expand Up @@ -129,12 +135,27 @@ export async function setup(
}
: undefined;

return [{ jsonPath, dbPath, config, schemaName }];
}

export async function setup({
config,
dbPath,
jsonPath,
schemaName,
}: {
jsonPath: string;
dbPath?: string | undefined;
config?: ClientConfig | undefined;
schemaName?: string | undefined;
}) {
const { data, prefix } = await prepare(jsonPath);

await setupTables({
config,
path: dbPath,
data,
prefix,
schemaName,
});
}
46 changes: 42 additions & 4 deletions src/commands/postgres/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "node:fs";
import { EOL } from "node:os";

import { createMigrations } from "../../postgres";
import { commands, requiredArgs } from "../constants";
import { commands, options, requiredArgs } from "../constants";
import { prepare } from "../prepare";
import { program } from "../program";

Expand All @@ -11,15 +11,53 @@ program
.description(commands.sql.description)
.argument(requiredArgs.jsonPath.name, requiredArgs.jsonPath.description)
.argument(requiredArgs.sqlPath.name, requiredArgs.sqlPath.description)
.action(sql);
.option(options.schema.name, options.schema.description)
.action((...args: Parameters<typeof parseArgs>) =>
writeSqlScriptToFile(...parseArgs(...args)),
);

export async function sql(jsonPath: string, sqlPath: string) {
function parseArgs(
jsonPath: string,
sqlPath: string,
{ schema: schemaName }: { schema?: string } = {},
): Parameters<typeof writeSqlScriptToFile> {
return [
{
jsonPath,
sqlPath,
schemaName,
},
];
}

export async function getSqlScript({
jsonPath,
schemaName,
}: {
jsonPath: string;
schemaName?: string | undefined;
}) {
const { data, prefix } = await prepare(jsonPath);

const { initialMigration, dataMigration } = createMigrations({
prefix,
data,
schemaName,
});

fs.writeFileSync(sqlPath, `${initialMigration}${EOL}${EOL}${dataMigration}`);
return `${initialMigration}${EOL}${EOL}${dataMigration}`;
}

export async function writeSqlScriptToFile({
jsonPath,
sqlPath,
schemaName,
}: {
jsonPath: string;
sqlPath: string;
schemaName?: string | undefined;
}) {
const sqlScript = await getSqlScript({ jsonPath, schemaName });

fs.writeFileSync(sqlPath, sqlScript);
}
7 changes: 2 additions & 5 deletions src/commands/program.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { Command } from "commander";

import { description } from "./constants";
export const program = new Command();

program
.name("jsonormalize")
.description(
`JSONormalize — Transform any JSON into a relational database schema. Automatically normalizes nested structures, detects relationships, and generates SQLite migrations. Perfect for rapid prototyping, data migrations, and structured data workflows.`,
);
program.name("jsonormalize").description(description);
4 changes: 2 additions & 2 deletions src/commands/sqlite/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { setup } from "./setup";
export { sql } from "./sql";
export * from "./setup";
export * from "./sql";
30 changes: 27 additions & 3 deletions src/commands/sqlite/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setupTables } from "../../sqlite";
import { commands, optionalArgs, requiredArgs } from "../constants";
import { commands, optionalArgs, options, requiredArgs } from "../constants";
import { prepare } from "../prepare";
import { program } from "../program";

Expand All @@ -8,14 +8,38 @@ program
.description(commands.setup.description)
.argument(requiredArgs.jsonPath.name, requiredArgs.jsonPath.description)
.argument(optionalArgs.dbPath.name, optionalArgs.dbPath.description)
.action(setup);
.option(options.schema.name, options.schema.description)
.action((...args: Parameters<typeof parseArgs>) =>
setup(...parseArgs(...args)),
);

export async function setup(jsonPath: string, dbPath: string) {
function parseArgs(
jsonPath: string,
dbPath: string | undefined,
options: {
schema?: string;
} = {},
): Parameters<typeof setup> {
const schemaName = options.schema;

return [{ jsonPath, dbPath, schemaName }];
}

export async function setup({
jsonPath,
dbPath,
schemaName,
}: {
jsonPath: string;
dbPath?: string | undefined;
schemaName?: string | string;
}) {
const { data, prefix } = await prepare(jsonPath);

await setupTables({
path: dbPath,
data,
prefix,
schemaName,
});
}
46 changes: 42 additions & 4 deletions src/commands/sqlite/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "node:fs";
import { EOL } from "node:os";

import { createMigrations } from "../../sqlite";
import { commands, requiredArgs } from "../constants";
import { commands, options, requiredArgs } from "../constants";
import { prepare } from "../prepare";
import { program } from "../program";

Expand All @@ -11,15 +11,53 @@ program
.description(commands.sql.description)
.argument(requiredArgs.jsonPath.name, requiredArgs.jsonPath.description)
.argument(requiredArgs.sqlPath.name, requiredArgs.sqlPath.description)
.action(sql);
.option(options.schema.name, options.schema.description)
.action((...args: Parameters<typeof parseArgs>) =>
writeSqlScriptToFile(...parseArgs(...args)),
);

export async function sql(jsonPath: string, sqlPath: string) {
function parseArgs(
jsonPath: string,
sqlPath: string,
{ schema: schemaName }: { schema?: string } = {},
): Parameters<typeof writeSqlScriptToFile> {
return [
{
jsonPath,
sqlPath,
schemaName,
},
];
}

export async function getSqlScript({
jsonPath,
schemaName,
}: {
jsonPath: string;
schemaName?: string | undefined;
}) {
const { data, prefix } = await prepare(jsonPath);

const { initialMigration, dataMigration } = createMigrations({
prefix,
data,
schemaName,
});

fs.writeFileSync(sqlPath, `${initialMigration}${EOL}${EOL}${dataMigration}`);
return `${initialMigration}${EOL}${EOL}${dataMigration}`;
}

export async function writeSqlScriptToFile({
jsonPath,
sqlPath,
schemaName,
}: {
jsonPath: string;
sqlPath: string;
schemaName?: string | undefined;
}) {
const sqlScript = await getSqlScript({ jsonPath, schemaName });

fs.writeFileSync(sqlPath, sqlScript);
}
11 changes: 9 additions & 2 deletions src/postgres/create-data-migration.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { EOL } from "node:os";

import type { RelationalTable } from "../create-relational-structure";
import { getFullTableName } from "../utils";
import { escapeValue } from "./escape-value";

export function createDataMigration(tables: RelationalTable[]) {
export function createDataMigration({
tables,
schemaName,
}: {
tables: RelationalTable[];
schemaName?: string | undefined;
}): string {
return tables
.filter((table) => table.data.length > 0)
.map((table) => {
const keys = table.fields.map(({ key }) => key);
return [
`INSERT INTO ${table.name} (${keys.join(", ")}) VALUES`,
`INSERT INTO ${getFullTableName({ tableName: table.name, schemaName })} (${keys.join(", ")}) VALUES`,
`${table.data
.map((row) => {
const values = keys.map((key) => row[key]);
Expand Down
11 changes: 9 additions & 2 deletions src/postgres/create-initial-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ import { EOL } from "node:os";

import type { RelationalTable } from "../create-relational-structure";
import { getForeignKeys, getPrimaryKeys } from "../create-relational-structure";
import { getFullTableName } from "../utils";
import { getFieldType } from "./get-field-type";

export function createInitialMigration(tables: RelationalTable[]): string {
export function createInitialMigration({
tables,
schemaName,
}: {
tables: RelationalTable[];
schemaName?: string | undefined;
}): string {
return tables
.map((table) => {
const foreignKeys = getForeignKeys(table);
return [
`CREATE TABLE ${table.name} (`,
`CREATE TABLE ${getFullTableName({ tableName: table.name, schemaName })} (`,
[
...table.fields.map(
(field) =>
Expand Down
6 changes: 4 additions & 2 deletions src/postgres/create-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import { createInitialMigration } from "./create-initial-migration";
export function createMigrations({
prefix,
data,
schemaName,
}: {
prefix: string;
data: unknown;
schemaName?: string | undefined;
}) {
const tables = createRelationalStructure(prefix, normalize(data));

return {
initialMigration: createInitialMigration(tables),
dataMigration: createDataMigration(tables),
initialMigration: createInitialMigration({ tables, schemaName }),
dataMigration: createDataMigration({ tables, schemaName }),
};
}
Loading