Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

medici

Package Overview
Dependencies
Maintainers
2
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

medici - npm Package Compare versions

Comparing version 5.0.0-next.10 to 5.0.0

5

build/helper/syncIndexes.js

@@ -8,2 +8,7 @@ "use strict";

const balance_1 = require("../models/balance");
/**
* Will execute mongoose model's `syncIndexes()` for all medici models.
* WARNING! This will erase any custom (non-builtin) indexes you might have added.
* @param [options] {{background: Boolean}}
*/
async function syncIndexes(options) {

@@ -10,0 +15,0 @@ await journal_1.journalModel.syncIndexes(options);

2

package.json
{
"name": "medici",
"version": "5.0.0-next.10",
"version": "5.0.0",
"description": "Simple double-entry accounting for Node + Mongoose",

@@ -5,0 +5,0 @@ "main": "build/index.js",

@@ -90,9 +90,7 @@ # medici

const { results, total } = await myBook.ledger(
{
account: "Income",
start_date: startDate,
end_date: endDate,
},
);
const { results, total } = await myBook.ledger({
account: "Income",
start_date: startDate,
end_date: endDate,
});
```

@@ -258,5 +256,39 @@

Medici v2 was slow when number of records reach 30k. Starting from v3.0 the [following](https://github.com/flash-oss/medici/commit/274528ef5d1dae0beedca4a98dbf706808be53bd) indexes are auto generated on the `medici_transactions` collection:
### Fast balance
In medici v5 we introduced the so-called "fast balance" feature. [Here is the discussion](https://github.com/flash-oss/medici/issues/38). TL;DR: it caches `.balance()` call result once a day (customisable) to `medici_balances` collection.
If a database has millions of records then calculating the balance on half of them would take like 5 seconds. When this result is cached it takes few milliseconds to calculate the balance after that.
#### How it works under the hood
There are two hard problems in programming: cache invalidation and naming things. (C) Phil Karlton
Be default, when you call `book.blanace(...)` for the first time medici will cache its result to `medici_balances` (aka balance snapshot). By default, every doc there will be auto-removed as they have TTL of 48 hours. Meaning this cache will definitely expire in 2 days. Although, medici will try doing a second balance snapshot every 24 hours (default value). Thus, at any point of time there will be present from zero to two snapshots per balance query.
When you would call the `book.balance(...)` with the same exact arguments the medici will:
- retrieve the most recent snapshot if present,
- sum up only transactions inserted after the snapshot, and
- add the snapshot's balance to the sum.
In a rare case you wanted to remove some ledger entries from `medici_transactions` you would also need to remove all the `medici_balances` docs. Otherwise, the `.balance()` would be returning inaccurate data for up to 24 hours.
**IMPORTANT!**
To make this feature consistent we had to switch from client-generated IDs to MongoDB server generated IDs. See [forceServerObjectId](https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html#constructor).
#### How to disable balance caching feature
When creating a book you need to pass the `balanceSnapshotSec: 0` option.
```js
const myBook = new Book("MyBook", { balanceSnapshotSec: 0 })
```
### Indexes
Medici <=v2 was slow when number of records reach 30k. Starting from v3.0 the [following](https://github.com/flash-oss/medici/commit/274528ef5d1dae0beedca4a98dbf706808be53bd) indexes are auto generated on the `medici_transactions` collection:
```
"_journal": 1

@@ -381,9 +413,25 @@ ```

- Added support for MongoDB sessions (aka **ACID** transactions). See `IOptions` type.
- Did number of consistency, stability, server disk space, and speed improvements.
- Mongoose v5 and v6 are supported now.
- Did number of consistency, stability, server disk space, and speed improvements. Balance querying on massive databases with millions of documents are going to be much-much faster now. Like 10 to 1000 times faster.
- Mongoose v5 and v6 are both supported now.
Technical changes of the release.
Major breaking changes:
- The "approved" feature was removed.
- Mongoose middlewares on medici models are not supported anymore.
- The `.balance()` method does not support pagination anymore.
- Rename constructor `book` -> `Book`.
- Plus some other potentially breaking changes. See below.
Step by step migration from v4 to v5.
- Adapt your code to all the breaking changes.
- On the app start medici (actually mongoose) will create all the new indexes. If you have any custom indexes containing the `approved` property, you'd need to create similar indexes but without the property.
- You'd need to manually remove all the indexes which contain `approved` property in it.
- Done.
All changes of the release.
- Added a `mongoTransaction`-method, which is a convenience shortcut for `mongoose.connection.transaction`.
- Added async helper method `initModels`, which initializes the underlying `transactionModel` and `journalModel`. Use this after you connected to the MongoDB-Server if you want to use transactions. Or else you could get `Unable to read from a snapshot due to pending collection catalog changes; please retry the operation.` error when acquiring a session because the actual database-collection is still being created by the underlying mongoose-instance.
- Added `syncIndexes`. Warning! This function will erase any custom (non-builtin) indexes you might have added.
- Added `setJournalSchema` and `setTransactionSchema` to use custom Schemas. It will ensure, that all relevant middlewares and methods are also added when using custom Schemas. Use `syncIndexes`-method from medici after setTransactionSchema to enforce the defined indexes on the models.

@@ -395,7 +443,8 @@ - Added `maxAccountPath`. You can set the maximum amount of account paths via the second parameter of Book. This can improve the performance of `.balance()` and `.ledger()` calls as it will then use the accounts attribute of the transactions as a filter.

- **POTENTIALLY BREAKING**: Node.js 12 is the lowest supported version. Although, 10 should still work fine.
- **POTENTIALLY BREAKING**: `.ledger()` returns lean Transaction-Objects for better performance. To retrieve hydrated mongoose models set `lean` to `false` in the third parameter of `.ledger()`. It is recommended to not hydrate the transactions, as it implies that the transactions could be manipulated and the data integrity of Medici could be risked.
- **POTENTIALLY BREAKING**: MongoDB v4.0 is the lowest supported version. The v3.6 support was dropped.
- **POTENTIALLY BREAKING**: `.ledger()` returns lean Transaction-Objects (POJO) for better performance. To retrieve hydrated mongoose models set `lean` to `false` in the third parameter of `.ledger()`. It is recommended to not hydrate the transactions, as it implies that the transactions could be manipulated and the data integrity of Medici could be risked.
- **POTENTIALLY BREAKING**: Rounding precision was changed from 7 to 8 floating point digits.
- The new default precision is 8 digits. The medici v4 had it 7 by default. Be careful if you are using values which have more than 8 digits after the comma.
- You can now specify the `precision` in the `Book` constructor as an optional second parameter `precision`. Simulating medici v4 behaviour: `new Book("MyBook", { precision: 7 })`.
- Also, you can enforce an "only-Integer"-mode, by setting the precision to 0. But keep in mind that Javascript has a max safe integer limit of 9007199254740991.
- Also, you can enforce an "only-Integer" mode, by setting the precision to 0. But keep in mind that Javascript has a max safe integer limit of 9007199254740991.
- **POTENTIALLY BREAKING**: Added validation for `name` of Book, `maxAccountPath` and `precision`.

@@ -408,4 +457,3 @@ - The `name` has to be not an empty string or a string containing only whitespace characters.

- **POTENTIALLY BREAKING**: Transaction document properties `meta`, `voided`, `void_reason`, `_original_journal` won't be stored to the database when have no data. In medici v4 they were `{}`, `false`, `null`, `null` correspondingly.
- **BREAKING**: Transactions are now committed/voided using native `insertMany`/`updateMany` instead of mongoose `.save()` method. If you had any "pre save" middlewares on the `medici_transactions` they won't be working anymore.
- **BREAKING**: If you had any other Mongoose middlewares installed onto medici `transactionModel` or `journalModel` then they won't work anymore. Medici v5 is not using the mongoose to do DB operations. Instead, we execute commands via bare `mongodb` driver.
- **BREAKING**: If you had any Mongoose middlewares (e.g. `"pre save"`) installed onto medici `transactionModel` or `journalModel` then they won't work anymore. Medici v5 is not using the mongoose to do DB operations. Instead, we execute commands via bare `mongodb` driver.
- **BREAKING**: `.balance()` does not support pagination anymore. To get the balance of a page sum up the values of credit and debit of a paginated `.ledger()`-call.

@@ -412,0 +460,0 @@ - **BREAKING**: You can't import `book` anymore. Only `Book` is supported. `require("medici").Book`.

@@ -104,2 +104,7 @@ // Generated by dts-bundle-generator v6.2.0

export declare function initModels(): Promise<void>;
/**
* Will execute mongoose model's `syncIndexes()` for all medici models.
* WARNING! This will erase any custom (non-builtin) indexes you might have added.
* @param [options] {{background: Boolean}}
*/
export declare function syncIndexes(options?: {

@@ -106,0 +111,0 @@ background: boolean;

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc