Skip to content

Commit

Permalink
feat: datetime should be main sorting, not the timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
koresar committed Dec 8, 2023
1 parent e91723c commit 68897f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
19 changes: 8 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ Medici adds a few **default** indexes on the `medici_transactions` collection:
```
"book": 1,
"accounts": 1,
"timestamp": -1,
"datetime": -1,
```

```
"book": 1,
"account_path.0": 1,
"account_path.1": 1,
"account_path.2": 1,
"timestamp": -1,
"datetime": -1,
```

However, if you are doing lots of queries using the `meta` data you probably would want to add the following index(es):
Expand All @@ -312,7 +312,7 @@ However, if you are doing lots of queries using the `meta` data you probably wou
"book": 1,
"accounts": 1,
"meta.myClientId": 1,
"timestamp": -1,
"datetime": -1,
```

and/or
Expand All @@ -323,7 +323,7 @@ and/or
"account_path.0": 1,
"account_path.1": 1,
"account_path.2": 1,
"timestamp": -1,
"datetime": -1,
```

Here is how to add an index manually via MongoDB CLI or other tool:
Expand All @@ -333,7 +333,7 @@ db.getSiblingDB("my_db_name").getCollection("medici_transactions").createIndex({
"book": 1,
"accounts": 1,
"meta.myClientId": 1,
"timestamp": -1,
"datetime": -1,
}, { background: true })
```

Expand All @@ -351,12 +351,9 @@ This release fixes the unfortunate mistake.
- Most of the default indexes were useless in majority of use cases. Thus, were removed. Only 4 default indexes left for `medici` v7.0:
- `_id`
- `_journal`
- `book,accounts,timestamp`
- `book,account_path.0,account_path.1,account_path.2,timestamp`
- The `timestamp` is the only one to be used in the default indexes. We won't index `datetime` by default. Reasons:
- `timestamp` is a read-only field created by `medici` thus `medici` can rely on it.
- but `datetime` is an **arbitrary** value you can pass when creating ledger entries. Almost nobody is doing that though.
- If you need indexes for the `datetime` field then please create yourself.
- `book,accounts,datetime`
- `book,account_path.0,account_path.1,account_path.2,datetime`
- The `datetime` is the only one to be used in the default indexes. Additional `timestamp` doesn't make any sense.
- Removed the `book.listAccounts()` caching which added in the previous release (v6.3) because the default indexes cover this use case now. Moreover, the index works faster than the cache.

### 6.3
Expand Down
4 changes: 2 additions & 2 deletions src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ export function setTransactionSchema(schema: Schema, collection?: string, option
schema.index({
book: 1,
accounts: 1,
timestamp: -1,
datetime: -1,
});
schema.index({
book: 1,
"account_path.0": 1,
"account_path.1": 1,
"account_path.2": 1,
timestamp: -1,
datetime: -1,
});
}

Expand Down

0 comments on commit 68897f5

Please sign in to comment.