@secondlayer/subgraphs 3.26.2 → 3.26.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/index.js CHANGED
@@ -231,6 +231,13 @@ function quotePgIdent(name) {
231
231
  return name;
232
232
  return `"${name.replace(/"/g, '""')}"`;
233
233
  }
234
+ function snakeToCamel(name) {
235
+ return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
236
+ }
237
+ function pascalCase(name) {
238
+ const camel = snakeToCamel(name);
239
+ return camel.charAt(0).toUpperCase() + camel.slice(1);
240
+ }
234
241
 
235
242
  // src/schema/generator.ts
236
243
  var TYPE_MAP = {
@@ -4436,13 +4443,6 @@ var SYSTEM_FIELDS = [
4436
4443
  line: "DateTime @default(now()) @db.Timestamptz"
4437
4444
  }
4438
4445
  ];
4439
- function snakeToCamel(name) {
4440
- return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
4441
- }
4442
- function pascalCase(name) {
4443
- const camel = snakeToCamel(name);
4444
- return camel.charAt(0).toUpperCase() + camel.slice(1);
4445
- }
4446
4446
  function quotePrismaDefault(value) {
4447
4447
  if (typeof value === "string")
4448
4448
  return `@default("${value.replace(/"/g, "\\\"")}")`;
@@ -4567,13 +4567,6 @@ var BUILDERS_USED = {
4567
4567
  timestamp: "timestamp",
4568
4568
  jsonb: "jsonb"
4569
4569
  };
4570
- function snakeToCamel2(name) {
4571
- return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
4572
- }
4573
- function pascalCase2(name) {
4574
- const camel = snakeToCamel2(name);
4575
- return camel.charAt(0).toUpperCase() + camel.slice(1);
4576
- }
4577
4570
  function literalDefault(value) {
4578
4571
  return typeof value === "string" ? `"${value.replace(/"/g, "\\\"")}"` : `${value}`;
4579
4572
  }
@@ -4585,7 +4578,7 @@ function renderTable2(varName, tableName, table) {
4585
4578
  ` createdAt: timestamp("_created_at", { withTimezone: true }).notNull().defaultNow(),`
4586
4579
  ];
4587
4580
  for (const [colName, col] of Object.entries(table.columns)) {
4588
- let line = ` ${snakeToCamel2(colName)}: ${DRIZZLE_BUILDER[col.type](colName)}`;
4581
+ let line = ` ${snakeToCamel(colName)}: ${DRIZZLE_BUILDER[col.type](colName)}`;
4589
4582
  if (!col.nullable)
4590
4583
  line += ".notNull()";
4591
4584
  if (col.default !== undefined)
@@ -4595,16 +4588,16 @@ function renderTable2(varName, tableName, table) {
4595
4588
  const extras = [];
4596
4589
  for (const [colName, col] of Object.entries(table.columns)) {
4597
4590
  if (col.indexed) {
4598
- const f = snakeToCamel2(colName);
4591
+ const f = snakeToCamel(colName);
4599
4592
  extras.push(` ${f}Idx: index("idx_${tableName}_${colName}").on(t.${f})`);
4600
4593
  }
4601
4594
  }
4602
4595
  table.indexes?.forEach((idxCols, i) => {
4603
- const on = idxCols.map((c) => `t.${snakeToCamel2(c)}`).join(", ");
4596
+ const on = idxCols.map((c) => `t.${snakeToCamel(c)}`).join(", ");
4604
4597
  extras.push(` idx${i}: index("idx_${tableName}_${i}").on(${on})`);
4605
4598
  });
4606
4599
  table.uniqueKeys?.forEach((uqCols, i) => {
4607
- const on = uqCols.map((c) => `t.${snakeToCamel2(c)}`).join(", ");
4600
+ const on = uqCols.map((c) => `t.${snakeToCamel(c)}`).join(", ");
4608
4601
  extras.push(` uq${i}: uniqueIndex("uq_${tableName}_${i}").on(${on})`);
4609
4602
  });
4610
4603
  const extrasBlock = extras.length ? `, (t) => ({
@@ -4622,19 +4615,19 @@ function renderRelations(schema) {
4622
4615
  for (const rel of table.relations ?? []) {
4623
4616
  const list = back.get(rel.references) ?? [];
4624
4617
  list.push({
4625
- field: `${snakeToCamel2(owning)}${pascalCase2(rel.name)}`,
4626
- from: snakeToCamel2(owning)
4618
+ field: `${snakeToCamel(owning)}${pascalCase(rel.name)}`,
4619
+ from: snakeToCamel(owning)
4627
4620
  });
4628
4621
  back.set(rel.references, list);
4629
4622
  }
4630
4623
  }
4631
4624
  const out = [];
4632
4625
  for (const [tableName, table] of Object.entries(schema)) {
4633
- const v = snakeToCamel2(tableName);
4626
+ const v = snakeToCamel(tableName);
4634
4627
  const ones = (table.relations ?? []).map((rel) => {
4635
- const localFields = rel.fields.map((f) => `${v}.${snakeToCamel2(f)}`).join(", ");
4636
- const refFields = rel.referencedColumns.map((c) => `${snakeToCamel2(rel.references)}.${snakeToCamel2(c)}`).join(", ");
4637
- return ` ${rel.name}: one(${snakeToCamel2(rel.references)}, { fields: [${localFields}], references: [${refFields}] }),`;
4628
+ const localFields = rel.fields.map((f) => `${v}.${snakeToCamel(f)}`).join(", ");
4629
+ const refFields = rel.referencedColumns.map((c) => `${snakeToCamel(rel.references)}.${snakeToCamel(c)}`).join(", ");
4630
+ return ` ${rel.name}: one(${snakeToCamel(rel.references)}, { fields: [${localFields}], references: [${refFields}] }),`;
4638
4631
  });
4639
4632
  const manys = (back.get(tableName) ?? []).map((b) => ` ${b.field}: many(${b.from}),`);
4640
4633
  if (ones.length === 0 && manys.length === 0)
@@ -4671,9 +4664,9 @@ function generateDrizzleSchema(def, opts = {}) {
4671
4664
  ...hasRelations ? [`import { relations } from "drizzle-orm";`] : []
4672
4665
  ].join(`
4673
4666
  `);
4674
- const tables = Object.entries(def.schema).map(([name, table]) => renderTable2(snakeToCamel2(name), name, table));
4667
+ const tables = Object.entries(def.schema).map(([name, table]) => renderTable2(snakeToCamel(name), name, table));
4675
4668
  const relationDecls = renderRelations(def.schema);
4676
- const typeExports = Object.keys(def.schema).map((name) => `export type ${pascalCase2(name)} = typeof ${snakeToCamel2(name)}.$inferSelect;`);
4669
+ const typeExports = Object.keys(def.schema).map((name) => `export type ${pascalCase(name)} = typeof ${snakeToCamel(name)}.$inferSelect;`);
4677
4670
  return [
4678
4671
  `// Generated by \`sl subgraphs codegen --target drizzle\` from subgraph "${def.name}". Do not edit by hand.`,
4679
4672
  imports,
@@ -4709,9 +4702,6 @@ var SYSTEM_FIELDS2 = [
4709
4702
  { col: "_tx_id", type: "string" },
4710
4703
  { col: "_created_at", type: "Generated<Date>" }
4711
4704
  ];
4712
- function pascalCase3(name) {
4713
- return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/^[a-z]/, (c) => c.toUpperCase());
4714
- }
4715
4705
  function renderTable3(tableName, table) {
4716
4706
  const lines = SYSTEM_FIELDS2.map((f) => ` ${f.col}: ${f.type};`);
4717
4707
  for (const [colName, col] of Object.entries(table.columns)) {
@@ -4722,7 +4712,7 @@ function renderTable3(tableName, table) {
4722
4712
  type = `Generated<${type}>`;
4723
4713
  lines.push(` ${colName}: ${type};`);
4724
4714
  }
4725
- return `export interface ${pascalCase3(tableName)} {
4715
+ return `export interface ${pascalCase(tableName)} {
4726
4716
  ${lines.join(`
4727
4717
  `)}
4728
4718
  }`;
@@ -4730,12 +4720,12 @@ ${lines.join(`
4730
4720
  function generateKyselySchema(def, opts = {}) {
4731
4721
  const schemaName = opts.schemaName ?? pgSchemaName(def.name);
4732
4722
  const interfaces = Object.entries(def.schema).map(([name, table]) => renderTable3(name, table));
4733
- const dbEntries = Object.keys(def.schema).map((name) => ` "${schemaName}.${name}": ${pascalCase3(name)};`);
4723
+ const dbEntries = Object.keys(def.schema).map((name) => ` "${schemaName}.${name}": ${pascalCase(name)};`);
4734
4724
  const dbRegistry = `export interface DB {
4735
4725
  ${dbEntries.join(`
4736
4726
  `)}
4737
4727
  }`;
4738
- const rowExports = Object.keys(def.schema).map((name) => `export type ${pascalCase3(name)}Row = Selectable<${pascalCase3(name)}>;`);
4728
+ const rowExports = Object.keys(def.schema).map((name) => `export type ${pascalCase(name)}Row = Selectable<${pascalCase(name)}>;`);
4739
4729
  return [
4740
4730
  `// Generated by \`sl subgraphs codegen --target kysely\` from subgraph "${def.name}". Do not edit by hand.`,
4741
4731
  `import type { Generated, Selectable } from "kysely";`,
@@ -4757,12 +4747,6 @@ import {
4757
4747
  SOURCE_READ_PKS,
4758
4748
  SOURCE_READ_TYPES
4759
4749
  } from "@secondlayer/shared/db";
4760
- function pascalCase4(name) {
4761
- return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase()).replace(/^[a-z]/, (c) => c.toUpperCase());
4762
- }
4763
- function snakeToCamel3(name) {
4764
- return name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
4765
- }
4766
4750
  function selectTables(opts) {
4767
4751
  const all = SOURCE_READ_TYPES;
4768
4752
  const names = opts.tables ?? Object.keys(all);
@@ -4787,16 +4771,16 @@ function generateKysely(entries, schemaName) {
4787
4771
  const type = def.nullable ? `${KYSELY_TYPE2[def.type]} | null` : KYSELY_TYPE2[def.type];
4788
4772
  return ` ${col}: ${type};`;
4789
4773
  });
4790
- return `export interface ${pascalCase4(table)} {
4774
+ return `export interface ${pascalCase(table)} {
4791
4775
  ${lines.join(`
4792
4776
  `)}
4793
4777
  }`;
4794
4778
  });
4795
4779
  const dbEntries = entries.map(([table]) => {
4796
4780
  const key2 = schemaName ? `${schemaName}.${table}` : table;
4797
- return ` "${key2}": ${pascalCase4(table)};`;
4781
+ return ` "${key2}": ${pascalCase(table)};`;
4798
4782
  });
4799
- const rows = entries.map(([table]) => `export type ${pascalCase4(table)}Row = Selectable<${pascalCase4(table)}>;`);
4783
+ const rows = entries.map(([table]) => `export type ${pascalCase(table)}Row = Selectable<${pascalCase(table)}>;`);
4800
4784
  return [
4801
4785
  "// Generated by `sl index codegen --target kysely`. Do not edit by hand.",
4802
4786
  `import type { Selectable } from "kysely";`,
@@ -4842,17 +4826,17 @@ function generateDrizzle(entries, schemaName) {
4842
4826
  const tableFn = schemaName ? "sg.table" : "pgTable";
4843
4827
  const tables = entries.map(([table, cols]) => {
4844
4828
  const lines = Object.entries(cols).map(([col, def]) => {
4845
- let line = ` ${snakeToCamel3(col)}: ${DRIZZLE_BUILDER2[def.type](col)}`;
4829
+ let line = ` ${snakeToCamel(col)}: ${DRIZZLE_BUILDER2[def.type](col)}`;
4846
4830
  if (!def.nullable)
4847
4831
  line += ".notNull()";
4848
4832
  return `${line},`;
4849
4833
  });
4850
- return `export const ${snakeToCamel3(table)} = ${tableFn}("${table}", {
4834
+ return `export const ${snakeToCamel(table)} = ${tableFn}("${table}", {
4851
4835
  ${lines.join(`
4852
4836
  `)}
4853
4837
  });`;
4854
4838
  });
4855
- const rows = entries.map(([table]) => `export type ${pascalCase4(table)} = typeof ${snakeToCamel3(table)}.$inferSelect;`);
4839
+ const rows = entries.map(([table]) => `export type ${pascalCase(table)} = typeof ${snakeToCamel(table)}.$inferSelect;`);
4856
4840
  return [
4857
4841
  "// Generated by `sl index codegen --target drizzle`. Do not edit by hand.",
4858
4842
  `import { ${[...used].sort().join(", ")} } from "drizzle-orm/pg-core";`,
@@ -4890,7 +4874,7 @@ function generateJsonSchema(entries) {
4890
4874
  required.push(col);
4891
4875
  }
4892
4876
  }
4893
- defs[pascalCase4(table)] = {
4877
+ defs[pascalCase(table)] = {
4894
4878
  type: "object",
4895
4879
  properties,
4896
4880
  required,
@@ -4923,7 +4907,7 @@ function generatePrisma(entries, schemaName, datasourceEnv) {
4923
4907
  }
4924
4908
  const single = pk.length === 1 ? pk[0] : null;
4925
4909
  const lines = Object.entries(cols).map(([col, def]) => {
4926
- const field = snakeToCamel3(col);
4910
+ const field = snakeToCamel(col);
4927
4911
  const mapped = PRISMA_TYPE2[def.type];
4928
4912
  const attrs = [];
4929
4913
  if (single === col)
@@ -4937,12 +4921,12 @@ function generatePrisma(entries, schemaName, datasourceEnv) {
4937
4921
  });
4938
4922
  const block = [];
4939
4923
  if (!single) {
4940
- block.push(` @@id([${pk.map(snakeToCamel3).join(", ")}])`);
4924
+ block.push(` @@id([${pk.map(snakeToCamel).join(", ")}])`);
4941
4925
  }
4942
4926
  block.push(` @@map("${table}")`);
4943
4927
  if (schemaName)
4944
4928
  block.push(` @@schema("${schemaName}")`);
4945
- models.push(`model ${pascalCase4(table)} {
4929
+ models.push(`model ${pascalCase(table)} {
4946
4930
  ${lines.join(`
4947
4931
  `)}
4948
4932
 
@@ -5377,5 +5361,5 @@ export {
5377
5361
  ByoBreakingChangeError
5378
5362
  };
5379
5363
 
5380
- //# debugId=99416B000428839064756E2164756E21
5364
+ //# debugId=720FEFA20DA031E864756E2164756E21
5381
5365
  //# sourceMappingURL=index.js.map