Skip to content

Commit

Permalink
feat(db): use ctx addProp
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jan 4, 2020
1 parent 6e964fe commit aa43fc0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/@nodepack/plugin-db-fauna/src/runtime/index.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/@nodepack/plugin-db-knex/src/runtime/index.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
10 changes: 5 additions & 5 deletions packages/@nodepack/plugin-db-sequelize/src/runtime/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { hook } from '@nodepack/app-context'
import { hook, addProp } from '@nodepack/app-context'
import { Sequelize } from 'sequelize'
import { loadModels } from './models'

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) {
Expand All @@ -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.')
Expand Down
7 changes: 3 additions & 4 deletions packages/@nodepack/plugin-db-sequelize/src/runtime/models.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit aa43fc0

Please sign in to comment.