cogsbox-shape 0.5.29 → 0.5.31

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.js CHANGED
@@ -1,6 +1,4 @@
1
1
  import { z } from "zod";
2
- import { v4 as uuidv4 } from "uuid";
3
- import zodToJsonSchema, {} from "zod-to-json-schema";
4
2
  export const isFunction = (fn) => typeof fn === "function";
5
3
  // Function to create a properly typed current timestamp config
6
4
  export function currentTimeStamp() {
@@ -235,29 +233,29 @@ export function manyToMany(config) {
235
233
  });
236
234
  }
237
235
  function inferDefaultFromZod(zodType, sqlConfig) {
238
- if (sqlConfig?.pk) {
239
- return uuidv4();
240
- }
241
- if (zodType instanceof z.ZodOptional) {
242
- return undefined;
236
+ // Check SQL type first for better defaults
237
+ if (sqlConfig && !sqlConfig.nullable) {
238
+ switch (sqlConfig.type) {
239
+ case "varchar":
240
+ case "text":
241
+ case "char":
242
+ case "longtext":
243
+ return "";
244
+ case "int":
245
+ return 0;
246
+ case "boolean":
247
+ return false;
248
+ case "date":
249
+ case "datetime":
250
+ return new Date();
251
+ }
243
252
  }
244
- if (zodType instanceof z.ZodNullable) {
253
+ if (sqlConfig?.nullable) {
245
254
  return null;
246
255
  }
247
- if (zodType instanceof z.ZodArray) {
248
- return [];
249
- }
250
- if (zodType instanceof z.ZodObject) {
251
- return {};
252
- }
253
- if (zodType instanceof z.ZodString) {
254
- return "";
255
- }
256
- if (zodType instanceof z.ZodNumber) {
257
- return 0;
258
- }
259
- if (zodType instanceof z.ZodBoolean) {
260
- return false;
256
+ // Fall back to existing zod-based inference
257
+ if (zodType instanceof z.ZodOptional) {
258
+ return undefined;
261
259
  }
262
260
  // Check for explicit default last
263
261
  if (zodType instanceof z.ZodDefault && zodType._def?.defaultValue) {
@@ -269,9 +267,9 @@ function inferDefaultFromZod(zodType, sqlConfig) {
269
267
  }
270
268
  export function reference(config) {
271
269
  return {
272
- ...config.field,
270
+ field: config.field,
273
271
  type: "reference",
274
- to: config.to,
272
+ to: typeof config.to === "function" ? config.to : () => config.to,
275
273
  };
276
274
  }
277
275
  export function createMixedValidationSchema(schema, clientSchema, dbSchema) {
@@ -394,3 +392,6 @@ export function createSchema(schema) {
394
392
  defaultValues: defaultValues,
395
393
  };
396
394
  }
395
+ export function createSyncSchema(config) {
396
+ return config;
397
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.29",
3
+ "version": "0.5.31",
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",