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
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.15",
"version": "0.0.16",
"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
18 changes: 18 additions & 0 deletions src/postgres/__snapshots__/create-initial-migration.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`postgres/create-initial-migration > should handle with schemaName 1`] = `
"CREATE TABLE schema_test.test (
normalize_id INTEGER NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (normalize_id)
);

CREATE TABLE schema_test.test_items (
normalize_id INTEGER NOT NULL,
normalize_parent_id INTEGER NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (normalize_id),
FOREIGN KEY (normalize_parent_id) REFERENCES schema_test.test (normalize_id)
);
CREATE INDEX idx_test_items_normalize_parent_id ON schema_test.test_items (normalize_parent_id);"
`;
22 changes: 22 additions & 0 deletions src/postgres/create-initial-migration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from "vitest";

import { createRelationalStructure } from "../create-relational-structure";
import { normalize } from "../normalize";
import { createInitialMigration } from "./create-initial-migration";

describe("postgres/create-initial-migration", () => {
it("should handle with schemaName", () => {
expect(
createInitialMigration({
tables: createRelationalStructure(
"test",
normalize([
{ value: "a", items: [{ value: "a.1" }, { value: "a.2" }] },
{ value: "b", items: [{ value: "b.1" }] },
]),
),
schemaName: "schema_test",
}),
).toMatchSnapshot();
});
});
7 changes: 5 additions & 2 deletions src/postgres/create-initial-migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ export function createInitialMigration({
...(foreignKeys.length
? foreignKeys.map(
({ key, reference }) =>
` FOREIGN KEY (${key}) REFERENCES ${reference.table} (${reference.key})`,
` FOREIGN KEY (${key}) REFERENCES ${getFullTableName({ tableName: reference.table, schemaName })} (${reference.key})`,
)
: []),
].join(`,${EOL}`),
`);`,
...foreignKeys.map(
({ key }) =>
`CREATE INDEX idx_${table.name}_${key} ON ${table.name} (${key});`,
`CREATE INDEX idx_${table.name}_${key} ON ${getFullTableName({
tableName: table.name,
schemaName,
})} (${key});`,
),
].join(EOL);
})
Expand Down
Loading