Skip to content

Commit 6c36951

Browse files
chore: wip
1 parent 229504d commit 6c36951

File tree

9 files changed

+99
-49
lines changed

9 files changed

+99
-49
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import User from '../../../../../app/models/User'
22
import { fieldAssociation, fieldEntity, modelEntity } from './fields'
33

44
const file = Bun.file('user-migration.ts')
5+
6+
const driver = 'mysql'
7+
58
const writer = file.writer()
69

710
writer.write('import { Kysely, sql } from \'kysely\'\n')
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
// import { generateMigrationFile } from '@stacksjs/database'
2+
// import { config } from '@stacksjs/config'
23

3-
// const driver = 'mysql'
4+
// const driver = config.database.default
5+
const driver = 'sqlite'
6+
7+
if (driver === 'sqlite')
8+
await import('./migration-sqlite.ts')
9+
else
10+
await import('./migration-mysql.ts')
Binary file not shown.
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1-
// loops over all the models to generate the model classes
1+
import { path as p } from '@stacksjs/path'
2+
3+
const file = Bun.file(p.projectStoragePath('framework/orm/UserModel.ts'))
4+
5+
const writer = file.writer()
6+
7+
writer.write(`import { dbDialect } from '@stacksjs/database'
8+
import { Kysely } from 'kysely'
9+
10+
class UserModel extends Kysely<any> {
11+
constructor() {
12+
super({ dialect: dbDialect })
13+
}
14+
15+
public async find(id: number) {
16+
return await this.selectFrom('users')
17+
.selectAll()
18+
.where('id', '=', id)
19+
.executeTakeFirst()
20+
}
21+
22+
public all() {
23+
return this.selectFrom('users')
24+
.selectAll()
25+
}
26+
}
27+
28+
export const User = new UserModel()
29+
`)
30+
31+
await writer.end()

.stacks/core/actions/src/generate/stacks.sqlite

Whitespace-only changes.

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

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,39 @@
11
// export * from './migrations'
22
// export * from './seeder''
3-
4-
import { storage } from '@stacksjs/storage'
3+
import { Kysely, MysqlDialect } from 'kysely'
4+
import { createPool } from 'mysql2'
55
import { BunWorkerDialect } from './kysely-bun-worker'
66

7-
// eslint-disable-next-line no-console
8-
console.log('Hello from database!', storage)
9-
10-
const dialect = new BunWorkerDialect({
11-
database: sqdb,
7+
// const driver = config.database.default
8+
const driver = 'mysql'
9+
10+
// const dbName = config.database.name
11+
12+
export interface DatabaseType {
13+
user: any
14+
}
15+
16+
let dialect
17+
18+
if (driver === 'sqlite') {
19+
dialect = new BunWorkerDialect({
20+
url: 'stacks.sqlite',
21+
})
22+
}
23+
else {
24+
dialect = new MysqlDialect({
25+
pool: createPool({
26+
database: 'stacks',
27+
host: '127.0.0.1',
28+
user: 'root',
29+
password: '',
30+
port: 3306,
31+
}),
32+
})
33+
}
34+
35+
export const QueryBuilder = new Kysely<DatabaseType>({
36+
dialect,
1237
})
1338

14-
console.log(BunWorkerDialect)
15-
16-
// import { Kysely, MysqlDialect, SqliteDialect } from 'kysely'
17-
// import { createPool } from 'mysql2'
18-
// import { Database } from "bun:sqlite"
19-
20-
// const driver = 'sqlite'
21-
22-
// export interface DatabaseType {
23-
// user: any
24-
// }
25-
26-
// const sqdb = new Database("mydb.sqlite", { create: true })
27-
28-
// let dialect
29-
30-
// if (driver === 'sqlite') {
31-
// dialect = new SqliteDialect({
32-
// database: sqdb,
33-
// })
34-
// }
35-
// else {
36-
// dialect = new MysqlDialect({
37-
// pool: createPool({
38-
// database: 'stacks',
39-
// host: '127.0.0.1',
40-
// user: 'root',
41-
// password: '',
42-
// port: 3306,
43-
// connectionLimit: 10,
44-
// }),
45-
// })
46-
// }
47-
48-
// console.log(dialect)
49-
50-
// export const db = new Kysely<Database>({
51-
// dialect,
52-
// })
39+
export const dbDialect = dialect

.stacks/core/database/src/kysely-bun-worker/driver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { DatabaseConnection, Driver, QueryResult } from 'kysely'
22
import { CompiledQuery } from 'kysely'
3+
import { path as p } from '@stacksjs/path'
34
import type { EmitterOnce } from './mitt'
45
import MittOnce from './mitt'
56
import type { EventWithError, MainMsg, WorkerMsg } from './type'
@@ -17,7 +18,7 @@ export class BunWorkerDriver implements Driver {
1718

1819
async init(): Promise<void> {
1920
this.worker = this.config?.worker ?? new Worker(
20-
new URL('./worker', import.meta.url),
21+
new URL('../src/kysely-bun-worker/worker', import.meta.url),
2122
{ type: 'module' },
2223
)
2324
this.mitt = MittOnce<EventWithError>()

bun.lockb

0 Bytes
Binary file not shown.

storage/framework/orm/UserModel.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { dbDialect } from '@stacksjs/database'
2+
import { Kysely } from 'kysely'
3+
4+
class UserModel extends Kysely<any> {
5+
constructor() {
6+
super({ dialect: dbDialect })
7+
}
8+
9+
public async find(id: number) {
10+
return await this.selectFrom('users')
11+
.selectAll()
12+
.where('id', '=', id)
13+
.executeTakeFirst()
14+
}
15+
16+
public all() {
17+
return this.selectFrom('users')
18+
.selectAll()
19+
}
20+
}
21+
22+
export const User = new UserModel()

0 commit comments

Comments
 (0)