cogsbox-shape 0.5.58 → 0.5.59

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 CHANGED
@@ -5,7 +5,7 @@ type CurrentTimestampConfig = {
5
5
  };
6
6
  export declare const isFunction: (fn: unknown) => fn is Function;
7
7
  export declare function currentTimeStamp(): CurrentTimestampConfig;
8
- type SQLType = ({
8
+ export type SQLType = ({
9
9
  type: "int";
10
10
  nullable?: boolean;
11
11
  default?: number;
@@ -205,7 +205,7 @@ export declare function manyToMany<T extends Schema<any>>(config: {
205
205
  schema: T;
206
206
  defaultCount: number | undefined;
207
207
  };
208
- type RelationType = "hasMany" | "belongsTo" | "hasOne" | "manyToMany";
208
+ export type RelationType = "hasMany" | "hasOne" | "manyToMany";
209
209
  type BaseSchemaField<T extends SQLType = SQLType> = {
210
210
  type: "field";
211
211
  sql: T;
@@ -331,49 +331,4 @@ export type InferSchemaTypes<T extends {
331
331
  /** The TypeScript type for the default values object. */
332
332
  defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
333
333
  }>;
334
- type SerializableFieldMetadata = {
335
- type: "field";
336
- sql: SQLType;
337
- };
338
- type SerializableRelationMetadata = {
339
- type: "relation";
340
- relationType: RelationType;
341
- fromKey: string;
342
- toKey: string;
343
- schema: SerializableSchemaMetadata;
344
- };
345
- type SerializableSchemaMetadata = {
346
- _tableName: string;
347
- primaryKey: string | null;
348
- fields: Record<string, SerializableFieldMetadata>;
349
- relations: Record<string, SerializableRelationMetadata>;
350
- };
351
- export type ProcessedSyncSchemaEntry<T extends {
352
- _tableName: string;
353
- }> = {
354
- rawSchema: T;
355
- schemas: ReturnType<typeof createSchema<T>>;
356
- validate: (data: unknown) => z.SafeParseReturnType<any, any>;
357
- validateClient: (data: unknown) => z.SafeParseReturnType<any, any>;
358
- serializable: {
359
- key: string;
360
- validationJsonSchema: object;
361
- clientJsonSchema: object;
362
- metadata: SerializableSchemaMetadata;
363
- };
364
- };
365
- export type ProcessedSyncSchemaMap<T extends Record<string, {
366
- _tableName: string;
367
- }>> = {
368
- [K in keyof T]: ProcessedSyncSchemaEntry<T[K]>;
369
- };
370
- export declare function createSyncSchema<T extends Record<string, {
371
- _tableName: string;
372
- }>>(config: {
373
- [K in keyof T]: {
374
- schema: T[K];
375
- validation?: (schema: ReturnType<typeof createSchema<T[K]>>["validationSchema"]) => z.ZodSchema;
376
- client?: (schema: ReturnType<typeof createSchema<T[K]>>["clientSchema"]) => z.ZodSchema;
377
- };
378
- }): ProcessedSyncSchemaMap<T>;
379
334
  export {};
package/dist/schema.js CHANGED
@@ -473,130 +473,3 @@ export function createSchema(schema) {
473
473
  defaultValues: defaultValues,
474
474
  };
475
475
  }
476
- // --- 2. The Smart Introspection Logic (Also good, keep it) ---
477
- /**
478
- * (This is the smart function from the last answer that resolves `toKey` functions)
479
- */
480
- // In your cogsbox-shape file, replace the entire `serializeSchemaMetadata` function.
481
- function serializeSchemaMetadata(schema) {
482
- const fields = {};
483
- const relations = {};
484
- let primaryKey = null;
485
- for (const key in schema) {
486
- if (key === "_tableName" || key.startsWith("__"))
487
- continue;
488
- const definition = schema[key];
489
- // Case 1: Is it a relation object? Check the `type` property.
490
- if (definition &&
491
- (definition.type === "hasMany" ||
492
- definition.type === "hasOne" ||
493
- definition.type === "belongsTo" ||
494
- definition.type === "manyToMany")) {
495
- const relation = definition;
496
- let toKeyName = null;
497
- try {
498
- let targetFieldDefinition = relation.toKey;
499
- if (targetFieldDefinition &&
500
- targetFieldDefinition.type === "reference") {
501
- targetFieldDefinition = targetFieldDefinition.to;
502
- }
503
- const targetSchemaObject = relation.schema;
504
- for (const targetKey in targetSchemaObject) {
505
- if (targetSchemaObject[targetKey] === targetFieldDefinition) {
506
- toKeyName = targetKey;
507
- break;
508
- }
509
- }
510
- if (!toKeyName)
511
- throw new Error(`Could not find field name for relation target.`);
512
- relations[key] = {
513
- type: "relation",
514
- relationType: relation.type,
515
- fromKey: relation.fromKey,
516
- toKey: toKeyName,
517
- schema: serializeSchemaMetadata(targetSchemaObject),
518
- };
519
- }
520
- catch (e) {
521
- console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
522
- throw e;
523
- }
524
- // This is the critical part: ADD the processed relation to the relations object.
525
- relations[key] = {
526
- type: "relation",
527
- relationType: relation.type,
528
- fromKey: relation.fromKey,
529
- toKey: toKeyName,
530
- schema: serializeSchemaMetadata(relation.schema),
531
- };
532
- }
533
- else if (definition && definition.config && definition.config.sql) {
534
- fields[key] = { type: "field", sql: definition.config.sql };
535
- if (definition.config.sql.pk === true) {
536
- if (primaryKey)
537
- console.warn(`[cogsbox-shape] Multiple primary keys found. Using last one: '${key}'.`);
538
- primaryKey = key;
539
- }
540
- }
541
- else if (definition && definition.type === "reference") {
542
- const targetFieldBuilder = definition.to();
543
- if (targetFieldBuilder &&
544
- targetFieldBuilder.config &&
545
- targetFieldBuilder.config.sql) {
546
- fields[key] = { type: "field", sql: targetFieldBuilder.config.sql };
547
- if (targetFieldBuilder.config.sql.pk) {
548
- if (primaryKey)
549
- console.warn(`[cogsbox-shape] Multiple primary keys found. Using last one: '${key}'.`);
550
- primaryKey = key;
551
- }
552
- }
553
- }
554
- }
555
- return { _tableName: schema._tableName, primaryKey, fields, relations };
556
- }
557
- // --- 4. The Final, Corrected `createSyncSchema` Function ---
558
- export function createSyncSchema(config) {
559
- const processedOutput = {};
560
- for (const key in config) {
561
- const entry = config[key];
562
- // Part 1: Generate Zod Schemas and Live Validators (same as before)
563
- const { sqlSchema, clientSchema, validationSchema, defaultValues } = createSchema(entry.schema);
564
- const finalValidationSchema = entry.validation
565
- ? entry.validation(validationSchema)
566
- : validationSchema;
567
- const finalClientSchema = entry.client
568
- ? entry.client(clientSchema)
569
- : clientSchema;
570
- // Part 2: Generate the Serializable Payload (NEW, integrated logic)
571
- const validationJsonSchema = zodToJsonSchema(finalValidationSchema, {
572
- target: "jsonSchema7",
573
- $refStrategy: "none",
574
- });
575
- const clientJsonSchema = zodToJsonSchema(finalClientSchema, {
576
- target: "jsonSchema7",
577
- $refStrategy: "none",
578
- });
579
- const metadata = serializeSchemaMetadata(entry.schema);
580
- // Part 3: Combine EVERYTHING into the final output object for this key
581
- processedOutput[key] = {
582
- // For runtime server use
583
- rawSchema: entry.schema,
584
- schemas: {
585
- sql: sqlSchema,
586
- client: clientSchema,
587
- validation: validationSchema,
588
- defaults: defaultValues,
589
- },
590
- validate: (data) => finalValidationSchema.safeParse(data),
591
- validateClient: (data) => finalClientSchema.safeParse(data),
592
- // For deployment to DO
593
- serializable: {
594
- key,
595
- validationJsonSchema,
596
- clientJsonSchema,
597
- metadata,
598
- },
599
- };
600
- }
601
- return processedOutput;
602
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-shape",
3
- "version": "0.5.58",
3
+ "version": "0.5.59",
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",