@stacksjs/database 0.61.20 → 0.61.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +794 -741
- package/package.json +4 -4
- package/src/drivers/index.ts +34 -16
- package/src/drivers/mysql.ts +11 -11
- package/src/drivers/postgres.ts +13 -13
- package/src/drivers/sqlite.ts +11 -11
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stacksjs/database",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.61.
|
|
4
|
+
"version": "0.61.21",
|
|
5
5
|
"description": "The Stacks database integration.",
|
|
6
6
|
"author": "Chris Breuer",
|
|
7
7
|
"license": "MIT",
|
|
@@ -71,14 +71,14 @@
|
|
|
71
71
|
"kysely-bun-worker": "^0.6.1"
|
|
72
72
|
},
|
|
73
73
|
"optionalDependencies": {
|
|
74
|
-
"mysql2": "^3.
|
|
74
|
+
"mysql2": "^3.10.0"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
77
|
"@stacksjs/development": "latest",
|
|
78
78
|
"@types/tar": "^6.1.13",
|
|
79
|
-
"debug": "^4.3.
|
|
79
|
+
"debug": "^4.3.5",
|
|
80
80
|
"mkdirp": "^3.0.1",
|
|
81
81
|
"q": "^1.5.1",
|
|
82
|
-
"tar": "^7.
|
|
82
|
+
"tar": "^7.2.0"
|
|
83
83
|
}
|
|
84
84
|
}
|
package/src/drivers/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { log } from '@stacksjs/cli'
|
|
2
2
|
import { db } from '@stacksjs/database'
|
|
3
|
+
import { getModelName, getTableName } from '@stacksjs/orm'
|
|
3
4
|
import { path } from '@stacksjs/path'
|
|
4
5
|
import { fs, glob } from '@stacksjs/storage'
|
|
5
6
|
import { plural, snakeCase } from '@stacksjs/strings'
|
|
@@ -130,7 +131,7 @@ export async function checkPivotMigration(dynamicPart: string): Promise<boolean>
|
|
|
130
131
|
})
|
|
131
132
|
}
|
|
132
133
|
|
|
133
|
-
export async function getRelations(model: Model): Promise<RelationConfig[]> {
|
|
134
|
+
export async function getRelations(model: Model, modelPath: string): Promise<RelationConfig[]> {
|
|
134
135
|
const relationsArray = ['hasOne', 'hasMany', 'belongsToMany', 'hasOneThrough']
|
|
135
136
|
const relationships = []
|
|
136
137
|
|
|
@@ -145,19 +146,25 @@ export async function getRelations(model: Model): Promise<RelationConfig[]> {
|
|
|
145
146
|
|
|
146
147
|
const modelRelationPath = path.userModelsPath(`${relationModel}.ts`)
|
|
147
148
|
const modelRelation = (await import(modelRelationPath)).default
|
|
148
|
-
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
const modelName = getModelName(model, modelPath)
|
|
152
|
+
const formattedModelName = modelName.toLowerCase()
|
|
153
|
+
const tableName = getTableName(model, modelPath)
|
|
154
|
+
|
|
155
|
+
const modelRelationTable = getModelName(modelRelation, modelRelationPath)
|
|
149
156
|
|
|
150
157
|
relationships.push({
|
|
151
158
|
relationship: relation,
|
|
152
159
|
model: relationModel,
|
|
153
|
-
table:
|
|
154
|
-
relationModel:
|
|
155
|
-
relationTable:
|
|
160
|
+
table: modelRelationTable,
|
|
161
|
+
relationModel: modelName,
|
|
162
|
+
relationTable: tableName,
|
|
156
163
|
foreignKey: relationInstance.foreignKey || `${formattedModelName}_id`,
|
|
157
164
|
relationName: relationInstance.relationName || '',
|
|
158
165
|
throughModel: relationInstance.through || '',
|
|
159
166
|
throughForeignKey: relationInstance.throughForeignKey || '',
|
|
160
|
-
pivotTable: relationInstance?.pivotTable || `${formattedModelName}_${
|
|
167
|
+
pivotTable: relationInstance?.pivotTable || `${formattedModelName}_${modelRelationTable}`,
|
|
161
168
|
})
|
|
162
169
|
}
|
|
163
170
|
}
|
|
@@ -166,7 +173,7 @@ export async function getRelations(model: Model): Promise<RelationConfig[]> {
|
|
|
166
173
|
return relationships
|
|
167
174
|
}
|
|
168
175
|
|
|
169
|
-
export async function fetchOtherModelRelations(model: Model): Promise<RelationConfig[]> {
|
|
176
|
+
export async function fetchOtherModelRelations(model: Model, modelPath: string): Promise<RelationConfig[]> {
|
|
170
177
|
const modelFiles = glob.sync(path.userModelsPath('*.ts'))
|
|
171
178
|
|
|
172
179
|
const modelRelations = []
|
|
@@ -174,15 +181,20 @@ export async function fetchOtherModelRelations(model: Model): Promise<RelationCo
|
|
|
174
181
|
for (let i = 0; i < modelFiles.length; i++) {
|
|
175
182
|
const modelFileElement = modelFiles[i] as string
|
|
176
183
|
|
|
177
|
-
const modelFile = await import(modelFileElement)
|
|
184
|
+
const modelFile = (await import(modelFileElement)).default as Model
|
|
185
|
+
|
|
186
|
+
const tableName = getTableName(model, modelPath)
|
|
187
|
+
const modelName = getModelName(model, modelPath)
|
|
178
188
|
|
|
179
|
-
|
|
189
|
+
const modelFileName = getTableName(modelFile, modelFileElement)
|
|
180
190
|
|
|
181
|
-
|
|
191
|
+
if (tableName === modelFileName) continue
|
|
192
|
+
|
|
193
|
+
const relations = await getRelations(modelFile, modelFileElement)
|
|
182
194
|
|
|
183
195
|
if (!relations.length) continue
|
|
184
196
|
|
|
185
|
-
const relation = relations.find((relation) => relation.model ===
|
|
197
|
+
const relation = relations.find((relation) => relation.model === modelName)
|
|
186
198
|
|
|
187
199
|
if (relation) modelRelations.push(relation)
|
|
188
200
|
}
|
|
@@ -192,23 +204,29 @@ export async function fetchOtherModelRelations(model: Model): Promise<RelationCo
|
|
|
192
204
|
|
|
193
205
|
export async function getPivotTables(
|
|
194
206
|
model: Model,
|
|
207
|
+
modelPath: string
|
|
195
208
|
): Promise<{ table: string; firstForeignKey: string | undefined; secondForeignKey: string | undefined }[]> {
|
|
196
209
|
const pivotTable = []
|
|
197
210
|
|
|
198
|
-
|
|
211
|
+
const modelName = getTableName(model, modelPath)
|
|
212
|
+
|
|
213
|
+
if (model.belongsToMany) {
|
|
199
214
|
if ('belongsToMany' in model) {
|
|
200
215
|
for (const belongsToManyRelation of model.belongsToMany) {
|
|
201
216
|
const modelRelationPath = path.userModelsPath(`${belongsToManyRelation}.ts`)
|
|
202
217
|
const modelRelation = (await import(modelRelationPath)).default
|
|
203
|
-
const formattedModelName =
|
|
218
|
+
const formattedModelName = modelName.toLowerCase()
|
|
219
|
+
|
|
220
|
+
const modelRelationTable = getTableName(modelRelation, modelRelationPath)
|
|
221
|
+
const modelRelationModelName = getModelName(modelRelation, modelRelationPath)
|
|
204
222
|
|
|
205
223
|
const firstForeignKey =
|
|
206
|
-
belongsToManyRelation.firstForeignKey || `${
|
|
224
|
+
belongsToManyRelation.firstForeignKey || `${modelName.toLowerCase()}_${model.primaryKey}`
|
|
207
225
|
const secondForeignKey =
|
|
208
|
-
belongsToManyRelation.secondForeignKey || `${
|
|
226
|
+
belongsToManyRelation.secondForeignKey || `${modelRelationModelName.toLowerCase()}_${model.primaryKey}`
|
|
209
227
|
|
|
210
228
|
pivotTable.push({
|
|
211
|
-
table: belongsToManyRelation?.pivotTable || `${formattedModelName}_${
|
|
229
|
+
table: belongsToManyRelation?.pivotTable || `${formattedModelName}_${modelRelationTable}`,
|
|
212
230
|
firstForeignKey: firstForeignKey,
|
|
213
231
|
secondForeignKey: secondForeignKey,
|
|
214
232
|
})
|
package/src/drivers/mysql.ts
CHANGED
|
@@ -11,8 +11,8 @@ import {
|
|
|
11
11
|
getPivotTables,
|
|
12
12
|
hasTableBeenMigrated,
|
|
13
13
|
mapFieldTypeToColumnType,
|
|
14
|
-
modelTableName,
|
|
15
14
|
} from '.'
|
|
15
|
+
import { getModelName, getTableName } from '@stacksjs/orm'
|
|
16
16
|
|
|
17
17
|
export async function resetMysqlDatabase() {
|
|
18
18
|
const tables = await fetchMysqlTables()
|
|
@@ -29,7 +29,7 @@ export async function resetMysqlDatabase() {
|
|
|
29
29
|
|
|
30
30
|
for (const userModel of userModelFiles) {
|
|
31
31
|
const model = (await import(userModel)).default as Model
|
|
32
|
-
const pivotTables = await getPivotTables(model)
|
|
32
|
+
const pivotTables = await getPivotTables(model, userModel)
|
|
33
33
|
|
|
34
34
|
for (const pivotTable of pivotTables) await db.schema.dropTable(pivotTable.table).ifExists().execute()
|
|
35
35
|
}
|
|
@@ -78,7 +78,7 @@ export async function generateMysqlMigration(modelPath: string) {
|
|
|
78
78
|
|
|
79
79
|
const model = (await import(modelPath)).default as Model
|
|
80
80
|
const fileName = path.basename(modelPath)
|
|
81
|
-
const tableName = await
|
|
81
|
+
const tableName = await getTableName(model, modelPath)
|
|
82
82
|
|
|
83
83
|
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
|
|
84
84
|
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
|
|
@@ -122,11 +122,11 @@ async function createTableMigration(modelPath: string) {
|
|
|
122
122
|
log.debug('createTableMigration modelPath:', modelPath)
|
|
123
123
|
|
|
124
124
|
const model = (await import(modelPath)).default as Model
|
|
125
|
-
const tableName = await
|
|
125
|
+
const tableName = await getTableName(model, modelPath)
|
|
126
126
|
|
|
127
|
-
await createPivotTableMigration(model)
|
|
127
|
+
await createPivotTableMigration(model, modelPath)
|
|
128
128
|
|
|
129
|
-
const otherModelRelations = await fetchOtherModelRelations(model)
|
|
129
|
+
const otherModelRelations = await fetchOtherModelRelations(model, modelPath)
|
|
130
130
|
|
|
131
131
|
const fields = model.attributes
|
|
132
132
|
const useTimestamps = model?.traits?.useTimestamps ?? model?.traits?.timestampable ?? true
|
|
@@ -185,8 +185,8 @@ async function createTableMigration(modelPath: string) {
|
|
|
185
185
|
log.success(`Created migration: ${italic(migrationFileName)}`)
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
async function createPivotTableMigration(model: Model) {
|
|
189
|
-
const pivotTables = await getPivotTables(model)
|
|
188
|
+
async function createPivotTableMigration(model: Model, modelPath: string) {
|
|
189
|
+
const pivotTables = await getPivotTables(model, modelPath)
|
|
190
190
|
|
|
191
191
|
if (!pivotTables.length) return
|
|
192
192
|
|
|
@@ -220,8 +220,8 @@ export async function createAlterTableMigration(modelPath: string) {
|
|
|
220
220
|
console.log('createAlterTableMigration')
|
|
221
221
|
|
|
222
222
|
const model = (await import(modelPath)).default as Model
|
|
223
|
-
const modelName =
|
|
224
|
-
const tableName = await
|
|
223
|
+
const modelName = getModelName(model, modelPath)
|
|
224
|
+
const tableName = await getTableName(model, modelPath)
|
|
225
225
|
|
|
226
226
|
// Assuming you have a function to get the fields from the last migration
|
|
227
227
|
// For simplicity, this is not implemented here
|
|
@@ -267,7 +267,7 @@ export async function fetchMysqlTables(): Promise<string[]> {
|
|
|
267
267
|
|
|
268
268
|
for (const modelPath of modelFiles) {
|
|
269
269
|
const model = (await import(modelPath)).default as Model
|
|
270
|
-
const tableName = await
|
|
270
|
+
const tableName = await getTableName(model, modelPath)
|
|
271
271
|
|
|
272
272
|
tables.push(tableName)
|
|
273
273
|
}
|
package/src/drivers/postgres.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { italic, log } from '@stacksjs/cli'
|
|
2
2
|
import { db } from '@stacksjs/database'
|
|
3
3
|
import { ok } from '@stacksjs/error-handling'
|
|
4
|
-
import {
|
|
4
|
+
import { getTableName } from '@stacksjs/orm'
|
|
5
5
|
import { path } from '@stacksjs/path'
|
|
6
6
|
import { fs, glob } from '@stacksjs/storage'
|
|
7
|
-
import type { Attribute,
|
|
7
|
+
import type { Attribute, Model } from '@stacksjs/types'
|
|
8
8
|
import {
|
|
9
9
|
checkPivotMigration,
|
|
10
10
|
fetchOtherModelRelations,
|
|
@@ -28,9 +28,9 @@ export async function resetPostgresDatabase() {
|
|
|
28
28
|
const userModelFiles = glob.sync(path.userModelsPath('*.ts'))
|
|
29
29
|
|
|
30
30
|
for (const userModel of userModelFiles) {
|
|
31
|
-
const userModelPath = await import(userModel)
|
|
31
|
+
const userModelPath = (await import(userModel)).default
|
|
32
32
|
|
|
33
|
-
const pivotTables = await getPivotTables(userModelPath)
|
|
33
|
+
const pivotTables = await getPivotTables(userModelPath, userModelPath)
|
|
34
34
|
|
|
35
35
|
for (const pivotTable of pivotTables) await db.schema.dropTable(pivotTable.table).ifExists().execute()
|
|
36
36
|
}
|
|
@@ -79,7 +79,7 @@ export async function generatePostgresMigration(modelPath: string) {
|
|
|
79
79
|
|
|
80
80
|
const model = (await import(modelPath)).default as Model
|
|
81
81
|
const fileName = path.basename(modelPath)
|
|
82
|
-
const tableName =
|
|
82
|
+
const tableName = getTableName(model, modelPath)
|
|
83
83
|
|
|
84
84
|
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
|
|
85
85
|
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
|
|
@@ -123,11 +123,11 @@ async function createTableMigration(modelPath: string) {
|
|
|
123
123
|
log.debug('createTableMigration modelPath:', modelPath)
|
|
124
124
|
|
|
125
125
|
const model = (await import(modelPath)).default as Model
|
|
126
|
-
const tableName =
|
|
126
|
+
const tableName = getTableName(model, modelPath)
|
|
127
127
|
|
|
128
|
-
await createPivotTableMigration(model)
|
|
128
|
+
await createPivotTableMigration(model, modelPath)
|
|
129
129
|
|
|
130
|
-
const otherModelRelations = await fetchOtherModelRelations(model)
|
|
130
|
+
const otherModelRelations = await fetchOtherModelRelations(model, modelPath)
|
|
131
131
|
const fields = model.attributes
|
|
132
132
|
const useTimestamps = model.traits?.useTimestamps ?? model.traits?.timestampable ?? true
|
|
133
133
|
const useSoftDeletes = model.traits?.useSoftDeletes ?? model.traits?.softDeletable ?? false
|
|
@@ -185,8 +185,8 @@ async function createTableMigration(modelPath: string) {
|
|
|
185
185
|
log.success(`Created migration: ${italic(migrationFileName)}`)
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
async function createPivotTableMigration(model:
|
|
189
|
-
const pivotTables = await getPivotTables(model)
|
|
188
|
+
async function createPivotTableMigration(model: Model, modelPath: string) {
|
|
189
|
+
const pivotTables = await getPivotTables(model, modelPath)
|
|
190
190
|
|
|
191
191
|
if (!pivotTables.length) return
|
|
192
192
|
for (const pivotTable of pivotTables) {
|
|
@@ -221,7 +221,7 @@ export async function createAlterTableMigration(modelPath: string) {
|
|
|
221
221
|
|
|
222
222
|
const model = (await import(modelPath)).default as Model
|
|
223
223
|
const modelName = path.basename(modelPath)
|
|
224
|
-
const tableName = await
|
|
224
|
+
const tableName = await getTableName(model, modelPath)
|
|
225
225
|
|
|
226
226
|
// Assuming you have a function to get the fields from the last migration
|
|
227
227
|
// For simplicity, this is not implemented here
|
|
@@ -266,9 +266,9 @@ export async function fetchMysqlTables(): Promise<string[]> {
|
|
|
266
266
|
const tables: string[] = []
|
|
267
267
|
|
|
268
268
|
for (const modelPath of modelFiles) {
|
|
269
|
-
const model = await import(modelPath)
|
|
269
|
+
const model = (await import(modelPath)).default
|
|
270
270
|
|
|
271
|
-
const tableName = await
|
|
271
|
+
const tableName = await getTableName(model, modelPath)
|
|
272
272
|
|
|
273
273
|
tables.push(tableName)
|
|
274
274
|
}
|
package/src/drivers/sqlite.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { italic, log } from '@stacksjs/cli'
|
|
2
2
|
import { db } from '@stacksjs/database'
|
|
3
3
|
import { ok } from '@stacksjs/error-handling'
|
|
4
|
-
import {
|
|
4
|
+
import { getTableName, getModelName } from '@stacksjs/orm'
|
|
5
5
|
import { path } from '@stacksjs/path'
|
|
6
6
|
import { fs, glob } from '@stacksjs/storage'
|
|
7
7
|
import type { Attribute, Attributes, Model } from '@stacksjs/types'
|
|
@@ -25,9 +25,9 @@ export async function resetSqliteDatabase() {
|
|
|
25
25
|
const userModelFiles = glob.sync(path.userModelsPath('*.ts'))
|
|
26
26
|
|
|
27
27
|
for (const userModel of userModelFiles) {
|
|
28
|
-
const userModelPath = await import(userModel)
|
|
28
|
+
const userModelPath = (await import(userModel)).default
|
|
29
29
|
|
|
30
|
-
const pivotTables = await getPivotTables(userModelPath)
|
|
30
|
+
const pivotTables = await getPivotTables(userModelPath, userModel)
|
|
31
31
|
|
|
32
32
|
for (const pivotTable of pivotTables) await db.schema.dropTable(pivotTable.table).ifExists().execute()
|
|
33
33
|
}
|
|
@@ -75,7 +75,7 @@ export async function generateSqliteMigration(modelPath: string) {
|
|
|
75
75
|
|
|
76
76
|
const model = (await import(modelPath)).default as Model
|
|
77
77
|
const fileName = path.basename(modelPath)
|
|
78
|
-
const tableName = await
|
|
78
|
+
const tableName = await getTableName(model, modelPath)
|
|
79
79
|
|
|
80
80
|
const fieldsString = JSON.stringify(model.attributes, null, 2) // Pretty print the JSON
|
|
81
81
|
const copiedModelPath = path.frameworkPath(`database/models/${fileName}`)
|
|
@@ -119,10 +119,10 @@ async function createTableMigration(modelPath: string): Promise<void> {
|
|
|
119
119
|
log.debug('createTableMigration modelPath:', modelPath)
|
|
120
120
|
|
|
121
121
|
const model = (await import(modelPath)).default as Model
|
|
122
|
-
const tableName = await
|
|
122
|
+
const tableName = await getTableName(model, modelPath)
|
|
123
123
|
|
|
124
|
-
await createPivotTableMigration(model)
|
|
125
|
-
const otherModelRelations = await fetchOtherModelRelations(model)
|
|
124
|
+
await createPivotTableMigration(model, modelPath)
|
|
125
|
+
const otherModelRelations = await fetchOtherModelRelations(model, modelPath)
|
|
126
126
|
const fields = model.attributes
|
|
127
127
|
const useTimestamps = model?.traits?.useTimestamps ?? model?.traits?.timestampable ?? true
|
|
128
128
|
const useSoftDeletes = model?.traits?.useSoftDeletes ?? model?.traits?.softDeletable ?? false
|
|
@@ -180,8 +180,8 @@ async function createTableMigration(modelPath: string): Promise<void> {
|
|
|
180
180
|
log.success(`Created migration: ${italic(migrationFileName)}`)
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
async function createPivotTableMigration(model: Model) {
|
|
184
|
-
const pivotTables = await getPivotTables(model)
|
|
183
|
+
async function createPivotTableMigration(model: Model, modelPath: string) {
|
|
184
|
+
const pivotTables = await getPivotTables(model, modelPath)
|
|
185
185
|
|
|
186
186
|
if (!pivotTables.length) return
|
|
187
187
|
|
|
@@ -215,8 +215,8 @@ export async function createAlterTableMigration(modelPath: string) {
|
|
|
215
215
|
console.log('createAlterTableMigration')
|
|
216
216
|
|
|
217
217
|
const model = (await import(modelPath)).default as Model
|
|
218
|
-
const modelName =
|
|
219
|
-
const tableName = await
|
|
218
|
+
const modelName = getModelName(model, modelPath)
|
|
219
|
+
const tableName = await getTableName(model, modelPath)
|
|
220
220
|
|
|
221
221
|
// Assuming you have a function to get the fields from the last migration
|
|
222
222
|
// For simplicity, this is not implemented here
|