Skip to content

Commit aa43fc0

Browse files
committed
feat(db): use ctx addProp
1 parent 6e964fe commit aa43fc0

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

packages/@nodepack/plugin-db-fauna/src/runtime/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { hook } from '@nodepack/app-context'
22
import { Client as FaunaClient } from 'faunadb'
33
import { readMigrationRecords, writeMigrationRecords } from './migration'
4+
import { addProp } from '@nodepack/app-context/src'
45

56
hook('create', async (ctx) => {
67
if (ctx.config.fauna) {
7-
ctx.fauna = new FaunaClient(ctx.config.fauna)
8+
addProp(ctx, 'fauna', () => new FaunaClient(ctx.config.fauna))
89
if (process.env.NODEPACK_MAINTENANCE_FRAGMENTS) {
910
// DB migrations
1011
ctx.readDbMigrationRecords = () => readMigrationRecords(ctx)

packages/@nodepack/plugin-db-knex/src/runtime/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { hook } from '@nodepack/app-context'
1+
import { hook, addProp } from '@nodepack/app-context'
22
import knex from 'knex'
33
import { readMigrationRecords, writeMigrationRecords } from './migration'
44

55
hook('create', async (ctx) => {
66
if (ctx.config.knex) {
7-
ctx.knex = knex(ctx.config.knex)
7+
addProp(ctx, 'knex', () => knex(ctx.config.knex))
88
if (process.env.NODEPACK_MAINTENANCE_FRAGMENTS) {
99
// DB migrations
1010
ctx.readDbMigrationRecords = () => readMigrationRecords(ctx)

packages/@nodepack/plugin-db-sequelize/src/runtime/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { hook } from '@nodepack/app-context'
1+
import { hook, addProp } from '@nodepack/app-context'
22
import { Sequelize } from 'sequelize'
33
import { loadModels } from './models'
44

55
let synced = false
66

77
hook('create', async (ctx) => {
88
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))
1111

1212
// Sync models for development
1313
if (ctx.config.syncModels && !synced) {
@@ -16,11 +16,11 @@ hook('create', async (ctx) => {
1616
if (typeof ctx.config.syncModels === 'object') {
1717
options = ctx.config.syncModels
1818
}
19-
await sequelize.sync(options)
19+
await ctx.sequelize.sync(options)
2020
}
2121

2222
hook('destroy', () => {
23-
sequelize.close()
23+
ctx.sequelize.close()
2424
})
2525
} else {
2626
console.warn('⚠️ No `sequelize` configuration found. Create a `config/sequelize.js` file that exports default a sequelize configuration object.')
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { pascal } from 'case'
22

33
export async function loadModels (ctx) {
4-
if (!ctx.models) {
5-
ctx.models = {}
6-
}
4+
const models = {}
75
const files = require.context('@', true, /^.\/models\/.*\.[jt]sx?$/)
86
for (const key of files.keys()) {
97
const module = files(key)
108
const fn = module.default || module
119
if (typeof fn === 'function') {
1210
const [, modelName] = /([a-z_-]+)\.[jt]sx?$/i.exec(key)
13-
ctx.models[pascal(modelName)] = fn(ctx)
11+
models[pascal(modelName)] = fn(ctx)
1412
}
1513
}
14+
return models
1615
}

0 commit comments

Comments
 (0)