@ronin/schema
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -8,3 +8,6 @@ import { Model as Model$1, ModelIndex, ModelField, ModelTrigger } from '@ronin/compiler'; | ||
} | ||
type Primitives = ReturnType<typeof link> | ReturnType<typeof string> | ReturnType<typeof boolean> | ReturnType<typeof number> | ReturnType<typeof json> | ReturnType<typeof date> | ReturnType<typeof blob>; | ||
type Primitives = ReturnType<typeof link> | ReturnType<typeof string> | ReturnType<typeof boolean> | ReturnType<typeof number> | ReturnType<typeof json> | ReturnType<typeof date> | ReturnType<typeof blob> | NestedFields; | ||
interface NestedFields { | ||
[key: string]: Primitives; | ||
} | ||
interface Model<Fields> extends Omit<Model$1, 'fields' | 'indexes' | 'triggers' | 'presets'> { | ||
@@ -57,3 +60,3 @@ fields?: Fields; | ||
type FieldsToTypes<F> = F extends Record<string, Primitives> ? { | ||
[K in keyof F]: F[K]['type'] extends 'string' ? string : F[K]['type'] extends 'number' ? number : F[K]['type'] extends 'boolean' ? boolean : F[K]['type'] extends 'link' ? string : F[K]['type'] extends 'json' ? object : F[K]['type'] extends 'blob' ? Blob : F[K]['type'] extends 'date' ? Date : never; | ||
[K in keyof F]: F[K] extends Record<string, Primitives> ? FieldsToTypes<F[K]> : F[K]['type'] extends 'string' ? string : F[K]['type'] extends 'number' ? number : F[K]['type'] extends 'boolean' ? boolean : F[K]['type'] extends 'link' ? string : F[K]['type'] extends 'json' ? object : F[K]['type'] extends 'blob' ? Blob : F[K]['type'] extends 'date' ? Date : never; | ||
} : RoninFields; | ||
@@ -60,0 +63,0 @@ type ForbiddenKeys = 'id' | 'ronin' | 'ronin.updatedAt' | 'ronin.createdBy' | 'ronin.updatedBy' | 'ronin.createdAt' | 'ronin.locked'; |
@@ -80,17 +80,27 @@ // src/model/primitives/blob.ts | ||
var serializeFields = (fields) => { | ||
return Object.entries(fields ?? {}).map(([key, value]) => { | ||
const { type, unique, defaultValue, required, name } = value; | ||
const { actions, model: model2 } = value; | ||
return { | ||
slug: key, | ||
name, | ||
unique: unique ?? false, | ||
required: required ?? false, | ||
defaultValue, | ||
type, | ||
// @ts-expect-error: The `target` property exists in the model. | ||
target: model2?.slug, | ||
actions | ||
}; | ||
}); | ||
return Object.entries(fields ?? {}).flatMap( | ||
([key, value]) => { | ||
if (!("type" in value)) { | ||
const result = {}; | ||
for (const k of Object.keys(value)) { | ||
result[`${key}.${k}`] = value[k]; | ||
} | ||
return serializeFields(result); | ||
} | ||
const { type, unique, defaultValue, required, name } = value; | ||
const { actions, model: model2 } = value; | ||
return { | ||
slug: key, | ||
name, | ||
unique: unique ?? false, | ||
required: required ?? false, | ||
defaultValue, | ||
// @ts-expect-error: The `type` property exists in the model. | ||
type, | ||
// @ts-expect-error: The `target` property exists in the model. | ||
target: model2?.slug, | ||
actions | ||
}; | ||
} | ||
); | ||
}; | ||
@@ -97,0 +107,0 @@ var serializePresets = (presets) => { |
{ | ||
"name": "@ronin/schema", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Allows for defining the schema of a RONIN database in code.", |
26471
389