Skip to content

Commit a9a650d

Browse files
committed
chore: wip
1 parent a0c8afa commit a9a650d

File tree

24 files changed

+367
-88
lines changed

24 files changed

+367
-88
lines changed

app/Models/AccessToken.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { collect } from '@stacksjs/collections'
12
import { faker } from '@stacksjs/faker'
23
import type { Model } from '@stacksjs/types'
34
import { schema } from '@stacksjs/validation'
45

56
export default {
67
name: 'AccessToken', // defaults to the sanitized file name
7-
table: 'access_tokens', // defaults to the lowercase, plural name of the model
8+
table: 'access_tokens', // defaults to the lowercase, plural name of the model name (or the name of the model file)
89
primaryKey: 'id', // defaults to `id`
910
autoIncrement: true, // defaults to true
1011

@@ -41,10 +42,20 @@ export default {
4142
abilities: {
4243
validator: {
4344
rule: schema.enum(['read', 'write', 'admin', 'read|write', 'read|admin', 'write|admin', 'read|write|admin']),
44-
message: '`abilities` must be string of either `read`, `write`, `admin`, `read|write`, `read|admin`, `write|admin`, or `read|write|admin`',
45+
message:
46+
'`abilities` must be string of either `read`, `write`, `admin`, `read|write`, `read|admin`, `write|admin`, or `read|write|admin`',
4547
},
4648

47-
factory: () => faker.random.arrayElement(['read', 'write', 'admin', 'read|write', 'read|admin', 'write|admin', 'read|write|admin']),
49+
factory: () =>
50+
collect([
51+
'read',
52+
'write',
53+
'admin',
54+
'read|write',
55+
'read|admin',
56+
'write|admin',
57+
'read|write|admin',
58+
]).random(),
4859
},
4960
},
5061
} satisfies Model

app/Models/Post.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import User from './User'
55

66
export default {
77
name: 'Post', // defaults to the sanitized file name
8-
table: 'posts', // defaults to the lowercase, plural name of the model
8+
table: 'posts', // defaults to the lowercase, plural name of the model name (or the name of the model file)
99
primaryKey: 'id', // defaults to `id`
1010
autoIncrement: true, // defaults to true
1111

app/Models/Project.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { faker } from '@stacksjs/faker'
22
import type { Model } from '@stacksjs/types'
33
import { schema } from '@stacksjs/validation'
4+
import { collect } from '@stacksjs/collections'
45

56
export default {
67
name: 'Project', // defaults to the sanitized file name
7-
table: 'projects', // defaults to the lowercase, plural name of the model
8+
table: 'projects', // defaults to the lowercase, plural name of the model name (or the name of the model file)
89
primaryKey: 'id', // defaults to `id`
910
autoIncrement: true, // defaults to true
1011

@@ -32,7 +33,7 @@ export default {
3233
message: '`description` must be a string',
3334
},
3435

35-
factory: () => faker.lorem.sentence({ min: 10, max: 10 }),
36+
factory: () => faker.lorem.sentence({ min: 10, max: 25 }),
3637
},
3738

3839
url: {
@@ -50,7 +51,7 @@ export default {
5051
message: '`status` must be a string',
5152
},
5253

53-
factory: () => faker.random.arrayElement(['active', 'inactive']),
54+
factory: () => collect(['active', 'inactive']).random(),
5455
},
5556
},
5657
} satisfies Model

app/Models/Subscriber.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { schema } from '@stacksjs/validation'
44

55
export default {
66
name: 'Subscriber', // defaults to the sanitized file name
7-
table: 'subscribers', // defaults to the lowercase, plural name of the model
7+
table: 'subscribers', // defaults to the lowercase, plural name of the model name (or the name of the model file)
88
primaryKey: 'id', // defaults to `id`
99
autoIncrement: true, // defaults to true
1010

app/Models/Team.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { collect } from '@stacksjs/collections'
12
import { faker } from '@stacksjs/faker'
23
import type { Model } from '@stacksjs/types'
34
import { schema } from '@stacksjs/validation'
45

56
export default {
67
name: 'Team', // defaults to the sanitized file name
7-
table: 'teams', // defaults to the lowercase, plural name of the model
8+
table: 'teams', // defaults to the lowercase, plural name of the model name (or the name of the model file)
89
primaryKey: 'id', // defaults to `id`
910
autoIncrement: true, // defaults to true
1011

@@ -61,7 +62,7 @@ export default {
6162
message: '`status` must be a string',
6263
},
6364

64-
factory: () => faker.random.arrayElement(['deployed', 'inactive']),
65+
factory: () => collect(['deployed', 'inactive']).random(),
6566
},
6667

6768
description: {
@@ -79,10 +80,7 @@ export default {
7980
message: '`path` must be a string',
8081
},
8182

82-
factory: () =>
83-
faker.random.arrayElement([
84-
`/Users/chrisbreuer/Code/${faker.lorem.words().toLowerCase().replace(/\s+/g, '-')}`,
85-
]),
83+
factory: () => `/Users/chrisbreuer/Code/${faker.lorem.words().toLowerCase().replace(/\s+/g, '-')}`,
8684
},
8785

8886
isPersonal: {

app/Models/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { schema } from '@stacksjs/validation'
77

88
export default {
99
name: 'User', // defaults to the sanitized file name
10-
table: 'users', // defaults to the lowercase, plural name of the model name
10+
table: 'users', // defaults to the lowercase, plural name of the model name (or the name of the model file)
1111
primaryKey: 'id', // defaults to `id`
1212
autoIncrement: true, // defaults to true
1313

database/migrations/1715868686184-create-posts-table.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

database/migrations/1715868686184-create-users-table.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

database/migrations/1715868686185-create-subscribers-table.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

storage/framework/core/actions/src/orm/generate-model.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { log } from '@stacksjs/logging'
22
import { path } from '@stacksjs/path'
33
import { fs, glob } from '@stacksjs/storage'
44
import { pascalCase } from '@stacksjs/strings'
5-
import { isString } from '@stacksjs/validation'
65
import type { Model, RelationConfig } from '@stacksjs/types'
6+
import { isString } from '@stacksjs/validation'
7+
import { modelTableName } from '@stacksjs/orm'
78

89
export interface FieldArrayElement {
910
entity: string
@@ -128,7 +129,7 @@ async function writeOrmActions(apiRoute: string, model: Model): Promise<void> {
128129

129130
async function writeApiRoutes(apiRoute: string, model: Model): Promise<string> {
130131
let routeString = ``
131-
const tableName = model.table
132+
const tableName = await modelTableName(model)
132133
const modelName = model.name
133134

134135
if (apiRoute === 'index') routeString += `await route.get('${tableName}', 'Actions/${modelName}IndexOrmAction')\n\n`
@@ -152,7 +153,7 @@ async function initiateModelGeneration(): Promise<void> {
152153
await deleteExistingOrmActions()
153154
await deleteExistingModelNameTypes()
154155
await writeModelNames()
155-
156+
156157
const modelFiles = glob.sync(path.userModelsPath('*.ts'))
157158

158159
for (const modelFile of modelFiles) {
@@ -193,7 +194,7 @@ async function getRelations(model: Model): Promise<RelationConfig[]> {
193194
const modelRelationPath = path.userModelsPath(`${relationModel}.ts`)
194195

195196
const modelRelation = (await import(modelRelationPath)).default
196-
197+
197198
const formattedModelName = model.name?.toLowerCase()
198199

199200
relationships.push({

0 commit comments

Comments
 (0)