Releases: xpresserjs/xpress-mongo
Update Mongodb And Fix save twice bug.
Updated Packages
Updated mongodb to version 4.9.0
Save Twice Bug
An error occurs when you generate id and save twice for example
const user = User.make({}).generateId();
await user.save()
user.data.username = "changed"
await user.save() // ❌ will throw a duplicate id.
This has been fixed.
Updated Packages & Added Pagination data with frontend support.
MongoDB Package has been updated to 4.7.0
.
Pagination types for backend and frontend usage are now available in a single file.
Paginated Type
import type {Paginated} from "xpress-mongo/src/types/pagination";
const users: Paginated<User> = await api.get('/path/to/paginated/resource');
users.total // Total results
users.perPage // Results per Page
users.page // current page
users.lastPage // last page
users.data // paginated data
Paginated Function
Paginated
is also a function that returns typed default pagination data
import {Paginated} from "xpress-mongo/src/types/pagination";
const users = Paginated<User>();
// users will have default paginated data.
An example using vue
<script lang="ts" setup>
import {Paginated} from "xpress-mongo/src/types/pagination";
const users = ref(Paginated<User>());
// users will have default paginated data.
</script>
Updated mongodb and applied deprecated `count` changes
The new mongodb v 4.4.0 to be exact deprecated collection.count()
but this won't be a breaking change for us as the new recommended countDocuments
and estimatedDocumentCount
has been applied.
Model.count(filter?, options?) // is same as native().countDocuments()
Model.countEstimated() // is same as native().estimatedDocumentCount()
It is recommended you use count
when you have a query filter and countEstimated
when you have none.
Nevertheless xpress-mongo's count
uses countEstimated
when no query is provided.
Added: sum & sumMany
Before now sum
does the work of both single
and multiple
keys but this gets difficult when dealing with types and readability.
Old
// sum one key
await Model.sum("fiat") // number
// sum multiple keys
await Model.sum(["fiat", "crypto"]) // {fiat: 200, crypto: 2.456}
// sum multiple fields with nick names
await Model.sum({totalFiat: 'fiat', totalBitcoin: 'crypto'}) // {totalFiat: 200, totalBitcoin: 2.456}
New
Docs: https://xpresserjs.com/xpress-mongo/model.html#sum-async
// sum one key
await Model.sum("fiat") // number
// sum multiple keys
const sum = await Model.sumMany(["fiat", "crypto"]) // {fiat: 200, crypto: 2.456}
// sum multiple fields with nick names
const sum2 = await Model.sumMany({totalFiat: 'fiat', totalBitcoin: 'crypto'}) // {totalFiat: 200, totalBitcoin: 2.456}
With types support: sum
and sum2
will be typed to have defined keys.
Upgraded packages and fixed uuid import issue
This Release fixes issue: #7
Reported by @Katalyst33
Refactored Typescript Pratices
- Now using
import
instead ofrequire
, in preparation for when typescript fully supportsESM
- Removed
lodash
as dependency sinceobject-collection/lodash
has it exported already.