cogsbox-shape 0.5.66 → 0.5.67
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/schema.d.ts +11 -46
- package/dist/schema.js +9 -7
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -176,9 +176,10 @@ type EnrichedField<K extends string, V, TSchema extends ShapeSchema> = V & {
|
|
|
176
176
|
type EnrichFields<T extends ShapeSchema> = {
|
|
177
177
|
[K in keyof T]: K extends string ? EnrichedField<K, T[K], T> : T[K];
|
|
178
178
|
};
|
|
179
|
+
declare const SchemaWrapperBrand: unique symbol;
|
|
179
180
|
export declare function schema<T extends ShapeSchema>(schema: T): EnrichFields<T> & {
|
|
180
181
|
_tableName: T["_tableName"];
|
|
181
|
-
|
|
182
|
+
[SchemaWrapperBrand]: true;
|
|
182
183
|
};
|
|
183
184
|
export type RelationType = "hasMany" | "hasOne" | "manyToMany";
|
|
184
185
|
type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
@@ -194,14 +195,14 @@ type BaseSchemaField<T extends SQLType = SQLType> = {
|
|
|
194
195
|
type SchemaField<T extends SQLType = SQLType> = BaseSchemaField<T>;
|
|
195
196
|
export type Schema<T extends Record<string, SchemaField | (() => Relation<any>)>> = {
|
|
196
197
|
_tableName: string;
|
|
197
|
-
_schemaWrapper: true;
|
|
198
198
|
__schemaId?: string;
|
|
199
199
|
[key: string]: T[keyof T] | string | ((id: number) => string) | true | undefined;
|
|
200
200
|
};
|
|
201
201
|
type ValidShapeField = ReturnType<typeof s.sql>;
|
|
202
202
|
export type ShapeSchema = {
|
|
203
203
|
_tableName: string;
|
|
204
|
-
[
|
|
204
|
+
[SchemaWrapperBrand]?: true;
|
|
205
|
+
[key: string]: string | ((id: number) => string) | ValidShapeField | true | undefined;
|
|
205
206
|
};
|
|
206
207
|
type Relation<U extends Schema<any>> = {
|
|
207
208
|
type: RelationType;
|
|
@@ -213,31 +214,6 @@ type Relation<U extends Schema<any>> = {
|
|
|
213
214
|
type Prettify<T> = {
|
|
214
215
|
[K in keyof T]: T[K];
|
|
215
216
|
} & {};
|
|
216
|
-
export type InferDBSchema<T> = {
|
|
217
|
-
[K in keyof T as K extends "_tableName" | "_schemaWrapper" | "__schemaId" ? never : K]: T[K] extends {
|
|
218
|
-
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
219
|
-
} ? DbType : T[K] extends {
|
|
220
|
-
dbType: infer DbType extends z.ZodTypeAny;
|
|
221
|
-
} ? DbType : T[K] extends () => {
|
|
222
|
-
type: "hasMany";
|
|
223
|
-
schema: infer S;
|
|
224
|
-
} ? z.ZodArray<z.ZodObject<{
|
|
225
|
-
[P in keyof S as P extends "_tableName" | "__schemaId" ? never : P]: S[P] extends {
|
|
226
|
-
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
227
|
-
} ? DbType : never;
|
|
228
|
-
}>> : T[K] extends () => {
|
|
229
|
-
type: "hasOne" | "belongsTo";
|
|
230
|
-
schema: infer S;
|
|
231
|
-
} ? z.ZodObject<{
|
|
232
|
-
[P in keyof S as P extends "_tableName" | "__schemaId" ? never : P]: S[P] extends {
|
|
233
|
-
zodDbSchema: infer DbType extends z.ZodTypeAny;
|
|
234
|
-
} ? DbType : never;
|
|
235
|
-
}> : never;
|
|
236
|
-
};
|
|
237
|
-
export declare function reference<TField extends object>(config: TField): {
|
|
238
|
-
type: "reference";
|
|
239
|
-
to: TField;
|
|
240
|
-
};
|
|
241
217
|
export declare function createMixedValidationSchema<T extends Schema<any>>(schema: T, clientSchema?: z.ZodObject<any>, dbSchema?: z.ZodObject<any>): z.ZodObject<any>;
|
|
242
218
|
type SchemaDefinition = {
|
|
243
219
|
_tableName: string;
|
|
@@ -302,28 +278,17 @@ type InferDefaultValues2<T> = {
|
|
|
302
278
|
schema: infer S extends SchemaDefinition;
|
|
303
279
|
} ? Prettify<InferDefaultValues2<S>> : never;
|
|
304
280
|
};
|
|
305
|
-
type PrettifiedSchema<T, K extends "Sql" | "Client" | "Validation"> = z.ZodObject<Prettify<Omit<K extends "Sql" ? InferSqlSchema<T> : K extends "Client" ? InferClientSchema<T> : InferValidationSchema<T>, "_schemaWrapper" | "_tableName">>>;
|
|
306
281
|
export declare function createSchema<T extends {
|
|
307
282
|
_tableName: string;
|
|
308
|
-
|
|
309
|
-
}, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R,
|
|
310
|
-
sqlSchema:
|
|
311
|
-
clientSchema:
|
|
312
|
-
validationSchema:
|
|
283
|
+
[SchemaWrapperBrand]?: true;
|
|
284
|
+
}, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R, typeof SchemaWrapperBrand> = Omit<T & R, typeof SchemaWrapperBrand>>(schema: T, relations?: R): {
|
|
285
|
+
sqlSchema: z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>;
|
|
286
|
+
clientSchema: z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>;
|
|
287
|
+
validationSchema: z.ZodObject<Prettify<InferValidationSchema<TActualSchema>>>;
|
|
313
288
|
defaultValues: Prettify<InferDefaultValues2<TActualSchema>>;
|
|
314
|
-
toClient: (dbObject: z.infer<
|
|
315
|
-
toDb: (clientObject: z.infer<
|
|
289
|
+
toClient: (dbObject: z.infer<z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>>) => z.infer<z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>>;
|
|
290
|
+
toDb: (clientObject: z.infer<z.ZodObject<Prettify<InferClientSchema<TActualSchema>>>>) => z.infer<z.ZodObject<Prettify<InferSqlSchema<TActualSchema>>>>;
|
|
316
291
|
};
|
|
317
|
-
export type InferSchemaTypes<T extends Schema<any>> = Prettify<{
|
|
318
|
-
/** The TypeScript type for data as it exists in the database. */
|
|
319
|
-
sql: z.infer<ReturnType<typeof createSchema<T>>["sqlSchema"]>;
|
|
320
|
-
/** The TypeScript type for data as it is represented on the client. */
|
|
321
|
-
client: z.infer<ReturnType<typeof createSchema<T>>["clientSchema"]>;
|
|
322
|
-
/** The TypeScript type for data during validation, often the most flexible shape. */
|
|
323
|
-
validation: z.infer<ReturnType<typeof createSchema<T>>["validationSchema"]>;
|
|
324
|
-
/** The TypeScript type for the default values object. */
|
|
325
|
-
defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
|
|
326
|
-
}>;
|
|
327
292
|
type RelationBuilders<TSchema> = {
|
|
328
293
|
reference: <TField extends object>(fieldGetter: () => TField) => {
|
|
329
294
|
type: "reference";
|
package/dist/schema.js
CHANGED
|
@@ -257,9 +257,12 @@ function createBuilder(config) {
|
|
|
257
257
|
return builderObject;
|
|
258
258
|
}
|
|
259
259
|
// The table function that enriches fields with their key information
|
|
260
|
+
const SchemaWrapperBrand = Symbol("SchemaWrapper");
|
|
261
|
+
// Update the schema function to use the symbol
|
|
260
262
|
export function schema(schema) {
|
|
261
263
|
const enrichedSchema = {
|
|
262
264
|
_tableName: schema._tableName,
|
|
265
|
+
[SchemaWrapperBrand]: true, // Add the symbol property
|
|
263
266
|
};
|
|
264
267
|
for (const key in schema) {
|
|
265
268
|
if (key !== "_tableName" &&
|
|
@@ -270,7 +273,6 @@ export function schema(schema) {
|
|
|
270
273
|
_key: key,
|
|
271
274
|
_fieldType: schema[key],
|
|
272
275
|
},
|
|
273
|
-
// FIX: Assign the parent schema directly and only once.
|
|
274
276
|
__parentTableType: schema,
|
|
275
277
|
};
|
|
276
278
|
}
|
|
@@ -326,12 +328,12 @@ function inferDefaultFromZod(zodType, sqlConfig) {
|
|
|
326
328
|
}
|
|
327
329
|
return undefined;
|
|
328
330
|
}
|
|
329
|
-
export function reference(config) {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
331
|
+
// export function reference<TField extends object>(config: TField) {
|
|
332
|
+
// return {
|
|
333
|
+
// type: "reference" as const,
|
|
334
|
+
// to: config,
|
|
335
|
+
// };
|
|
336
|
+
// }
|
|
335
337
|
export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
|
|
336
338
|
// If schemas are provided, use them (to avoid circular calls)
|
|
337
339
|
if (clientSchema && dbSchema) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.67",
|
|
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",
|