Skip to content

Commit

Permalink
fix node 14 and 16 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Oct 13, 2023
1 parent 6c13481 commit b2d232e
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 120 deletions.
2 changes: 1 addition & 1 deletion database/parkingDB/getLicencePlateOwner.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sqlite from 'better-sqlite3';
import { LicencePlateOwner } from '../../types/recordTypes.js';
import type { LicencePlateOwner } from '../../types/recordTypes.js';
export declare function getLicencePlateOwnerWithDB(database: sqlite.Database, licencePlateCountry: string, licencePlateProvince: string, licencePlateNumber: string, recordDateOrBefore: number): LicencePlateOwner | undefined;
export declare function getLicencePlateOwner(licencePlateCountry: string, licencePlateProvince: string, licencePlateNumber: string, recordDateOrBefore: number): LicencePlateOwner | undefined;
export default getLicencePlateOwner;
4 changes: 2 additions & 2 deletions database/parkingDB/getLicencePlateOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as configFunctions from '../../helpers/functions.config.js';
import * as vehicleFunctions from '../../helpers/functions.vehicle.js';
export function getLicencePlateOwnerWithDB(database, licencePlateCountry, licencePlateProvince, licencePlateNumber, recordDateOrBefore) {
const licencePlateCountryAlias = configFunctions.getProperty('licencePlateCountryAliases')[licencePlateCountry] || licencePlateCountry;
const licencePlateProvinceAlias = (configFunctions.getProperty('licencePlateProvinceAliases')[licencePlateCountryAlias] || {})[licencePlateProvince] || licencePlateProvince;
const licencePlateProvinceAlias = configFunctions.getProperty('licencePlateProvinceAliases')[licencePlateCountryAlias]?.[licencePlateProvince] || licencePlateProvince;
const possibleOwners = database
.prepare('select * from LicencePlateOwners' +
' where recordDelete_timeMillis is null' +
Expand All @@ -15,7 +15,7 @@ export function getLicencePlateOwnerWithDB(database, licencePlateCountry, licenc
.all(licencePlateNumber, recordDateOrBefore);
for (const possibleOwnerObject of possibleOwners) {
const ownerPlateCountryAlias = configFunctions.getProperty('licencePlateCountryAliases')[possibleOwnerObject.licencePlateCountry] || possibleOwnerObject.licencePlateCountry;
const ownerPlateProvinceAlias = (configFunctions.getProperty('licencePlateProvinceAliases')[ownerPlateCountryAlias] || {})[possibleOwnerObject.licencePlateProvince] ||
const ownerPlateProvinceAlias = configFunctions.getProperty('licencePlateProvinceAliases')[ownerPlateCountryAlias]?.[possibleOwnerObject.licencePlateProvince] ||
possibleOwnerObject.licencePlateProvince;
if (licencePlateCountryAlias === ownerPlateCountryAlias &&
licencePlateProvinceAlias === ownerPlateProvinceAlias) {
Expand Down
12 changes: 6 additions & 6 deletions database/parkingDB/getLicencePlateOwner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sqlite from 'better-sqlite3'
import { parkingDB as databasePath } from '../../data/databasePaths.js'
import * as configFunctions from '../../helpers/functions.config.js'
import * as vehicleFunctions from '../../helpers/functions.vehicle.js'
import { LicencePlateOwner } from '../../types/recordTypes.js'
import type { LicencePlateOwner } from '../../types/recordTypes.js'

export function getLicencePlateOwnerWithDB(
database: sqlite.Database,
Expand All @@ -19,9 +19,9 @@ export function getLicencePlateOwnerWithDB(
] || licencePlateCountry

const licencePlateProvinceAlias =
(configFunctions.getProperty('licencePlateProvinceAliases')[
configFunctions.getProperty('licencePlateProvinceAliases')[
licencePlateCountryAlias
] || {})[licencePlateProvince] || licencePlateProvince
]?.[licencePlateProvince] || licencePlateProvince

const possibleOwners = database
.prepare(
Expand All @@ -31,7 +31,7 @@ export function getLicencePlateOwnerWithDB(
' and recordDate >= ?' +
' order by recordDate'
)
.all(licencePlateNumber, recordDateOrBefore) as pts.LicencePlateOwner[]
.all(licencePlateNumber, recordDateOrBefore) as LicencePlateOwner[]

for (const possibleOwnerObject of possibleOwners) {
const ownerPlateCountryAlias =
Expand All @@ -40,9 +40,9 @@ export function getLicencePlateOwnerWithDB(
] || possibleOwnerObject.licencePlateCountry

const ownerPlateProvinceAlias =
(configFunctions.getProperty('licencePlateProvinceAliases')[
configFunctions.getProperty('licencePlateProvinceAliases')[
ownerPlateCountryAlias
] || {})[possibleOwnerObject.licencePlateProvince] ||
]?.[possibleOwnerObject.licencePlateProvince] ||
possibleOwnerObject.licencePlateProvince

if (
Expand Down
7 changes: 1 addition & 6 deletions helpers/functions.config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export declare function getProperty(propertyName: 'session.cookieName'): string;
export declare function getProperty(propertyName: 'session.doKeepAlive'): boolean;
export declare function getProperty(propertyName: 'session.maxAgeMillis'): number;
export declare function getProperty(propertyName: 'session.secret'): string;
export declare function getProperty(propertyName: 'users.testing'): string[];
export declare function getProperty(propertyName: 'users.canLogin'): string[];
export declare function getProperty(propertyName: 'users.canUpdate'): string[];
export declare function getProperty(propertyName: 'users.isAdmin'): string[];
export declare function getProperty(propertyName: 'users.isOperator'): string[];
export declare function getProperty(propertyName: 'users.createUpdateWindowMillis'): number;
export declare function getProperty(propertyName: 'users.testing' | 'users.canLogin' | 'users.canUpdate' | 'users.isAdmin' | 'users.isOperator'): string[];
export declare function getProperty(propertyName: 'application.feature_mtoExportImport'): boolean;
export declare function getProperty(propertyName: 'mtoExportImport.authorizedUser'): string;
export declare function getProperty(propertyName: 'application.task_nhtsa.runTask'): boolean;
Expand Down
14 changes: 7 additions & 7 deletions helpers/functions.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ export function getProperty(propertyName: 'session.doKeepAlive'): boolean
export function getProperty(propertyName: 'session.maxAgeMillis'): number
export function getProperty(propertyName: 'session.secret'): string

export function getProperty(propertyName: 'users.testing'): string[]
export function getProperty(propertyName: 'users.canLogin'): string[]
export function getProperty(propertyName: 'users.canUpdate'): string[]
export function getProperty(propertyName: 'users.isAdmin'): string[]
export function getProperty(propertyName: 'users.isOperator'): string[]
export function getProperty(
propertyName: 'users.createUpdateWindowMillis'
): number
propertyName:
| 'users.testing'
| 'users.canLogin'
| 'users.canUpdate'
| 'users.isAdmin'
| 'users.isOperator'
): string[]

export function getProperty(
propertyName: 'application.feature_mtoExportImport'
Expand Down
6 changes: 3 additions & 3 deletions helpers/functions.vehicle.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NHTSAMakeModel } from '../types/recordTypes';
export declare const getModelsByMakeFromCache: (makeSearchStringOriginal: string) => NHTSAMakeModel[];
export declare const getModelsByMake: (makeSearchStringOriginal: string) => Promise<NHTSAMakeModel[]>;
import type { NHTSAMakeModel } from '../types/recordTypes.js';
export declare function getModelsByMakeFromCache(makeSearchStringOriginal: string): NHTSAMakeModel[];
export declare function getModelsByMake(makeSearchStringOriginal: string): Promise<NHTSAMakeModel[]>;
export declare const getMakeFromNCIC: (vehicleNCIC: string) => string;
export declare const isNCICExclusivelyTrailer: (vehicleNCIC: string) => boolean;
79 changes: 41 additions & 38 deletions helpers/functions.vehicle.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import sqlite from 'better-sqlite3';
import nhtsa from '@shaggytools/nhtsa-api-wrapper';
import sqlite from 'better-sqlite3';
import { nhtsaDB as databasePath } from '../data/databasePaths.js';
import * as ncic from '../data/ncicCodes.js';
import { trailerNCIC } from '../data/ncicCodes/trailer.js';
import * as ncic from '../data/ncicCodes.js';
const { GetModelsForMake } = nhtsa;
const nhtsaGetModelsForMake = new GetModelsForMake();
const nhtsaSearchExpiryDurationMillis = 14 * 86400 * 1000;
const getModelsByMakeFromDB = (makeSearchString, database) => {
function getModelsByMakeFromDB(makeSearchString, database) {
return database
.prepare('select makeID, makeName, modelID, modelName' +
' from MakeModel' +
' where instr(lower(makeName), ?)' +
' and recordDelete_timeMillis is null' +
' order by makeName, modelName')
.prepare(`select makeID, makeName, modelID, modelName
from MakeModel
where instr(lower(makeName), ?)
and recordDelete_timeMillis is null
order by makeName, modelName`)
.all(makeSearchString);
};
export const getModelsByMakeFromCache = (makeSearchStringOriginal) => {
}
export function getModelsByMakeFromCache(makeSearchStringOriginal) {
const makeSearchString = makeSearchStringOriginal.trim().toLowerCase();
const database = sqlite(databasePath);
const makeModelResults = getModelsByMakeFromDB(makeSearchString, database);
database.close();
return makeModelResults;
};
export const getModelsByMake = async (makeSearchStringOriginal) => {
}
export async function getModelsByMake(makeSearchStringOriginal) {
const makeSearchString = makeSearchStringOriginal.trim().toLowerCase();
const database = sqlite(databasePath);
const queryCloseCallbackFunction = () => {
Expand All @@ -32,41 +33,43 @@ export const getModelsByMake = async (makeSearchStringOriginal) => {
let useAPI = false;
const nowMillis = Date.now();
const searchRecord = database
.prepare('select searchExpiryMillis from MakeModelSearchHistory' +
' where searchString = ?')
.prepare(`select searchExpiryMillis
from MakeModelSearchHistory
where searchString = ?`)
.get(makeSearchString);
if (searchRecord) {
if (searchRecord === undefined) {
useAPI = true;
database
.prepare(`insert into MakeModelSearchHistory (
searchString, resultCount, searchExpiryMillis)
values (?, ?, ?)`)
.run(makeSearchString, 0, nowMillis + nhtsaSearchExpiryDurationMillis);
}
else {
if (searchRecord.searchExpiryMillis < nowMillis) {
useAPI = true;
database
.prepare('update MakeModelSearchHistory' +
' set searchExpiryMillis = ?' +
' where searchString = ?')
.prepare(`update MakeModelSearchHistory
set searchExpiryMillis = ?
where searchString = ?`)
.run(nowMillis + nhtsaSearchExpiryDurationMillis, makeSearchString);
}
}
else {
useAPI = true;
database
.prepare('insert into MakeModelSearchHistory' +
' (searchString, resultCount, searchExpiryMillis)' +
' values (?, ?, ?)')
.run(makeSearchString, 0, nowMillis + nhtsaSearchExpiryDurationMillis);
}
if (useAPI) {
const data = await GetModelsForMake(makeSearchString);
const data = await nhtsaGetModelsForMake.GetModelsForMake(makeSearchString);
database
.prepare('update MakeModelSearchHistory' +
' set resultCount = ?' +
'where searchString = ?')
.prepare(`update MakeModelSearchHistory
set resultCount = ?
where searchString = ?`)
.run(data.Count, makeSearchString);
const insertSQL = 'insert or ignore into MakeModel (makeID, makeName, modelID, modelName,' +
' recordCreate_timeMillis, recordUpdate_timeMillis)' +
' values (?, ?, ?, ?, ?, ?)';
const updateSQL = 'update MakeModel' +
' set recordUpdate_timeMillis = ?' +
' where makeName = ?' +
' and modelName = ?';
const insertSQL = `insert or ignore into MakeModel (
makeID, makeName, modelID, modelName,
recordCreate_timeMillis, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?)`;
const updateSQL = `update MakeModel
set recordUpdate_timeMillis = ?
where makeName = ?
and modelName = ?`;
for (const record of data.Results) {
const info = database
.prepare(insertSQL)
Expand All @@ -79,7 +82,7 @@ export const getModelsByMake = async (makeSearchStringOriginal) => {
}
}
return queryCloseCallbackFunction();
};
}
export const getMakeFromNCIC = (vehicleNCIC) => {
return ncic.allNCIC[vehicleNCIC] || vehicleNCIC;
};
Expand Down
94 changes: 46 additions & 48 deletions helpers/functions.vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
import sqlite from 'better-sqlite3'

import nhtsa from '@shaggytools/nhtsa-api-wrapper'
import sqlite from 'better-sqlite3'

import { nhtsaDB as databasePath } from '../data/databasePaths.js'

import * as ncic from '../data/ncicCodes.js'
import { trailerNCIC } from '../data/ncicCodes/trailer.js'

import type { NHTSAMakeModel } from '../types/recordTypes'
import * as ncic from '../data/ncicCodes.js'
import type { NHTSAMakeModel } from '../types/recordTypes.js'

/*
* API
*/

const { GetModelsForMake } = nhtsa
const nhtsaGetModelsForMake = new GetModelsForMake()

const nhtsaSearchExpiryDurationMillis = 14 * 86_400 * 1000

/*
* More Data
*/

const getModelsByMakeFromDB = (
function getModelsByMakeFromDB(
makeSearchString: string,
database: sqlite.Database
): NHTSAMakeModel[] => {
): NHTSAMakeModel[] {
return database
.prepare(
'select makeID, makeName, modelID, modelName' +
' from MakeModel' +
' where instr(lower(makeName), ?)' +
' and recordDelete_timeMillis is null' +
' order by makeName, modelName'
`select makeID, makeName, modelID, modelName
from MakeModel
where instr(lower(makeName), ?)
and recordDelete_timeMillis is null
order by makeName, modelName`
)
.all(makeSearchString) as NHTSAMakeModel[]
}

export const getModelsByMakeFromCache = (
export function getModelsByMakeFromCache(
makeSearchStringOriginal: string
): NHTSAMakeModel[] => {
): NHTSAMakeModel[] {
const makeSearchString = makeSearchStringOriginal.trim().toLowerCase()

const database = sqlite(databasePath)
Expand All @@ -50,14 +48,14 @@ export const getModelsByMakeFromCache = (
return makeModelResults
}

export const getModelsByMake = async (
export async function getModelsByMake(
makeSearchStringOriginal: string
): Promise<NHTSAMakeModel[]> => {
): Promise<NHTSAMakeModel[]> {
const makeSearchString = makeSearchStringOriginal.trim().toLowerCase()

const database = sqlite(databasePath)

const queryCloseCallbackFunction = () => {
const queryCloseCallbackFunction = (): NHTSAMakeModel[] => {
const makeModelResults = getModelsByMakeFromDB(makeSearchString, database)
database.close()
return makeModelResults
Expand All @@ -71,12 +69,23 @@ export const getModelsByMake = async (

const searchRecord = database
.prepare(
'select searchExpiryMillis from MakeModelSearchHistory' +
' where searchString = ?'
`select searchExpiryMillis
from MakeModelSearchHistory
where searchString = ?`
)
.get(makeSearchString) as { searchExpiryMillis: number }
.get(makeSearchString) as { searchExpiryMillis: number } | undefined

if (searchRecord === undefined) {
useAPI = true

if (searchRecord) {
database
.prepare(
`insert into MakeModelSearchHistory (
searchString, resultCount, searchExpiryMillis)
values (?, ?, ?)`
)
.run(makeSearchString, 0, nowMillis + nhtsaSearchExpiryDurationMillis)
} else {
if (searchRecord.searchExpiryMillis < nowMillis) {
// expired
// update searchExpiryMillis to avoid multiple queries
Expand All @@ -85,45 +94,34 @@ export const getModelsByMake = async (

database
.prepare(
'update MakeModelSearchHistory' +
' set searchExpiryMillis = ?' +
' where searchString = ?'
`update MakeModelSearchHistory
set searchExpiryMillis = ?
where searchString = ?`
)
.run(nowMillis + nhtsaSearchExpiryDurationMillis, makeSearchString)
}
} else {
useAPI = true

database
.prepare(
'insert into MakeModelSearchHistory' +
' (searchString, resultCount, searchExpiryMillis)' +
' values (?, ?, ?)'
)
.run(makeSearchString, 0, nowMillis + nhtsaSearchExpiryDurationMillis)
}

if (useAPI) {
const data = await GetModelsForMake(makeSearchString)
const data = await nhtsaGetModelsForMake.GetModelsForMake(makeSearchString)

database
.prepare(
'update MakeModelSearchHistory' +
' set resultCount = ?' +
'where searchString = ?'
`update MakeModelSearchHistory
set resultCount = ?
where searchString = ?`
)
.run(data.Count, makeSearchString)

const insertSQL =
'insert or ignore into MakeModel (makeID, makeName, modelID, modelName,' +
' recordCreate_timeMillis, recordUpdate_timeMillis)' +
' values (?, ?, ?, ?, ?, ?)'
const insertSQL = `insert or ignore into MakeModel (
makeID, makeName, modelID, modelName,
recordCreate_timeMillis, recordUpdate_timeMillis)
values (?, ?, ?, ?, ?, ?)`

const updateSQL =
'update MakeModel' +
' set recordUpdate_timeMillis = ?' +
' where makeName = ?' +
' and modelName = ?'
const updateSQL = `update MakeModel
set recordUpdate_timeMillis = ?
where makeName = ?
and modelName = ?`

for (const record of data.Results) {
const info = database
Expand Down
Loading

0 comments on commit b2d232e

Please sign in to comment.