mevn-orm 4.0.0 → 4.0.2
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/README.md +78 -170
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/src/config.d.ts +46 -0
- package/dist/src/config.js +196 -0
- package/dist/src/model.d.ts +51 -0
- package/dist/src/model.js +243 -0
- package/dist/src/relationships.d.ts +18 -0
- package/dist/src/relationships.js +50 -0
- package/package.json +15 -3
- package/.env.example +0 -9
- package/.eslintrc.json +0 -36
- package/.gitattributes +0 -8
- package/CODE_OF_CONDUCT.md +0 -76
- package/changelog.md +0 -135
- package/index.ts +0 -31
- package/initDb.ts +0 -112
- package/knexfile.ts +0 -46
- package/pnpm-workspace.yaml +0 -9
- package/scripts/migrate.ts +0 -97
- package/src/config.ts +0 -270
- package/src/model.ts +0 -301
- package/src/relationships.ts +0 -93
- package/tsconfig.json +0 -27
- package/types/pluralize.d.ts +0 -4
package/initDb.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { existsSync } from 'node:fs'
|
|
3
|
-
import { pathToFileURL } from 'node:url'
|
|
4
|
-
import 'dotenv/config'
|
|
5
|
-
import type { Knex } from 'knex'
|
|
6
|
-
import knexModule from 'knex'
|
|
7
|
-
|
|
8
|
-
interface KnexEnvConfig {
|
|
9
|
-
development: Knex.Config
|
|
10
|
-
staging: Knex.Config
|
|
11
|
-
production: Knex.Config
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
type KnexFactory = (config: Knex.Config) => Knex
|
|
15
|
-
|
|
16
|
-
const KnexFactoryImpl: KnexFactory =
|
|
17
|
-
(knexModule as unknown as { knex?: KnexFactory }).knex ??
|
|
18
|
-
(knexModule as unknown as KnexFactory)
|
|
19
|
-
|
|
20
|
-
const knexfilePath = `${process.cwd()}/knexfile.ts`
|
|
21
|
-
if (!existsSync(knexfilePath)) {
|
|
22
|
-
throw new Error('knexfile.ts not found in current working directory')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const knexConfigModule = await import(pathToFileURL(knexfilePath).href)
|
|
26
|
-
const knexConfig = (knexConfigModule.default ?? knexConfigModule) as Partial<KnexEnvConfig>
|
|
27
|
-
const { development, staging, production } = knexConfig
|
|
28
|
-
|
|
29
|
-
if (!development || !staging || !production) {
|
|
30
|
-
throw new Error('knexfile.ts must export development, staging, and production config')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const config: Knex.Config = (() => {
|
|
34
|
-
switch (process.env.NODE_ENV) {
|
|
35
|
-
case 'testing':
|
|
36
|
-
case 'test':
|
|
37
|
-
case 'development':
|
|
38
|
-
return development
|
|
39
|
-
case 'staging':
|
|
40
|
-
return staging
|
|
41
|
-
default:
|
|
42
|
-
return production
|
|
43
|
-
}
|
|
44
|
-
})()
|
|
45
|
-
|
|
46
|
-
const initDatabase = async (): Promise<void> => {
|
|
47
|
-
const DB = KnexFactoryImpl(config)
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
await DB.schema.dropTableIfExists('farmers')
|
|
51
|
-
await DB.schema.dropTableIfExists('farms')
|
|
52
|
-
await DB.schema.dropTableIfExists('profiles')
|
|
53
|
-
await DB.schema.dropTableIfExists('articles')
|
|
54
|
-
|
|
55
|
-
await DB.schema.createTable('farmers', (table: Knex.CreateTableBuilder) => {
|
|
56
|
-
table.bigIncrements('id')
|
|
57
|
-
table.string('name')
|
|
58
|
-
table.string('email').unique()
|
|
59
|
-
table.string('password')
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
await DB.schema.createTable('farms', (table: Knex.CreateTableBuilder) => {
|
|
63
|
-
table.bigIncrements('id')
|
|
64
|
-
table.bigInteger('farmer_id')
|
|
65
|
-
table.string('name')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
await DB.schema.createTable('profiles', (table: Knex.CreateTableBuilder) => {
|
|
69
|
-
table.bigIncrements('id')
|
|
70
|
-
table.bigInteger('farmer_id')
|
|
71
|
-
table.string('bio')
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
await DB.schema.createTable('articles', (table: Knex.CreateTableBuilder) => {
|
|
75
|
-
table.bigIncrements('id')
|
|
76
|
-
table.string('title')
|
|
77
|
-
table.text('body')
|
|
78
|
-
table.bigInteger('postable_id')
|
|
79
|
-
table.string('postable_type')
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
await DB.table('Farmers').insert([
|
|
83
|
-
{ name: 'Jane Doe', email: 'jane@mail.com', password: 'pasword' },
|
|
84
|
-
{ name: 'Ashley Doe', email: 'ashley@mail.com', password: 'pasword' },
|
|
85
|
-
{ name: 'Alice Doe', email: 'alice@mail.com', password: 'pasword' },
|
|
86
|
-
])
|
|
87
|
-
|
|
88
|
-
await DB.table('farms').insert([
|
|
89
|
-
{ farmer_id: 1, name: 'Awesome Farm' },
|
|
90
|
-
{ farmer_id: 1, name: 'Awesome Farm two' },
|
|
91
|
-
{ farmer_id: 1, name: 'Awesome Farm three' },
|
|
92
|
-
])
|
|
93
|
-
|
|
94
|
-
await DB.table('profiles').insert([{ farmer_id: 1, bio: 'Profile for farmer one' }])
|
|
95
|
-
|
|
96
|
-
await DB.table('articles').insert([
|
|
97
|
-
{
|
|
98
|
-
title: 'Awesome Post',
|
|
99
|
-
body: 'fffgjdfjdbdb something #1',
|
|
100
|
-
postable_id: 1,
|
|
101
|
-
postable_type: 'Farmer',
|
|
102
|
-
},
|
|
103
|
-
])
|
|
104
|
-
|
|
105
|
-
process.exit(0)
|
|
106
|
-
} catch (error: unknown) {
|
|
107
|
-
console.error(error)
|
|
108
|
-
process.exit(1)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
await initDatabase()
|
package/knexfile.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { Knex } from 'knex'
|
|
2
|
-
|
|
3
|
-
const config = {
|
|
4
|
-
development: {
|
|
5
|
-
client: 'sqlite3',
|
|
6
|
-
connection: {
|
|
7
|
-
filename: './dev.sqlite',
|
|
8
|
-
},
|
|
9
|
-
useNullAsDefault: true,
|
|
10
|
-
migrations: {
|
|
11
|
-
tableName: 'migrations',
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
staging: {
|
|
15
|
-
client: process.env.DB_CLIENT ?? 'mysql2',
|
|
16
|
-
connection: {
|
|
17
|
-
database: process.env.DB_DATABASE ?? 'my_db',
|
|
18
|
-
user: process.env.DB_USER ?? 'username',
|
|
19
|
-
password: process.env.DB_PASSWORD ?? 'password',
|
|
20
|
-
},
|
|
21
|
-
pool: {
|
|
22
|
-
min: 2,
|
|
23
|
-
max: 10,
|
|
24
|
-
},
|
|
25
|
-
migrations: {
|
|
26
|
-
tableName: 'migrations',
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
production: {
|
|
30
|
-
client: process.env.DB_CLIENT ?? 'mysql2',
|
|
31
|
-
connection: {
|
|
32
|
-
database: process.env.DB_DATABASE ?? 'my_db',
|
|
33
|
-
user: process.env.DB_USER ?? 'username',
|
|
34
|
-
password: process.env.DB_PASSWORD ?? 'password',
|
|
35
|
-
},
|
|
36
|
-
pool: {
|
|
37
|
-
min: 2,
|
|
38
|
-
max: 10,
|
|
39
|
-
},
|
|
40
|
-
migrations: {
|
|
41
|
-
tableName: 'migrations',
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
} satisfies Record<'development' | 'staging' | 'production', Knex.Config>
|
|
45
|
-
|
|
46
|
-
export default config
|
package/pnpm-workspace.yaml
DELETED
package/scripts/migrate.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
configureDatabase,
|
|
4
|
-
setMigrationConfig,
|
|
5
|
-
makeMigration,
|
|
6
|
-
migrateLatest,
|
|
7
|
-
migrateRollback,
|
|
8
|
-
migrateList,
|
|
9
|
-
migrateCurrentVersion,
|
|
10
|
-
} from '../index.js'
|
|
11
|
-
|
|
12
|
-
const [command = 'latest', name] = process.argv.slice(2)
|
|
13
|
-
|
|
14
|
-
const databaseConfig: Parameters<typeof configureDatabase>[0] = {
|
|
15
|
-
dialect: (process.env.DB_DIALECT as
|
|
16
|
-
| 'sqlite'
|
|
17
|
-
| 'better-sqlite3'
|
|
18
|
-
| 'mysql'
|
|
19
|
-
| 'mysql2'
|
|
20
|
-
| 'postgres'
|
|
21
|
-
| 'postgresql'
|
|
22
|
-
| 'pg'
|
|
23
|
-
| 'pgnative'
|
|
24
|
-
| 'cockroachdb'
|
|
25
|
-
| 'redshift'
|
|
26
|
-
| 'mssql'
|
|
27
|
-
| 'oracledb'
|
|
28
|
-
| 'oracle') ?? 'sqlite',
|
|
29
|
-
filename: process.env.DB_FILENAME ?? './dev.sqlite',
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (process.env.DATABASE_URL) {
|
|
33
|
-
databaseConfig.connectionString = process.env.DATABASE_URL
|
|
34
|
-
}
|
|
35
|
-
if (process.env.DB_HOST) {
|
|
36
|
-
databaseConfig.host = process.env.DB_HOST
|
|
37
|
-
}
|
|
38
|
-
if (process.env.DB_PORT) {
|
|
39
|
-
databaseConfig.port = Number(process.env.DB_PORT)
|
|
40
|
-
}
|
|
41
|
-
if (process.env.DB_USER) {
|
|
42
|
-
databaseConfig.user = process.env.DB_USER
|
|
43
|
-
}
|
|
44
|
-
if (process.env.DB_PASSWORD) {
|
|
45
|
-
databaseConfig.password = process.env.DB_PASSWORD
|
|
46
|
-
}
|
|
47
|
-
if (process.env.DB_NAME) {
|
|
48
|
-
databaseConfig.database = process.env.DB_NAME
|
|
49
|
-
}
|
|
50
|
-
if (process.env.DB_SSL === 'true') {
|
|
51
|
-
databaseConfig.ssl = true
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
configureDatabase(databaseConfig)
|
|
55
|
-
|
|
56
|
-
setMigrationConfig({
|
|
57
|
-
directory: process.env.MIGRATIONS_DIR ?? './migrations',
|
|
58
|
-
extension: process.env.MIGRATIONS_EXT ?? 'ts',
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
const run = async (): Promise<void> => {
|
|
62
|
-
switch (command) {
|
|
63
|
-
case 'make': {
|
|
64
|
-
if (!name) {
|
|
65
|
-
throw new Error('Usage: node --import tsx scripts/migrate.ts make <name>')
|
|
66
|
-
}
|
|
67
|
-
const file = await makeMigration(name)
|
|
68
|
-
console.log(file)
|
|
69
|
-
return
|
|
70
|
-
}
|
|
71
|
-
case 'latest': {
|
|
72
|
-
const result = await migrateLatest()
|
|
73
|
-
console.log(JSON.stringify(result, null, 2))
|
|
74
|
-
return
|
|
75
|
-
}
|
|
76
|
-
case 'rollback': {
|
|
77
|
-
const all = process.argv.includes('--all')
|
|
78
|
-
const result = await migrateRollback(undefined, all)
|
|
79
|
-
console.log(JSON.stringify(result, null, 2))
|
|
80
|
-
return
|
|
81
|
-
}
|
|
82
|
-
case 'list': {
|
|
83
|
-
const result = await migrateList()
|
|
84
|
-
console.log(JSON.stringify(result, null, 2))
|
|
85
|
-
return
|
|
86
|
-
}
|
|
87
|
-
case 'version': {
|
|
88
|
-
const version = await migrateCurrentVersion()
|
|
89
|
-
console.log(version)
|
|
90
|
-
return
|
|
91
|
-
}
|
|
92
|
-
default:
|
|
93
|
-
throw new Error(`Unknown command "${command}". Use make|latest|rollback|list|version`)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
await run()
|
package/src/config.ts
DELETED
|
@@ -1,270 +0,0 @@
|
|
|
1
|
-
import type { Knex } from 'knex'
|
|
2
|
-
import knexModule from 'knex'
|
|
3
|
-
|
|
4
|
-
type KnexFactory = (config: Knex.Config) => Knex
|
|
5
|
-
|
|
6
|
-
const knexFactory: KnexFactory =
|
|
7
|
-
(knexModule as unknown as { knex?: KnexFactory }).knex ??
|
|
8
|
-
(knexModule as unknown as KnexFactory)
|
|
9
|
-
|
|
10
|
-
const toError = (error: unknown): Error => {
|
|
11
|
-
if (error instanceof Error) {
|
|
12
|
-
return error
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
return new Error(String(error))
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
let DB: Knex | undefined
|
|
19
|
-
let defaultMigrationConfig: Knex.MigratorConfig = {}
|
|
20
|
-
|
|
21
|
-
type SimpleDialect =
|
|
22
|
-
| 'sqlite'
|
|
23
|
-
| 'better-sqlite3'
|
|
24
|
-
| 'mysql'
|
|
25
|
-
| 'mysql2'
|
|
26
|
-
| 'postgres'
|
|
27
|
-
| 'postgresql'
|
|
28
|
-
| 'pg'
|
|
29
|
-
| 'pgnative'
|
|
30
|
-
| 'cockroachdb'
|
|
31
|
-
| 'redshift'
|
|
32
|
-
| 'mssql'
|
|
33
|
-
| 'oracledb'
|
|
34
|
-
| 'oracle'
|
|
35
|
-
|
|
36
|
-
interface SimpleDatabaseConfig {
|
|
37
|
-
dialect: SimpleDialect
|
|
38
|
-
connectionString?: string
|
|
39
|
-
filename?: string
|
|
40
|
-
host?: string
|
|
41
|
-
port?: number
|
|
42
|
-
user?: string
|
|
43
|
-
password?: string
|
|
44
|
-
database?: string
|
|
45
|
-
ssl?: boolean | Record<string, unknown>
|
|
46
|
-
debug?: boolean
|
|
47
|
-
pool?: Knex.PoolConfig
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface MigrationResult {
|
|
51
|
-
batch: number
|
|
52
|
-
log: string[]
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** Returns the active Knex instance or throws if the ORM has not been configured. */
|
|
56
|
-
const getDB = (): Knex => {
|
|
57
|
-
if (!DB) {
|
|
58
|
-
throw new Error('Mevn ORM is not configured. Call configure({ client, connection, ... }) before using Model.')
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return DB
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Configures the ORM using a Knex config object or an existing Knex instance. */
|
|
65
|
-
const configure = (config: Knex.Config | Knex): Knex => {
|
|
66
|
-
if (typeof config === 'function') {
|
|
67
|
-
DB = config
|
|
68
|
-
return DB
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
DB = knexFactory(config)
|
|
72
|
-
return DB
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** Sets default migration options used by migration helpers. */
|
|
76
|
-
const setMigrationConfig = (config: Knex.MigratorConfig): Knex.MigratorConfig => {
|
|
77
|
-
defaultMigrationConfig = { ...config }
|
|
78
|
-
return { ...defaultMigrationConfig }
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Returns the currently configured default migration options. */
|
|
82
|
-
const getMigrationConfig = (): Knex.MigratorConfig => ({ ...defaultMigrationConfig })
|
|
83
|
-
|
|
84
|
-
const resolveMigrationConfig = (config?: Knex.MigratorConfig): Knex.MigratorConfig => ({
|
|
85
|
-
...defaultMigrationConfig,
|
|
86
|
-
...(config ?? {}),
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
const normalizeDialect = (dialect: SimpleDialect): string => {
|
|
90
|
-
switch (dialect) {
|
|
91
|
-
case 'sqlite':
|
|
92
|
-
return 'sqlite3'
|
|
93
|
-
case 'mysql':
|
|
94
|
-
return 'mysql2'
|
|
95
|
-
case 'postgres':
|
|
96
|
-
case 'postgresql':
|
|
97
|
-
case 'pg':
|
|
98
|
-
return 'pg'
|
|
99
|
-
case 'oracle':
|
|
100
|
-
return 'oracledb'
|
|
101
|
-
default:
|
|
102
|
-
return dialect
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const requireField = (value: unknown, field: string, dialect: string): void => {
|
|
107
|
-
if (value === undefined || value === null || value === '') {
|
|
108
|
-
throw new Error(`Missing required field "${field}" for dialect "${dialect}".`)
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const buildConnection = (config: SimpleDatabaseConfig): NonNullable<Knex.Config['connection']> => {
|
|
113
|
-
if (config.connectionString) {
|
|
114
|
-
return config.connectionString
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const client = normalizeDialect(config.dialect)
|
|
118
|
-
|
|
119
|
-
if (client === 'sqlite3' || client === 'better-sqlite3') {
|
|
120
|
-
requireField(config.filename, 'filename', config.dialect)
|
|
121
|
-
return { filename: config.filename as string }
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (client === 'mssql') {
|
|
125
|
-
const server = config.host
|
|
126
|
-
requireField(server, 'host', config.dialect)
|
|
127
|
-
requireField(config.user, 'user', config.dialect)
|
|
128
|
-
requireField(config.database, 'database', config.dialect)
|
|
129
|
-
const connection: Record<string, unknown> = {
|
|
130
|
-
server: server as string,
|
|
131
|
-
user: config.user as string,
|
|
132
|
-
database: config.database as string,
|
|
133
|
-
}
|
|
134
|
-
if (typeof config.port === 'number') {
|
|
135
|
-
connection.port = config.port
|
|
136
|
-
}
|
|
137
|
-
if (config.password !== undefined) {
|
|
138
|
-
connection.password = config.password
|
|
139
|
-
}
|
|
140
|
-
if (config.ssl) {
|
|
141
|
-
connection.options = { encrypt: true }
|
|
142
|
-
}
|
|
143
|
-
return connection
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
requireField(config.host, 'host', config.dialect)
|
|
147
|
-
requireField(config.user, 'user', config.dialect)
|
|
148
|
-
requireField(config.database, 'database', config.dialect)
|
|
149
|
-
const connection: Record<string, unknown> = {
|
|
150
|
-
host: config.host as string,
|
|
151
|
-
user: config.user as string,
|
|
152
|
-
database: config.database as string,
|
|
153
|
-
}
|
|
154
|
-
if (typeof config.port === 'number') {
|
|
155
|
-
connection.port = config.port
|
|
156
|
-
}
|
|
157
|
-
if (config.password !== undefined) {
|
|
158
|
-
connection.password = config.password
|
|
159
|
-
}
|
|
160
|
-
if (config.ssl !== undefined) {
|
|
161
|
-
connection.ssl = config.ssl
|
|
162
|
-
}
|
|
163
|
-
return connection
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/** Builds a Knex config from a simplified, dialect-first configuration object. */
|
|
167
|
-
const createKnexConfig = (config: SimpleDatabaseConfig): Knex.Config => {
|
|
168
|
-
const client = normalizeDialect(config.dialect)
|
|
169
|
-
const base: Knex.Config = {
|
|
170
|
-
client,
|
|
171
|
-
connection: buildConnection(config),
|
|
172
|
-
}
|
|
173
|
-
if (config.pool) {
|
|
174
|
-
base.pool = config.pool
|
|
175
|
-
}
|
|
176
|
-
if (typeof config.debug === 'boolean') {
|
|
177
|
-
base.debug = config.debug
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (client === 'sqlite3') {
|
|
181
|
-
base.useNullAsDefault = true
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
return base
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** Configures the ORM from simplified database options. */
|
|
188
|
-
const configureDatabase = (config: SimpleDatabaseConfig): Knex => configure(createKnexConfig(config))
|
|
189
|
-
|
|
190
|
-
/** Generates a migration file and returns its path. */
|
|
191
|
-
const makeMigration = async (name: string, config?: Knex.MigratorConfig): Promise<string> => {
|
|
192
|
-
try {
|
|
193
|
-
return await getDB().migrate.make(name, resolveMigrationConfig(config))
|
|
194
|
-
} catch (error) {
|
|
195
|
-
throw toError(error)
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/** Runs pending migrations and returns the batch number and migration filenames. */
|
|
200
|
-
const migrateLatest = async (config?: Knex.MigratorConfig): Promise<MigrationResult> => {
|
|
201
|
-
try {
|
|
202
|
-
const [batch, log] = await getDB().migrate.latest(resolveMigrationConfig(config))
|
|
203
|
-
return { batch, log }
|
|
204
|
-
} catch (error) {
|
|
205
|
-
throw toError(error)
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/** Rolls back migrations. Set `all` to true to rollback all completed batches. */
|
|
210
|
-
const migrateRollback = async (config?: Knex.MigratorConfig, all = false): Promise<MigrationResult> => {
|
|
211
|
-
try {
|
|
212
|
-
const [batch, log] = all
|
|
213
|
-
? await getDB().migrate.rollback(resolveMigrationConfig(config), true)
|
|
214
|
-
: await getDB().migrate.rollback(resolveMigrationConfig(config))
|
|
215
|
-
return { batch, log }
|
|
216
|
-
} catch (error) {
|
|
217
|
-
throw toError(error)
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/** Returns the current migration version recorded by Knex. */
|
|
222
|
-
const migrateCurrentVersion = async (config?: Knex.MigratorConfig): Promise<string> => {
|
|
223
|
-
try {
|
|
224
|
-
return await getDB().migrate.currentVersion(resolveMigrationConfig(config))
|
|
225
|
-
} catch (error) {
|
|
226
|
-
throw toError(error)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/** Returns completed and pending migration filenames. */
|
|
231
|
-
const migrateList = async (config?: Knex.MigratorConfig): Promise<{ completed: string[]; pending: string[] }> => {
|
|
232
|
-
try {
|
|
233
|
-
const [completed, pending] = await getDB().migrate.list(resolveMigrationConfig(config))
|
|
234
|
-
const toName = (entry: unknown): string => {
|
|
235
|
-
if (typeof entry === 'string') {
|
|
236
|
-
return entry
|
|
237
|
-
}
|
|
238
|
-
if (entry && typeof entry === 'object') {
|
|
239
|
-
if ('name' in entry) {
|
|
240
|
-
return String((entry as { name: unknown }).name)
|
|
241
|
-
}
|
|
242
|
-
if ('file' in entry) {
|
|
243
|
-
return String((entry as { file: unknown }).file)
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return String(entry)
|
|
247
|
-
}
|
|
248
|
-
return {
|
|
249
|
-
completed: completed.map(toName),
|
|
250
|
-
pending: pending.map(toName),
|
|
251
|
-
}
|
|
252
|
-
} catch (error) {
|
|
253
|
-
throw toError(error)
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export {
|
|
258
|
-
DB,
|
|
259
|
-
getDB,
|
|
260
|
-
configure,
|
|
261
|
-
createKnexConfig,
|
|
262
|
-
configureDatabase,
|
|
263
|
-
setMigrationConfig,
|
|
264
|
-
getMigrationConfig,
|
|
265
|
-
makeMigration,
|
|
266
|
-
migrateLatest,
|
|
267
|
-
migrateRollback,
|
|
268
|
-
migrateCurrentVersion,
|
|
269
|
-
migrateList,
|
|
270
|
-
}
|