Skip to content

Commit 1b3b43f

Browse files
chore: wip
1 parent e09e614 commit 1b3b43f

File tree

8 files changed

+88
-24
lines changed

8 files changed

+88
-24
lines changed
Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,47 @@
1-
import { generateMigrationFile } from '@stacksjs/database'
2-
3-
generateMigrationFile({
4-
name: 'CreateUsersTable',
5-
up: `
6-
CREATE TABLE users (
7-
id SERIAL PRIMARY KEY,
8-
email VARCHAR(255) NOT NULL,
9-
password VARCHAR(255) NOT NULL,
10-
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
11-
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
12-
);
13-
`,
14-
down: `
15-
DROP TABLE users;
16-
`,
17-
})
1+
// import { generateMigrationFile } from '@stacksjs/database'
2+
import { projectPath } from "@stacksjs/path"
3+
import User from '../../../../../app/models/User'
4+
5+
// generateMigrationFile({
6+
// name: 'CreateUsersTable',
7+
// up: `
8+
// CREATE TABLE users (
9+
// id SERIAL PRIMARY KEY,
10+
// email VARCHAR(255) NOT NULL,
11+
// password VARCHAR(255) NOT NULL,
12+
// created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
13+
// updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
14+
// );
15+
// `,
16+
// down: `
17+
// DROP TABLE users;
18+
// `,
19+
// })
20+
21+
const fields = User.fields
22+
23+
const file = Bun.file("user-migration.ts");
24+
const writer = file.writer();
25+
26+
writer.write("import { Kysely, sql } from 'kysely'\n");
27+
writer.write("import { db } from '@stacksjs/database'\n");
28+
writer.write(" \n");
29+
writer.write("export async function up(db: Kysely<any>): Promise<void> { \n");
30+
31+
writer.write(" await db.schema \n");
32+
writer.write(` .createTable('${User.table}') \n`);
33+
34+
writer.write(".addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`now()`).notNull()) \n")
35+
36+
writer.end();
37+
38+
// // Rules
39+
// if (fields)
40+
// console.log(Object.keys(fields))
41+
42+
// const regex = /rule:.*$/gm;
43+
// let match;
44+
45+
// while ((match = regex.exec(code)) !== null) {
46+
// console.log(match[0]); // Outputs: 'rule: validate.string().minLength(3).maxLength(255).nullable()', etc.
47+
// }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Kysely, sql } from 'kysely'
2+
import { db } from '@stacksjs/database'
3+
4+
export async function up(db: Kysely<any>): Promise<void> {
5+
await db.schema
6+
.createTable('users')
7+
.addColumn('created_at', 'timestamp', (col) => col.defaultTo(sql`now()`).notNull())

.stacks/core/database/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
1-
export * from './migrations'
2-
export * from './seeder'
1+
// export * from './migrations'
2+
// export * from './seeder''
3+
4+
import { Kysely } from 'kysely'
5+
import { MysqlDialect } from 'kysely'
6+
import { createPool } from 'mysql2'
7+
import { Database } from "bun:sqlite";
8+
9+
const dialect = new MysqlDialect({
10+
pool: createPool({
11+
database: 'stacks',
12+
host: '127.0.0.1',
13+
user: 'root',
14+
password: '',
15+
port: 3306,
16+
connectionLimit: 10,
17+
}),
18+
})
19+
20+
export const db = new Kysely<Database>({
21+
dialect,
22+
})

.stacks/core/database/src/mydb.sqlite

8 KB
Binary file not shown.
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import { Kysely, sql } from 'kysely'
2+
import { db } from '@stacksjs/database'
23

34
export async function up(db: Kysely<any>): Promise<void> {
45
await db.schema
5-
.createTable('person')
6-
.addColumn('id', 'serial', (col) => col.primaryKey())
7-
.addColumn('first_name', 'varchar', (col) => col.notNull())
8-
.addColumn('last_name', 'varchar')
9-
.addColumn('gender', 'varchar(50)', (col) => col.notNull())
6+
.createTable('users')
7+
.addColumn('id', 'integer', (col) => col.primaryKey())
8+
.addColumn('name', 'varchar(255)')
9+
.addColumn('email', 'varchar(255)', (col) => col.notNull())
10+
.addColumn('password', 'varchar(255)', (col) => col.notNull())
1011
.addColumn('created_at', 'timestamp', (col) =>
1112
col.defaultTo(sql`now()`).notNull()
1213
)
1314
.execute()
1415
}
16+
17+
await up(db)
18+
process.exit(0)

.stacks/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"../*.d.ts",
6565
"../**/*.ts",
6666
"../**/*.d.ts",
67+
"./database/**/*",
6768
"./views/**/*",
6869
"./stacks/**/*",
6970
"./scripts/**/*",

bun.lockb

753 Bytes
Binary file not shown.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
"stx": "bun buddy"
9898
},
9999
"dependencies": {
100+
"kysely-bun-sqlite": "^0.1.1",
101+
"mysql2": "^3.6.0",
100102
"stacks": "workspace:*"
101103
},
102104
"lint-staged": {

0 commit comments

Comments
 (0)