lazypock 0.10.2 → 0.12.0

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/index.cjs CHANGED
@@ -1437,6 +1437,7 @@ function fieldTypeScriptType(field, fallback = "unknown") {
1437
1437
  case "editor":
1438
1438
  case "date":
1439
1439
  case "datetime":
1440
+ case "autodate":
1440
1441
  return "string";
1441
1442
  case "number":
1442
1443
  return "number";
@@ -1480,6 +1481,7 @@ function fieldTypeKind(field) {
1480
1481
  case "editor":
1481
1482
  case "date":
1482
1483
  case "datetime":
1484
+ case "autodate":
1483
1485
  case "select":
1484
1486
  case "file":
1485
1487
  return "string";
@@ -1525,6 +1527,13 @@ function schemaFieldType(field) {
1525
1527
  }
1526
1528
 
1527
1529
  // src/codegen.ts
1530
+ var BASE_RECORD_KEYS = /* @__PURE__ */ new Set([
1531
+ "id",
1532
+ "collectionId",
1533
+ "collectionName",
1534
+ "created",
1535
+ "updated"
1536
+ ]);
1528
1537
  function collectionTypeName(name) {
1529
1538
  return name.split(/[^a-zA-Z0-9]+/).filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("").replace(/^[0-9]/, "_$&");
1530
1539
  }
@@ -1553,10 +1562,13 @@ function generateTypes(collections, options = {}) {
1553
1562
  updated: string;
1554
1563
  }`);
1555
1564
  }
1565
+ const collById = new Map(
1566
+ filtered.filter((c) => c.id !== void 0 && c.id !== "").map((c) => [String(c.id), c])
1567
+ );
1556
1568
  for (const coll of filtered) {
1557
1569
  const typeName = collectionTypeName(coll.name);
1558
1570
  const fields = coll.fields ?? [];
1559
- const lines = fields.map((f) => memberLine(f)).filter((l) => l !== "");
1571
+ const lines = fields.map((f) => memberLine(f, includeBaseFields)).filter((l) => l !== "");
1560
1572
  const body = lines.join("\n");
1561
1573
  sections.push(
1562
1574
  `export interface ${typeName}Record${renderInterface({
@@ -1582,6 +1594,7 @@ function generateTypes(collections, options = {}) {
1582
1594
  for (const f of fields) {
1583
1595
  if (f.type === "password") continue;
1584
1596
  const key = fieldKey(f.name);
1597
+ if (includeBaseFields && BASE_RECORD_KEYS.has(key)) continue;
1585
1598
  const type = fieldTypeScriptType(f);
1586
1599
  if (type === "never") continue;
1587
1600
  queryLines.push(` ${JSON.stringify(key)}?: ${type};`);
@@ -1592,6 +1605,24 @@ function generateTypes(collections, options = {}) {
1592
1605
  body: queryLines.join("\n")
1593
1606
  })}`
1594
1607
  );
1608
+ const expandLines = [];
1609
+ for (const f of fields) {
1610
+ if (f.type !== "relation") continue;
1611
+ const target = collById.get(String(f.options?.collectionId ?? "")) ?? filtered.find(
1612
+ (c) => c.name === String(f.options?.collection ?? "")
1613
+ );
1614
+ if (!target) continue;
1615
+ const targetType = `${collectionTypeName(target.name)}Record${target.type === "auth" ? " & AuthRecord" : ""}`;
1616
+ const many = (f.options?.maxSelect ?? 1) > 1;
1617
+ expandLines.push(
1618
+ ` ${JSON.stringify(fieldKey(f.name))}: ${many ? `Array<${targetType}>` : targetType};`
1619
+ );
1620
+ }
1621
+ sections.push(
1622
+ `export interface ${typeName}ExpandMap${renderInterface({
1623
+ body: expandLines.join("\n")
1624
+ })}`
1625
+ );
1595
1626
  }
1596
1627
  sections.push(`export interface AuthRecord extends BaseRecord {
1597
1628
  email: string;
@@ -1612,6 +1643,10 @@ ${createMapEntries}
1612
1643
  ).join("\n");
1613
1644
  sections.push(`export interface LazypockQueryFields {
1614
1645
  ${queryMapEntries}
1646
+ }`);
1647
+ const expandMapEntries = filtered.map((c) => ` "${c.name}": ${collectionTypeName(c.name)}ExpandMap;`).join("\n");
1648
+ sections.push(`export interface LazypockExpandMaps {
1649
+ ${expandMapEntries}
1615
1650
  }`);
1616
1651
  const schemaEntries = collections.map(
1617
1652
  (c) => ` {
@@ -1665,25 +1700,31 @@ export class TypedClient extends LazypockClient {
1665
1700
  // The (string & {}) intersection keeps the union from collapsing to plain
1666
1701
  // string (which would silently kill the suggestions).
1667
1702
  //
1668
- // The service binds three types: the read model, the write-only *CreateData,
1669
- // and the *QueryFields (filter/sort/expand/select keys). QueryFields is what
1670
- // lets hidden relation fields \u2014 excluded from the read model but still
1671
- // expandable/filterable at runtime \u2014 typecheck:
1703
+ // The service binds four types: the read model, the write-only *CreateData,
1704
+ // the *QueryFields (filter/sort/expand/select keys), and the *ExpandMap
1705
+ // (relation field \u2192 target record type). QueryFields is what lets hidden
1706
+ // relation fields \u2014 excluded from the read model but still expandable/
1707
+ // filterable at runtime \u2014 typecheck:
1672
1708
  // client.collection("project_members").getFullList({ expand: "user" }) // \u2713
1709
+ // ExpandMap is what types the expanded data on the result:
1710
+ // const [rec] = await client.collection("posts").getFullList({ expand: "user" });
1711
+ // rec.expand?.user // \u2713 UsersRecord, not unknown
1673
1712
  override collection<K extends keyof LazypockCollections | (string & {})>(
1674
1713
  name: K,
1675
1714
  ): K extends keyof LazypockCollections
1676
1715
  ? CollectionService<
1677
1716
  LazypockCollections[K],
1678
1717
  LazypockCreateData[K],
1679
- LazypockQueryFields[K]
1718
+ LazypockQueryFields[K],
1719
+ LazypockExpandMaps[K]
1680
1720
  >
1681
1721
  : CollectionService<unknown> {
1682
1722
  return super.collection(name) as K extends keyof LazypockCollections
1683
1723
  ? CollectionService<
1684
1724
  LazypockCollections[K],
1685
1725
  LazypockCreateData[K],
1686
- LazypockQueryFields[K]
1726
+ LazypockQueryFields[K],
1727
+ LazypockExpandMaps[K]
1687
1728
  >
1688
1729
  : CollectionService<unknown>;
1689
1730
  }
@@ -1698,9 +1739,10 @@ function renderInterface(opts) {
1698
1739
  ${opts.body}
1699
1740
  }`;
1700
1741
  }
1701
- function memberLine(f) {
1742
+ function memberLine(f, skipBaseKeys = false) {
1702
1743
  if (f.hidden) return "";
1703
1744
  const key = fieldKey(f.name);
1745
+ if (skipBaseKeys && BASE_RECORD_KEYS.has(key)) return "";
1704
1746
  const req = f.required || f.type === "password" ? "" : "?";
1705
1747
  const type = fieldTypeScriptType(f);
1706
1748
  if (type === "never") return "";