Skip to content

Commit

Permalink
Implemented: add --bare as an option in generators that gets rid of t…
Browse files Browse the repository at this point in the history
…he boiler plate #75
  • Loading branch information
rizen committed Apr 10, 2024
1 parent 0dd1148 commit c6f58fb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 36 deletions.
1 change: 1 addition & 0 deletions docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ outline: deep
* Fixed: displaying cache objects from CLI shows [object Object] instead of the actual object #78
* Fixed: isRoleOrDie not documented correctly #77
* Implemented: document Nuxt stuff #76
* Implemented: add --bare as an option in generators that gets rid of the boiler plate #75

## 2024-04-08
* Breaking change: Refactored VingRecord isOwner(), canEdit(), and propsOptions() to be async.
Expand Down
7 changes: 6 additions & 1 deletion ving/cli/record.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ export default defineCommand({
valueHint: "ClassName",
alias: "w",
},
bare: {
type: "boolean",
description: "Generate without any extra examples, just the skeleton.",
alias: "b",
},
},
async run({ args }) {
try {
if (args.new) {
await generateRecord({ name: args.new, schema: findVingSchema(args.new, 'kind') });
await generateRecord({ name: args.new, bare: args.bare, schema: findVingSchema(args.new, 'kind') });
}
else if (args.rest) {
await generateRest({ name: args.rest, schema: findVingSchema(args.rest, 'kind') });
Expand Down
83 changes: 48 additions & 35 deletions ving/generator/vingrecord.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,60 @@ function addRelationshipDelete({ schema }) {
return '';
}

const recordTemplate = ({ name, schema }) =>
const describeExample = ({ bare }) => {
if (bare)
return '';
return `
/**
* Extends \`describe()\` in \`VingRecord\` to add \`meta\` property \`bar\`
* and the \`extra\` property \`foo\`.
*
* Note, these don't do anything, this is just here to explain how to extend
* the describe method.
*
* @see VingRecord.describe()
*/
async describe(params = {}) {
const out = await super.describe(params);
if (params?.include?.meta && out.meta) {
out.meta.bar = 'bar';
}
if (params?.include?.extra.includes('foo')) {
if (out.extra === undefined) {
out.extra = {};
}
out.extra.foo = 'foo';
}
return out;
}
`;
}

const shortcutQueryExample = ({ bare, name }) => {
if (bare)
return '';
return `
/**
* An example of a shortcut for a common query you might want to make.
*
* @returns a list of \`${name}Record\`s that are cool
*/
async findCool() {
return this.select.findMany(eq(this.table.isCool, true));
}
`;
}

const recordTemplate = ({ name, bare, schema }) =>
`import { VingRecord, VingKind } from "#ving/record/VingRecord.mjs";
import {eq} from '#ving/drizzle/orm.mjs';
${bare ? '' : "import {eq} from '#ving/drizzle/orm.mjs';"}
/** Management of individual ${name}s.
* @class
*/
export class ${name}Record extends VingRecord {
// add custom Record code here
/**
* Extends \`describe()\` in \`VingRecord\` to add \`meta\` property \`bar\`
* and the \`extra\` property \`foo\`.
*
* Note, these don't do anything, this is just here to explain how to extend
* the describe method.
*
* @see VingRecord.describe()
*/
async describe(params = {}) {
const out = await super.describe(params);
if (params?.include?.meta && out.meta) {
out.meta.bar = 'bar';
}
if (params?.include?.extra.includes('foo')) {
if (out.extra === undefined) {
out.extra = {};
}
out.extra.foo = 'foo';
}
return out;
}
${describeExample({ bare })}
${addRelationshipDelete({ schema })}
}
Expand All @@ -86,15 +107,7 @@ export class ${name}Record extends VingRecord {
*/
export class ${name}Kind extends VingKind {
// add custom Kind code here
/**
* An example of a shortcut for a common query you might want to make.
*
* @returns a list of \`${name}Record\`s that are cool
*/
async findCool() {
return this.select.findMany(eq(this.table.isCool, true));
}
${shortcutQueryExample({ bare, name })}
}`;

export const generateRecord = (params) => {
Expand Down

0 comments on commit c6f58fb

Please sign in to comment.