@uploadflow/mongoose
v0.1.0
Published
Mongoose AttachmentStore for the uploadflow file-upload pipeline (zero-setup schema + $lookup helper)
Downloads
155
Maintainers
Readme
@uploadflow/mongoose
Zero-setup Mongoose AttachmentStore for the uploadflow file-upload pipeline, plus a
lookupStages() aggregation $lookup helper (no N+1). Use it so @LinkedUpload / @WithAttachments
(NestJS) or linked() (Express) persist attachments to MongoDB without you writing any DB code.
npm i @uploadflow/mongoose @uploadflow/corePeer dep: mongoose (^7 or ^8).
Setup
The store registers its own schema/collection — no model to define.
import { MongooseAttachmentStore } from '@uploadflow/mongoose';
const store = MongooseAttachmentStore.forRoot({
connection, // a mongoose Connection
collection: 'attachments' // optional (default 'attachments')
});NestJS (inject the connection, e.g. via getConnectionToken()):
UploadModule.forRootAsync({
inject: [getConnectionToken()],
useFactory: (connection) => ({
storage: { /* ... */ },
attachmentStore: MongooseAttachmentStore.forRoot({ connection }),
}),
});Stored schema
Each attachment document: recordId, module, key, url, size, mime, originalName,
createdBy, createdAt. recordId + module are indexed.
Reading in an aggregation (no N+1)
Drop the join into your existing pipeline:
const docs = await MediaModel.aggregate([
{ $match: { companyId } },
...MongooseAttachmentStore.lookupStages({ module: 'MEDIA', as: 'attachments' }),
{ $sort: { createdAt: -1 } },
]);lookupStages({ localField?, foreignField?, module?, as?, collection? }) returns the $lookup stages;
it casts the local record id to a string to match the stored recordId. Also available as an instance
method: store.lookupStages({ ... }) (uses the store's collection).
Under urlMode: 'signed', stamp fresh signed URLs on the joined array afterwards:
await storage.resolveDownloadUrls(docs, { path: 'attachments[].key' });Full docs: https://github.com/OWNER/uploadflow#readme
MIT
