appflare 0.0.18 → 0.0.19

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.
@@ -13,6 +13,16 @@ type NumericKeys<T> =
13
13
  {
14
14
  [K in keyof T]: NonNil<T[K]> extends number | bigint ? K : never;
15
15
  }[keyof T] & string;
16
+
17
+ type Friendly<T> = T extends Id<any>
18
+ ? T | string
19
+ : T extends Array<infer TItem>
20
+ ? Array<Friendly<TItem>>
21
+ : T;
22
+
23
+ type FriendlyDoc<TDoc> = {
24
+ [K in keyof TDoc]: Friendly<TDoc[K]>;
25
+ };
16
26
 
17
27
  type PopulateValue<T> = T extends Id<infer TTable>
18
28
  ? (TTable extends TableNames ? Doc<TTable> : never)
@@ -53,16 +63,16 @@ type RegexOperand<T> = NonNil<T> extends string
53
63
  type ArrayOperand<T> = ReadonlyArray<NonNil<T>>;
54
64
 
55
65
  type QueryWhereField<T> =
56
- | NonNil<T>
66
+ | Friendly<NonNil<T>>
57
67
  | {
58
- eq?: NonNil<T>;
59
- $eq?: NonNil<T>;
60
- ne?: NonNil<T>;
61
- $ne?: NonNil<T>;
62
- in?: ArrayOperand<T>;
63
- $in?: ArrayOperand<T>;
64
- nin?: ArrayOperand<T>;
65
- $nin?: ArrayOperand<T>;
68
+ eq?: Friendly<NonNil<T>>;
69
+ $eq?: Friendly<NonNil<T>>;
70
+ ne?: Friendly<NonNil<T>>;
71
+ $ne?: Friendly<NonNil<T>>;
72
+ in?: ReadonlyArray<Friendly<NonNil<T>>>;
73
+ $in?: ReadonlyArray<Friendly<NonNil<T>>>;
74
+ nin?: ReadonlyArray<Friendly<NonNil<T>>>;
75
+ $nin?: ReadonlyArray<Friendly<NonNil<T>>>;
66
76
  gt?: Comparable<T>;
67
77
  $gt?: Comparable<T>;
68
78
  gte?: Comparable<T>;
@@ -605,9 +615,8 @@ export const query = <TArgs extends QueryArgsShape, TResult>(
605
615
  definition: QueryDefinition<TArgs, TResult>
606
616
  ): QueryDefinition<TArgs, TResult> => definition;
607
617
 
608
- export type EditableDoc<TableName extends TableNames> = Omit<
609
- TableDocMap[TableName],
610
- "_id" | "_creationTime"
618
+ export type EditableDoc<TableName extends TableNames> = FriendlyDoc<
619
+ Omit<TableDocMap[TableName], "_id" | "_creationTime">
611
620
  >;
612
621
 
613
622
  export interface DatabaseWriter extends DatabaseReader {}
@@ -5,7 +5,7 @@ import { getZodObjectShape, renderField } from "../utils/zod-utils";
5
5
 
6
6
  function generateDocParts(
7
7
  tableNames: string[],
8
- schema: any
8
+ schema: any,
9
9
  ): {
10
10
  docInterfaces: string[];
11
11
  docMapEntries: string[];
@@ -40,13 +40,13 @@ function generateDocParts(
40
40
  }
41
41
 
42
42
  export async function getSchemaTableNames(
43
- schemaPathAbs: string
43
+ schemaPathAbs: string,
44
44
  ): Promise<string[]> {
45
45
  const mod = await import(pathToFileURL(schemaPathAbs).href);
46
46
  const schema = mod?.default ?? mod;
47
47
  if (!schema || typeof schema !== "object") {
48
48
  throw new Error(
49
- `Invalid schema export in ${schemaPathAbs} (expected default export object)`
49
+ `Invalid schema export in ${schemaPathAbs} (expected default export object)`,
50
50
  );
51
51
  }
52
52
  return Object.keys(schema).sort();
@@ -61,7 +61,7 @@ export async function generateSchemaTypes(params: {
61
61
  const schema = mod?.default ?? mod;
62
62
  if (!schema || typeof schema !== "object") {
63
63
  throw new Error(
64
- `Invalid schema export in ${params.schemaPathAbs} (expected default export object)`
64
+ `Invalid schema export in ${params.schemaPathAbs} (expected default export object)`,
65
65
  );
66
66
  }
67
67
 
@@ -72,7 +72,7 @@ export async function generateSchemaTypes(params: {
72
72
 
73
73
  const { docInterfaces, docMapEntries, tableIndexEntries } = generateDocParts(
74
74
  tableNames,
75
- schema
75
+ schema,
76
76
  );
77
77
 
78
78
  const configImportPath =
@@ -130,7 +130,7 @@ export type AnyValidator = SchemaValidator<unknown>;
130
130
 
131
131
  export type TableNames = ${tableNames.map((t) => JSON.stringify(t)).join(" | ")};
132
132
 
133
- export type Id<TableName extends TableNames> = string & { __table?: TableName };
133
+ export type Id<TableName extends TableNames> = string & { __table: TableName };
134
134
 
135
135
  ${docInterfaces.join("\n\n")}
136
136
 
@@ -63,8 +63,8 @@ function renderType(schema: any): { tsType: string; optional: boolean } {
63
63
  const description: string | undefined =
64
64
  schema?.description ?? def?.description;
65
65
  if (typeof description === "string" && description.startsWith("ref:")) {
66
- // Treat reference fields as plain strings in generated types for friendlier typing/UX.
67
- return { tsType: "string", optional: false };
66
+ const refTableName = description.slice(4);
67
+ return { tsType: `Id<${JSON.stringify(refTableName)}>`, optional: false };
68
68
  }
69
69
  return { tsType: "string", optional: false };
70
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appflare",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "bin": {
5
5
  "appflare": "./cli/index.ts"
6
6
  },