Skip to content

Commit

Permalink
Removed the ving schema helper baseSchemaProps in favor of the new …
Browse files Browse the repository at this point in the history
…exports `baseSchemaId`, `baseSchemaCreatedAt`, and `baseSchemaUpdatedAt`. This is a breaking change that allows you to modify the base schema props in your schema.
  • Loading branch information
rizen committed Jan 13, 2025
1 parent 6e4582c commit 16b01c5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 46 deletions.
16 changes: 16 additions & 0 deletions ving/docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions ving/docs/subsystems/ving-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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.

Expand Down
6 changes: 4 additions & 2 deletions ving/generator/vingschema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
72 changes: 38 additions & 34 deletions ving/schema/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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: [],
};
6 changes: 4 additions & 2 deletions ving/schema/schemas/APIKey.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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 = {
kind: 'APIKey',
tableName: 'apikeys',
owner: ['$userId', 'admin'],
props: [
...baseSchemaProps,
{ ...baseSchemaId },
{ ...baseSchemaCreatedAt },
{ ...baseSchemaUpdatedAt },
{
type: "string",
name: 'name',
Expand Down
6 changes: 4 additions & 2 deletions ving/schema/schemas/CronJob.mjs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 4 additions & 2 deletions ving/schema/schemas/S3File.mjs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 4 additions & 2 deletions ving/schema/schemas/User.mjs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 16b01c5

Please sign in to comment.