cogsbox-shape 0.5.107 → 0.5.108

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.
Files changed (2) hide show
  1. package/dist/schema.d.ts +29 -12
  2. package/package.json +1 -1
package/dist/schema.d.ts CHANGED
@@ -163,7 +163,7 @@ export type ShapeSchema<T extends string = string> = {
163
163
  };
164
164
  type Relation<U extends Schema<any>> = {
165
165
  type: RelationType;
166
- fromKey: string;
166
+ fromKey: keyof U;
167
167
  toKey: () => SchemaField;
168
168
  schema: U;
169
169
  defaultCount?: number;
@@ -192,20 +192,37 @@ type SchemaWithPlaceholders = {
192
192
  _tableName: string;
193
193
  [key: string]: any | PlaceholderReference | PlaceholderRelation<any>;
194
194
  };
195
- type ResolutionConfig<S extends Record<string, SchemaWithPlaceholders>> = {
195
+ type KnownKeys<T> = keyof {
196
+ [K in keyof T as string extends K ? never : K]: T[K];
197
+ };
198
+ /**
199
+ * This is the new, core type. It is NOT a validator. It is a strict TEMPLATE
200
+ * that defines the exact shape the resolver's return object must have.
201
+ * It provides the structure for autocompletion and error checking.
202
+ */
203
+ type ResolutionMap<S extends Record<string, SchemaWithPlaceholders>> = {
196
204
  [TableName in keyof S]?: {
197
- [FieldName in keyof S[TableName] as S[TableName][FieldName] extends PlaceholderReference | PlaceholderRelation<any> ? FieldName : never]?: S[TableName][FieldName] extends PlaceholderReference ? any : S[TableName][FieldName] extends PlaceholderRelation<any> ? {
198
- fromKey: string;
199
- toKey: any;
205
+ [FieldName in keyof S[TableName] as S[TableName][FieldName] extends PlaceholderReference | PlaceholderRelation<any> ? FieldName : never]?: S[TableName][FieldName] extends PlaceholderRelation<any> ? {
206
+ /**
207
+ * The key on the current table (`users`) to join from.
208
+ * Autocompletes with: 'id', 'name', etc.
209
+ */
210
+ fromKey: KnownKeys<S[TableName]>;
211
+ /**
212
+ * The target key on the related table.
213
+ * Must be a field reference from the proxy, e.g., `s.pets.userId`
214
+ */
215
+ toKey: {
216
+ __meta: any;
217
+ __parentTableType: any;
218
+ };
200
219
  defaultCount?: number;
220
+ } : S[TableName][FieldName] extends PlaceholderReference ? {
221
+ __meta: any;
222
+ __parentTableType: any;
201
223
  } : never;
202
224
  };
203
225
  };
204
- type ValidateResolution<T, S extends Record<string, SchemaWithPlaceholders>> = {
205
- [K in keyof T]: K extends keyof S ? T[K] extends object ? {
206
- [F in keyof T[K]]: F extends keyof NonNullable<ResolutionConfig<S>[K]> ? T[K][F] : never;
207
- } : never : never;
208
- };
209
226
  type ResolveField<Field, Resolution, AllSchemas extends Record<string, any>> = Field extends PlaceholderReference ? Resolution : Field extends PlaceholderRelation<infer RelType> ? Resolution extends {
210
227
  toKey: infer ToKey;
211
228
  } ? ToKey extends {
@@ -222,7 +239,7 @@ type ResolveField<Field, Resolution, AllSchemas extends Record<string, any>> = F
222
239
  type ResolveSchema<Schema extends SchemaWithPlaceholders, Resolutions extends Record<string, any>, AllSchemas extends Record<string, any>> = {
223
240
  [K in keyof Schema]: K extends keyof Resolutions ? ResolveField<Schema[K], Resolutions[K], AllSchemas> : Schema[K];
224
241
  };
225
- type ResolvedRegistryWithSchemas<S extends Record<string, SchemaWithPlaceholders>, R extends ResolutionConfig<S>> = {
242
+ type ResolvedRegistryWithSchemas<S extends Record<string, SchemaWithPlaceholders>, R extends ResolutionMap<S>> = {
226
243
  [K in keyof S]: {
227
244
  rawSchema: ResolveSchema<S[K], K extends keyof R ? (R[K] extends object ? R[K] : {}) : {}, S>;
228
245
  zodSchemas: {
@@ -235,7 +252,7 @@ type ResolvedRegistryWithSchemas<S extends Record<string, SchemaWithPlaceholders
235
252
  };
236
253
  };
237
254
  };
238
- export declare function createSchemaBox<S extends Record<string, SchemaWithPlaceholders>, R extends ResolutionConfig<S>>(schemas: S, resolver: (proxy: SchemaProxy<S>) => R & ValidateResolution<R, S>): { [key in keyof ResolvedRegistryWithSchemas<S, R>]: {
255
+ export declare function createSchemaBox<S extends Record<string, SchemaWithPlaceholders>, R extends ResolutionMap<S>>(schemas: S, resolver: (proxy: SchemaProxy<S>) => R): { [key in keyof ResolvedRegistryWithSchemas<S, R>]: {
239
256
  rawSchema: ResolvedRegistryWithSchemas<S, R>[key]["rawSchema"];
240
257
  zodSchemas: ResolvedRegistryWithSchemas<S, R>[key]["zodSchemas"];
241
258
  test: S;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.107",
3
+ "version": "0.5.108",
4
4
  "description": "A TypeScript library for creating type-safe database schemas with Zod validation, SQL type definitions, and automatic client/server transformations. Unifies client, server, and database types through a single schema definition, with built-in support for relationships and serialization.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",