cogsbox-shape 0.5.159 → 0.5.160
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 +1 -0
- package/dist/schema.js +47 -18
- package/package.json +1 -1
package/dist/schema.d.ts
CHANGED
|
@@ -213,6 +213,7 @@ export declare function createSchema<T extends {
|
|
|
213
213
|
}, R extends Record<string, any> = {}, TActualSchema extends Omit<T & R, typeof SchemaWrapperBrand> = Omit<T & R, typeof SchemaWrapperBrand>>(schema: T, relations?: R): {
|
|
214
214
|
pk: string[] | null;
|
|
215
215
|
clientPk: string[] | null;
|
|
216
|
+
isClientRecord: ((record: any) => boolean) | undefined;
|
|
216
217
|
sqlSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodSqlSchema">>>;
|
|
217
218
|
clientSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodClientSchema">>>;
|
|
218
219
|
validationSchema: z.ZodObject<Prettify<DeriveSchemaByKey<TActualSchema, "zodValidationSchema">>>;
|
package/dist/schema.js
CHANGED
|
@@ -460,6 +460,7 @@ export function createSchema(schema, relations) {
|
|
|
460
460
|
const fullSchema = { ...schema, ...(relations || {}) };
|
|
461
461
|
let pkKeys = [];
|
|
462
462
|
let clientPkKeys = [];
|
|
463
|
+
// FIRST PASS: Collect all fields and PKs
|
|
463
464
|
for (const key in fullSchema) {
|
|
464
465
|
const value = fullSchema[key];
|
|
465
466
|
if (key === "_tableName" ||
|
|
@@ -470,7 +471,6 @@ export function createSchema(schema, relations) {
|
|
|
470
471
|
typeof value === "function")
|
|
471
472
|
continue;
|
|
472
473
|
const definition = fullSchema[key];
|
|
473
|
-
// Handle new-style references
|
|
474
474
|
if (isReference(definition)) {
|
|
475
475
|
const targetField = definition.getter();
|
|
476
476
|
if (targetField && targetField.config) {
|
|
@@ -488,27 +488,18 @@ export function createSchema(schema, relations) {
|
|
|
488
488
|
fieldTransforms[key] = config.transforms;
|
|
489
489
|
}
|
|
490
490
|
}
|
|
491
|
-
continue;
|
|
491
|
+
continue;
|
|
492
492
|
}
|
|
493
|
-
// THEN, handle all other fields that have a config (builders, relations, etc.)
|
|
494
493
|
if (definition && definition.config) {
|
|
495
494
|
const config = definition.config;
|
|
496
|
-
|
|
497
|
-
pkKeys.push(key);
|
|
498
|
-
}
|
|
499
|
-
if (config.sql?.isClientPk) {
|
|
500
|
-
clientPkKeys.push(key);
|
|
501
|
-
}
|
|
502
|
-
// The rest of the logic for builders
|
|
495
|
+
// ... pk collection logic ...
|
|
503
496
|
const sqlConfig = config.sql;
|
|
504
497
|
if (sqlConfig &&
|
|
505
498
|
typeof sqlConfig === "object" &&
|
|
506
499
|
["hasMany", "hasOne", "belongsTo", "manyToMany"].includes(sqlConfig.type)) {
|
|
507
|
-
// This is for relations, which also aren't PKs, so we just continue.
|
|
508
500
|
continue;
|
|
509
501
|
}
|
|
510
502
|
else {
|
|
511
|
-
// This is for regular s.sql() fields
|
|
512
503
|
sqlFields[key] = config.zodSqlSchema;
|
|
513
504
|
clientFields[key] = config.zodClientSchema;
|
|
514
505
|
serverFields[key] = config.zodValidationSchema;
|
|
@@ -517,19 +508,56 @@ export function createSchema(schema, relations) {
|
|
|
517
508
|
}
|
|
518
509
|
const initialValueOrFn = config.initialValue;
|
|
519
510
|
defaultGenerators[key] = initialValueOrFn;
|
|
520
|
-
|
|
511
|
+
// Get the raw default value
|
|
512
|
+
let rawDefault = isFunction(initialValueOrFn)
|
|
521
513
|
? initialValueOrFn()
|
|
522
514
|
: initialValueOrFn;
|
|
515
|
+
// Apply toClient transform if it exists
|
|
516
|
+
if (config.transforms?.toClient && rawDefault !== undefined) {
|
|
517
|
+
defaultValues[key] = config.transforms.toClient(rawDefault);
|
|
518
|
+
}
|
|
519
|
+
else {
|
|
520
|
+
defaultValues[key] = rawDefault;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
// AFTER THE LOOP: Build isClientRecord checker
|
|
526
|
+
let isClientRecord;
|
|
527
|
+
const explicitChecker = fullSchema.__isClientChecker;
|
|
528
|
+
if (explicitChecker) {
|
|
529
|
+
isClientRecord = explicitChecker;
|
|
530
|
+
}
|
|
531
|
+
else if (clientPkKeys.length > 0) {
|
|
532
|
+
const autoChecks = [];
|
|
533
|
+
for (const key of clientPkKeys) {
|
|
534
|
+
const field = fullSchema[key];
|
|
535
|
+
const sqlType = field?.config?.sql?.type;
|
|
536
|
+
const initialValue = field?.config?.initialValue;
|
|
537
|
+
const dbIsNumeric = sqlType === "int";
|
|
538
|
+
const clientIsString = typeof initialValue === "string";
|
|
539
|
+
if (dbIsNumeric && clientIsString) {
|
|
540
|
+
autoChecks.push({ key, check: (val) => typeof val === "string" });
|
|
523
541
|
}
|
|
524
542
|
}
|
|
543
|
+
if (autoChecks.length > 0) {
|
|
544
|
+
isClientRecord = (record) => autoChecks.some(({ key, check }) => check(record[key]));
|
|
545
|
+
}
|
|
525
546
|
}
|
|
526
547
|
const generateDefaults = () => {
|
|
527
548
|
const freshDefaults = {};
|
|
528
549
|
for (const key in defaultGenerators) {
|
|
529
550
|
const generatorOrValue = defaultGenerators[key];
|
|
530
|
-
|
|
531
|
-
? generatorOrValue()
|
|
532
|
-
: generatorOrValue;
|
|
551
|
+
let rawValue = isFunction(generatorOrValue)
|
|
552
|
+
? generatorOrValue()
|
|
553
|
+
: generatorOrValue;
|
|
554
|
+
// Apply toClient transform if it exists
|
|
555
|
+
if (fieldTransforms[key]?.toClient && rawValue !== undefined) {
|
|
556
|
+
freshDefaults[key] = fieldTransforms[key].toClient(rawValue);
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
freshDefaults[key] = rawValue;
|
|
560
|
+
}
|
|
533
561
|
}
|
|
534
562
|
return freshDefaults;
|
|
535
563
|
};
|
|
@@ -552,8 +580,9 @@ export function createSchema(schema, relations) {
|
|
|
552
580
|
return dbObject;
|
|
553
581
|
};
|
|
554
582
|
return {
|
|
555
|
-
pk: pkKeys
|
|
556
|
-
clientPk: clientPkKeys ? clientPkKeys : null,
|
|
583
|
+
pk: pkKeys.length ? pkKeys : null,
|
|
584
|
+
clientPk: clientPkKeys.length ? clientPkKeys : null,
|
|
585
|
+
isClientRecord, // NOW IT'S IN SCOPE
|
|
557
586
|
sqlSchema: z.object(sqlFields),
|
|
558
587
|
clientSchema: z.object(clientFields),
|
|
559
588
|
validationSchema: z.object(serverFields),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cogsbox-shape",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.160",
|
|
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",
|