Skip to content

Commit

Permalink
Added mediumtext fields to schema and drizzle generation
Browse files Browse the repository at this point in the history
  • Loading branch information
rizen committed Apr 4, 2024
1 parent a8e50be commit 661ee70
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ outline: deep
## 2024-04-04
* Provided a little more documentation about virtual columns
* Fixed camelCasing of schema names in the schema generator
* Added mediumtext fields to schema and drizzle generation

## 2024-03-16
* Created a function for fetching ving config in `ving/config.mjs`
Expand Down
33 changes: 33 additions & 0 deletions docs/subsystems/ving-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,39 @@ These are used to add a parent relationship.

```

##### Text Props

```js
{
type: "string",
name: "memo",
required: true,
length: 256,
default: '',
db: (prop) => dbText(prop),
zod: (prop) => zodText(prop),
view: [],
edit: ['owner'],
},

```

##### MediumText Props

```js
{
type: "string",
name: "description",
required: true,
default: '',
db: (prop) => dbMediumText(prop),
zod: (prop) => zodMediumText(prop),
view: [],
edit: ['owner'],
},

```

##### Virtual Props

These are used to add a child relationship. They are virtual because they make no modification to the database table they represent.
Expand Down
10 changes: 8 additions & 2 deletions ving/drizzle/orm.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export { drizzle } from 'drizzle-orm/mysql2';
export { boolean, mysqlEnum, mysqlTable, timestamp, uniqueIndex, varchar, text, datetime, int, json } from 'drizzle-orm/mysql-core';
export { boolean, mysqlEnum, mysqlTable, timestamp, uniqueIndex, varchar, text, datetime, int, json, customType } from 'drizzle-orm/mysql-core';
export { like, eq, asc, desc, and, or, ne, gt, gte, lt, lte, inArray, getTableName } from 'drizzle-orm';
export { sql } from 'drizzle-orm/sql';
export { sql } from 'drizzle-orm/sql';

export const mediumText = customType({
dataType() {
return 'mediumtext';
},
});
2 changes: 1 addition & 1 deletion ving/generator/drizzletable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const makeTable = ({ schema }) => {
references.push(`import {${prop.relation.kind}Table} from '#ving/drizzle/schema/${prop.relation.kind}.mjs';`);
}
}
return `import { boolean, mysqlEnum, mysqlTable, timestamp, datetime, uniqueIndex, varchar, text, int, json } from '#ving/drizzle/orm.mjs';
return `import { boolean, mysqlEnum, mysqlTable, timestamp, datetime, uniqueIndex, varchar, text, int, json, mediumText } from '#ving/drizzle/orm.mjs';
${references.join("\n")}
${makeBaseTable(schema)}
Expand Down
2 changes: 1 addition & 1 deletion ving/generator/vingschema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getContext, renderTemplate, toFile, after, inject } from '@feathersclou
import { camelCase } from 'scule';

const schemaTemplate = ({ name }) =>
`import { baseSchemaProps, dbString, zodString, dbEnum, dbBoolean, dbText, zodText, dbRelation, dbDateTime, dbTimestamp, dbInt, dbJson, zodNumber, zodJsonObject } from '../helpers.mjs';
`import { baseSchemaProps, dbString, zodString, dbEnum, dbBoolean, dbText, zodText, dbRelation, dbDateTime, dbTimestamp, dbInt, dbJson, zodNumber, zodJsonObject, dbMediumText, zodMediumText } from '../helpers.mjs';
export const ${camelCase(name)}Schema = {
kind: '${name}',
Expand Down
18 changes: 18 additions & 0 deletions ving/schema/helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ export const zodText = (prop) => {
return z.string().min(1).max(prop.length);
}

/**
* Generates a zod rule for a text prop which must be a string with at least 1 character and not more 162,777,215
* @param {Object} prop An object containing the properties of this prop
* @returns a zod rule
*/
export const zodMediumText = (prop) => {
return z.string().min(1).max(162777215);
}

/**
* Generates a drizzle schema field definition for a timestamp prop that defaults itself to now and is not null, and `onUpdateNow()` if `autoUpdate` is set to `true`
* @param {Object} prop An object containing the properties of this prop
Expand Down Expand Up @@ -143,6 +152,15 @@ export const dbText = (prop) => {
return `text('${prop.name}').notNull()`;
}

/**
* Generates a drizzle schema field definition for a mediumtext prop setting it to not null
* @param {Object} prop An object containing the properties of this prop
* @returns a drizzle field schema definition
*/
export const dbMediumText = (prop) => {
return `mediumText('${prop.name}').notNull()`;
}

/**
* Generates a drizzle schema field definition for an enum prop setting it to not null with its default value and its list of enaums from the props
* @param {Object} prop An object containing the properties of this prop
Expand Down

0 comments on commit 661ee70

Please sign in to comment.