Skip to content

Commit

Permalink
attribute supplement schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Feb 14, 2024
1 parent 25a6a72 commit 6f85899
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/db/generated/attributeSupplementSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from 'zod'

export const attributeSupplementSchema = {
accessInstructions: z.object({
access_type: z.enum(['email', 'file', 'link', 'location', 'other', 'phone']),
access_value: z.union([z.string(), z.null()]).optional(),
instructions: z.string(),
}),
currency: z.any(),
incompatible: z.any(),
incompatibleData: z.array(z.record(z.any())),
number: z.object({ num: z.number() }),
'num-max': z.any(),
numMax: z.object({ max: z.number() }),
'num-min': z.any(),
numMin: z.object({ min: z.number() }),
'num-min-max': z.any(),
numMinMaxOrRange: z.union([
z.object({ min: z.number() }),
z.object({ max: z.number() }),
z.object({ max: z.number(), min: z.number() }),
]),
numRange: z.object({ max: z.number(), min: z.number() }),
otherDescribe: z.object({ other: z.string() }),
}

export const isAttributeSupplementSchema = (schema: string): schema is AttributeSupplementSchemas =>
Object.keys(attributeSupplementSchema).includes(schema)

export type AttributeSupplementSchemas = keyof typeof attributeSupplementSchema
1 change: 1 addition & 0 deletions packages/db/lib/generateData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const tasks = new Listr<Context>(
defineJob('Service Categories', job.generateServiceCategories),
defineJob('Language lists', job.generateLanguageFiles),
defineJob('Translation Namespaces', job.generateNamespaces),
defineJob('Attribute Supplement Data Schemas', job.generateDataSchemas),
],
{ concurrent: true }
),
Expand Down
35 changes: 35 additions & 0 deletions packages/db/lib/generators/attributeSuppDataSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { type JsonSchemaObject, jsonSchemaToZod } from 'json-schema-to-zod'

import { prisma } from '~db/client'
import { type ListrTask } from '~db/lib/generateData'

import { writeOutput } from './common'

export const generateDataSchemas = async (task: ListrTask) => {
const data = await prisma.attributeSupplementDataSchema.findMany({
where: {
active: true,
},
select: {
tag: true,
schema: true,
},
orderBy: { tag: 'asc' },
})
const schemas = data.map(({ tag, schema }) => {
return `"${tag}": ${jsonSchemaToZod(schema as JsonSchemaObject)},`
})

const out = `
import { z } from 'zod';
export const attributeSupplementSchema = {
${schemas.join('\n')}
}
export const isAttributeSupplementSchema = (schema: string): schema is AttributeSupplementSchemas => Object.keys(attributeSupplementSchema).includes(schema)
export type AttributeSupplementSchemas = keyof typeof attributeSupplementSchema
`
await writeOutput('attributeSupplementSchema', out)
task.title = `${task.title} (${data.length} items)`
}
1 change: 1 addition & 0 deletions packages/db/lib/generators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export * from './allAttributes'
export * from './attributeCategory'
export * from './attributesByCategory'
export * from './attributeSuppDataSchemas'
export * from './langs'
export * from './namespaces'
export * from './permission'
Expand Down
1 change: 1 addition & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@weareinreach/env": "workspace:*",
"@weareinreach/util": "workspace:*",
"id128": "1.6.6",
"json-schema-to-zod": "2.0.14",
"kysely": "0.27.2",
"pg": "8.11.3",
"prisma-kysely": "1.8.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Warnings:
- Added the required column `schema` to the `AttributeSupplementDataSchema` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "AttributeSupplementDataSchema"
ADD COLUMN "schema" JSONB;

UPDATE
"AttributeSupplementDataSchema"
SET
"schema" = "definition";

ALTER TABLE "AttributeSupplementDataSchema"
ALTER COLUMN "schema" SET NOT NULL;

-- CreateIndex
CREATE INDEX IF NOT EXISTS "AttributeSupplement_active_attributeId_idx" ON
"AttributeSupplement"("active", "attributeId");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "OrgLocationService_active_serviceId_idx" ON
"OrgLocationService"("active", "serviceId");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "OrgService_organizationId_published_deleted_idx" ON
"OrgService"("organizationId", "published" DESC, "deleted");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "ServiceArea_active_organizationId_idx" ON
"ServiceArea"("active", "organizationId");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "ServiceArea_active_orgLocationId_idx" ON
"ServiceArea"("active", "orgLocationId");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "ServiceArea_active_orgServiceId_idx" ON
"ServiceArea"("active", "orgServiceId");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "ServiceAreaCountry_active_serviceAreaId_idx" ON
"ServiceAreaCountry"("active", "serviceAreaId");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "ServiceAreaDist_active_serviceAreaId_idx" ON
"ServiceAreaDist"("active", "serviceAreaId");
1 change: 1 addition & 0 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ model AttributeSupplementDataSchema {
name String
active Boolean @default(true)
definition Json
schema Json
// entryComponent String?
createdAt DateTime @default(now())
Expand Down
34 changes: 34 additions & 0 deletions packages/db/zod_util/attributeSupplement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,37 @@ export const AttSuppSchemas = {
}

export type AttributeSupplementSchemas = keyof typeof AttSuppSchemas
/** Dynamic Fields for Supplement Data Schemas */

export enum FieldType {
text = 'text',
select = 'select',
number = 'number',
currency = 'currency',
}
interface BaseFieldAttributes {
key: string
label: string
name: string
type: FieldType
required?: boolean
}

interface TextFieldAttributes extends BaseFieldAttributes {
type: FieldType.text
}
interface SelectFieldAttributes extends BaseFieldAttributes {
type: FieldType.select
options: { value: string; label: string }[]
}
interface NumberFieldAttributes extends BaseFieldAttributes {
type: FieldType.number
}
interface CurrencyFieldAttributes extends BaseFieldAttributes {
type: FieldType.currency
}
export type FieldAttributes =
| TextFieldAttributes
| SelectFieldAttributes
| NumberFieldAttributes
| CurrencyFieldAttributes
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 6f85899

Please sign in to comment.