Skip to content
Merged
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 @@ -71,6 +71,7 @@
"destr": "^2.0.5",
"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 @@ -101,8 +102,7 @@
"unist-util-visit": "^5.0.0",
"ws": "^8.18.2",
"zod": "^3.25.48",
"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
54 changes: 40 additions & 14 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ onlyBuiltDependencies:
- esbuild
- sharp
- sqlite3
- unrs-resolver
- vue-demi
- workerd
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
117 changes: 115 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,106 @@ export function getOrderedSchemaKeys(shape: ZodRawShape | Record<string, unknown

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

function isJSONProperty(property: Draft07DefinitionProperty | Draft07DefinitionPropertyAllOf | Draft07DefinitionPropertyAnyOf) {
const propertyType = (property as Draft07DefinitionProperty).type
return propertyType === 'object'
|| propertyType === 'array'
|| !!(property as Draft07DefinitionPropertyAnyOf).anyOf
|| !!(property as Draft07DefinitionPropertyAllOf).allOf
}

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, enum?: string[], json?: boolean } = {
name: property,
sqlType: getPropertyType(shape[property]),
type,
nullable: false,
maxLength: (shape[property] as Draft07DefinitionProperty).maxLength,
enum: (shape[property] as Draft07DefinitionProperty).enum,
json: isJSONProperty(shape[property]),
}
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
}

export function getCollectionFieldsTypes(schema: Draft07) {
const sortedKeys = getOrderedSchemaKeys(schema)
return sortedKeys.reduce((acc, key) => {
const property = describeProperty(schema, key)
if (property.json) {
acc[key] = 'json'
}
else if (property.sqlType === 'BOOLEAN') {
acc[key] = 'boolean'
}
else if (property.sqlType === 'DATE') {
acc[key] = 'date'
}
else if (property.sqlType === 'INT') {
acc[key] = 'number'
}
else {
acc[key] = 'string'
}
return acc
}, {} as Record<string, 'string' | 'number' | 'boolean' | 'date' | 'json'>)
}
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'
37 changes: 37 additions & 0 deletions src/types/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export interface Draft07 {
$schema: 'http://json-schema.org/draft-07/schema#'
$ref: string
definitions: Record<string, Draft07Definition>
}

export interface Draft07Definition {
type: string
properties: Record<string, Draft07DefinitionProperty | Draft07DefinitionPropertyAnyOf | Draft07DefinitionPropertyAllOf>
required: string[]
additionalProperties: boolean
}

export interface Draft07DefinitionProperty {
type?: string // missing type means any
properties?: Record<string, Draft07DefinitionProperty>
required?: string[]
default?: unknown
maxLength?: number
format?: string
enum?: string[]
additionalProperties?: boolean | Record<string, Draft07DefinitionProperty>
$content?: {
editor?: {
input?: 'media' | 'icon' // Override the default input for the field
hidden?: boolean // Do not display the field in the editor
}
}
}

export interface Draft07DefinitionPropertyAnyOf {
anyOf: Draft07DefinitionProperty[]
}

export interface Draft07DefinitionPropertyAllOf {
allOf: Draft07DefinitionProperty[]
}
Loading