From 16b01c5b025ea5ae740ac97bc06f41f6e023ff11 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Sun, 12 Jan 2025 20:50:41 -0600 Subject: [PATCH] Removed the ving schema helper `baseSchemaProps` in favor of the new exports `baseSchemaId`, `baseSchemaCreatedAt`, and `baseSchemaUpdatedAt`. This is a breaking change that allows you to modify the base schema props in your schema. --- ving/docs/change-log.md | 16 +++++++ ving/docs/subsystems/ving-schema.md | 6 ++- ving/generator/vingschema.mjs | 6 ++- ving/schema/helpers.mjs | 72 +++++++++++++++-------------- ving/schema/schemas/APIKey.mjs | 6 ++- ving/schema/schemas/CronJob.mjs | 6 ++- ving/schema/schemas/S3File.mjs | 6 ++- ving/schema/schemas/User.mjs | 6 ++- 8 files changed, 78 insertions(+), 46 deletions(-) diff --git a/ving/docs/change-log.md b/ving/docs/change-log.md index 81c3bd8d..0b16a7ee 100644 --- a/ving/docs/change-log.md +++ b/ving/docs/change-log.md @@ -11,6 +11,22 @@ outline: deep * refactored to use Intl instead of date-fns #204 * NOTE: If you are using the date formating strings in dateTime.mjs you will need to update them to use Intl instead of date-fns. * Fixed: note in docs how to reference drizzle schema #174 + * Removed the ving schema helper `baseSchemaProps` in favor of the new exports `baseSchemaId`, `baseSchemaCreatedAt`, and `baseSchemaUpdatedAt`. This is a breaking change that allows you to modify the base schema props in your schema. + * NOTE: Update your Ving Schemas to use the new baseSchemaId, baseSchemaCreatedAt, and baseSchemaUpdatedAt exports in place of the baseSchemaProps array. In each of your ving schemas replace this: + +```js + ...baseSchemaProps, +``` + +With this: + +```js + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, +``` + +Also don't forget to update the imports in your schema files. ## December 2024 diff --git a/ving/docs/subsystems/ving-schema.md b/ving/docs/subsystems/ving-schema.md index ca4fae7b..63e0c4be 100644 --- a/ving/docs/subsystems/ving-schema.md +++ b/ving/docs/subsystems/ving-schema.md @@ -10,7 +10,9 @@ You'll find the schemas in `#ving/schema/schemas`. A schema looks like this: tableName: 'users', owner: ['$id', 'admin'], props: [ - ...baseSchemaProps, + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, { type: "string", name: "username", @@ -69,7 +71,7 @@ It can also contain any number of roles. By default there are 3 roles: `admin`, It can also defer to a parent object. So let's say you had a record called Invoice and another called LineItem. Each LineItem would have a parent relation to the Invoice called `invoice`. So you could then use `^invoice` (notice the carat) to indicate that you'd like to ask the Invoice if the User owns it, and if the answer is yes, then the LineItem will be considered to also be owned by that user. The carat means "look for a parent relation in the schema" and whatever comes after the carat is the name of that relation. ### props -All schemas should have the base props of `id`, `createdAt`, and `updatedAt` by using `...baseSchemaProps`. After that it's up to you to add your own props to the list. There are many different types of props for different field types. +All schemas should have the base props of `id`, `createdAt`, and `updatedAt` by using `{...baseSchemaId}`, `{...baseSchemaCreatedAt}`, and `{...baseSchemaUpdatedAt}`. After that it's up to you to add your own props to the list. There are many different types of props for different field types. Props all have the fields `type`, `name`, `required`, `default`, `db`, `zod`, `view`, and `edit`, but can have more or less fields from there. diff --git a/ving/generator/vingschema.mjs b/ving/generator/vingschema.mjs index ee397506..fb27b064 100644 --- a/ving/generator/vingschema.mjs +++ b/ving/generator/vingschema.mjs @@ -2,14 +2,16 @@ import { getContext, renderTemplate, toFile, after, inject } from '@feathersclou import { camelCase } from 'scule'; const schemaTemplate = ({ name }) => - `import { baseSchemaProps, dbVarChar, zodString, dbEnum, dbBoolean, dbText, dbRelation, dbDateTime, dbTimestamp, dbBigInt, dbInt, dbUuid, dbJson, zodNumber, zodJsonObject, dbMediumText } from '../helpers.mjs'; + `import { baseSchemaId, baseSchemaCreatedAt, baseSchemaUpdatedAt, dbVarChar, zodString, dbEnum, dbBoolean, dbText, dbRelation, dbDateTime, dbTimestamp, dbBigInt, dbInt, dbUuid, dbJson, zodNumber, zodJsonObject, dbMediumText } from '../helpers.mjs'; export const ${camelCase(name)}Schema = { kind: '${name}', tableName: '${name.toLowerCase()}s', owner: ['$userId', 'admin'], props: [ - ...baseSchemaProps, + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, // name field { type: "string", diff --git a/ving/schema/helpers.mjs b/ving/schema/helpers.mjs index c7e2ffa9..f203bae1 100644 --- a/ving/schema/helpers.mjs +++ b/ving/schema/helpers.mjs @@ -218,7 +218,7 @@ export const dbUuid = (prop) => { } /** - * Generates a drizzle schema field definition for a primary key prop. This is included in the `baseSchemaProps` and likely won't be used by you. + * Generates a drizzle schema field definition for a primary key prop. This is included in the `baseSchemaId` and likely won't be used by you. * @param {Object} prop An object containing the properties of this prop * @returns a drizzle field schema definition */ @@ -243,37 +243,41 @@ export const dbRelation = (prop) => { } /** - * The base set of props that all Ving schemas share. It includes an `id`, `createdAt`, and `updatedAt`. + * The base set of props that all Ving schemas share. */ -export const baseSchemaProps = [ - { - type: "id", - name: "id", - required: false, - default: undefined, - db: (prop) => dbPk(prop), - view: ['public'], - edit: [], - }, - { - type: "date", - name: "createdAt", - filterRange: true, - required: true, - default: () => new Date(), - db: (prop) => dbTimestamp(prop), - view: ['public'], - edit: [], - }, - { - type: "date", - name: "updatedAt", - filterRange: true, - required: true, - autoUpdate: true, - default: () => new Date(), - db: (prop) => dbTimestamp(prop), - view: ['public'], - edit: [], - }, -]; + +// The Schema's unique identifier. +export const baseSchemaId = { + type: "id", + name: "id", + required: false, + default: undefined, + db: (prop) => dbPk(prop), + view: ['public'], + edit: [], +}; + +// The Schema's creation timestamp. +export const baseSchemaCreatedAt = { + type: "date", + name: "createdAt", + filterRange: true, + required: true, + default: () => new Date(), + db: (prop) => dbTimestamp(prop), + view: ['public'], + edit: [], +}; + +// The Schema's last update timestamp. +export const baseSchemaUpdatedAt = { + type: "date", + name: "updatedAt", + filterRange: true, + required: true, + autoUpdate: true, + default: () => new Date(), + db: (prop) => dbTimestamp(prop), + view: ['public'], + edit: [], +}; diff --git a/ving/schema/schemas/APIKey.mjs b/ving/schema/schemas/APIKey.mjs index 88c8ffc8..073bd815 100644 --- a/ving/schema/schemas/APIKey.mjs +++ b/ving/schema/schemas/APIKey.mjs @@ -1,4 +1,4 @@ -import { baseSchemaProps, dbVarChar, zodString, dbText, dbRelation } from '../helpers.mjs'; +import { baseSchemaId, baseSchemaCreatedAt, baseSchemaUpdatedAt, dbVarChar, zodString, dbText, dbRelation } from '../helpers.mjs'; import crypto from 'crypto'; export const apikeySchema = { @@ -6,7 +6,9 @@ export const apikeySchema = { tableName: 'apikeys', owner: ['$userId', 'admin'], props: [ - ...baseSchemaProps, + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, { type: "string", name: 'name', diff --git a/ving/schema/schemas/CronJob.mjs b/ving/schema/schemas/CronJob.mjs index 4fb112d7..12731c05 100644 --- a/ving/schema/schemas/CronJob.mjs +++ b/ving/schema/schemas/CronJob.mjs @@ -1,11 +1,13 @@ -import { baseSchemaProps, dbVarChar, zodString, dbBoolean, dbText, dbJson, zodJsonObject } from '../helpers.mjs'; +import { baseSchemaId, baseSchemaCreatedAt, baseSchemaUpdatedAt, dbVarChar, zodString, dbBoolean, dbText, dbJson, zodJsonObject } from '../helpers.mjs'; export const cronJobSchema = { kind: 'CronJob', tableName: 'cronjobs', owner: ['admin'], props: [ - ...baseSchemaProps, + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, { type: "string", name: "schedule", diff --git a/ving/schema/schemas/S3File.mjs b/ving/schema/schemas/S3File.mjs index 4465f293..6aecaa97 100644 --- a/ving/schema/schemas/S3File.mjs +++ b/ving/schema/schemas/S3File.mjs @@ -1,11 +1,13 @@ -import { baseSchemaProps, dbVarChar, zodString, dbEnum, dbBoolean, dbText, dbRelation, dbDateTime, dbTimestamp, dbInt, dbJson, zodNumber, zodJsonObject } from '../helpers.mjs'; +import { baseSchemaId, baseSchemaCreatedAt, baseSchemaUpdatedAt, dbVarChar, zodString, dbEnum, dbBoolean, dbText, dbRelation, dbDateTime, dbTimestamp, dbInt, dbJson, zodNumber, zodJsonObject } from '../helpers.mjs'; export const s3fileSchema = { kind: 'S3File', tableName: 's3files', owner: ['$userId', 'admin'], props: [ - ...baseSchemaProps, + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, { type: "string", name: "filename", diff --git a/ving/schema/schemas/User.mjs b/ving/schema/schemas/User.mjs index bb0e6b1b..4f7b5a3b 100644 --- a/ving/schema/schemas/User.mjs +++ b/ving/schema/schemas/User.mjs @@ -1,11 +1,13 @@ -import { baseSchemaProps, dbVarChar, zodString, dbEnum, dbBoolean, dbRelation, dbMediumText } from '../helpers.mjs'; +import { baseSchemaId, baseSchemaCreatedAt, baseSchemaUpdatedAt, dbVarChar, zodString, dbEnum, dbBoolean, dbRelation, dbMediumText } from '../helpers.mjs'; export const userSchema = { kind: 'User', tableName: 'users', owner: ['$id', 'admin'], props: [ - ...baseSchemaProps, + { ...baseSchemaId }, + { ...baseSchemaCreatedAt }, + { ...baseSchemaUpdatedAt }, { type: "string", name: "username",