nextjs-cms 0.7.9 → 0.7.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"update-sections.d.ts","sourceRoot":"","sources":["../../../src/cli/lib/update-sections.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"update-sections.d.ts","sourceRoot":"","sources":["../../../src/cli/lib/update-sections.ts"],"names":[],"mappings":"AAolCA,wBAAsB,cAAc,CAAC,SAAS,UAAQ,iBAoBrD"}
|
|
@@ -11,7 +11,7 @@ import { FileField } from '../../core/fields/index.js';
|
|
|
11
11
|
import chalk from 'chalk';
|
|
12
12
|
import { intro, select, spinner, log } from '@clack/prompts';
|
|
13
13
|
import { MysqlTableChecker } from '../../core/db/index.js';
|
|
14
|
-
import { generateDrizzleSchema } from '../utils/schema-generator.js';
|
|
14
|
+
import { generateDrizzleSchema, resolveCaseStyleFns } from '../utils/schema-generator.js';
|
|
15
15
|
import { addTableKeys } from '../utils/add-table-keys.js';
|
|
16
16
|
function generateFieldSQL(input) {
|
|
17
17
|
let fieldSQL = `\`${input.name}\` `;
|
|
@@ -525,8 +525,9 @@ const main = async (s) => {
|
|
|
525
525
|
else {
|
|
526
526
|
console.log(chalk.yellow(`Schema generation is disabled. Skipping Drizzle schema generation.`));
|
|
527
527
|
}
|
|
528
|
+
const caseStyleFns = resolveCaseStyleFns(cmsConfig.schemaGeneration.drizzle.naming);
|
|
528
529
|
const drizzleTableSchemas = [];
|
|
529
|
-
const
|
|
530
|
+
const drizzleImports = new Set(['mysqlTable']);
|
|
530
531
|
let sections = [];
|
|
531
532
|
const desiredTables = [];
|
|
532
533
|
let existingTables = [];
|
|
@@ -556,17 +557,13 @@ const main = async (s) => {
|
|
|
556
557
|
/**
|
|
557
558
|
* Generate the Drizzle schema for the table
|
|
558
559
|
*/
|
|
559
|
-
const drizzleSchema =
|
|
560
|
+
const drizzleSchema = generateDrizzleSchema({
|
|
560
561
|
name: s.db.table,
|
|
561
562
|
fields: s.fieldConfigs,
|
|
562
563
|
identifier: s.db.identifier,
|
|
563
|
-
});
|
|
564
|
+
}, caseStyleFns);
|
|
564
565
|
drizzleTableSchemas.push(drizzleSchema.schema);
|
|
565
|
-
drizzleSchema.
|
|
566
|
-
if (!drizzleColumnTypes.includes(type)) {
|
|
567
|
-
drizzleColumnTypes.push(type);
|
|
568
|
-
}
|
|
569
|
-
});
|
|
566
|
+
drizzleSchema.drizzleImports.forEach((type) => drizzleImports.add(type));
|
|
570
567
|
desiredTables.push({
|
|
571
568
|
name: s.db.table,
|
|
572
569
|
fields: s.fields,
|
|
@@ -614,6 +611,17 @@ const main = async (s) => {
|
|
|
614
611
|
*/
|
|
615
612
|
primaryKey: [referenceIdFieldConfig, selectIdFieldConfig],
|
|
616
613
|
});
|
|
614
|
+
const destDbFields = [
|
|
615
|
+
{ name: field.destinationDb.itemIdentifier, type: 'text', required: true },
|
|
616
|
+
{ name: field.destinationDb.selectIdentifier, type: 'text', required: true },
|
|
617
|
+
];
|
|
618
|
+
const destDbSchema = generateDrizzleSchema({
|
|
619
|
+
name: field.destinationDb.table,
|
|
620
|
+
fields: destDbFields,
|
|
621
|
+
compositePrimaryKey: destDbFields,
|
|
622
|
+
}, caseStyleFns);
|
|
623
|
+
drizzleTableSchemas.push(destDbSchema.schema);
|
|
624
|
+
destDbSchema.drizzleImports.forEach((type) => drizzleImports.add(type));
|
|
617
625
|
}
|
|
618
626
|
}
|
|
619
627
|
/**
|
|
@@ -654,6 +662,32 @@ const main = async (s) => {
|
|
|
654
662
|
identifier: photoField,
|
|
655
663
|
primaryKey: [photoField],
|
|
656
664
|
});
|
|
665
|
+
const gallerySchema = generateDrizzleSchema({
|
|
666
|
+
name: gallery.db.tableName,
|
|
667
|
+
fields: [
|
|
668
|
+
{
|
|
669
|
+
name: gallery.db.referenceIdentifierField || 'reference_id',
|
|
670
|
+
type: 'text',
|
|
671
|
+
required: true,
|
|
672
|
+
},
|
|
673
|
+
{
|
|
674
|
+
name: gallery.db.photoNameField || 'photo',
|
|
675
|
+
type: 'text',
|
|
676
|
+
required: true,
|
|
677
|
+
},
|
|
678
|
+
{
|
|
679
|
+
name: gallery.db.metaField || 'meta',
|
|
680
|
+
type: 'textarea',
|
|
681
|
+
required: false,
|
|
682
|
+
},
|
|
683
|
+
],
|
|
684
|
+
identifier: {
|
|
685
|
+
name: gallery.db.photoNameField || 'photo',
|
|
686
|
+
type: 'text',
|
|
687
|
+
},
|
|
688
|
+
}, caseStyleFns);
|
|
689
|
+
drizzleTableSchemas.push(gallerySchema.schema);
|
|
690
|
+
gallerySchema.drizzleImports.forEach((type) => drizzleImports.add(type));
|
|
657
691
|
}
|
|
658
692
|
}
|
|
659
693
|
/**
|
|
@@ -667,9 +701,9 @@ const main = async (s) => {
|
|
|
667
701
|
fs.mkdirSync(schemaOutDir, { recursive: true });
|
|
668
702
|
}
|
|
669
703
|
/**
|
|
670
|
-
* Append the Drizzle
|
|
704
|
+
* Append the Drizzle imports to the schema file
|
|
671
705
|
*/
|
|
672
|
-
fs.appendFileSync(schemaFilePath, 'import {' +
|
|
706
|
+
fs.appendFileSync(schemaFilePath, 'import {' + [...drizzleImports].join(',') + "} from 'drizzle-orm/mysql-core'\n\n");
|
|
673
707
|
/**
|
|
674
708
|
* Append the Drizzle table schemas to the schema file
|
|
675
709
|
*/
|
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
import type { FieldConfig } from '../../core/fields/index.js';
|
|
2
|
+
export interface CaseStyleFns {
|
|
3
|
+
tableCaseStyleFn: (str: string) => string;
|
|
4
|
+
columnCaseStyleFn: (str: string) => string;
|
|
5
|
+
}
|
|
6
|
+
export interface NamingConfig {
|
|
7
|
+
tables: 'camelCase' | 'snakeCase' | 'pascalCase';
|
|
8
|
+
columns: 'camelCase' | 'snakeCase' | 'pascalCase';
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Resolves case-style functions from the naming configuration.
|
|
12
|
+
* Centralises the switch logic that was previously duplicated across generators.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveCaseStyleFns(naming: NamingConfig): CaseStyleFns;
|
|
2
15
|
export declare function generateDrizzleSchema(table: {
|
|
3
16
|
name: string;
|
|
4
17
|
fields: FieldConfig[];
|
|
5
|
-
identifier
|
|
6
|
-
|
|
18
|
+
identifier?: FieldConfig;
|
|
19
|
+
compositePrimaryKey?: {
|
|
20
|
+
name: string;
|
|
21
|
+
}[];
|
|
22
|
+
}, caseStyleFns: CaseStyleFns): {
|
|
7
23
|
schema: string;
|
|
8
|
-
|
|
9
|
-
}
|
|
24
|
+
drizzleImports: string[];
|
|
25
|
+
};
|
|
10
26
|
//# sourceMappingURL=schema-generator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema-generator.d.ts","sourceRoot":"","sources":["../../../src/cli/utils/schema-generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AAoB7D,
|
|
1
|
+
{"version":3,"file":"schema-generator.d.ts","sourceRoot":"","sources":["../../../src/cli/utils/schema-generator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AAoB7D,MAAM,WAAW,YAAY;IACzB,gBAAgB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAA;IACzC,iBAAiB,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,MAAM,CAAA;CAC7C;AAED,MAAM,WAAW,YAAY;IACzB,MAAM,EAAE,WAAW,GAAG,WAAW,GAAG,YAAY,CAAA;IAChD,OAAO,EAAE,WAAW,GAAG,WAAW,GAAG,YAAY,CAAA;CACpD;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,YAAY,GAAG,YAAY,CA8BtE;AAED,wBAAgB,qBAAqB,CACjC,KAAK,EAAE;IACH,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,WAAW,EAAE,CAAA;IACrB,UAAU,CAAC,EAAE,WAAW,CAAA;IACxB,mBAAmB,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,CAAA;CAC3C,EACD,YAAY,EAAE,YAAY,GAC3B;IACC,MAAM,EAAE,MAAM,CAAA;IACd,cAAc,EAAE,MAAM,EAAE,CAAA;CAC3B,CAwIA"}
|
|
@@ -13,12 +13,13 @@ function toSnakeCase(str) {
|
|
|
13
13
|
.replace(/^_/, '') // Remove leading underscore if any
|
|
14
14
|
.toLowerCase();
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Resolves case-style functions from the naming configuration.
|
|
18
|
+
* Centralises the switch logic that was previously duplicated across generators.
|
|
19
|
+
*/
|
|
20
|
+
export function resolveCaseStyleFns(naming) {
|
|
18
21
|
let tableCaseStyleFn;
|
|
19
|
-
|
|
20
|
-
const lzConfig = await getCMSConfig();
|
|
21
|
-
switch (lzConfig.schemaGeneration.drizzle.naming.tables) {
|
|
22
|
+
switch (naming.tables) {
|
|
22
23
|
case 'snakeCase':
|
|
23
24
|
tableCaseStyleFn = toSnakeCase;
|
|
24
25
|
break;
|
|
@@ -30,7 +31,8 @@ export async function generateDrizzleSchema(table) {
|
|
|
30
31
|
tableCaseStyleFn = toPascalCase;
|
|
31
32
|
break;
|
|
32
33
|
}
|
|
33
|
-
|
|
34
|
+
let columnCaseStyleFn;
|
|
35
|
+
switch (naming.columns) {
|
|
34
36
|
case 'snakeCase':
|
|
35
37
|
columnCaseStyleFn = toSnakeCase;
|
|
36
38
|
break;
|
|
@@ -42,12 +44,11 @@ export async function generateDrizzleSchema(table) {
|
|
|
42
44
|
columnCaseStyleFn = toCamelCase;
|
|
43
45
|
break;
|
|
44
46
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const drizzleColumnTypes = [];
|
|
47
|
+
return { tableCaseStyleFn, columnCaseStyleFn };
|
|
48
|
+
}
|
|
49
|
+
export function generateDrizzleSchema(table, caseStyleFns) {
|
|
50
|
+
const { tableCaseStyleFn, columnCaseStyleFn } = caseStyleFns;
|
|
51
|
+
const drizzleImports = [];
|
|
51
52
|
let schema = '';
|
|
52
53
|
const schemaName = tableCaseStyleFn(`${table.name}_table`);
|
|
53
54
|
schema += `export const ${schemaName} = mysqlTable('${table.name}', {\n`;
|
|
@@ -58,7 +59,6 @@ export async function generateDrizzleSchema(table) {
|
|
|
58
59
|
continue;
|
|
59
60
|
let columnType;
|
|
60
61
|
let drizzleColumnType;
|
|
61
|
-
// let columnOptions = ''
|
|
62
62
|
switch (input.type) {
|
|
63
63
|
case 'text':
|
|
64
64
|
columnType = `varchar('${input.name}', { length: ${input.maxLength ?? 255} })`;
|
|
@@ -145,27 +145,30 @@ export async function generateDrizzleSchema(table) {
|
|
|
145
145
|
if (input.required) {
|
|
146
146
|
columnType += `.notNull()`;
|
|
147
147
|
}
|
|
148
|
-
if (input === table.identifier) {
|
|
148
|
+
if (table.identifier && input === table.identifier) {
|
|
149
149
|
columnType += `.primaryKey()`;
|
|
150
150
|
}
|
|
151
151
|
/*if (input.type === 'date') {
|
|
152
152
|
columnType += `.defaultNow()`
|
|
153
153
|
}*/
|
|
154
154
|
schema += ` ${columnCaseStyleFn(input.name)}: ${columnType},\n`;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
*/
|
|
158
|
-
if (drizzleColumnType && !drizzleColumnTypes.includes(drizzleColumnType)) {
|
|
159
|
-
/**
|
|
160
|
-
* If it's not, add it to the array
|
|
161
|
-
*/
|
|
162
|
-
drizzleColumnTypes.push(drizzleColumnType);
|
|
155
|
+
if (drizzleColumnType && !drizzleImports.includes(drizzleColumnType)) {
|
|
156
|
+
drizzleImports.push(drizzleColumnType);
|
|
163
157
|
}
|
|
164
158
|
}
|
|
165
159
|
schema = schema.slice(0, -2); // Remove the last comma and newline
|
|
166
|
-
|
|
160
|
+
if (table.compositePrimaryKey && table.compositePrimaryKey.length > 1) {
|
|
161
|
+
const pkCols = table.compositePrimaryKey.map((f) => `table.${columnCaseStyleFn(f.name)}`).join(', ');
|
|
162
|
+
schema += `\n}, (table) => [primaryKey({ columns: [${pkCols}] })]);\n\n`;
|
|
163
|
+
if (!drizzleImports.includes('primaryKey')) {
|
|
164
|
+
drizzleImports.push('primaryKey');
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
schema += `\n});\n\n`;
|
|
169
|
+
}
|
|
167
170
|
return {
|
|
168
171
|
schema: schema,
|
|
169
|
-
|
|
172
|
+
drizzleImports,
|
|
170
173
|
};
|
|
171
174
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nextjs-cms",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.10",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -200,9 +200,9 @@
|
|
|
200
200
|
"prettier": "^3.3.3",
|
|
201
201
|
"tsx": "^4.20.6",
|
|
202
202
|
"typescript": "^5.9.2",
|
|
203
|
-
"@lzcms/
|
|
203
|
+
"@lzcms/eslint-config": "0.3.0",
|
|
204
204
|
"@lzcms/prettier-config": "0.1.0",
|
|
205
|
-
"@lzcms/
|
|
205
|
+
"@lzcms/tsconfig": "0.1.0"
|
|
206
206
|
},
|
|
207
207
|
"license": "MIT",
|
|
208
208
|
"keywords": [
|