123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- import type { Context } from '../types'
- import { ObjectId } from 'mongodb'
- import type { Task, TaskCreate, TaskUpdate } from './types'
- export type TaskModel = Awaited<ReturnType<typeof createTaskModel>>
- async function createTaskModel(ctx: Context) {
- const collection = ctx.db.collection<Task>('task')
-
- async function create(input: TaskCreate) {
- const result = await collection.insertOne({
- _herd: input._herd,
- _account: input._account,
- description: input.description,
- position: input.position,
- })
- return await collection.findOne({ _id: result.insertedId })
- }
-
- async function init() {
- await ctx.db.createCollection('task')
- }
-
- async function update(id: ObjectId | string, input: TaskUpdate) {
- const changes = <Partial<Task>>{}
- if (input._herd) changes._herd = input._herd
- if (input._account) changes._account = input._account
- if (input.description) changes.description = input.description
- if (input.position !== undefined) changes.position = input.position
- return collection.findOneAndUpdate(
- { _id: new ObjectId(id) },
- { $set: changes },
- { returnDocument: 'after' },
- )
- }
-
- await init()
- return {
- collection,
- create,
- init,
- update,
- }
- }
- export default createTaskModel
|