Skip to content

Commit

Permalink
feat: 🏷️ use Hookable<T> in Model
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgbn committed Apr 5, 2023
1 parent 2711f57 commit 60fb495
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion playground/server/api/[...todos].ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const setReadOnlyProp = (d: any) => { d.readOnlyProp = 'privateN=' + (d.privateN
Todo.hook('create:after', ({ data }) => setReadOnlyProp(data))

Todo.hook('update:after', ({ data }) => setReadOnlyProp(data))
Todo.hook('archive:done', ({ event }) => consola.log(`Todo #${event.context.params.id} archived`))
Todo.hook('archive:done', ({ event }) => consola.log(`Todo #${event?.context.params?.id} archived`))

const log = oaHandler((ev: H3Event) => {
consola.log('log::', ev.node.req.method)
Expand Down
30 changes: 26 additions & 4 deletions src/runtime/server/helpers/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ addFormats(ajv)
const cipherAlgo = config.cipherAlgo
const cipherKey = config.cipherKey

type HookResult = Promise<void> | void
type HookArgData = { data: Schema }
type HookArgEv = { event?: H3Event }
type HookArgIds = { id: string|ObjectId|undefined, _id: ObjectId }
export interface ModelNuxtOaHooks {
'collection:ready': ({ collection }: { collection: Collection }) => HookResult
'model:cleanJSON': (d: HookArgData) => HookResult
'getAll:before': (d: HookArgEv) => HookResult
'create:before': (d: HookArgData & HookArgEv) => HookResult
'create:after': (d: HookArgData & HookArgEv) => HookResult
'create:done': (d: HookArgData & HookArgEv) => HookResult
'update:before': (d: HookArgData & HookArgEv & HookArgIds) => HookResult
'update:after': (d: HookArgData & HookArgEv & HookArgIds) => HookResult
'update:done': (d: HookArgData & HookArgEv) => HookResult
'archive:before': (d: HookArgEv & HookArgIds) => HookResult
'archive:after': (d: HookArgData & HookArgEv & HookArgIds) => HookResult
'archive:done': (d: HookArgData & HookArgEv) => HookResult
'delete:before': (d: HookArgEv & HookArgIds) => HookResult
'delete:done': (d: HookArgData & HookArgEv & { deletedCount: number }) => HookResult
}

export function cleanSchema (schema: Schema): Schema {
schema.type = 'object' // type must be object
delete schema.encryptedProperties
Expand All @@ -28,7 +49,7 @@ export function cleanSchema (schema: Schema): Schema {
return schema
}

export default class Model extends Hookable {
export default class Model extends Hookable<ModelNuxtOaHooks> {
name: string
collection: Collection
encryptedProps: string[]
Expand All @@ -38,6 +59,7 @@ export default class Model extends Hookable {
userstamps: { createdBy?: Boolean, updatedBy?: Boolean, deletedBy?: Boolean }
schema: Schema
validator: ValidateFunction
getAllCleaner: (el: object) => object

constructor (name: string) {
super()
Expand Down Expand Up @@ -80,6 +102,8 @@ export default class Model extends Hookable {
cleanSchema(this.schema)

this.validator = ajv.compile(this.schema)

this.getAllCleaner = (el: object) => this.cleanJSON(el)
}

/**
Expand Down Expand Up @@ -178,9 +202,7 @@ export default class Model extends Hookable {
*/
async getAll (event?: H3Event) {
await this.callHook('getAll:before', { event })
const hookClean = await this.callHook('getAll') ?? ((el: object) => this.cleanJSON(el))
return (await this.collection.find({}).toArray())
.map(hookClean)
return (await this.collection.find({}).toArray()).map(this.getAllCleaner)
}

/**
Expand Down

0 comments on commit 60fb495

Please sign in to comment.