more tables, table indexes

deepsource-autofix-76c6eb20
dangowans 2022-07-12 14:21:15 -04:00
parent ed5d7b8d4b
commit 2ddd02d40a
5 changed files with 294 additions and 9 deletions

2
app.js
View File

@ -20,7 +20,7 @@ import { version } from "./version.js";
import * as databaseInitializer from "./helpers/databaseInitializer.js"; import * as databaseInitializer from "./helpers/databaseInitializer.js";
import debug from "debug"; import debug from "debug";
const debugApp = debug("general-licence-manager:app"); const debugApp = debug("general-licence-manager:app");
databaseInitializer.initLicencesDB(); databaseInitializer.initLotOccupancyDB();
const __dirname = "."; const __dirname = ".";
export const app = express(); export const app = express();
if (!configFunctions.getProperty("reverseProxy.disableEtag")) { if (!configFunctions.getProperty("reverseProxy.disableEtag")) {

2
app.ts
View File

@ -33,7 +33,7 @@ const debugApp = debug("general-licence-manager:app");
*/ */
databaseInitializer.initLicencesDB(); databaseInitializer.initLotOccupancyDB();
/* /*

View File

@ -1 +1 @@
export declare const initLotsDB: () => boolean; export declare const initLotOccupancyDB: () => boolean;

View File

@ -8,7 +8,7 @@ const recordColumns = " recordCreate_userName varchar(30) not null," +
" recordUpdate_timeMillis integer not null," + " recordUpdate_timeMillis integer not null," +
" recordDelete_userName varchar(30)," + " recordDelete_userName varchar(30)," +
" recordDelete_timeMillis integer"; " recordDelete_timeMillis integer";
export const initLotsDB = () => { export const initLotOccupancyDB = () => {
const lotOccupancyDB = sqlite(databasePath); const lotOccupancyDB = sqlite(databasePath);
const row = lotOccupancyDB const row = lotOccupancyDB
.prepare("select name from sqlite_master where type = 'table' and name = 'Lots'") .prepare("select name from sqlite_master where type = 'table' and name = 'Lots'")
@ -23,6 +23,7 @@ export const initLotsDB = () => {
" orderNumber smallint not null default 0," + " orderNumber smallint not null default 0," +
recordColumns + recordColumns +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_contacttypes_ordernumber (orderNumber, contactType)").run();
lotOccupancyDB.prepare("create table if not exists Contacts (" + lotOccupancyDB.prepare("create table if not exists Contacts (" +
"contactId integer not null primary key autoincrement," + "contactId integer not null primary key autoincrement," +
" contactTypeId integer not null," + " contactTypeId integer not null," +
@ -45,6 +46,7 @@ export const initLotsDB = () => {
" orderNumber smallint not null default 0," + " orderNumber smallint not null default 0," +
recordColumns + recordColumns +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypes_ordernumber (orderNumber, lotType)").run();
lotOccupancyDB.prepare("create table if not exists LotTypeFields (" + lotOccupancyDB.prepare("create table if not exists LotTypeFields (" +
"lotTypeFieldId integer not null primary key autoincrement," + "lotTypeFieldId integer not null primary key autoincrement," +
" lotTypeId integer not null," + " lotTypeId integer not null," +
@ -58,6 +60,7 @@ export const initLotsDB = () => {
recordColumns + "," + recordColumns + "," +
" foreign key (lotTypeId) references LotTypes (lotTypeId)" + " foreign key (lotTypeId) references LotTypes (lotTypeId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypefields_ordernumber (lotTypeId, orderNumber, lotTypeField)").run();
lotOccupancyDB.prepare("create table if not exists LotTypeStatuses (" + lotOccupancyDB.prepare("create table if not exists LotTypeStatuses (" +
"lotTypeStatusId integer not null primary key autoincrement," + "lotTypeStatusId integer not null primary key autoincrement," +
" lotTypeId integer not null," + " lotTypeId integer not null," +
@ -66,6 +69,7 @@ export const initLotsDB = () => {
recordColumns + "," + recordColumns + "," +
" foreign key (lotTypeId) references LotTypes (lotTypeId)" + " foreign key (lotTypeId) references LotTypes (lotTypeId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypestatuses_ordernumber (lotTypeId, orderNumber, lotTypeStatus)").run();
lotOccupancyDB.prepare("create table if not exists Lots (" + lotOccupancyDB.prepare("create table if not exists Lots (" +
"lotId integer not null primary key autoincrement," + "lotId integer not null primary key autoincrement," +
" lotTypeId integer not null," + " lotTypeId integer not null," +
@ -79,15 +83,139 @@ export const initLotsDB = () => {
" foreign key (lotContactId) references Contacts (contactId)," + " foreign key (lotContactId) references Contacts (contactId)," +
" foreign key (lotTypeStatusId) references LotTypeStatuses (lotTypeStatusId)" + " foreign key (lotTypeStatusId) references LotTypeStatuses (lotTypeStatusId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create table if not exists LotFields (" +
"lotId integer not null," +
" lotTypeFieldId integer not null," +
" lotFieldValue text not null," +
recordColumns + "," +
" primary key (lotId, lotTypeFieldId)," +
" foreign key (lotId) references Lots (lotId)," +
" foreign key (lotTypeFieldId) references LotTypeFields (lotTypeFieldId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists LotComments (" + lotOccupancyDB.prepare("create table if not exists LotComments (" +
"lotCommentId integer not null primary key autoincrement," + "lotCommentId integer not null primary key autoincrement," +
" lotId integer not null," + " lotId integer not null," +
" lotCommentDate integer not null," + " lotCommentDate integer not null check (lotCommentDate > 0)," +
" lotCommentTime integer not null," + " lotCommentTime integer not null check (lotCommentTime >= 0)," +
" lotComment text not null," + " lotComment text not null," +
recordColumns + "," + recordColumns + "," +
" foreign key (lotId) references Lots (lotId)" + " foreign key (lotId) references Lots (lotId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lotcomments_datetime (lotId, lotCommentDate, lotCommentTime)").run();
lotOccupancyDB.prepare("create table if not exists OccupancyTypes (" +
"occupancyTypeId integer not null primary key autoincrement," +
" occupancyType varchar(100) not null," +
" orderNumber smallint not null default 0," +
recordColumns +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_occupancytypes_ordernumber (orderNumber, occupancyType)").run();
lotOccupancyDB.prepare("create table if not exists OccupancyTypeFields (" +
"occupancyTypeFieldId integer not null primary key autoincrement," +
" occupancyTypeId integer not null," +
" occupancyTypeField varchar(100) not null," +
" occupancyTypeFieldValues text," +
" isRequired bit not null default 0," +
" pattern varchar(100)," +
" minimumLength smallint not null default 1 check (minimumLength >= 0)," +
" maximumLength smallint not null default 100 check (maximumLength >= 0)," +
" orderNumber smallint not null default 0," +
recordColumns + "," +
" foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_occupancytypefields_ordernumber (occupancyTypeId, orderNumber, occupancyTypeField)").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancies (" +
"lotOccupancyId integer not null primary key autoincrement," +
" occupancyTypeId integer not null," +
" lotId integer not null," +
" occupantContactId integer," +
" occupancyStartDate integer not null check (occupancyStartDate > 0)," +
" occupancyEndDate integer check (occupancyEndDate > 0)," +
recordColumns + "," +
" foreign key (lotId) references Lots (lotId)," +
" foreign key (occupantContactId) references Contacts (contactId)" +
")").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyFields (" +
"lotOccupancyId integer not null," +
" occupancyTypeFieldId integer not null," +
" lotOccupancyFieldValue text not null," +
recordColumns + "," +
" primary key (lotOccupancyId, occupancyTypeFieldId)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," +
" foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists Fees (" +
"feeId integer not null primary key autoincrement," +
" feeName varchar(100) not null," +
" contactTypeId integer," +
" contactId integer," +
" lotTypeId integer," +
" feeAmount decimal(6, 2)," +
" feeFunction varchar(100)," +
" isRequired bit not null default 0," +
" orderNumber smallint not null default 0," +
recordColumns +
" foreign key (contactTypeId) references ContactTypes (contactTypeId)," +
" foreign key (contactId) references Contacts (contactId)," +
" foreign key (lotTypeId) references LotTypes (lotTypeId)" +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_fees_ordernumber (orderNumber, feeName)").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyFees (" +
"lotOccupancyId integer not null," +
" feeId integer not null," +
" feeAmount decimal(6, 2) not null," +
recordColumns + "," +
" primary key (lotOccupancyId, feeId)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," +
" foreign key (feeId) references Fees (feeId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyTransactions (" +
"lotOccupancyId integer not null," +
" transactionIndex integer not null," +
" transactionDate integer not null check (transactionDate > 0)," +
" transactionTime integer not null check (transactionTime >= 0)," +
" transactionAmount decimal(6, 2) not null," +
" externalReceiptNumber varchar(100)," +
" transactionNote text," +
recordColumns + "," +
" primary key (lotOccupancyId, transactionIndex)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)" +
") without rowid").run();
lotOccupancyDB.prepare("create index if not exists idx_lotoccupancytransactions_ordernumber (lotOccupancyId, transactionDate, transactionTime)").run();
lotOccupancyDB.prepare("create table if not exists WorkOrderTypes (" +
"workOrderTypeId integer not null primary key autoincrement," +
" workOrderType varchar(100) not null," +
" orderNumber smallint not null default 0," +
recordColumns +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_workordertypes_ordernumber (orderNumber, workOrderType)").run();
lotOccupancyDB.prepare("create table if not exists WorkOrders (" +
"workOrderId integer not null primary key autoincrement," +
" workOrderTypeId integer not null," +
" workOrderNumber varchar(50) not null," +
" workOrderDescription text," +
" workOrderOpenDate integer check (workOrderOpenDate > 0)," +
" workOrderCloseDate integer check (workOrderCloseDate > 0)," +
recordColumns + "," +
" foreign key (workOrderTypeId) references WorkOrderTypes (workOrderTypeId)" +
")").run();
lotOccupancyDB.prepare("create table if not exists WorkOrderLots (" +
"workOrderId integer not null," +
" lotId integer not null," +
recordColumns + "," +
" primary key (workOrderId, lotId)," +
" foreign key (workOrderId) references WorkOrders (workOrderId)," +
" foreign key (lotId) references Lots (lotId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists WorkOrderComments (" +
"workOrderCommentId integer not null primary key autoincrement," +
" workOrderId integer not null," +
" workOrderCommentDate integer not null check (workOrderCommentDate > 0)," +
" workOrderCommentTime integer not null check (workOrderCommentTime >= 0)," +
" workOrderComment text not null," +
recordColumns + "," +
" foreign key (workOrderId) references WorkOrders (workOrderId)" +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_workordercomments_datetime (workOrderId, workOrderCommentDate, workOrderCommentTime)").run();
lotOccupancyDB.close(); lotOccupancyDB.close();
return true; return true;
} }

View File

@ -13,7 +13,7 @@ const recordColumns = " recordCreate_userName varchar(30) not null," +
" recordDelete_userName varchar(30)," + " recordDelete_userName varchar(30)," +
" recordDelete_timeMillis integer"; " recordDelete_timeMillis integer";
export const initLotsDB = (): boolean => { export const initLotOccupancyDB = (): boolean => {
const lotOccupancyDB = sqlite(databasePath); const lotOccupancyDB = sqlite(databasePath);
@ -36,6 +36,8 @@ export const initLotsDB = (): boolean => {
recordColumns + recordColumns +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_contacttypes_ordernumber (orderNumber, contactType)").run();
lotOccupancyDB.prepare("create table if not exists Contacts (" + lotOccupancyDB.prepare("create table if not exists Contacts (" +
"contactId integer not null primary key autoincrement," + "contactId integer not null primary key autoincrement," +
" contactTypeId integer not null," + " contactTypeId integer not null," +
@ -65,6 +67,8 @@ export const initLotsDB = (): boolean => {
recordColumns + recordColumns +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypes_ordernumber (orderNumber, lotType)").run();
lotOccupancyDB.prepare("create table if not exists LotTypeFields (" + lotOccupancyDB.prepare("create table if not exists LotTypeFields (" +
"lotTypeFieldId integer not null primary key autoincrement," + "lotTypeFieldId integer not null primary key autoincrement," +
" lotTypeId integer not null," + " lotTypeId integer not null," +
@ -79,6 +83,8 @@ export const initLotsDB = (): boolean => {
" foreign key (lotTypeId) references LotTypes (lotTypeId)" + " foreign key (lotTypeId) references LotTypes (lotTypeId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypefields_ordernumber (lotTypeId, orderNumber, lotTypeField)").run();
lotOccupancyDB.prepare("create table if not exists LotTypeStatuses (" + lotOccupancyDB.prepare("create table if not exists LotTypeStatuses (" +
"lotTypeStatusId integer not null primary key autoincrement," + "lotTypeStatusId integer not null primary key autoincrement," +
" lotTypeId integer not null," + " lotTypeId integer not null," +
@ -88,6 +94,8 @@ export const initLotsDB = (): boolean => {
" foreign key (lotTypeId) references LotTypes (lotTypeId)" + " foreign key (lotTypeId) references LotTypes (lotTypeId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lottypestatuses_ordernumber (lotTypeId, orderNumber, lotTypeStatus)").run();
// Lots // Lots
lotOccupancyDB.prepare("create table if not exists Lots (" + lotOccupancyDB.prepare("create table if not exists Lots (" +
@ -107,16 +115,165 @@ export const initLotsDB = (): boolean => {
" foreign key (lotTypeStatusId) references LotTypeStatuses (lotTypeStatusId)" + " foreign key (lotTypeStatusId) references LotTypeStatuses (lotTypeStatusId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create table if not exists LotFields (" +
"lotId integer not null," +
" lotTypeFieldId integer not null," +
" lotFieldValue text not null," +
recordColumns + "," +
" primary key (lotId, lotTypeFieldId)," +
" foreign key (lotId) references Lots (lotId)," +
" foreign key (lotTypeFieldId) references LotTypeFields (lotTypeFieldId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists LotComments (" + lotOccupancyDB.prepare("create table if not exists LotComments (" +
"lotCommentId integer not null primary key autoincrement," + "lotCommentId integer not null primary key autoincrement," +
" lotId integer not null," + " lotId integer not null," +
" lotCommentDate integer not null," + " lotCommentDate integer not null check (lotCommentDate > 0)," +
" lotCommentTime integer not null," + " lotCommentTime integer not null check (lotCommentTime >= 0)," +
" lotComment text not null," + " lotComment text not null," +
recordColumns + "," + recordColumns + "," +
" foreign key (lotId) references Lots (lotId)" + " foreign key (lotId) references Lots (lotId)" +
")").run(); ")").run();
lotOccupancyDB.prepare("create index if not exists idx_lotcomments_datetime (lotId, lotCommentDate, lotCommentTime)").run();
// Occupancies
lotOccupancyDB.prepare("create table if not exists OccupancyTypes (" +
"occupancyTypeId integer not null primary key autoincrement," +
" occupancyType varchar(100) not null," +
" orderNumber smallint not null default 0," +
recordColumns +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_occupancytypes_ordernumber (orderNumber, occupancyType)").run();
lotOccupancyDB.prepare("create table if not exists OccupancyTypeFields (" +
"occupancyTypeFieldId integer not null primary key autoincrement," +
" occupancyTypeId integer not null," +
" occupancyTypeField varchar(100) not null," +
" occupancyTypeFieldValues text," +
" isRequired bit not null default 0," +
" pattern varchar(100)," +
" minimumLength smallint not null default 1 check (minimumLength >= 0)," +
" maximumLength smallint not null default 100 check (maximumLength >= 0)," +
" orderNumber smallint not null default 0," +
recordColumns + "," +
" foreign key (occupancyTypeId) references OccupancyTypes (occupancyTypeId)" +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_occupancytypefields_ordernumber (occupancyTypeId, orderNumber, occupancyTypeField)").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancies (" +
"lotOccupancyId integer not null primary key autoincrement," +
" occupancyTypeId integer not null," +
" lotId integer not null," +
" occupantContactId integer," +
" occupancyStartDate integer not null check (occupancyStartDate > 0)," +
" occupancyEndDate integer check (occupancyEndDate > 0)," +
recordColumns + "," +
" foreign key (lotId) references Lots (lotId)," +
" foreign key (occupantContactId) references Contacts (contactId)" +
")").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyFields (" +
"lotOccupancyId integer not null," +
" occupancyTypeFieldId integer not null," +
" lotOccupancyFieldValue text not null," +
recordColumns + "," +
" primary key (lotOccupancyId, occupancyTypeFieldId)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," +
" foreign key (occupancyTypeFieldId) references OccupancyTypeFields (occupancyTypeFieldId)" +
") without rowid").run();
// Occupancy Fees and Transactions
lotOccupancyDB.prepare("create table if not exists Fees (" +
"feeId integer not null primary key autoincrement," +
" feeName varchar(100) not null," +
" contactTypeId integer," +
" contactId integer," +
" lotTypeId integer," +
" feeAmount decimal(6, 2)," +
" feeFunction varchar(100)," +
" isRequired bit not null default 0," +
" orderNumber smallint not null default 0," +
recordColumns +
" foreign key (contactTypeId) references ContactTypes (contactTypeId)," +
" foreign key (contactId) references Contacts (contactId)," +
" foreign key (lotTypeId) references LotTypes (lotTypeId)" +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_fees_ordernumber (orderNumber, feeName)").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyFees (" +
"lotOccupancyId integer not null," +
" feeId integer not null," +
" feeAmount decimal(6, 2) not null," +
recordColumns + "," +
" primary key (lotOccupancyId, feeId)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)," +
" foreign key (feeId) references Fees (feeId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists LotOccupancyTransactions (" +
"lotOccupancyId integer not null," +
" transactionIndex integer not null," +
" transactionDate integer not null check (transactionDate > 0)," +
" transactionTime integer not null check (transactionTime >= 0)," +
" transactionAmount decimal(6, 2) not null," +
" externalReceiptNumber varchar(100)," +
" transactionNote text," +
recordColumns + "," +
" primary key (lotOccupancyId, transactionIndex)," +
" foreign key (lotOccupancyId) references LotOccupancies (lotOccupancyId)" +
") without rowid").run();
lotOccupancyDB.prepare("create index if not exists idx_lotoccupancytransactions_ordernumber (lotOccupancyId, transactionDate, transactionTime)").run();
// Work Orders
lotOccupancyDB.prepare("create table if not exists WorkOrderTypes (" +
"workOrderTypeId integer not null primary key autoincrement," +
" workOrderType varchar(100) not null," +
" orderNumber smallint not null default 0," +
recordColumns +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_workordertypes_ordernumber (orderNumber, workOrderType)").run();
lotOccupancyDB.prepare("create table if not exists WorkOrders (" +
"workOrderId integer not null primary key autoincrement," +
" workOrderTypeId integer not null," +
" workOrderNumber varchar(50) not null," +
" workOrderDescription text," +
" workOrderOpenDate integer check (workOrderOpenDate > 0)," +
" workOrderCloseDate integer check (workOrderCloseDate > 0)," +
recordColumns + "," +
" foreign key (workOrderTypeId) references WorkOrderTypes (workOrderTypeId)" +
")").run();
lotOccupancyDB.prepare("create table if not exists WorkOrderLots (" +
"workOrderId integer not null," +
" lotId integer not null," +
recordColumns + "," +
" primary key (workOrderId, lotId)," +
" foreign key (workOrderId) references WorkOrders (workOrderId)," +
" foreign key (lotId) references Lots (lotId)" +
") without rowid").run();
lotOccupancyDB.prepare("create table if not exists WorkOrderComments (" +
"workOrderCommentId integer not null primary key autoincrement," +
" workOrderId integer not null," +
" workOrderCommentDate integer not null check (workOrderCommentDate > 0)," +
" workOrderCommentTime integer not null check (workOrderCommentTime >= 0)," +
" workOrderComment text not null," +
recordColumns + "," +
" foreign key (workOrderId) references WorkOrders (workOrderId)" +
")").run();
lotOccupancyDB.prepare("create index if not exists idx_workordercomments_datetime (workOrderId, workOrderCommentDate, workOrderCommentTime)").run();
lotOccupancyDB.close(); lotOccupancyDB.close();
return true; return true;