cogsbox-shape 0.5.38 → 0.5.40
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 +26 -7
- package/dist/schema.js +73 -9
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -4751,7 +4751,7 @@ export type ShapeSchema = {
|
|
|
4751
4751
|
type Relation<U extends Schema<any>> = {
|
|
4752
4752
|
type: RelationType;
|
|
4753
4753
|
fromKey: string;
|
|
4754
|
-
toKey: SchemaField;
|
|
4754
|
+
toKey: () => SchemaField;
|
|
4755
4755
|
schema: U;
|
|
4756
4756
|
defaultCount?: number;
|
|
4757
4757
|
};
|
|
@@ -4849,17 +4849,36 @@ export type InferSchemaTypes<T extends {
|
|
|
4849
4849
|
/** The TypeScript type for the default values object. */
|
|
4850
4850
|
defaults: ReturnType<typeof createSchema<T>>["defaultValues"];
|
|
4851
4851
|
}>;
|
|
4852
|
+
type SerializableFieldMetadata = {
|
|
4853
|
+
type: "field";
|
|
4854
|
+
sql: SQLType;
|
|
4855
|
+
};
|
|
4856
|
+
type SerializableRelationMetadata = {
|
|
4857
|
+
type: "relation";
|
|
4858
|
+
relationType: RelationType;
|
|
4859
|
+
fromKey: string;
|
|
4860
|
+
toKey: string;
|
|
4861
|
+
schema: SerializableSchemaMetadata;
|
|
4862
|
+
};
|
|
4863
|
+
type SerializableSchemaMetadata = {
|
|
4864
|
+
_tableName: string;
|
|
4865
|
+
primaryKey: string | null;
|
|
4866
|
+
fields: Record<string, SerializableFieldMetadata>;
|
|
4867
|
+
relations: Record<string, SerializableRelationMetadata>;
|
|
4868
|
+
};
|
|
4852
4869
|
export type ProcessedSyncSchemaEntry<T extends {
|
|
4853
4870
|
_tableName: string;
|
|
4854
4871
|
}> = {
|
|
4855
4872
|
rawSchema: T;
|
|
4856
4873
|
schemas: ReturnType<typeof createSchema<T>>;
|
|
4857
|
-
validate: (data: unknown) => z.SafeParseReturnType<
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4874
|
+
validate: (data: unknown) => z.SafeParseReturnType<any, any>;
|
|
4875
|
+
validateClient: (data: unknown) => z.SafeParseReturnType<any, any>;
|
|
4876
|
+
serializable: {
|
|
4877
|
+
key: string;
|
|
4878
|
+
validationJsonSchema: object;
|
|
4879
|
+
clientJsonSchema: object;
|
|
4880
|
+
metadata: SerializableSchemaMetadata;
|
|
4881
|
+
};
|
|
4863
4882
|
};
|
|
4864
4883
|
export type ProcessedSyncSchemaMap<T extends Record<string, {
|
|
4865
4884
|
_tableName: string;
|
package/dist/schema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
|
2
3
|
export const isFunction = (fn) => typeof fn === "function";
|
|
3
4
|
// Function to create a properly typed current timestamp config
|
|
4
5
|
export function currentTimeStamp() {
|
|
@@ -399,37 +400,100 @@ export function createSchema(schema) {
|
|
|
399
400
|
defaultValues: defaultValues,
|
|
400
401
|
};
|
|
401
402
|
}
|
|
402
|
-
// The
|
|
403
|
+
// --- 2. The Smart Introspection Logic (Also good, keep it) ---
|
|
404
|
+
/**
|
|
405
|
+
* (This is the smart function from the last answer that resolves `toKey` functions)
|
|
406
|
+
*/
|
|
407
|
+
function serializeSchemaMetadata(schema) {
|
|
408
|
+
const fields = {};
|
|
409
|
+
const relations = {};
|
|
410
|
+
let primaryKey = null;
|
|
411
|
+
for (const key in schema) {
|
|
412
|
+
if (key === "_tableName" || key.startsWith("__"))
|
|
413
|
+
continue;
|
|
414
|
+
const definition = schema[key];
|
|
415
|
+
if (isFunction(definition)) {
|
|
416
|
+
const relation = definition();
|
|
417
|
+
if (!isRelation(relation))
|
|
418
|
+
continue;
|
|
419
|
+
let toKeyName = null;
|
|
420
|
+
try {
|
|
421
|
+
const targetFieldBuilder = relation.toKey();
|
|
422
|
+
for (const targetKey in relation.schema) {
|
|
423
|
+
if (relation.schema[targetKey] === targetFieldBuilder) {
|
|
424
|
+
toKeyName = targetKey;
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (!toKeyName)
|
|
429
|
+
throw new Error(`Could not find field name for relation target in schema '${relation.schema._tableName}'.`);
|
|
430
|
+
}
|
|
431
|
+
catch (e) {
|
|
432
|
+
console.error(`[cogsbox-shape] Error resolving 'toKey' for relation '${key}' in schema '${schema._tableName}'.`);
|
|
433
|
+
throw e;
|
|
434
|
+
}
|
|
435
|
+
relations[key] = {
|
|
436
|
+
type: "relation",
|
|
437
|
+
relationType: relation.type,
|
|
438
|
+
fromKey: relation.fromKey,
|
|
439
|
+
toKey: toKeyName,
|
|
440
|
+
schema: serializeSchemaMetadata(relation.schema),
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
else if (definition && definition.config && definition.config.sql) {
|
|
444
|
+
fields[key] = { type: "field", sql: definition.config.sql };
|
|
445
|
+
if (definition.config.sql.pk === true) {
|
|
446
|
+
if (primaryKey)
|
|
447
|
+
console.warn(`[cogsbox-shape] Multiple primary keys in schema '${schema._tableName}'. Using last one found: '${key}'.`);
|
|
448
|
+
primaryKey = key;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return { _tableName: schema._tableName, primaryKey, fields, relations };
|
|
453
|
+
}
|
|
454
|
+
// --- 4. The Final, Corrected `createSyncSchema` Function ---
|
|
403
455
|
export function createSyncSchema(config) {
|
|
404
456
|
const processedOutput = {};
|
|
405
|
-
// Loop through each entry in your config (e.g., 'chatMessages')
|
|
406
457
|
for (const key in config) {
|
|
407
458
|
const entry = config[key];
|
|
408
|
-
// 1
|
|
459
|
+
// Part 1: Generate Zod Schemas and Live Validators (same as before)
|
|
409
460
|
const { sqlSchema, clientSchema, validationSchema, defaultValues } = createSchema(entry.schema);
|
|
410
|
-
// 2. Determine the FINAL validation schema.
|
|
411
461
|
const finalValidationSchema = entry.validation
|
|
412
462
|
? entry.validation(validationSchema)
|
|
413
463
|
: validationSchema;
|
|
414
|
-
// 3. Determine the FINAL client schema.
|
|
415
464
|
const finalClientSchema = entry.client
|
|
416
465
|
? entry.client(clientSchema)
|
|
417
466
|
: clientSchema;
|
|
418
|
-
//
|
|
467
|
+
// Part 2: Generate the Serializable Payload (NEW, integrated logic)
|
|
468
|
+
const validationJsonSchema = zodToJsonSchema(finalValidationSchema, {
|
|
469
|
+
target: "jsonSchema7",
|
|
470
|
+
$refStrategy: "none",
|
|
471
|
+
});
|
|
472
|
+
const clientJsonSchema = zodToJsonSchema(finalClientSchema, {
|
|
473
|
+
target: "jsonSchema7",
|
|
474
|
+
$refStrategy: "none",
|
|
475
|
+
});
|
|
476
|
+
const metadata = serializeSchemaMetadata(entry.schema);
|
|
477
|
+
// Part 3: Combine EVERYTHING into the final output object for this key
|
|
419
478
|
processedOutput[key] = {
|
|
479
|
+
// For runtime server use
|
|
420
480
|
rawSchema: entry.schema,
|
|
421
|
-
// Keep the generated schemas for reference or other uses
|
|
422
481
|
schemas: {
|
|
423
482
|
sql: sqlSchema,
|
|
424
483
|
client: clientSchema,
|
|
425
484
|
validation: validationSchema,
|
|
426
485
|
defaults: defaultValues,
|
|
427
486
|
},
|
|
428
|
-
// Create the final validator FUNCTIONS that the DO can call directly.
|
|
429
487
|
validate: (data) => finalValidationSchema.safeParse(data),
|
|
430
488
|
validateClient: (data) => finalClientSchema.safeParse(data),
|
|
489
|
+
// For deployment to DO
|
|
490
|
+
serializable: {
|
|
491
|
+
key,
|
|
492
|
+
validationJsonSchema,
|
|
493
|
+
clientJsonSchema,
|
|
494
|
+
metadata,
|
|
495
|
+
},
|
|
431
496
|
};
|
|
432
497
|
}
|
|
433
|
-
// Return the new object containing the schemas AND the validator functions.
|
|
434
498
|
return processedOutput;
|
|
435
499
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.40",
|
|
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",
|