File tree Expand file tree Collapse file tree 4 files changed +12
-12
lines changed
plugin-db-fauna/src/runtime
plugin-db-knex/src/runtime
plugin-db-sequelize/src/runtime Expand file tree Collapse file tree 4 files changed +12
-12
lines changed Original file line number Diff line number Diff line change 1
1
import { hook } from '@nodepack/app-context'
2
2
import { Client as FaunaClient } from 'faunadb'
3
3
import { readMigrationRecords , writeMigrationRecords } from './migration'
4
+ import { addProp } from '@nodepack/app-context/src'
4
5
5
6
hook ( 'create' , async ( ctx ) => {
6
7
if ( ctx . config . fauna ) {
7
- ctx . fauna = new FaunaClient ( ctx . config . fauna )
8
+ addProp ( ctx , ' fauna' , ( ) => new FaunaClient ( ctx . config . fauna ) )
8
9
if ( process . env . NODEPACK_MAINTENANCE_FRAGMENTS ) {
9
10
// DB migrations
10
11
ctx . readDbMigrationRecords = ( ) => readMigrationRecords ( ctx )
Original file line number Diff line number Diff line change 1
- import { hook } from '@nodepack/app-context'
1
+ import { hook , addProp } from '@nodepack/app-context'
2
2
import knex from 'knex'
3
3
import { readMigrationRecords , writeMigrationRecords } from './migration'
4
4
5
5
hook ( 'create' , async ( ctx ) => {
6
6
if ( ctx . config . knex ) {
7
- ctx . knex = knex ( ctx . config . knex )
7
+ addProp ( ctx , ' knex' , ( ) => knex ( ctx . config . knex ) )
8
8
if ( process . env . NODEPACK_MAINTENANCE_FRAGMENTS ) {
9
9
// DB migrations
10
10
ctx . readDbMigrationRecords = ( ) => readMigrationRecords ( ctx )
Original file line number Diff line number Diff line change 1
- import { hook } from '@nodepack/app-context'
1
+ import { hook , addProp } from '@nodepack/app-context'
2
2
import { Sequelize } from 'sequelize'
3
3
import { loadModels } from './models'
4
4
5
5
let synced = false
6
6
7
7
hook ( 'create' , async ( ctx ) => {
8
8
if ( ctx . config . sequelize ) {
9
- const sequelize = ctx . sequelize = new Sequelize ( ctx . config . sequelize )
10
- loadModels ( ctx )
9
+ addProp ( ctx , ' sequelize' , ( ) => new Sequelize ( ctx . config . sequelize ) )
10
+ addProp ( ctx , 'models' , ( ) => loadModels ( ctx ) )
11
11
12
12
// Sync models for development
13
13
if ( ctx . config . syncModels && ! synced ) {
@@ -16,11 +16,11 @@ hook('create', async (ctx) => {
16
16
if ( typeof ctx . config . syncModels === 'object' ) {
17
17
options = ctx . config . syncModels
18
18
}
19
- await sequelize . sync ( options )
19
+ await ctx . sequelize . sync ( options )
20
20
}
21
21
22
22
hook ( 'destroy' , ( ) => {
23
- sequelize . close ( )
23
+ ctx . sequelize . close ( )
24
24
} )
25
25
} else {
26
26
console . warn ( '⚠️ No `sequelize` configuration found. Create a `config/sequelize.js` file that exports default a sequelize configuration object.' )
Original file line number Diff line number Diff line change 1
1
import { pascal } from 'case'
2
2
3
3
export async function loadModels ( ctx ) {
4
- if ( ! ctx . models ) {
5
- ctx . models = { }
6
- }
4
+ const models = { }
7
5
const files = require . context ( '@' , true , / ^ .\/ m o d e l s \/ .* \. [ j t ] s x ? $ / )
8
6
for ( const key of files . keys ( ) ) {
9
7
const module = files ( key )
10
8
const fn = module . default || module
11
9
if ( typeof fn === 'function' ) {
12
10
const [ , modelName ] = / ( [ a - z _ - ] + ) \. [ j t ] s x ? $ / i. exec ( key )
13
- ctx . models [ pascal ( modelName ) ] = fn ( ctx )
11
+ models [ pascal ( modelName ) ] = fn ( ctx )
14
12
}
15
13
}
14
+ return models
16
15
}
You can’t perform that action at this time.
0 commit comments