Skip to content

Commit 7e38c9c

Browse files
chore: wip
1 parent 1926c42 commit 7e38c9c

23 files changed

+5441
-5007
lines changed

storage/framework/core/orm/src/utils.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ export async function generateModelString(
10751075
jsonFields += `${snakeCase(attribute.field)}: this.${snakeCase(attribute.field)},\n `
10761076

10771077
whereStatements += `static where${pascalCase(attribute.field)}(value: string): ${modelName}Model {
1078-
const instance = new this(null)
1078+
const instance = new ${modelName}Model(null)
10791079
10801080
instance.query = instance.query.where('${attribute.field}', '=', value)
10811081
@@ -1227,7 +1227,7 @@ export async function generateModelString(
12271227
static async find(id: number): Promise<${modelName}Model | undefined> {
12281228
let query = db.selectFrom('${tableName}').where('id', '=', id).selectAll()
12291229
1230-
const instance = new this(null)
1230+
const instance = new ${modelName}Model(null)
12311231
12321232
const model = await query.executeTakeFirst()
12331233
@@ -1236,13 +1236,13 @@ export async function generateModelString(
12361236
12371237
cache.getOrSet(\`${formattedModelName}:\${id}\`, JSON.stringify(model))
12381238
1239-
return instance.parseResult(new this(model))
1239+
return instance.parseResult(new ${modelName}Model(model))
12401240
}
12411241
12421242
static async all(): Promise<${modelName}Model[]> {
12431243
let query = db.selectFrom('${tableName}').selectAll()
12441244
1245-
const instance = new this(null)
1245+
const instance = new ${modelName}Model(null)
12461246
12471247
if (instance.softDeletes) {
12481248
query = query.where('deleted_at', 'is', null)
@@ -1257,7 +1257,7 @@ export async function generateModelString(
12571257
static async findOrFail(id: number): Promise<${modelName}Model> {
12581258
let query = db.selectFrom('${tableName}').where('id', '=', id)
12591259
1260-
const instance = new this(null)
1260+
const instance = new ${modelName}Model(null)
12611261
12621262
if (instance.softDeletes) {
12631263
query = query.where('deleted_at', 'is', null);
@@ -1272,13 +1272,13 @@ export async function generateModelString(
12721272
12731273
cache.getOrSet(\`${formattedModelName}:\${id}\`, JSON.stringify(model))
12741274
1275-
return instance.parseResult(new this(model))
1275+
return instance.parseResult(new ${modelName}Model(model))
12761276
}
12771277
12781278
static async findMany(ids: number[]): Promise<${modelName}Model[]> {
12791279
let query = db.selectFrom('${tableName}').where('id', 'in', ids)
12801280
1281-
const instance = new this(null)
1281+
const instance = new ${modelName}Model(null)
12821282
12831283
if (instance.softDeletes) {
12841284
query = query.where('deleted_at', 'is', null);
@@ -1293,7 +1293,7 @@ export async function generateModelString(
12931293
12941294
// Method to get a User by criteria
12951295
static async get(): Promise<UserModel[]> {
1296-
const instance = new this(null)
1296+
const instance = new ${modelName}Model(null)
12971297
12981298
if (instance.hasSelect) {
12991299
if (instance.softDeletes) {
@@ -1338,7 +1338,7 @@ export async function generateModelString(
13381338
}
13391339
13401340
static async count(): Promise<number> {
1341-
const instance = new this(null)
1341+
const instance = new ${modelName}Model(null)
13421342
13431343
if (instance.softDeletes) {
13441344
instance.query = instance.query.where('deleted_at', 'is', null);
@@ -1399,7 +1399,7 @@ export async function generateModelString(
13991399
14001400
// Method to create a new ${formattedModelName}
14011401
static async create(new${modelName}: New${modelName}): Promise<${modelName}Model> {
1402-
const instance = new this(null)
1402+
const instance = new ${modelName}Model(null)
14031403
14041404
const filteredValues = Object.fromEntries(
14051405
Object.entries(new${modelName}).filter(([key]) => instance.fillable.includes(key)),
@@ -1430,7 +1430,7 @@ export async function generateModelString(
14301430
14311431
// Method to remove a ${modelName}
14321432
static async remove(id: number): Promise<void> {
1433-
const instance = new this(null)
1433+
const instance = new ${modelName}Model(null)
14341434
const model = await instance.find(id)
14351435
14361436
if (instance.softDeletes) {
@@ -1474,7 +1474,7 @@ export async function generateModelString(
14741474
let operator: any
14751475
let value: any
14761476
1477-
const instance = new this(null)
1477+
const instance = new ${modelName}Model(null)
14781478
14791479
if (args.length === 2) {
14801480
[column, value] = args
@@ -1493,7 +1493,7 @@ export async function generateModelString(
14931493
${whereStatements}
14941494
14951495
static whereIn(column: keyof ${modelName}Type, values: any[]): ${modelName}Model {
1496-
const instance = new this(null)
1496+
const instance = new ${modelName}Model(null)
14971497
14981498
instance.query = instance.query.where(column, 'in', values)
14991499
@@ -1540,7 +1540,7 @@ export async function generateModelString(
15401540
}
15411541
15421542
static orderBy(column: keyof ${modelName}Type, order: 'asc' | 'desc'): ${modelName}Model {
1543-
const instance = new this(null)
1543+
const instance = new ${modelName}Model(null)
15441544
15451545
instance.query = instance.query.orderBy(column, order)
15461546
@@ -1554,7 +1554,7 @@ export async function generateModelString(
15541554
}
15551555
15561556
static orderByDesc(column: keyof ${modelName}Type): ${modelName}Model {
1557-
const instance = new this(null)
1557+
const instance = new ${modelName}Model(null)
15581558
15591559
instance.query = instance.query.orderBy(column, 'desc')
15601560
@@ -1568,7 +1568,7 @@ export async function generateModelString(
15681568
}
15691569
15701570
static orderByAsc(column: keyof ${modelName}Type): ${modelName}Model {
1571-
const instance = new this(null)
1571+
const instance = new ${modelName}Model(null)
15721572
15731573
instance.query = instance.query.orderBy(column, 'asc')
15741574
@@ -1671,7 +1671,7 @@ export async function generateModelString(
16711671
}
16721672
16731673
static distinct(column: keyof ${modelName}Type): ${modelName}Model {
1674-
const instance = new this(null)
1674+
const instance = new ${modelName}Model(null)
16751675
16761676
instance.query = instance.query.select(column).distinct()
16771677
@@ -1687,7 +1687,7 @@ export async function generateModelString(
16871687
}
16881688
16891689
static join(table: string, firstCol: string, secondCol: string): ${modelName}Model {
1690-
const instance = new this(null)
1690+
const instance = new ${modelName}Model(null)
16911691
16921692
instance.query = instance.query.innerJoin(table, firstCol, secondCol)
16931693
@@ -1701,9 +1701,6 @@ export async function generateModelString(
17011701
toJSON() {
17021702
const output: Partial<${modelName}Type> = ${jsonFields}
17031703
1704-
this.hidden.forEach((attr: string) => {
1705-
if (attr in output) delete (output as Record<string, any>)[attr]
1706-
})
17071704
17081705
type ${modelName} = Omit<${modelName}Type, 'password'>
17091706
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
export type ModelNames =
2-
| 'Project'
3-
| 'SubscriberEmail'
4-
| 'AccessToken'
5-
| 'Team'
6-
| 'Subscriber'
7-
| 'Deployment'
8-
| 'Release'
9-
| 'User'
10-
| 'Post'
1+
export type ModelNames = 'Project' | 'SubscriberEmail' | 'AccessToken' | 'Team' | 'Subscriber' | 'Deployment' | 'Release' | 'User' | 'Post'

storage/framework/orm/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { route } from '@stacksjs/router'
22

3+
34
route.get('users', 'UserIndexOrmAction').middleware(['Api'])
45

56
route.post('users', 'UserStoreOrmAction').middleware(['Api'])
67

78
route.get('users/{id}', 'UserShowOrmAction').middleware(['Api'])
9+

0 commit comments

Comments
 (0)