Skip to content

Commit cc4a300

Browse files
committed
switch to cacheable/node-cache
1 parent 3c809e6 commit cc4a300

4 files changed

Lines changed: 82 additions & 54 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { NodeCache } from '@cacheable/node-cache';
12
import { minutesToSeconds, secondsToMillis } from '@cityssm/to-millis';
2-
import NodeCache from 'node-cache';
33
import _extendGpInvoice from './diamond/extendGpInvoice.js';
44
import _getCashReceiptByDocumentNumber from './diamond/getCashReceiptByDocumentNumber.js';
55
import _getAccountByAccountIndex from './gp/getAccountByAccountIndex.js';

index.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import { NodeCache } from '@cacheable/node-cache'
12
import { minutesToSeconds, secondsToMillis } from '@cityssm/to-millis'
23
import type { config as MSSQLConfig } from 'mssql'
3-
import NodeCache from 'node-cache'
44

55
import _extendGpInvoice from './diamond/extendGpInvoice.js'
66
import _getCashReceiptByDocumentNumber from './diamond/getCashReceiptByDocumentNumber.js'
@@ -55,17 +55,17 @@ export class DynamicsGP {
5555
readonly #mssqlConfig: MSSQLConfig
5656
readonly #options: DynamicsGPOptions
5757

58-
readonly #accountCache: NodeCache
59-
readonly #customerCache: NodeCache
60-
readonly #invoiceCache: NodeCache
61-
readonly #itemCache: NodeCache
62-
readonly #vendorCache: NodeCache
58+
readonly #accountCache: NodeCache<GPAccount | undefined>
59+
readonly #customerCache: NodeCache<GPCustomer | undefined>
60+
readonly #invoiceCache: NodeCache<GPInvoice | undefined>
61+
readonly #itemCache: NodeCache<GPItemWithQuantities | undefined>
62+
readonly #vendorCache: NodeCache<GPVendor | undefined>
6363

6464
#invoiceDocumentTypesCache: GPInvoiceDocumentType[] = []
6565
#invoiceDocumentTypesCacheExpiryMillis = 0
6666

67-
readonly #diamondCashReceiptCache: NodeCache
68-
readonly #diamondInvoiceCache: NodeCache
67+
readonly #diamondCashReceiptCache: NodeCache<DiamondCashReceipt | undefined>
68+
readonly #diamondInvoiceCache: NodeCache<DiamondExtendedGPInvoice | undefined>
6969

7070
constructor(mssqlConfig: MSSQLConfig, options?: Partial<DynamicsGPOptions>) {
7171
this.#mssqlConfig = mssqlConfig
@@ -107,8 +107,7 @@ export class DynamicsGP {
107107
async getAccountByAccountIndex(
108108
accountIndex: number | string
109109
): Promise<GPAccount | undefined> {
110-
let account: GPAccount | undefined =
111-
this.#accountCache.get(accountIndex) ?? undefined
110+
let account = this.#accountCache.get(accountIndex) ?? undefined
112111

113112
if (account === undefined) {
114113
account = await _getAccountByAccountIndex(this.#mssqlConfig, accountIndex)
@@ -121,8 +120,7 @@ export class DynamicsGP {
121120
async getCustomerByCustomerNumber(
122121
customerNumber: string
123122
): Promise<GPCustomer | undefined> {
124-
let customer: GPCustomer | undefined =
125-
this.#customerCache.get(customerNumber) ?? undefined
123+
let customer = this.#customerCache.get(customerNumber) ?? undefined
126124

127125
if (customer === undefined) {
128126
customer = await _getCustomerByCustomerNumber(
@@ -145,8 +143,7 @@ export class DynamicsGP {
145143
invoiceDocumentTypeOrAbbreviationOrName
146144
)
147145

148-
let invoice: GPInvoice | undefined =
149-
this.#invoiceCache.get(cacheKey) ?? undefined
146+
let invoice = this.#invoiceCache.get(cacheKey) ?? undefined
150147

151148
if (invoice === undefined || skipCache) {
152149
invoice = await _getInvoiceByInvoiceNumber(
@@ -189,8 +186,7 @@ export class DynamicsGP {
189186
async getItemByItemNumber(
190187
itemNumber: string
191188
): Promise<GPItemWithQuantities | undefined> {
192-
let item: GPItemWithQuantities | undefined =
193-
this.#itemCache.get(itemNumber) ?? undefined
189+
let item = this.#itemCache.get(itemNumber) ?? undefined
194190

195191
if (item === undefined) {
196192
item = await _getItemByItemNumber(this.#mssqlConfig, itemNumber)
@@ -207,8 +203,7 @@ export class DynamicsGP {
207203
}
208204

209205
async getVendorByVendorId(vendorId: string): Promise<GPVendor | undefined> {
210-
let vendor: GPVendor | undefined =
211-
this.#vendorCache.get(vendorId) ?? undefined
206+
let vendor = this.#vendorCache.get(vendorId) ?? undefined
212207

213208
if (vendor === undefined) {
214209
const vendors = await this.getVendors({ vendorId })
@@ -228,8 +223,7 @@ export class DynamicsGP {
228223
async getDiamondCashReceiptByDocumentNumber(
229224
documentNumber: number | string
230225
): Promise<DiamondCashReceipt | undefined> {
231-
let receipt: DiamondCashReceipt | undefined =
232-
this.#diamondCashReceiptCache.get(documentNumber)
226+
let receipt = this.#diamondCashReceiptCache.get(documentNumber)
233227

234228
if (receipt === undefined) {
235229
receipt = await _getCashReceiptByDocumentNumber(
@@ -251,8 +245,7 @@ export class DynamicsGP {
251245
invoiceDocumentTypeOrAbbreviationOrName
252246
)
253247

254-
let diamondInvoice: DiamondExtendedGPInvoice | undefined =
255-
this.#diamondInvoiceCache.get(cacheKey) ?? undefined
248+
let diamondInvoice = this.#diamondInvoiceCache.get(cacheKey) ?? undefined
256249

257250
if (diamondInvoice === undefined) {
258251
const gpInvoice = await this.#getInvoiceByInvoiceNumber(

package-lock.json

Lines changed: 63 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
"test": "node --test"
3232
},
3333
"dependencies": {
34+
"@cacheable/node-cache": "^1.6.0",
3435
"@cityssm/mssql-multi-pool": "^5.1.0",
35-
"@cityssm/to-millis": "^1.0.0",
36-
"node-cache": "^5.1.2"
36+
"@cityssm/to-millis": "^1.0.0"
3737
},
3838
"devDependencies": {
3939
"@types/debug": "^4.1.12",
40-
"@types/node": "^24.0.12",
40+
"@types/node": "^24.0.14",
4141
"debug": "^4.4.1",
4242
"eslint-config-cityssm": "^26.0.0",
4343
"prettier-config-cityssm": "^1.0.0"

0 commit comments

Comments
 (0)