Skip to content

Commit

Permalink
more async, dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Nov 28, 2023
1 parent 514eb3a commit 23e4572
Show file tree
Hide file tree
Showing 37 changed files with 282 additions and 288 deletions.
2 changes: 1 addition & 1 deletion database/addUser.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import sqlite from 'better-sqlite3';
import type sqlite from 'better-sqlite3';
export declare function addUser(user: EmileUser, sessionUser: EmileUser, connectedEmileDB?: sqlite.Database): Promise<boolean>;
5 changes: 2 additions & 3 deletions database/addUser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sqlite from 'better-sqlite3';
import { databasePath } from '../helpers/functions.database.js';
import { getConnectionWhenAvailable } from '../helpers/functions.database.js';
export async function addUser(user, sessionUser, connectedEmileDB) {
const emileDB = connectedEmileDB ?? sqlite(databasePath);
const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable());
const rightNowMillis = Date.now();
const recordDeleteTimeMillis = emileDB
.prepare('select recordDelete_timeMillis from Users where userName = ?')
Expand Down
6 changes: 3 additions & 3 deletions database/addUser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sqlite from 'better-sqlite3'
import type sqlite from 'better-sqlite3'

import { databasePath } from '../helpers/functions.database.js'
import { getConnectionWhenAvailable } from '../helpers/functions.database.js'

export async function addUser(
user: EmileUser,
sessionUser: EmileUser,
connectedEmileDB?: sqlite.Database
): Promise<boolean> {
const emileDB = connectedEmileDB ?? sqlite(databasePath)
const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable())

const rightNowMillis = Date.now()

Expand Down
2 changes: 1 addition & 1 deletion database/getAssetCategories.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import type { AssetCategory } from '../types/recordTypes.js';
export declare function getAssetCategories(): AssetCategory[];
export declare function getAssetCategories(): Promise<AssetCategory[]>;
7 changes: 3 additions & 4 deletions database/getAssetCategories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sqlite from 'better-sqlite3';
import { databasePath } from '../helpers/functions.database.js';
export function getAssetCategories() {
const emileDB = sqlite(databasePath);
import { getConnectionWhenAvailable } from '../helpers/functions.database.js';
export async function getAssetCategories() {
const emileDB = await getConnectionWhenAvailable();
const assetCategories = emileDB
.prepare(`select categoryId, category, fontAwesomeIconClasses, orderNumber
from AssetCategories
Expand Down
8 changes: 3 additions & 5 deletions database/getAssetCategories.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sqlite from 'better-sqlite3'

import { databasePath } from '../helpers/functions.database.js'
import { getConnectionWhenAvailable } from '../helpers/functions.database.js'
import type { AssetCategory } from '../types/recordTypes.js'

export function getAssetCategories(): AssetCategory[] {
const emileDB = sqlite(databasePath)
export async function getAssetCategories(): Promise<AssetCategory[]> {
const emileDB = await getConnectionWhenAvailable()

const assetCategories = emileDB
.prepare(
Expand Down
2 changes: 1 addition & 1 deletion database/getEnergyData.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sqlite from 'better-sqlite3';
import type sqlite from 'better-sqlite3';
import type { EnergyData } from '../types/recordTypes.js';
interface GetEnergyDataFilters {
assetId?: number | string;
Expand Down
8 changes: 2 additions & 6 deletions database/getEnergyData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { powerOfTenMultipliers } from '@cityssm/green-button-parser/lookups.js';
import { dateStringToDate } from '@cityssm/utils-datetime';
import sqlite from 'better-sqlite3';
import { databasePath, getConnectionWhenAvailable, getTempTableName } from '../helpers/functions.database.js';
import { getConnectionWhenAvailable, getTempTableName } from '../helpers/functions.database.js';
import { ensureEnergyDataTablesExists } from './manageEnergyDataTables.js';
export function userFunction_getPowerOfTenMultiplierName(powerOfTenMultiplier) {
if (powerOfTenMultiplier === 0) {
Expand Down Expand Up @@ -160,10 +159,7 @@ export async function getEnergyData(filters, options) {
return data;
}
export async function getEnergyDataPoint(filters, connectedEmileDB) {
const emileDB = connectedEmileDB ??
sqlite(databasePath, {
readonly: true
});
const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable(true));
const tableNames = await ensureEnergyDataTablesExists(filters.assetId, emileDB);
const dataPoint = emileDB
.prepare(`select dataId, assetId, dataTypeId, fileId,
Expand Down
9 changes: 2 additions & 7 deletions database/getEnergyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import { powerOfTenMultipliers } from '@cityssm/green-button-parser/lookups.js'
import { dateStringToDate } from '@cityssm/utils-datetime'
import sqlite from 'better-sqlite3'
import type sqlite from 'better-sqlite3'

import {
databasePath,
getConnectionWhenAvailable,
getTempTableName
} from '../helpers/functions.database.js'
Expand Down Expand Up @@ -252,11 +251,7 @@ export async function getEnergyDataPoint(
},
connectedEmileDB?: sqlite.Database
): Promise<EnergyData | undefined> {
const emileDB =
connectedEmileDB ??
sqlite(databasePath, {
readonly: true
})
const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable(true))

const tableNames = await ensureEnergyDataTablesExists(
filters.assetId,
Expand Down
4 changes: 2 additions & 2 deletions database/getEnergyDataType.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sqlite from 'better-sqlite3';
import type sqlite from 'better-sqlite3';
import type { EnergyDataType } from '../types/recordTypes.js';
export declare function getEnergyDataType(dataTypeId: number | string, connectedEmileDB?: sqlite.Database): EnergyDataType | undefined;
export declare function getEnergyDataType(dataTypeId: number | string, connectedEmileDB?: sqlite.Database): Promise<EnergyDataType | undefined>;
interface EnergyDataTypeGreenButtonIds {
serviceCategoryId: string;
unitId: string;
Expand Down
14 changes: 5 additions & 9 deletions database/getEnergyDataType.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { lookups as greenButtonLookups } from '@cityssm/green-button-parser';
import sqlite from 'better-sqlite3';
import NodeCache from 'node-cache';
import { databasePath, getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { addEnergyDataType } from './addEnergyDataType.js';
import { addEnergyUnit } from './addEnergyUnit.js';
import { getEnergyAccumulationBehaviour } from './getEnergyAccumulationBehaviour.js';
import { getEnergyCommodity } from './getEnergyCommodity.js';
import { getEnergyReadingType } from './getEnergyReadingType.js';
import { getEnergyServiceCategory } from './getEnergyServiceCategory.js';
import { getEnergyUnit } from './getEnergyUnit.js';
export function getEnergyDataType(dataTypeId, connectedEmileDB) {
const emileDB = connectedEmileDB ??
sqlite(databasePath, {
readonly: true
});
export async function getEnergyDataType(dataTypeId, connectedEmileDB) {
const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable(true));
const energyDataType = emileDB
.prepare(`select t.dataTypeId,
t.serviceCategoryId, s.serviceCategory,
Expand Down Expand Up @@ -181,7 +177,7 @@ export async function getEnergyDataTypeByGreenButtonIds(greenButtonIds, sessionU
commodityId: relatedIds.commodityId,
accumulationBehaviourId: relatedIds.accumulationBehaviourId
}, sessionUser, emileDB);
energyDataType = getEnergyDataType(dataTypeId, emileDB);
energyDataType = await getEnergyDataType(dataTypeId, emileDB);
}
catch {
return undefined;
Expand Down Expand Up @@ -256,7 +252,7 @@ export async function getEnergyDataTypeByNames(names, sessionUser, createIfUnava
commodityId: relatedIds.commodityId,
accumulationBehaviourId: relatedIds.accumulationBehaviourId
}, sessionUser, emileDB);
energyDataType = getEnergyDataType(dataTypeId, emileDB);
energyDataType = await getEnergyDataType(dataTypeId, emileDB);
}
catch {
return undefined;
Expand Down
21 changes: 7 additions & 14 deletions database/getEnergyDataType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
/* eslint-disable @typescript-eslint/indent */

import { lookups as greenButtonLookups } from '@cityssm/green-button-parser'
import sqlite from 'better-sqlite3'
import type sqlite from 'better-sqlite3'
import NodeCache from 'node-cache'

import {
databasePath,
getConnectionWhenAvailable
} from '../helpers/functions.database.js'
import { getConnectionWhenAvailable } from '../helpers/functions.database.js'
import type { EnergyDataType } from '../types/recordTypes.js'

import { addEnergyDataType } from './addEnergyDataType.js'
Expand All @@ -19,15 +16,11 @@ import { getEnergyReadingType } from './getEnergyReadingType.js'
import { getEnergyServiceCategory } from './getEnergyServiceCategory.js'
import { getEnergyUnit } from './getEnergyUnit.js'

export function getEnergyDataType(
export async function getEnergyDataType(
dataTypeId: number | string,
connectedEmileDB?: sqlite.Database
): EnergyDataType | undefined {
const emileDB =
connectedEmileDB ??
sqlite(databasePath, {
readonly: true
})
): Promise<EnergyDataType | undefined> {
const emileDB = connectedEmileDB ?? (await getConnectionWhenAvailable(true))

const energyDataType = emileDB
.prepare(
Expand Down Expand Up @@ -370,7 +363,7 @@ export async function getEnergyDataTypeByGreenButtonIds(
emileDB
)

energyDataType = getEnergyDataType(dataTypeId, emileDB)
energyDataType = await getEnergyDataType(dataTypeId, emileDB)
} catch {
return undefined
} finally {
Expand Down Expand Up @@ -471,7 +464,7 @@ export async function getEnergyDataTypeByNames(
emileDB
)

energyDataType = getEnergyDataType(dataTypeId, emileDB)
energyDataType = await getEnergyDataType(dataTypeId, emileDB)
} catch {
return undefined
} finally {
Expand Down
2 changes: 1 addition & 1 deletion database/initializeDatabase.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import sqlite from 'better-sqlite3';
import type sqlite from 'better-sqlite3';
export declare const recordColumns = " recordCreate_userName varchar(30) not null,\n recordCreate_timeMillis integer not null,\n recordUpdate_userName varchar(30) not null,\n recordUpdate_timeMillis integer not null,\n recordDelete_userName varchar(30),\n recordDelete_timeMillis integer";
export declare function initializeDatabase(connectedEmileDB?: sqlite.Database): Promise<void>;
5 changes: 2 additions & 3 deletions database/initializeDatabase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { lookups as greenButtonLookups } from '@cityssm/green-button-parser';
import sqlite from 'better-sqlite3';
import Debug from 'debug';
import { databasePath } from '../helpers/functions.database.js';
import { databasePath, getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { addAssetAliasType } from './addAssetAliasType.js';
import { addAssetCategory } from './addAssetCategory.js';
import { addEnergyAccumulationBehaviour } from './addEnergyAccumulationBehaviour.js';
Expand Down Expand Up @@ -220,7 +219,7 @@ async function initializeAssetAliasTypes(emileDB) {
}
}
export async function initializeDatabase(connectedEmileDB) {
const emileDB = connectedEmileDB ?? sqlite(databasePath);
const emileDB = connectedEmileDB ?? await getConnectionWhenAvailable();
const row = emileDB
.prepare(`select name from sqlite_master
where type = 'table'
Expand Down
6 changes: 3 additions & 3 deletions database/initializeDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { lookups as greenButtonLookups } from '@cityssm/green-button-parser'
import sqlite from 'better-sqlite3'
import type sqlite from 'better-sqlite3'
import Debug from 'debug'

import { databasePath } from '../helpers/functions.database.js'
import { databasePath, getConnectionWhenAvailable } from '../helpers/functions.database.js'

import { addAssetAliasType } from './addAssetAliasType.js'
import { addAssetCategory } from './addAssetCategory.js'
Expand Down Expand Up @@ -358,7 +358,7 @@ async function initializeAssetAliasTypes(
export async function initializeDatabase(
connectedEmileDB?: sqlite.Database
): Promise<void> {
const emileDB = connectedEmileDB ?? sqlite(databasePath)
const emileDB = connectedEmileDB ?? await getConnectionWhenAvailable()

const row = emileDB
.prepare(
Expand Down
15 changes: 7 additions & 8 deletions database/moveRecord.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import sqlite from 'better-sqlite3';
import { clearCacheByTableName } from '../helpers/functions.cache.js';
import { databasePath, getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { getConnectionWhenAvailable } from '../helpers/functions.database.js';
import { updateRecordOrderNumber } from './updateRecordOrderNumber.js';
const recordIdColumns = new Map();
recordIdColumns.set('AssetCategories', 'categoryId');
function getCurrentOrderNumber(recordTable, recordId, database) {
const currentOrderNumber = database
return database
.prepare(`select orderNumber
from ${recordTable}
where ${recordIdColumns.get(recordTable)} = ?`)
.get(recordId).orderNumber;
return currentOrderNumber;
from ${recordTable}
where ${recordIdColumns.get(recordTable)} = ?`)
.pluck()
.get(recordId);
}
export async function moveRecordDown(recordTable, recordId) {
const emileDB = await getConnectionWhenAvailable();
Expand Down Expand Up @@ -48,7 +47,7 @@ export async function moveRecordDownToBottom(recordTable, recordId) {
return true;
}
export async function moveRecordUp(recordTable, recordId) {
const emileDB = sqlite(databasePath);
const emileDB = await getConnectionWhenAvailable();
const currentOrderNumber = getCurrentOrderNumber(recordTable, recordId, emileDB);
if (currentOrderNumber <= 0) {
emileDB.close();
Expand Down
28 changes: 11 additions & 17 deletions database/moveRecord.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import sqlite from 'better-sqlite3'
import type sqlite from 'better-sqlite3'

import { clearCacheByTableName } from '../helpers/functions.cache.js'
import {
databasePath,
getConnectionWhenAvailable
} from '../helpers/functions.database.js'
import { getConnectionWhenAvailable } from '../helpers/functions.database.js'

import { updateRecordOrderNumber } from './updateRecordOrderNumber.js'

Expand All @@ -18,17 +15,14 @@ function getCurrentOrderNumber(
recordId: number | string,
database: sqlite.Database
): number {
const currentOrderNumber: number = (
database
.prepare(
`select orderNumber
from ${recordTable}
where ${recordIdColumns.get(recordTable) as string} = ?`
)
.get(recordId) as { orderNumber: number }
).orderNumber

return currentOrderNumber
return database
.prepare(
`select orderNumber
from ${recordTable}
where ${recordIdColumns.get(recordTable) as string} = ?`
)
.pluck()
.get(recordId) as number
}

export async function moveRecordDown(
Expand Down Expand Up @@ -112,7 +106,7 @@ export async function moveRecordUp(
recordTable: RecordTable,
recordId: number
): Promise<boolean> {
const emileDB = sqlite(databasePath)
const emileDB = await getConnectionWhenAvailable()

const currentOrderNumber = getCurrentOrderNumber(
recordTable,
Expand Down
4 changes: 2 additions & 2 deletions database/updateAsset.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sqlite from 'better-sqlite3';
import type sqlite from 'better-sqlite3';
import type { Asset } from '../types/recordTypes.js';
export declare function updateAsset(asset: Asset, sessionUser: EmileUser): boolean;
export declare function updateAsset(asset: Asset, sessionUser: EmileUser): Promise<boolean>;
export declare function updateAssetTimeSeconds(assetId: number | string, connectedEmileDB?: sqlite.Database): Promise<boolean>;
export declare function updateAllAssetTimeSeconds(): Promise<void>;
7 changes: 3 additions & 4 deletions database/updateAsset.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import sqlite from 'better-sqlite3';
import { databasePath, getConnectionWhenAvailable, getTempTableName, queryMaxRetryCount } from '../helpers/functions.database.js';
import { getConnectionWhenAvailable, getTempTableName, queryMaxRetryCount } from '../helpers/functions.database.js';
import { delay } from '../helpers/functions.utilities.js';
import { getAssets } from './getAssets.js';
import { ensureEnergyDataTablesExists } from './manageEnergyDataTables.js';
export function updateAsset(asset, sessionUser) {
const emileDB = sqlite(databasePath);
export async function updateAsset(asset, sessionUser) {
const emileDB = await getConnectionWhenAvailable();
const result = emileDB
.prepare(`update Assets
set assetName = ?,
Expand Down
10 changes: 6 additions & 4 deletions database/updateAsset.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/indent */

import sqlite from 'better-sqlite3'
import type sqlite from 'better-sqlite3'

import {
databasePath,
getConnectionWhenAvailable,
getTempTableName,
queryMaxRetryCount
Expand All @@ -15,8 +14,11 @@ import type { Asset } from '../types/recordTypes.js'
import { getAssets } from './getAssets.js'
import { ensureEnergyDataTablesExists } from './manageEnergyDataTables.js'

export function updateAsset(asset: Asset, sessionUser: EmileUser): boolean {
const emileDB = sqlite(databasePath)
export async function updateAsset(
asset: Asset,
sessionUser: EmileUser
): Promise<boolean> {
const emileDB = await getConnectionWhenAvailable()

const result = emileDB
.prepare(
Expand Down
2 changes: 1 addition & 1 deletion database/updateAssetGroup.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import type { AssetGroup } from '../types/recordTypes.js';
export declare function updateAssetGroup(group: AssetGroup, sessionUser: EmileUser): boolean;
export declare function updateAssetGroup(group: AssetGroup, sessionUser: EmileUser): Promise<boolean>;
Loading

0 comments on commit 23e4572

Please sign in to comment.