Skip to content

Commit da74c4a

Browse files
chore: wip
1 parent 77d310b commit da74c4a

File tree

4 files changed

+192
-120
lines changed

4 files changed

+192
-120
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import User from '../../../../../app/models/User'
2+
3+
const fields: any = User.fields
4+
5+
const fieldkeys = Object.keys(fields)
6+
7+
const input = [
8+
{
9+
field: 'name',
10+
rule: 'validate.string().maxLength(50).nullable()'
11+
},
12+
{
13+
field: 'status',
14+
rule: 'validate.string().maxLength(50).nullable()'
15+
},
16+
{
17+
field: 'email',
18+
rule: 'validate.string().maxLength(50).nullable()'
19+
},
20+
{
21+
field: 'password',
22+
rule: 'validate.string().maxLength(50).nullable()'
23+
}
24+
]
25+
26+
27+
const modelEntity = input.map((item) => {
28+
// Split the input string by "validate."
29+
const parts = item.rule.split('validate.');
30+
31+
// Extract the string after "validate."
32+
const extractedString = parts[1];
33+
34+
if (extractedString) {
35+
// Split the input string by periods (.)
36+
const extractedParts = extractedString.split('.');
37+
38+
const regex = /\(([^)]+)\)/;
39+
40+
const fieldArray = extractedParts.map(input => {
41+
// Use the regular expression to extract values inside parentheses
42+
const match = regex.exec(input);
43+
const value = match ? match[1] : null;
44+
45+
// Remove the parentheses from the string
46+
const field = input.replace(regex, '').replace(/\(|\)/g, '');;
47+
48+
return { entity: field, charValue: value };
49+
});
50+
51+
return { field: item.field, fieldArray }
52+
}
53+
})
54+
55+
56+
const fieldAssociation: { [key: string]: string } = {
57+
string: 'varchar',
58+
number: 'integer',
59+
boolean: 'boolean',
60+
text: 'text'
61+
};
62+
63+
const fieldEntity = [
64+
'maxLength',
65+
'minLength',
66+
]
67+
68+
69+
export { fieldEntity, fieldAssociation, modelEntity }
Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
// import { generateMigrationFile } from '@stacksjs/database'
22
import User from '../../../../../app/models/User'
3-
4-
// generateMigrationFile({
5-
// name: 'CreateUsersTable',
6-
// up: `
7-
// CREATE TABLE users (
8-
// id SERIAL PRIMARY KEY,
9-
// email VARCHAR(255) NOT NULL,
10-
// password VARCHAR(255) NOT NULL,
11-
// created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
12-
// updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
13-
// );
14-
// `,
15-
// down: `
16-
// DROP TABLE users;
17-
// `,
18-
// })
19-
20-
const fields = User.fields
3+
import { modelEntity, fieldAssociation, fieldEntity } from './fields'
214

225
const file = Bun.file('user-migration.ts')
236
const writer = file.writer()
@@ -30,17 +13,38 @@ writer.write('export async function up(db: Kysely<any>): Promise<void> { \n')
3013
writer.write(' await db.schema \n')
3114
writer.write(` .createTable('${User.table}') \n`)
3215

33-
writer.write('.addColumn(\'created_at\', \'timestamp\', (col) => col.defaultTo(sql`now()`).notNull()) \n')
16+
for (let modelIndex = 0; modelIndex < modelEntity.length; modelIndex++) {
17+
const modelElement: any = modelEntity[modelIndex];
3418

35-
writer.end()
19+
let entity = '';
20+
21+
for (let fieldIndex = 0; fieldIndex < modelElement.fieldArray.length; fieldIndex++) {
22+
const fieldArrayElement = modelElement.fieldArray[fieldIndex];
23+
24+
console.log(fieldArrayElement)
25+
26+
if (fieldAssociation[fieldArrayElement.entity]) {
27+
entity += `${fieldAssociation[fieldArrayElement.entity]}`;
28+
}
3629

37-
// // Rules
38-
// if (fields)
39-
// console.log(Object.keys(fields))
30+
if (fieldArrayElement.charValue)
31+
entity += `(${fieldArrayElement.charValue})`;
4032

41-
// const regex = /rule:.*$/gm;
42-
// let match;
33+
fieldEntity.forEach((entity) => {
34+
if (fieldArrayElement.entity === entity) {
35+
console.log(fieldArrayElement.charValue)
36+
entity += `(${fieldArrayElement.charValue})`;
37+
}
38+
})
39+
}
4340

44-
// while ((match = regex.exec(code)) !== null) {
45-
// console.log(match[0]); // Outputs: 'rule: validate.string().minLength(3).maxLength(255).nullable()', etc.
46-
// }
41+
writer.write(` .addColumn('${modelElement.field}', '${entity}', (col) => col.notNull()) \n`);
42+
}
43+
44+
writer.write(' .addColumn(\'created_at\', \'timestamp\', (col) => col.defaultTo(sql`now()`).notNull()) \n')
45+
writer.write(' .execute() \n')
46+
writer.write('} \n\n')
47+
48+
writer.write('process.exit(0) \n')
49+
50+
writer.end()

.stacks/core/actions/src/database/user-migration.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ import { db } from '@stacksjs/database'
44
export async function up(db: Kysely<any>): Promise<void> {
55
await db.schema
66
.createTable('users')
7-
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`now()`).notNull())
7+
.addColumn('name', 'varchar(50)', (col) => col.notNull())
8+
.addColumn('status', 'varchar(50)', (col) => col.notNull())
9+
.addColumn('email', 'varchar(50)', (col) => col.notNull())
10+
.addColumn('password', 'varchar(50)', (col) => col.notNull())
11+
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`now()`).notNull())
12+
.execute()
13+
}
14+
15+
process.exit(0)

package.json

Lines changed: 82 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "your-project-name",
33
"type": "module",
4-
"version": "alpha-v0.0.0",
4+
"version": "0.0.0",
55
"private": true,
6-
"description": "Your project description placeholder.",
6+
"packageManager": "pnpm@8.2.0",
7+
"description": "Your project description",
78
"author": "Chris Breuer",
89
"license": "MIT",
910
"funding": "https://github.com/sponsors/chrisbbreuer",
@@ -19,85 +20,86 @@
1920
"Chris Breuer <chris@ow3.org>"
2021
],
2122
"scripts": {
22-
"buddy": "bun .stacks/core/buddy/src/cli.ts",
23-
"stacks": "bun buddy",
24-
"setup": "./.stacks/scripts/setup.sh",
25-
"fresh": "bun buddy fresh",
26-
"clean": "bun buddy clean",
27-
"upgrade": "bun buddy upgrade",
28-
"upgrade:dependencies": "bun buddy upgrade:dependencies",
29-
"upgrade:framework": "bun buddy upgrade:framework",
30-
"upgrade:bun": "bun buddy upgrade:bun",
31-
"dev": "bun buddy dev",
32-
"dev:view": "bunx --bun vite serve ./storage/framework/web -c ./.stacks/core/vite/src/views.ts",
33-
"dev:components": "bun buddy dev:components",
34-
"dev:desktop": "bun buddy dev:desktop",
35-
"dev:docs": "bun buddy dev:docs",
36-
"dev:pages": "bun buddy dev:pages",
37-
"dev:functions": "bun buddy dev:functions // todo: opens the auto-generated web server of functions",
38-
"development": "bun buddy dev",
39-
"build": "bun buddy build",
40-
"build:components": "bun buddy build:components",
41-
"build:vue-components": "bun buddy build:web-components",
42-
"build:web-components": "bun buddy build:web-components",
43-
"build:functions": "bun buddy build:functions // needs to have option to ",
44-
"build:docs": "bun buddy build:docs",
45-
"build:pages": "bun buddy build:pages // needs to auto-generate server",
46-
"build:stacks": "bun buddy build:stacks",
47-
"prod": "bun buddy build",
48-
"prod:components": "bun buddy build:components",
49-
"prod:vue-components": "bun buddy build:web-components",
50-
"prod:web-components": "bun buddy build:web-components",
51-
"prod:functions": "bun buddy build:functions // needs to have option to ",
52-
"prod:docs": "bun buddy build:docs",
53-
"prod:pages": "bun buddy build:pages // needs to auto-generate server",
54-
"prod:stacks": "bun buddy build:stacks",
55-
"prod:all": "bun buddy build:all",
56-
"production": "bun buddy build",
57-
"deploy": "bun buddy deploy",
58-
"deploy:functions": "bun buddy deploy:functions",
59-
"deploy:pages": "bun buddy deploy:functions",
60-
"deploy:docs": "bun buddy deploy:docs",
61-
"deploy:all": "bun buddy deploy:all",
62-
"example": "bun buddy example",
63-
"example:vue": "bun buddy example:vue",
64-
"example:web-components": "bun buddy example:web-components",
65-
"lint": "bun buddy lint",
66-
"lint:fix": "bun buddy lint:fix",
67-
"serve": "bun buddy serve",
68-
"serve:pages": "bun buddy serve:pages",
69-
"serve:functions": "bun buddy serve:functions",
70-
"make": "bun buddy make",
71-
"make:component": "bun buddy make:component",
72-
"make:function": "bun buddy make:function",
73-
"make:database": "bun buddy make:migration",
74-
"make:migration": "bun buddy make:migration",
75-
"make:notification": "bun buddy make:notification",
76-
"make:factory": "bun buddy make:factory",
77-
"make:lang": "bun buddy make:lang",
78-
"make:stack": "bun buddy make:stack",
79-
"key": "bun buddy key",
80-
"key:generate": "bun buddy key:generate",
81-
"commit": "bun buddy commit",
82-
"release": "bun buddy release",
83-
"changelog": "bun buddy changelog",
84-
"generate": "bun buddy generate",
85-
"generate:entries": "bun buddy generate:entries",
86-
"generate:vue-compat": "bun buddy generate:vue-compat",
87-
"generate:vscode-custom-data": "bun buddy generate:vscode-custom-data",
88-
"generate:web-types": "bun buddy generate:web-types",
89-
"generate:component-meta": "bun buddy generate:component-meta",
90-
"generate:all": "bun buddy generate:all",
91-
"types:generate": "bun buddy types:generate",
92-
"types:fix": "bun buddy types:fix",
93-
"test": "bun buddy test",
94-
"test:ui": "bun buddy test:ui",
95-
"test:coverage": "bun buddy test:coverage",
96-
"test:types": "bun buddy test:types",
97-
"bud": "bun buddy",
98-
"stx": "bun buddy"
23+
"buddy": "esno .stacks/core/buddy/src/cli.ts",
24+
"stacks": "pnpm buddy",
25+
"fresh": "pnpm buddy fresh",
26+
"clean": "pnpm buddy clean",
27+
"update": "pnpm buddy update",
28+
"update:dependencies": "pnpm buddy update:dependencies",
29+
"update:framework": "pnpm buddy update:framework",
30+
"update:package-manager": "pnpm buddy update:package-manager",
31+
"update:node": "pnpm buddy update:node",
32+
"dev": "pnpm buddy dev",
33+
"dev:components": "pnpm buddy dev:components",
34+
"dev:desktop": "pnpm buddy dev:desktop",
35+
"dev:docs": "pnpm buddy dev:docs",
36+
"dev:pages": "pnpm buddy dev:pages",
37+
"dev:functions": "pnpm buddy dev:functions // todo: opens the auto-generated web server of functions",
38+
"development": "pnpm buddy dev",
39+
"build": "pnpm buddy build",
40+
"build:components": "pnpm buddy build:components",
41+
"build:vue-components": "pnpm buddy build:web-components",
42+
"build:web-components": "pnpm buddy build:web-components",
43+
"build:functions": "pnpm buddy build:functions // needs to have option to ",
44+
"build:docs": "pnpm buddy build:docs",
45+
"build:pages": "pnpm buddy build:pages // needs to auto-generate server",
46+
"build:stacks": "pnpm buddy build:stacks",
47+
"prod": "pnpm buddy build",
48+
"prod:components": "pnpm buddy build:components",
49+
"prod:vue-components": "pnpm buddy build:web-components",
50+
"prod:web-components": "pnpm buddy build:web-components",
51+
"prod:functions": "pnpm buddy build:functions // needs to have option to ",
52+
"prod:docs": "pnpm buddy build:docs",
53+
"prod:pages": "pnpm buddy build:pages // needs to auto-generate server",
54+
"prod:stacks": "pnpm buddy build:stacks",
55+
"prod:all": "pnpm buddy build:all",
56+
"production": "pnpm buddy build",
57+
"deploy": "pnpm buddy deploy",
58+
"deploy:functions": "pnpm buddy deploy:functions",
59+
"deploy:pages": "pnpm buddy deploy:functions",
60+
"deploy:docs": "pnpm buddy deploy:docs",
61+
"deploy:all": "pnpm buddy deploy:all",
62+
"example": "pnpm buddy example",
63+
"example:vue": "pnpm buddy example:vue",
64+
"example:web-components": "pnpm buddy example:web-components",
65+
"lint": "pnpm buddy lint",
66+
"lint:stacks": "pnpm buddy lint:stacks",
67+
"lint:fix": "pnpm buddy lint:fix",
68+
"serve": "pnpm buddy serve",
69+
"serve:pages": "pnpm buddy serve:pages",
70+
"serve:functions": "pnpm buddy serve:functions",
71+
"make": "pnpm buddy make",
72+
"make:component": "pnpm buddy make:component",
73+
"make:function": "pnpm buddy make:function",
74+
"make:database": "pnpm buddy make:migration",
75+
"make:migration": "pnpm buddy make:migration",
76+
"make:notification": "pnpm buddy make:notification",
77+
"make:factory": "pnpm buddy make:factory",
78+
"make:lang": "pnpm buddy make:lang",
79+
"make:stack": "pnpm buddy make:stack",
80+
"key": "pnpm buddy key",
81+
"key:generate": "pnpm buddy key:generate",
82+
"commit": "pnpm buddy commit",
83+
"release": "pnpm buddy release",
84+
"changelog": "pnpm buddy changelog",
85+
"generate": "pnpm buddy generate",
86+
"generate:entries": "pnpm buddy generate:entries",
87+
"generate:vue-compat": "pnpm buddy generate:vue-compat",
88+
"generate:vscode-custom-data": "pnpm buddy generate:vscode-custom-data",
89+
"generate:web-types": "pnpm buddy generate:web-types",
90+
"generate:component-meta": "pnpm buddy generate:component-meta",
91+
"generate:all": "pnpm buddy generate:all",
92+
"types:generate": "pnpm buddy types:generate",
93+
"types:fix": "pnpm buddy types:fix",
94+
"test": "pnpm buddy test",
95+
"test:ui": "pnpm buddy test:ui",
96+
"test:coverage": "pnpm buddy test:coverage",
97+
"test:types": "pnpm buddy test:types",
98+
"bud": "pnpm buddy",
99+
"stx": "pnpm buddy"
99100
},
100101
"dependencies": {
102+
"pg": "^8.10.0",
101103
"stacks": "workspace:*"
102104
},
103105
"lint-staged": {
@@ -107,16 +109,5 @@
107109
"commitizen": {
108110
"path": ".stacks/node_modules/cz-git"
109111
}
110-
},
111-
"workspaces": [
112-
".stacks/core/*",
113-
".stacks/docs/*",
114-
".stacks/fonts/*",
115-
".stacks/ide/*",
116-
".stacks/scripts/*",
117-
".stacks/stacks/*",
118-
".stacks/vcs/*",
119-
".stacks",
120-
"storage/framework/*"
121-
]
112+
}
122113
}

0 commit comments

Comments
 (0)