Compare commits

..

No commits in common. "721544e7b27015a62b238f4a157b52698da746a1" and "55a33b98e50474532d3b3fecc768e4621d6112e6" have entirely different histories.

6 changed files with 64 additions and 64 deletions

View File

@ -1,4 +1,4 @@
# inject orders = ~order_rev(user) # orders := ~order_rev(user)
id,name id,name
string,string string,string
u01,Alice u01,Alice

1 # inject orders = ~order_rev(user) # orders := ~order_rev(user)
2 id,name id,name
3 string,string string,string
4 u01,Alice u01,Alice

View File

@ -104,7 +104,7 @@ describe("csvToModule - circular reference support", () => {
expect(result.js).toContain("_self_refLookup"); expect(result.js).toContain("_self_refLookup");
expect(result.js).toContain("_self_refLookup = new Map(_raw.map"); expect(result.js).toContain("_self_refLookup = new Map(_raw.map");
expect(result.js).toContain( expect(result.js).toContain(
"row.parent = _self_refLookup.get(String(row.parent));", "parent: _self_refLookup.get(String(row.parent))",
); );
}); });
@ -119,7 +119,7 @@ describe("csvToModule - circular reference support", () => {
expect(result.js).toContain("import _circular_b from './circular_b.csv'"); expect(result.js).toContain("import _circular_b from './circular_b.csv'");
expect(result.js).toContain("export default function getData()"); expect(result.js).toContain("export default function getData()");
expect(result.js).toContain("_circular_bLookup"); expect(result.js).toContain("_circular_bLookup");
expect(result.js).toContain("row.related ="); expect(result.js).toContain("related:");
}); });
it("should emit accessor for self-referencing table with nested reference in tuple", () => { it("should emit accessor for self-referencing table with nested reference in tuple", () => {
@ -137,7 +137,7 @@ describe("csvToModule - circular reference support", () => {
expect(result.js).not.toContain("import _self_ref from './self_ref.csv'"); expect(result.js).not.toContain("import _self_ref from './self_ref.csv'");
expect(result.js).toContain("_self_refLookup"); expect(result.js).toContain("_self_refLookup");
expect(result.js).toContain("export default function getData()"); expect(result.js).toContain("export default function getData()");
expect(result.js).toContain("row.parent_info ="); expect(result.js).toContain("parent_info:");
expect(result.js).toContain( expect(result.js).toContain(
"_self_refLookup.get(String(row.parent_info[0]))", "_self_refLookup.get(String(row.parent_info[0]))",
); );
@ -159,7 +159,7 @@ describe("csvToModule - circular reference support", () => {
expect(result.js).not.toContain("import _self_ref from './self_ref.csv'"); expect(result.js).not.toContain("import _self_ref from './self_ref.csv'");
expect(result.js).toContain("_self_refLookup"); expect(result.js).toContain("_self_refLookup");
expect(result.js).toContain("export default function getData()"); expect(result.js).toContain("export default function getData()");
expect(result.js).toContain("row.ref_or_val ="); expect(result.js).toContain("ref_or_val:");
expect(result.js).toContain( expect(result.js).toContain(
"_self_refLookup.get(String(row.ref_or_val)) ?? row.ref_or_val", "_self_refLookup.get(String(row.ref_or_val)) ?? row.ref_or_val",
); );
@ -180,7 +180,7 @@ describe("csvToModule - circular reference support", () => {
expect(result.js).not.toContain("import _self_ref from './self_ref.csv'"); expect(result.js).not.toContain("import _self_ref from './self_ref.csv'");
expect(result.js).toContain("_self_refLookup"); expect(result.js).toContain("_self_refLookup");
expect(result.js).toContain("export default function getData()"); expect(result.js).toContain("export default function getData()");
expect(result.js).toContain("row.children ="); expect(result.js).toContain("children:");
}); });
it("should generate correct type definition for self-referencing table using local singular type", () => { it("should generate correct type definition for self-referencing table using local singular type", () => {
@ -247,7 +247,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -255,7 +255,7 @@ describe("csvToModule - reverse reference output", () => {
expect(result.js).toContain("import _orders from './orders.csv'"); expect(result.js).toContain("import _orders from './orders.csv'");
expect(result.js).toContain("_ordersBy_customer"); expect(result.js).toContain("_ordersBy_customer");
expect(result.js).toContain("row.orders ="); expect(result.js).toContain("orders:");
expect(result.js).toContain("_ordersBy_customer.get(String(row.id))"); expect(result.js).toContain("_ordersBy_customer.get(String(row.id))");
}); });
@ -263,7 +263,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)?", "# orders := ~orders(customer)?",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -278,7 +278,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -291,7 +291,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject children = ~self_ref(parent)", "# children := ~self_ref(parent)",
"1,Root", "1,Root",
"2,Child", "2,Child",
].join("\n"); ].join("\n");
@ -304,14 +304,14 @@ describe("csvToModule - reverse reference output", () => {
expect(result.js).not.toContain("import _self_ref from './self_ref.csv'"); expect(result.js).not.toContain("import _self_ref from './self_ref.csv'");
expect(result.js).toContain("_self_refBy_parent"); expect(result.js).toContain("_self_refBy_parent");
expect(result.js).toContain("for (const r of _raw)"); expect(result.js).toContain("for (const r of _raw)");
expect(result.js).toContain("row.children ="); expect(result.js).toContain("children:");
}); });
it("should generate correct type definition for reverse references", () => { it("should generate correct type definition for reverse references", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -329,7 +329,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)?", "# orders := ~orders(customer)?",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -346,7 +346,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name,manager", "id,name,manager",
"string,string,@users", "string,string,@users",
"# inject reports = ~users(manager)", "# reports := ~users(manager)",
"1,Alice,2", "1,Alice,2",
"2,Bob,", "2,Bob,",
].join("\n"); ].join("\n");
@ -365,7 +365,7 @@ describe("csvToModule - reverse reference output", () => {
const csv = [ const csv = [
"id,name,creator", "id,name,creator",
"string,string,@users", "string,string,@users",
"# inject reviews = ~users(reviewer)", "# reviews := ~users(reviewer)",
"1,Doc,1", "1,Doc,1",
].join("\n"); ].join("\n");

View File

@ -186,7 +186,7 @@ export function csvToModule(
const reverseLookupVar = (tableName: string, foreignKey: string) => const reverseLookupVar = (tableName: string, foreignKey: string) =>
reverseLookupVarMap.get(`${tableName}:${foreignKey}`)!; reverseLookupVarMap.get(`${tableName}:${foreignKey}`)!;
const rowResolvers: { name: string; code: string }[] = []; const rowResolvers: string[] = [];
for (const config of result.propertyConfigs) { for (const config of result.propertyConfigs) {
if (config.isReverseReference) { if (config.isReverseReference) {
// Reverse reference resolution // Reverse reference resolution
@ -196,15 +196,13 @@ export function csvToModule(
if (decl) { if (decl) {
const revLookup = reverseLookupVar(decl.tableName, decl.foreignKey); const revLookup = reverseLookupVar(decl.tableName, decl.foreignKey);
if (decl.isOptional) { if (decl.isOptional) {
rowResolvers.push({ rowResolvers.push(
name: config.name, ` ${config.name}: (${revLookup}.get(String(row.${defaultPrimaryKey})) || null),`,
code: `(${revLookup}.get(String(row.${defaultPrimaryKey})) || null)`, );
});
} else { } else {
rowResolvers.push({ rowResolvers.push(
name: config.name, ` ${config.name}: (${revLookup}.get(String(row.${defaultPrimaryKey})) || []),`,
code: `(${revLookup}.get(String(row.${defaultPrimaryKey})) || [])`, );
});
} }
} }
} else if (hasNestedReferences(config.schema)) { } else if (hasNestedReferences(config.schema)) {
@ -215,7 +213,7 @@ export function csvToModule(
defaultPrimaryKey, defaultPrimaryKey,
reverseLookupVar, reverseLookupVar,
); );
rowResolvers.push({ name: config.name, code: resolveCode }); rowResolvers.push(` ${config.name}: ${resolveCode},`);
} }
} }
@ -235,10 +233,9 @@ export function csvToModule(
...reverseLookupInits.map((l) => ` ${l}`), ...reverseLookupInits.map((l) => ` ${l}`),
...(rowResolvers.length > 0 ...(rowResolvers.length > 0
? [ ? [
" _resolved = _raw.map(row => {", " _resolved = _raw.map(row => (",
...rowResolvers.map((r) => ` row.${r.name} = ${r.code};`), ...rowResolvers.map((r) => ` row${r.slice(1)}`),
" return row;", " row));",
" });",
] ]
: []), : []),
" return _resolved;", " return _resolved;",

View File

@ -20,7 +20,7 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~rev_orders(customer)", "# orders := ~rev_orders(customer)",
"1,Alice", "1,Alice",
"2,Bob", "2,Bob",
].join("\n"); ].join("\n");
@ -57,7 +57,7 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~rev_orders(customer)", "# orders := ~rev_orders(customer)",
"1,Alice", "1,Alice",
"99,Nobody", "99,Nobody",
].join("\n"); ].join("\n");
@ -79,7 +79,7 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)?", "# orders := ~orders(customer)?",
"99,Nobody", "99,Nobody",
].join("\n"); ].join("\n");
@ -96,7 +96,7 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -124,7 +124,7 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -140,8 +140,8 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"# inject parts = ~parts(user)", "# parts := ~parts(user)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -162,7 +162,7 @@ describe("parseCsv - reverse reference resolution", () => {
"id,name", "id,name",
"string,string", "string,string",
"# This is just a comment", "# This is just a comment",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -190,7 +190,7 @@ describe("parseCsv - reverse reference resolution", () => {
const csv = [ const csv = [
"# id: id of user", "# id: id of user",
"# orders: list of related orders", "# orders: list of related orders",
"# inject orders = ~order(user)", "# orders := ~order(user)",
"id,name", "id,name",
"string,string", "string,string",
"u01,Alice", "u01,Alice",
@ -225,7 +225,7 @@ describe("parseCsv - reverse reference with resolveReferences: false", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject orders = ~orders(customer)", "# orders := ~orders(customer)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");
@ -255,7 +255,7 @@ describe("parseCsv - reverse reference with resolveReferences: false", () => {
const csv = [ const csv = [
"id,name", "id,name",
"string,string", "string,string",
"# inject nonexistent = ~nonexistent(some_key)", "# nonexistent := ~nonexistent(some_key)",
"1,Alice", "1,Alice",
].join("\n"); ].join("\n");

View File

@ -6,7 +6,7 @@ import { fixturesDir } from "../test-utils";
describe("parseCsv - type declarations", () => { describe("parseCsv - type declarations", () => {
it("should parse type declaration from comment line", () => { it("should parse type declaration from comment line", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw' | 'onDiscard'", "# Trigger := 'onPlay' | 'onDraw' | 'onDiscard'",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -30,7 +30,7 @@ describe("parseCsv - type declarations", () => {
it("should use declared type as column schema", () => { it("should use declared type as column schema", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw' | 'onDiscard'", "# Trigger := 'onPlay' | 'onDraw' | 'onDiscard'",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -44,7 +44,7 @@ describe("parseCsv - type declarations", () => {
it("should include declared types in type definition", () => { it("should include declared types in type definition", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw' | 'onDiscard'", "# Trigger := 'onPlay' | 'onDraw' | 'onDiscard'",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -64,8 +64,8 @@ describe("parseCsv - type declarations", () => {
it("should parse multiple type declarations", () => { it("should parse multiple type declarations", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw'", "# Trigger := 'onPlay' | 'onDraw'",
"# type Status = 'active' | 'inactive'", "# Status := 'active' | 'inactive'",
"id,trigger,status", "id,trigger,status",
"string,Trigger,Status", "string,Trigger,Status",
"attack,onPlay,active", "attack,onPlay,active",
@ -80,8 +80,8 @@ describe("parseCsv - type declarations", () => {
it("should include multiple type declarations in type definition", () => { it("should include multiple type declarations in type definition", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw'", "# Trigger := 'onPlay' | 'onDraw'",
"# type Status = 'active' | 'inactive'", "# Status := 'active' | 'inactive'",
"id,trigger,status", "id,trigger,status",
"string,Trigger,Status", "string,Trigger,Status",
"attack,onPlay,active", "attack,onPlay,active",
@ -100,7 +100,7 @@ describe("parseCsv - type declarations", () => {
it("should ignore comment lines that are not type declarations", () => { it("should ignore comment lines that are not type declarations", () => {
const csv = [ const csv = [
"# This is just a comment", "# This is just a comment",
"# type Trigger = 'onPlay' | 'onDraw'", "# Trigger := 'onPlay' | 'onDraw'",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -114,7 +114,7 @@ describe("parseCsv - type declarations", () => {
it("should handle type declaration with array schema", () => { it("should handle type declaration with array schema", () => {
const csv = [ const csv = [
"# type Tags = string[]", "# Tags := string[]",
"id,tags", "id,tags",
"string,Tags", "string,Tags",
"1,[dev; admin; user]", "1,[dev; admin; user]",
@ -129,7 +129,7 @@ describe("parseCsv - type declarations", () => {
it("should include type declaration before table type in output", () => { it("should include type declaration before table type in output", () => {
const csv = [ const csv = [
"# type Status = 'active' | 'inactive'", "# Status := 'active' | 'inactive'",
"id,status", "id,status",
"string,Status", "string,Status",
"1,active", "1,active",
@ -150,7 +150,7 @@ describe("parseCsv - type declarations", () => {
it("should work with type declarations alongside reverse references", () => { it("should work with type declarations alongside reverse references", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw'", "# Trigger := 'onPlay' | 'onDraw'",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -164,8 +164,8 @@ describe("parseCsv - type declarations", () => {
it("should resolve type references inside tuple type declarations", () => { it("should resolve type references inside tuple type declarations", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw' | 'onDiscard'", "# Trigger := 'onPlay' | 'onDraw' | 'onDiscard'",
"# type Effect = [Trigger; @effect; int]", "# Effect := [Trigger; @effect; int]",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -182,8 +182,8 @@ describe("parseCsv - type declarations", () => {
it("should include resolved type references in generated type definition", () => { it("should include resolved type references in generated type definition", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw' | 'onDiscard'", "# Trigger := 'onPlay' | 'onDraw' | 'onDiscard'",
"# type Effect = [Trigger; @effect; int]", "# Effect := [Trigger; @effect; int]",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",
@ -206,7 +206,7 @@ describe("parseCsv - type declarations", () => {
describe("parseCsv - type declarations with resolveReferences: false", () => { describe("parseCsv - type declarations with resolveReferences: false", () => {
it("should populate typeDeclarations even when resolveReferences is false", () => { it("should populate typeDeclarations even when resolveReferences is false", () => {
const csv = [ const csv = [
"# type Trigger = 'onPlay' | 'onDraw'", "# Trigger := 'onPlay' | 'onDraw'",
"id,trigger", "id,trigger",
"string,Trigger", "string,Trigger",
"attack,onPlay", "attack,onPlay",

View File

@ -1,4 +1,7 @@
import type { Schema, ReverseReferenceSchema } from "../types.js"; import type {
Schema,
ReverseReferenceSchema,
} from "../types.js";
import { parseSchema } from "../parser.js"; import { parseSchema } from "../parser.js";
export interface ReverseReferenceDeclaration { export interface ReverseReferenceDeclaration {
@ -16,7 +19,7 @@ export interface TypeDeclaration {
/** /**
* Parse a type declaration from a comment line. * Parse a type declaration from a comment line.
* Format: # type TypeName = schema * Format: # TypeName := schema
* Returns null if the line is not a type declaration. * Returns null if the line is not a type declaration.
*/ */
export function parseTypeDeclaration( export function parseTypeDeclaration(
@ -29,8 +32,8 @@ export function parseTypeDeclaration(
const content = trimmed.slice(commentChar.length).trim(); const content = trimmed.slice(commentChar.length).trim();
// Match pattern: type TypeName = schema // Match pattern: TypeName := schema
const match = content.match(/^type\s+([A-Z][a-zA-Z0-9]*)\s*=\s*(.+)$/); const match = content.match(/^([A-Z][a-zA-Z0-9]*)\s*:=\s*(.+)$/);
if (!match) return null; if (!match) return null;
const [, typeName, schemaString] = match; const [, typeName, schemaString] = match;
@ -211,7 +214,7 @@ export function resolveTypeDeclarationSchema(
/** /**
* Parse a reverse reference declaration from a comment line. * Parse a reverse reference declaration from a comment line.
* Format: # inject fieldName = ~tableName(foreignKey) * Format: # fieldName := ~tableName(foreignKey)
* Returns null if the line is not a reverse reference declaration. * Returns null if the line is not a reverse reference declaration.
*/ */
export function parseReverseReferenceDeclaration( export function parseReverseReferenceDeclaration(
@ -224,8 +227,8 @@ export function parseReverseReferenceDeclaration(
const content = trimmed.slice(commentChar.length).trim(); const content = trimmed.slice(commentChar.length).trim();
// Match pattern: inject fieldName = ~tableName(foreignKey) // Match pattern: fieldName := ~tableName(foreignKey)
const match = content.match(/^inject\s+(\w+)\s*=\s*~(\w+)\((\w+)\)(\?)?$/); const match = content.match(/^(\w+)\s*:=\s*~(\w+)\((\w+)\)(\?)?$/);
if (!match) return null; if (!match) return null;
const [, fieldName, tableName, foreignKey, optionalMark] = match; const [, fieldName, tableName, foreignKey, optionalMark] = match;