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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ Check out the examples/ directory for ready-to-use JSON samples:

## 📦 Installation

Add an `.npmrc` file to your project:

```
@analtools:registry=https://npm.pkg.github.com
```

Then run one of the following commands:

```sh
npm install jsonormalize
npm install @analtools/jsonormalize
# or
yarn add jsonormalize
yarn add @analtools/jsonormalize
# or
pnpm add jsonormalize
pnpm add @analtools/jsonormalize
```

## 🚀 Quick Start
Expand Down
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.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@analtools/jsonormalize",
"version": "0.0.6",
"version": "0.0.12",
"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 Expand Up @@ -33,7 +33,8 @@
"data-formatting"
],
"publishConfig": {
"registry": "https://npm.pkg.github.com"
"registry": "https://npm.pkg.github.com",
"access": "publics"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/postgres/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export async function setup(
clientEncoding?: string;
fallbackApplicationName?: string;
options?: string;
},
} = {},
) {
const config: ClientConfig | undefined =
dbPath === undefined && Object.keys(options).length
Expand Down
3 changes: 1 addition & 2 deletions src/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async function getRawData(jsonPath: string): Promise<unknown> {
}

export async function prepare(jsonPath: string) {
const rawData = await getRawData(jsonPath);
const data: unknown[] = Array.isArray(rawData) ? rawData : [rawData];
const data = await getRawData(jsonPath);

const jsonPathNameWithoutExt = path.basename(
jsonPath,
Expand Down
40 changes: 40 additions & 0 deletions src/normalize/normalize-dictionaries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { isDictionary } from "../utils";
import type { NormalizedData } from "./types";

export function normalizeDictionaries(data: any): NormalizedData {
if (Array.isArray(data)) {
return data;
} else if (isDictionary(data)) {
const types = Array.from(
new Set(
Object.values(data)
.filter((value) => value !== null)
.map((value) => typeof value),
),
);

if (types.length === 1) {
return Object.entries(data).map(([key, value]: [string, any]) => ({
key,
value,
}));
} else {
const baseRow = Object.fromEntries(
types.map((type) => [`value_${type}`, null]),
);
return Object.entries(data).map(([key, value]) => ({
key,
...baseRow,
...(value === null
? {}
: {
[`value_${typeof value}`]: value,
}),
}));
}
} else if (Object(data) === data) {
return [data];
} else {
return [];
}
}
77 changes: 77 additions & 0 deletions src/normalize/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,83 @@ import { describe, expect, it } from "vitest";
import { normalize } from "./normalize";

describe("normalize", () => {
it("should normalize dictionary", () => {
expect(
normalize({
a: "A",
b: "B",
c: null,
}),
).toEqual([
{ key: "a", value: "A" },
{ key: "b", value: "B" },
{ key: "c", value: null },
]);

expect(
normalize({
a: 1,
b: null,
c: 3,
}),
).toEqual([
{ key: "a", value: 1 },
{ key: "b", value: null },
{ key: "c", value: 3 },
]);

expect(
normalize({
a: null,
b: true,
c: false,
}),
).toEqual([
{ key: "a", value: null },
{ key: "b", value: true },
{ key: "c", value: false },
]);

expect(
normalize({
a: 1,
b: "B",
c: true,
d: null,
}),
).toEqual([
{ key: "a", value_number: 1, value_string: null, value_boolean: null },
{ key: "b", value_number: null, value_string: "B", value_boolean: null },
{ key: "c", value_number: null, value_string: null, value_boolean: true },
{ key: "d", value_number: null, value_string: null, value_boolean: null },
]);

expect(
normalize({
obj: {
sub: {
a: 1,
b: 2,
c: 3,
},
},
}),
).toEqual([
{
key: "obj_sub_a",
value: 1,
},
{
key: "obj_sub_b",
value: 2,
},
{
key: "obj_sub_c",
value: 3,
},
]);
});

it("should flatten nested objects with localization and arrays", () => {
expect(
normalize([
Expand Down
21 changes: 12 additions & 9 deletions src/normalize/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { normalizePrimitiveArrays } from "./normalize-array-of-arrays";
import { normalizeArrayOfPrimitives } from "./normalize-array-of-primitives";
import { normalizeDeepObjects } from "./normalize-deep-objects";
import { normalizeDictionaries } from "./normalize-dictionaries";
import { normalizeLocalization } from "./normalize-localization";
import type { NormalizedData } from "./types";

export function normalize(data: unknown[]): NormalizedData {
/* replace {a:{b:'c'}} to {'a_b':'c'} */
return normalizeDeepObjects(
/* replace [[...],[...]] to [{items:[...]},{items:[...]}] */
normalizePrimitiveArrays(
/* replace [1,2,3] to [{value:1},{value:2},{value:3}] */
normalizeArrayOfPrimitives(
/* replace { en: string, zh: string, ... } to { lang: string, text: string }[]*/
normalizeLocalization(data),
export function normalize(data: unknown): NormalizedData {
return normalizeDictionaries(
/* replace {a:{b:'c'}} to {'a_b':'c'} */
normalizeDeepObjects(
/* replace [[...],[...]] to [{items:[...]},{items:[...]}] */
normalizePrimitiveArrays(
/* replace [1,2,3] to [{value:1},{value:2},{value:3}] */
normalizeArrayOfPrimitives(
/* replace { en: string, zh: string, ... } to { lang: string, text: string }[]*/
normalizeLocalization(data),
),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/postgres/create-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function createMigrations({
data,
}: {
prefix: string;
data: unknown[];
data: unknown;
}) {
const tables = createRelationalStructure(prefix, normalize(data));

Expand Down
2 changes: 1 addition & 1 deletion src/postgres/setup-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function setupTables({
config?: ClientConfig;
path?: string;
prefix: string;
data: unknown[];
data: unknown;
}) {
const { initialMigration, dataMigration } = createMigrations({
prefix,
Expand Down
2 changes: 1 addition & 1 deletion src/sqlite/create-migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function createMigrations({
data,
}: {
prefix: string;
data: unknown[];
data: unknown;
}) {
const tables = createRelationalStructure(prefix, normalize(data));

Expand Down
2 changes: 1 addition & 1 deletion src/sqlite/setup-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function setupTables({
}: {
path?: string;
prefix: string;
data: unknown[];
data: unknown;
}) {
const { initialMigration, dataMigration } = createMigrations({
prefix,
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./is-array-of-arrays";
export * from "./is-array-of-primitives";
export * from "./is-dictionary";
export * from "./is-localization-object";
export * from "./is-url";
export * from "./snake-case";
10 changes: 10 additions & 0 deletions src/utils/is-dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const simpleTypes = ["string", "number", "boolean"];

export function isDictionary(data: any) {
return (
Object(data) === data ||
Object.values(data).every(
(value) => value === null || simpleTypes.includes(typeof value),
)
);
}