diff --git a/packages/@nodepack/plugin-db-fauna/src/runtime/index.js b/packages/@nodepack/plugin-db-fauna/src/runtime/index.js index c21a6fd..c08bf51 100644 --- a/packages/@nodepack/plugin-db-fauna/src/runtime/index.js +++ b/packages/@nodepack/plugin-db-fauna/src/runtime/index.js @@ -1,10 +1,11 @@ import { hook } from '@nodepack/app-context' import { Client as FaunaClient } from 'faunadb' import { readMigrationRecords, writeMigrationRecords } from './migration' +import { addProp } from '@nodepack/app-context/src' hook('create', async (ctx) => { if (ctx.config.fauna) { - ctx.fauna = new FaunaClient(ctx.config.fauna) + addProp(ctx, 'fauna', () => new FaunaClient(ctx.config.fauna)) if (process.env.NODEPACK_MAINTENANCE_FRAGMENTS) { // DB migrations ctx.readDbMigrationRecords = () => readMigrationRecords(ctx) diff --git a/packages/@nodepack/plugin-db-knex/src/runtime/index.js b/packages/@nodepack/plugin-db-knex/src/runtime/index.js index 52c8a3c..1c86544 100644 --- a/packages/@nodepack/plugin-db-knex/src/runtime/index.js +++ b/packages/@nodepack/plugin-db-knex/src/runtime/index.js @@ -1,10 +1,10 @@ -import { hook } from '@nodepack/app-context' +import { hook, addProp } from '@nodepack/app-context' import knex from 'knex' import { readMigrationRecords, writeMigrationRecords } from './migration' hook('create', async (ctx) => { if (ctx.config.knex) { - ctx.knex = knex(ctx.config.knex) + addProp(ctx, 'knex', () => knex(ctx.config.knex)) if (process.env.NODEPACK_MAINTENANCE_FRAGMENTS) { // DB migrations ctx.readDbMigrationRecords = () => readMigrationRecords(ctx) diff --git a/packages/@nodepack/plugin-db-sequelize/src/runtime/index.js b/packages/@nodepack/plugin-db-sequelize/src/runtime/index.js index 392b9d8..3d7bc1b 100644 --- a/packages/@nodepack/plugin-db-sequelize/src/runtime/index.js +++ b/packages/@nodepack/plugin-db-sequelize/src/runtime/index.js @@ -1,4 +1,4 @@ -import { hook } from '@nodepack/app-context' +import { hook, addProp } from '@nodepack/app-context' import { Sequelize } from 'sequelize' import { loadModels } from './models' @@ -6,8 +6,8 @@ let synced = false hook('create', async (ctx) => { if (ctx.config.sequelize) { - const sequelize = ctx.sequelize = new Sequelize(ctx.config.sequelize) - loadModels(ctx) + addProp(ctx, 'sequelize', () => new Sequelize(ctx.config.sequelize)) + addProp(ctx, 'models', () => loadModels(ctx)) // Sync models for development if (ctx.config.syncModels && !synced) { @@ -16,11 +16,11 @@ hook('create', async (ctx) => { if (typeof ctx.config.syncModels === 'object') { options = ctx.config.syncModels } - await sequelize.sync(options) + await ctx.sequelize.sync(options) } hook('destroy', () => { - sequelize.close() + ctx.sequelize.close() }) } else { console.warn('⚠️ No `sequelize` configuration found. Create a `config/sequelize.js` file that exports default a sequelize configuration object.') diff --git a/packages/@nodepack/plugin-db-sequelize/src/runtime/models.js b/packages/@nodepack/plugin-db-sequelize/src/runtime/models.js index d81879b..1dcc105 100644 --- a/packages/@nodepack/plugin-db-sequelize/src/runtime/models.js +++ b/packages/@nodepack/plugin-db-sequelize/src/runtime/models.js @@ -1,16 +1,15 @@ import { pascal } from 'case' export async function loadModels (ctx) { - if (!ctx.models) { - ctx.models = {} - } + const models = {} const files = require.context('@', true, /^.\/models\/.*\.[jt]sx?$/) for (const key of files.keys()) { const module = files(key) const fn = module.default || module if (typeof fn === 'function') { const [, modelName] = /([a-z_-]+)\.[jt]sx?$/i.exec(key) - ctx.models[pascal(modelName)] = fn(ctx) + models[pascal(modelName)] = fn(ctx) } } + return models }