Skip to content

feat: use json schema instead of zod for internal routines #3347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"fast-glob": "^3.3.3",
"git-url-parse": "^16.1.0",
"jiti": "^2.4.2",
"json-schema-to-typescript": "^15.0.4",
"knitwork": "^1.2.0",
"listhen": "^1.9.0",
"mdast-util-to-hast": "^13.2.0",
Expand Down Expand Up @@ -95,8 +96,7 @@
"unist-util-visit": "^5.0.0",
"ws": "^8.18.1",
"zod": "^3.24.3",
"zod-to-json-schema": "^3.24.5",
"zod-to-ts": "^1.2.0"
"zod-to-json-schema": "^3.24.5"
},
"peerDependencies": {
"@electric-sql/pglite": "*",
Expand Down
73 changes: 50 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/runtime/internal/preview/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function computeValuesBasedOnCollectionSchema(collection: CollectionInfo, data:
const fields: string[] = []
const values: Array<string | number | boolean> = []
const properties = (collection.schema.definitions![collection.name] as JsonSchema7ObjectType).properties
const sortedKeys = getOrderedSchemaKeys(properties)
const sortedKeys = getOrderedSchemaKeys(collection.schema)

sortedKeys.forEach((key) => {
const value = (properties)[key]
Expand Down
84 changes: 82 additions & 2 deletions src/runtime/internal/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import type { ZodRawShape } from 'zod'
import type { Draft07, Draft07DefinitionProperty, Draft07DefinitionPropertyAllOf, Draft07DefinitionPropertyAnyOf } from '@nuxt/content'

export function getOrderedSchemaKeys(shape: ZodRawShape | Record<string, unknown>) {
const propertyTypes = {
string: 'VARCHAR',
number: 'INT',
boolean: 'BOOLEAN',
date: 'DATE',
enum: 'VARCHAR',
object: 'TEXT',
}

export function getOrderedSchemaKeys(schema: Draft07) {
const shape = Object.values(schema.definitions)[0].properties
const keys = new Set([
shape.id ? 'id' : undefined,
shape.title ? 'title' : undefined,
Expand All @@ -9,3 +19,73 @@ export function getOrderedSchemaKeys(shape: ZodRawShape | Record<string, unknown

return Array.from(keys) as string[]
}

function getPropertyType(property: Draft07DefinitionProperty | Draft07DefinitionPropertyAllOf | Draft07DefinitionPropertyAnyOf) {
const propertyType = (property as Draft07DefinitionProperty).type
let type = propertyTypes[propertyType as keyof typeof propertyTypes] || 'TEXT'

if ((property as Draft07DefinitionProperty).format === 'date-time') {
type = 'DATE'
}

if ((property as Draft07DefinitionPropertyAllOf).allOf) {
type = 'TEXT'
}

if ((property as Draft07DefinitionPropertyAnyOf).anyOf) {
type = 'TEXT'

const anyOf = (property as Draft07DefinitionPropertyAnyOf).anyOf
const nullIndex = anyOf.findIndex(t => t.type === 'null')
if (anyOf.length === 2 && nullIndex !== -1) {
type = nullIndex === 0
? getPropertyType(anyOf[1])
: getPropertyType(anyOf[0])
}
}

if (Array.isArray(propertyType) && propertyType.includes('null') && propertyType.length === 2) {
type = propertyType[0] === 'null'
? propertyTypes[propertyType[1] as keyof typeof propertyTypes] || 'TEXT'
: propertyTypes[propertyType[0] as keyof typeof propertyTypes] || 'TEXT'
}

return type
}

export function describeProperty(schema: Draft07, property: string) {
const def = Object.values(schema.definitions)[0]
const shape = def.properties
if (!shape[property]) {
throw new Error(`Property ${property} not found in schema`)
}

const type = (shape[property] as Draft07DefinitionProperty).type

const result: { name: string, sqlType: string, type?: string, default?: unknown, nullable: boolean, maxLength?: number } = {
name: property,
sqlType: getPropertyType(shape[property]),
type,
nullable: false,
maxLength: (shape[property] as Draft07DefinitionProperty).maxLength,
}
if ((shape[property] as Draft07DefinitionPropertyAnyOf).anyOf) {
if (((shape[property] as Draft07DefinitionPropertyAnyOf).anyOf).find(t => t.type === 'null')) {
result.nullable = true
}
}

if (Array.isArray(type) && type.includes('null')) {
result.nullable = true
}

// default value
if ('default' in shape[property]) {
result.default = shape[property].default
}
else if (!def.required.includes(property)) {
result.nullable = true
}

return result
}
17 changes: 6 additions & 11 deletions src/types/collection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ZodObject, ZodRawShape } from 'zod'
import type { zodToJsonSchema } from 'zod-to-json-schema'
import type { Draft07 } from '../types/schema'
import type { MarkdownRoot } from './content'

export interface PageCollections {}
Expand Down Expand Up @@ -52,22 +52,17 @@ export interface DataCollection<T extends ZodRawShape = ZodRawShape> {

export type Collection<T extends ZodRawShape = ZodRawShape> = PageCollection<T> | DataCollection<T>

export interface DefinedCollection<T extends ZodRawShape = ZodRawShape> {
export interface DefinedCollection {
type: CollectionType
source: ResolvedCollectionSource[] | undefined
schema: ZodObject<T>
extendedSchema: ZodObject<T>
schema: Draft07
extendedSchema: Draft07
fields: Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>
}

export interface ResolvedCollection<T extends ZodRawShape = ZodRawShape> {
export interface ResolvedCollection extends DefinedCollection {
name: string
tableName: string
type: CollectionType
source: ResolvedCollectionSource[] | undefined
schema: ZodObject<T>
extendedSchema: ZodObject<T>
fields: Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>
/**
* Whether the collection is private or not.
* Private collections will not be available in the runtime.
Expand All @@ -81,7 +76,7 @@ export interface CollectionInfo {
tableName: string
source: ResolvedCollectionSource[]
type: CollectionType
schema: ReturnType<typeof zodToJsonSchema>
schema: Draft07
fields: Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>
tableDefinition: string
}
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export type * from './content'
export type * from './tree'
export type * from './database'
export type * from './preview'
export type * from './schema'
Loading