generathor-laravel 1.0.0

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.
Files changed (92) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +419 -0
  3. package/dist/configuration.d.ts +25 -0
  4. package/dist/configuration.js +43 -0
  5. package/dist/helpers/handlebars.d.ts +3 -0
  6. package/dist/helpers/handlebars.js +20 -0
  7. package/dist/helpers/naming.d.ts +18 -0
  8. package/dist/helpers/naming.js +98 -0
  9. package/dist/helpers/string.d.ts +27 -0
  10. package/dist/helpers/string.js +120 -0
  11. package/dist/helpers/type.d.ts +4 -0
  12. package/dist/helpers/type.js +41 -0
  13. package/dist/index.d.ts +2 -0
  14. package/dist/index.js +18 -0
  15. package/dist/template.d.ts +20 -0
  16. package/dist/template.js +284 -0
  17. package/dist/transformers/collection/attachForm.d.ts +4 -0
  18. package/dist/transformers/collection/attachForm.js +55 -0
  19. package/dist/transformers/collection/attachRequest.d.ts +5 -0
  20. package/dist/transformers/collection/attachRequest.js +92 -0
  21. package/dist/transformers/collection/base.d.ts +20 -0
  22. package/dist/transformers/collection/base.js +54 -0
  23. package/dist/transformers/collection/baseController.d.ts +4 -0
  24. package/dist/transformers/collection/baseController.js +14 -0
  25. package/dist/transformers/collection/belongsToRelation.d.ts +5 -0
  26. package/dist/transformers/collection/belongsToRelation.js +86 -0
  27. package/dist/transformers/collection/controller.d.ts +8 -0
  28. package/dist/transformers/collection/controller.js +163 -0
  29. package/dist/transformers/collection/createRelationForm.d.ts +4 -0
  30. package/dist/transformers/collection/createRelationForm.js +163 -0
  31. package/dist/transformers/collection/filterRelationForm.d.ts +4 -0
  32. package/dist/transformers/collection/filterRelationForm.js +164 -0
  33. package/dist/transformers/collection/hasManyRelation.d.ts +5 -0
  34. package/dist/transformers/collection/hasManyRelation.js +91 -0
  35. package/dist/transformers/collection/menu.d.ts +4 -0
  36. package/dist/transformers/collection/menu.js +25 -0
  37. package/dist/transformers/collection/route.d.ts +5 -0
  38. package/dist/transformers/collection/route.js +123 -0
  39. package/dist/transformers/item/base.d.ts +19 -0
  40. package/dist/transformers/item/base.js +33 -0
  41. package/dist/transformers/item/edit.d.ts +4 -0
  42. package/dist/transformers/item/edit.js +21 -0
  43. package/dist/transformers/item/eloquent.d.ts +4 -0
  44. package/dist/transformers/item/eloquent.js +158 -0
  45. package/dist/transformers/item/filter.d.ts +5 -0
  46. package/dist/transformers/item/filter.js +41 -0
  47. package/dist/transformers/item/forms.d.ts +4 -0
  48. package/dist/transformers/item/forms.js +226 -0
  49. package/dist/transformers/item/laravel.d.ts +15 -0
  50. package/dist/transformers/item/laravel.js +36 -0
  51. package/dist/transformers/item/list.d.ts +4 -0
  52. package/dist/transformers/item/list.js +36 -0
  53. package/dist/transformers/item/requests.d.ts +4 -0
  54. package/dist/transformers/item/requests.js +125 -0
  55. package/dist/transformers/item/show.d.ts +5 -0
  56. package/dist/transformers/item/show.js +54 -0
  57. package/package.json +46 -0
  58. package/templates/eloquent/child.handlebars +13 -0
  59. package/templates/eloquent/filter.handlebars +88 -0
  60. package/templates/eloquent/parent.handlebars +64 -0
  61. package/templates/others/base-controller.handlebars +46 -0
  62. package/templates/others/controller.handlebars +185 -0
  63. package/templates/others/pk-trait.handlebars +157 -0
  64. package/templates/others/request.handlebars +32 -0
  65. package/templates/others/routes.handlebars +16 -0
  66. package/templates/views/breadcrumbs.handlebars +21 -0
  67. package/templates/views/edit.handlebars +41 -0
  68. package/templates/views/form.handlebars +103 -0
  69. package/templates/views/icons/check-circle.handlebars +3 -0
  70. package/templates/views/icons/chevron-down.handlebars +3 -0
  71. package/templates/views/icons/chevron-right.handlebars +3 -0
  72. package/templates/views/icons/computer.handlebars +3 -0
  73. package/templates/views/icons/eye.handlebars +4 -0
  74. package/templates/views/icons/funnel.handlebars +3 -0
  75. package/templates/views/icons/home.handlebars +3 -0
  76. package/templates/views/icons/link.handlebars +3 -0
  77. package/templates/views/icons/list-bullet.handlebars +3 -0
  78. package/templates/views/icons/pencil.handlebars +3 -0
  79. package/templates/views/icons/plus.handlebars +3 -0
  80. package/templates/views/icons/trash.handlebars +3 -0
  81. package/templates/views/icons/unlink.handlebars +3 -0
  82. package/templates/views/icons/x-circle.handlebars +3 -0
  83. package/templates/views/icons/x.handlebars +3 -0
  84. package/templates/views/index.handlebars +181 -0
  85. package/templates/views/loader.handlebars +6 -0
  86. package/templates/views/menu.handlebars +39 -0
  87. package/templates/views/modal.handlebars +36 -0
  88. package/templates/views/record-input.handlebars +90 -0
  89. package/templates/views/relation-item.handlebars +180 -0
  90. package/templates/views/relation-list.handlebars +199 -0
  91. package/templates/views/show.handlebars +134 -0
  92. package/templates/views/tabs.handlebars +16 -0
@@ -0,0 +1,164 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FilterRelationForm = void 0;
7
+ const base_1 = require("./base");
8
+ const naming_1 = __importDefault(require("../../helpers/naming"));
9
+ class FilterRelationForm extends base_1.BaseTransformer {
10
+ transform() {
11
+ const map = this.mapByTable();
12
+ const result = [];
13
+ const fksMap = this.fksByTable();
14
+ for (const item of this.$items) {
15
+ const context = naming_1.default.context(item.table);
16
+ for (const relation of item.relations) {
17
+ if (relation.type !== 'has-many') {
18
+ continue;
19
+ }
20
+ const itemRelated = map[relation.on.table];
21
+ const inputs = [];
22
+ const hiddenInputs = [];
23
+ const references = this.references(relation);
24
+ const fks = fksMap[itemRelated.table];
25
+ itemRelated.columns.forEach((column) => {
26
+ if (references[column.name]) {
27
+ return;
28
+ }
29
+ if (column.autoincrement) {
30
+ return;
31
+ }
32
+ if (fks[column.name]) {
33
+ inputs.push({
34
+ type: 'record',
35
+ name: column.name,
36
+ label: naming_1.default.columnLabel(column),
37
+ data: {
38
+ context: naming_1.default.context(fks[column.name].table),
39
+ relationName: fks[column.name].relationAttribute,
40
+ },
41
+ });
42
+ return;
43
+ }
44
+ if (column.type === 'string' &&
45
+ column.enum &&
46
+ Array.isArray(column.enum) &&
47
+ column.enum.length > 0) {
48
+ inputs.push({
49
+ type: 'select',
50
+ name: column.name,
51
+ label: naming_1.default.columnLabel(column),
52
+ data: {
53
+ options: [
54
+ {
55
+ value: '',
56
+ label: 'Select',
57
+ },
58
+ ].concat(column.enum.map(function (value) {
59
+ return {
60
+ value: value,
61
+ label: naming_1.default.label(value),
62
+ };
63
+ })),
64
+ },
65
+ });
66
+ return;
67
+ }
68
+ if (column.type === 'int' || column.type === 'float') {
69
+ const decimals = column.type === 'int' ? 0 : column.scale;
70
+ inputs.push({
71
+ type: 'number',
72
+ name: column.name + '_from',
73
+ label: naming_1.default.label(column.name + '_from'),
74
+ data: {
75
+ decimals,
76
+ },
77
+ });
78
+ inputs.push({
79
+ type: 'number',
80
+ name: column.name + '_to',
81
+ label: naming_1.default.label(column.name + '_to'),
82
+ data: {
83
+ decimals,
84
+ },
85
+ });
86
+ return;
87
+ }
88
+ if (column.type === 'bool') {
89
+ inputs.push({
90
+ type: 'select',
91
+ name: column.name,
92
+ label: naming_1.default.columnLabel(column),
93
+ data: {
94
+ options: [
95
+ {
96
+ value: '',
97
+ label: 'Select',
98
+ },
99
+ {
100
+ value: '0',
101
+ label: 'No',
102
+ },
103
+ {
104
+ value: '1',
105
+ label: 'Yes',
106
+ },
107
+ ],
108
+ },
109
+ });
110
+ return;
111
+ }
112
+ const dates = {
113
+ datetime: true,
114
+ time: true,
115
+ date: true,
116
+ };
117
+ if (dates[column.subType]) {
118
+ inputs.push({
119
+ type: column.subType,
120
+ name: column.name + '_from',
121
+ label: naming_1.default.label(column.name + '_from'),
122
+ });
123
+ inputs.push({
124
+ type: column.subType,
125
+ name: column.name + '_to',
126
+ label: naming_1.default.label(column.name + '_to'),
127
+ });
128
+ return;
129
+ }
130
+ inputs.push({
131
+ type: 'text',
132
+ name: column.name,
133
+ label: naming_1.default.columnLabel(column),
134
+ });
135
+ });
136
+ result.push({
137
+ context,
138
+ contextFile: naming_1.default.relationContext(relation, item.table),
139
+ record: null,
140
+ routeRecord: null,
141
+ method: 'get',
142
+ prefix: 'filter_',
143
+ getOld: 'request',
144
+ errorBag: 'filter',
145
+ buttonLabel: 'Filter',
146
+ buttonColor: 'blue',
147
+ columns: '4',
148
+ route: '',
149
+ cancelLink: {
150
+ label: 'Clear filters',
151
+ route: 'request()->url()',
152
+ icon: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="h-4 w-4">
153
+ <path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
154
+ </svg>`,
155
+ },
156
+ inputs: inputs,
157
+ hiddenInputs: hiddenInputs,
158
+ });
159
+ }
160
+ }
161
+ return result;
162
+ }
163
+ }
164
+ exports.FilterRelationForm = FilterRelationForm;
@@ -0,0 +1,5 @@
1
+ import { BaseTransformer, TransformedItem } from './base';
2
+ export declare class HasManyRelation extends BaseTransformer {
3
+ transform(): TransformedItem[];
4
+ private tabs;
5
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.HasManyRelation = void 0;
7
+ const base_1 = require("./base");
8
+ const naming_1 = __importDefault(require("../../helpers/naming"));
9
+ class HasManyRelation extends base_1.BaseTransformer {
10
+ transform() {
11
+ const result = [];
12
+ const fksByTable = this.fksByTable();
13
+ const map = this.mapByTable();
14
+ for (const item of this.$items) {
15
+ const context = naming_1.default.context(item.table);
16
+ const listLabel = naming_1.default.listLabel(item.table);
17
+ const recordName = naming_1.default.recordName(item.table);
18
+ for (const relation of item.relations) {
19
+ if (relation.type !== 'has-many') {
20
+ continue;
21
+ }
22
+ const relationAttribute = naming_1.default.relationAttribute(relation, item.table);
23
+ const columns = [];
24
+ const references = this.references(relation);
25
+ const fks = fksByTable[relation.on.table];
26
+ map[relation.on.table].columns.forEach((column) => {
27
+ if (references[column.name]) {
28
+ return;
29
+ }
30
+ let type = column.type;
31
+ let data = null;
32
+ if (fks[column.name]) {
33
+ type = 'record';
34
+ data = fks[column.name];
35
+ }
36
+ columns.push({
37
+ data,
38
+ type,
39
+ name: column.name,
40
+ label: naming_1.default.columnLabel(column),
41
+ nullable: column.nullable,
42
+ });
43
+ });
44
+ result.push({
45
+ context,
46
+ columns,
47
+ layout: this.$config.layout(),
48
+ homeRoute: this.$config.homeRoute(),
49
+ header: listLabel,
50
+ listLabel: listLabel,
51
+ relationLabel: naming_1.default.relationLabel(relation, item.table),
52
+ relationListLabel: naming_1.default.listLabel(relation.on.table),
53
+ record: recordName,
54
+ routeRelationRecordName: naming_1.default.recordName(relation.on.table),
55
+ capitalizedRelationAttribute: naming_1.default.capitalizedRelationAttribute(relation, item.table),
56
+ relationContext: naming_1.default.relationContext(relation, item.table),
57
+ routeRelationContext: naming_1.default.context(relation.on.table),
58
+ relationAttribute: relationAttribute,
59
+ tabs: this.tabs(context, item, recordName, relationAttribute),
60
+ });
61
+ }
62
+ }
63
+ return result;
64
+ }
65
+ tabs(context, item, recordName, relationAttribute) {
66
+ const tabs = [];
67
+ tabs.push({
68
+ label: 'Details',
69
+ route: `generathor.${context}.show`,
70
+ routeRecord: recordName,
71
+ });
72
+ for (const relation of item.relations) {
73
+ const currentRelationAttribute = naming_1.default.relationAttribute(relation, item.table);
74
+ const relationLabel = naming_1.default.relationLabel(relation, item.table);
75
+ if (currentRelationAttribute === relationAttribute) {
76
+ tabs.push({
77
+ label: relationLabel,
78
+ });
79
+ continue;
80
+ }
81
+ const capitalizedRelationAttribute = naming_1.default.capitalizedRelationAttribute(relation, item.table);
82
+ tabs.push({
83
+ label: relationLabel,
84
+ route: `generathor.${context}.show${capitalizedRelationAttribute}`,
85
+ routeRecord: recordName,
86
+ });
87
+ }
88
+ return tabs;
89
+ }
90
+ }
91
+ exports.HasManyRelation = HasManyRelation;
@@ -0,0 +1,4 @@
1
+ import { BaseTransformer, TransformedItem } from './base';
2
+ export declare class Menu extends BaseTransformer {
3
+ transform(): TransformedItem[];
4
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Menu = void 0;
7
+ const base_1 = require("./base");
8
+ const naming_1 = __importDefault(require("../../helpers/naming"));
9
+ class Menu extends base_1.BaseTransformer {
10
+ transform() {
11
+ return [
12
+ {
13
+ layout: this.$config.layout(),
14
+ homeRoute: this.$config.homeRoute(),
15
+ items: this.$items.map((item) => {
16
+ return {
17
+ context: naming_1.default.context(item.table),
18
+ label: naming_1.default.listLabel(item.table),
19
+ };
20
+ }),
21
+ },
22
+ ];
23
+ }
24
+ }
25
+ exports.Menu = Menu;
@@ -0,0 +1,5 @@
1
+ import { BaseTransformer, TransformedItem } from './base';
2
+ export declare class Route extends BaseTransformer {
3
+ transform(): TransformedItem[];
4
+ private controller;
5
+ }
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Route = void 0;
7
+ const base_1 = require("./base");
8
+ const naming_1 = __importDefault(require("../../helpers/naming"));
9
+ class Route extends base_1.BaseTransformer {
10
+ transform() {
11
+ const result = [];
12
+ for (const item of this.$items) {
13
+ const routes = [];
14
+ const context = naming_1.default.context(item.table);
15
+ const controller = this.controller(item.table);
16
+ routes.push({
17
+ context,
18
+ method: 'get',
19
+ path: context,
20
+ action: 'index',
21
+ precognition: true,
22
+ });
23
+ routes.push({
24
+ context,
25
+ method: 'post',
26
+ path: context,
27
+ action: 'store',
28
+ precognition: true,
29
+ });
30
+ if (item.primaryKey && item.primaryKey.columns.length === 1) {
31
+ routes.push({
32
+ context,
33
+ method: 'get',
34
+ path: `${context}/search`,
35
+ action: 'search',
36
+ precognition: false,
37
+ });
38
+ }
39
+ const recordName = naming_1.default.recordName(item.table);
40
+ routes.push({
41
+ context,
42
+ method: 'get',
43
+ path: `${context}/{${recordName}}`,
44
+ action: 'show',
45
+ precognition: false,
46
+ });
47
+ routes.push({
48
+ context,
49
+ method: 'get',
50
+ path: `${context}/{${recordName}}/edit`,
51
+ action: 'edit',
52
+ precognition: false,
53
+ });
54
+ routes.push({
55
+ context,
56
+ method: 'put',
57
+ path: `${context}/{${recordName}}`,
58
+ action: 'update',
59
+ precognition: true,
60
+ });
61
+ routes.push({
62
+ context,
63
+ method: 'delete',
64
+ path: `${context}/{${recordName}}`,
65
+ action: 'destroy',
66
+ precognition: false,
67
+ });
68
+ for (const relation of item.relations) {
69
+ const relationContext = naming_1.default.relationContext(relation, item.table);
70
+ const relationName = naming_1.default.capitalizedRelationAttribute(relation, item.table);
71
+ if (relation.type === 'has-many') {
72
+ routes.push({
73
+ context,
74
+ method: 'get',
75
+ path: `${context}/{${recordName}}/${relationContext}`,
76
+ action: 'show' + relationName,
77
+ precognition: true,
78
+ });
79
+ }
80
+ else {
81
+ routes.push({
82
+ context,
83
+ method: 'get',
84
+ path: `${context}/{${recordName}}/${relationContext}`,
85
+ action: 'show' + relationName,
86
+ precognition: false,
87
+ });
88
+ routes.push({
89
+ context,
90
+ method: 'post',
91
+ path: `${context}/{${recordName}}/${relationContext}`,
92
+ action: 'store' + relationName,
93
+ precognition: true,
94
+ });
95
+ routes.push({
96
+ context,
97
+ method: 'post',
98
+ path: `${context}/{${recordName}}/${relationContext}/attach`,
99
+ action: 'attach' + relationName,
100
+ precognition: true,
101
+ });
102
+ routes.push({
103
+ context,
104
+ method: 'post',
105
+ path: `${context}/{${recordName}}/${relationContext}/detach`,
106
+ action: 'detach' + relationName,
107
+ precognition: true,
108
+ });
109
+ }
110
+ }
111
+ result.push({
112
+ controller,
113
+ label: naming_1.default.listLabel(item.table),
114
+ routes,
115
+ });
116
+ }
117
+ return result;
118
+ }
119
+ controller(table) {
120
+ return 'App\\Http\\Controllers\\Generathor\\' + naming_1.default.controller(table);
121
+ }
122
+ }
123
+ exports.Route = Route;
@@ -0,0 +1,19 @@
1
+ import { Configuration } from '../../configuration';
2
+ import { Item } from 'generathor-db';
3
+ export interface CurrentItem extends Item {
4
+ [x: string | number | symbol]: any;
5
+ }
6
+ type FKDetails = {
7
+ relationAttribute: string;
8
+ table: string;
9
+ context: string;
10
+ routeRecordName: string;
11
+ };
12
+ export declare abstract class BaseTransformer {
13
+ protected $item: CurrentItem;
14
+ protected $config: Configuration;
15
+ constructor($item: CurrentItem, $config: Configuration);
16
+ abstract transform(): void;
17
+ protected fks(): Record<string, FKDetails>;
18
+ }
19
+ export {};
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseTransformer = void 0;
7
+ const naming_1 = __importDefault(require("../../helpers/naming"));
8
+ class BaseTransformer {
9
+ constructor($item, $config) {
10
+ this.$item = $item;
11
+ this.$config = $config;
12
+ }
13
+ fks() {
14
+ const fks = {};
15
+ this.$item.relations.forEach((relation) => {
16
+ if (relation.type !== 'belongs-to') {
17
+ return;
18
+ }
19
+ const relationColumnKeys = Object.keys(relation.columns);
20
+ const relationAttribute = naming_1.default.relationAttribute(relation, this.$item.table);
21
+ if (relationColumnKeys.length === 1) {
22
+ fks[relation.columns[0]] = {
23
+ relationAttribute,
24
+ table: relation.on.table,
25
+ context: naming_1.default.context(relation.on.table),
26
+ routeRecordName: naming_1.default.recordName(relation.on.table),
27
+ };
28
+ }
29
+ });
30
+ return fks;
31
+ }
32
+ }
33
+ exports.BaseTransformer = BaseTransformer;
@@ -0,0 +1,4 @@
1
+ import { BaseTransformer } from './base';
2
+ export declare class Edit extends BaseTransformer {
3
+ transform(): void;
4
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Edit = void 0;
7
+ const naming_1 = __importDefault(require("../../helpers/naming"));
8
+ const base_1 = require("./base");
9
+ class Edit extends base_1.BaseTransformer {
10
+ transform() {
11
+ this.$item.laravel.edit = {
12
+ layout: this.$config.layout(),
13
+ homeRoute: this.$config.homeRoute(),
14
+ context: naming_1.default.context(this.$item.table),
15
+ header: naming_1.default.listLabel(this.$item.table),
16
+ listLabel: naming_1.default.listLabel(this.$item.table),
17
+ recordName: naming_1.default.recordName(this.$item.table),
18
+ };
19
+ }
20
+ }
21
+ exports.Edit = Edit;
@@ -0,0 +1,4 @@
1
+ import { BaseTransformer } from './base';
2
+ export declare class Eloquent extends BaseTransformer {
3
+ transform(): void;
4
+ }
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.Eloquent = void 0;
30
+ const naming_1 = __importDefault(require("../../helpers/naming"));
31
+ const type = __importStar(require("../../helpers/type"));
32
+ const base_1 = require("./base");
33
+ class Eloquent extends base_1.BaseTransformer {
34
+ transform() {
35
+ const primaryKey = this.$item.primaryKey.columns.length === 0
36
+ ? [this.$item.columns[0].name]
37
+ : this.$item.primaryKey.columns;
38
+ const result = {
39
+ model: naming_1.default.modelClass(this.$item.table),
40
+ table: this.$item.table,
41
+ imports: [],
42
+ namespace: 'App\\Models',
43
+ recordLabel: null,
44
+ belongsToRelations: [],
45
+ hasManyRelations: [],
46
+ attributes: [],
47
+ primaryKey: primaryKey.length === 1
48
+ ? `'${primaryKey[0]}'`
49
+ : `['${primaryKey.join("', '")}']`,
50
+ onlyReadAttributes: [],
51
+ autoincrement: false,
52
+ timestamps: false,
53
+ constants: [],
54
+ };
55
+ const imports = {
56
+ [this.$config.eloquentParent(this.$item.table)]: true,
57
+ 'Kyslik\\ColumnSortable\\Sortable': true,
58
+ 'EloquentFilter\\Filterable': true,
59
+ };
60
+ let importCollection = false;
61
+ this.$item.relations.forEach((relation) => {
62
+ let relationClass = naming_1.default.modelClass(relation.on.table);
63
+ const relationName = naming_1.default.relationAttribute(relation, this.$item.table);
64
+ if (relation.on.table !== this.$item.table) {
65
+ imports['App\\Models\\' + relationClass] = true;
66
+ }
67
+ else {
68
+ imports[`App\\Models\\${relationClass} as ChildModel`] = true;
69
+ relationClass = 'ChildModel';
70
+ }
71
+ let attributeType = relationClass;
72
+ if (relation.type === 'has-many') {
73
+ attributeType = `Collection<${attributeType}>`;
74
+ importCollection = true;
75
+ }
76
+ result.onlyReadAttributes.push({
77
+ name: relationName,
78
+ type: attributeType,
79
+ });
80
+ const columns = {};
81
+ let reference = null;
82
+ if (relation.columns.length === 1) {
83
+ reference =
84
+ relation.type === 'has-many'
85
+ ? {
86
+ foreignKey: relation.references[0],
87
+ localKey: relation.columns[0],
88
+ }
89
+ : {
90
+ foreignKey: relation.columns[0],
91
+ ownerKey: relation.references[0],
92
+ };
93
+ }
94
+ for (const index in relation.columns) {
95
+ columns[relation.references[index]] = relation.columns[index];
96
+ }
97
+ if (relation.type === 'has-many') {
98
+ result.hasManyRelations.push({
99
+ reference,
100
+ name: relationName,
101
+ model: relationClass,
102
+ });
103
+ }
104
+ else {
105
+ result.belongsToRelations.push({
106
+ reference,
107
+ name: relationName,
108
+ model: relationClass,
109
+ });
110
+ }
111
+ });
112
+ let firstStringColumn = null;
113
+ const firstColumn = this.$item.columns[0].name;
114
+ let autoincrement = false;
115
+ const timestamps = {
116
+ created_at: false,
117
+ updated_at: false,
118
+ };
119
+ this.$item.columns.forEach((column) => {
120
+ if (column.enum && Array.isArray(column.enum)) {
121
+ column.enum.forEach((value) => {
122
+ result.constants.push({
123
+ name: `${column.name}_${value}`.toUpperCase(),
124
+ value: value,
125
+ });
126
+ });
127
+ }
128
+ if (!firstStringColumn && column.type === 'string') {
129
+ firstStringColumn = column.name;
130
+ }
131
+ if (!autoincrement && column.autoincrement) {
132
+ autoincrement = true;
133
+ }
134
+ if (!timestamps.created_at && column.name === 'created_at') {
135
+ timestamps.created_at = true;
136
+ }
137
+ if (!timestamps.updated_at && column.name === 'updated_at') {
138
+ timestamps.updated_at = true;
139
+ }
140
+ if (column.type === 'date') {
141
+ imports['Illuminate\\Support\\Carbon'] = true;
142
+ }
143
+ result.attributes.push({
144
+ type: type.phpType(column.type),
145
+ name: column.name,
146
+ });
147
+ });
148
+ result.recordLabel = firstStringColumn || firstColumn;
149
+ result.autoincrement = autoincrement;
150
+ result.timestamps = timestamps.created_at && timestamps.updated_at;
151
+ if (importCollection) {
152
+ imports['Illuminate\\Database\\Eloquent\\Collection'] = true;
153
+ }
154
+ result.imports = Object.keys(imports).sort();
155
+ this.$item.laravel.eloquent = result;
156
+ }
157
+ }
158
+ exports.Eloquent = Eloquent;
@@ -0,0 +1,5 @@
1
+ import { BaseTransformer } from './base';
2
+ export declare class Filter extends BaseTransformer {
3
+ transform(): void;
4
+ private pkMap;
5
+ }