-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from DigitalCommons/20_backend_class_structure
[CWM] Create back-end class structure (re-merge)
- Loading branch information
Showing
8 changed files
with
538 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
name: docs | ||
on: | ||
push: | ||
paths: | ||
- "docs/**" | ||
branches: | ||
- main | ||
permissions: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { TsRestResponseError } from "@ts-rest/core"; | ||
|
||
import { contract } from "@mykomap/common"; | ||
|
||
export class Dataset { | ||
id: string; | ||
folderPath: string; | ||
searchable: ({ [prop: string]: string } & { searchString: string })[]; | ||
|
||
constructor(id: string, dataRoot: string) { | ||
this.id = id; | ||
this.folderPath = path.join(dataRoot, "datasets", id); | ||
this.searchable = JSON.parse( | ||
fs.readFileSync(path.join(this.folderPath, "searchable.json"), "utf8"), | ||
); | ||
} | ||
|
||
getItem = (itemId: number) => { | ||
if ( | ||
!fs.existsSync( | ||
path.join(this.folderPath, "initiatives", `${itemId}.json`), | ||
) | ||
) { | ||
throw new TsRestResponseError(contract.getDatasetItem, { | ||
status: 404, | ||
body: { | ||
message: `can't retrieve data for dataset ${this.id} item ${itemId}`, | ||
}, | ||
}); | ||
} | ||
|
||
return JSON.parse( | ||
fs.readFileSync( | ||
path.join(this.folderPath, "initiatives", `${itemId}.json`), | ||
"utf8", | ||
), | ||
); | ||
}; | ||
|
||
getConfig = () => { | ||
// TODO: implementation | ||
return {}; | ||
}; | ||
|
||
getLocations = (): fs.ReadStream => | ||
fs.createReadStream(path.join(this.folderPath, "locations.json"), "utf8"); | ||
|
||
search = (filter?: string[], text?: string): number[] => { | ||
// TODO: implementation | ||
return []; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { TsRestResponseError } from "@ts-rest/core"; | ||
|
||
import { contract } from "@mykomap/common"; | ||
import { Dataset } from "./Dataset.js"; | ||
|
||
const datasets: { [id: string]: Dataset } = {}; | ||
|
||
/** | ||
* This method instantiates a Dataset object for each of the datasets in the dataRoot/datasets | ||
* directory in the filesystem. | ||
*/ | ||
export const initDatasets = (dataRoot: string) => { | ||
const datasetIds = fs | ||
.readdirSync(path.join(dataRoot, "datasets"), { withFileTypes: true }) | ||
.filter((f) => f.isDirectory()) | ||
.map((f) => f.name); | ||
|
||
console.log("Found datasets:", datasetIds); | ||
|
||
for (const datasetId of datasetIds) { | ||
datasets[datasetId] = new Dataset(datasetId, dataRoot); | ||
} | ||
}; | ||
|
||
const getDatasetOrThrow404 = (datasetId: string): Dataset => { | ||
const dataset = datasets[datasetId]; | ||
|
||
if (!dataset) | ||
throw new TsRestResponseError(contract.searchDataset, { | ||
status: 404, | ||
body: { message: `dataset ${datasetId} doesn't exist` }, | ||
}); | ||
|
||
return dataset; | ||
}; | ||
|
||
export const getDatasetItem = (datasetId: string, datasetItemId: number) => { | ||
const dataset = getDatasetOrThrow404(datasetId); | ||
return dataset.getItem(datasetItemId); | ||
}; | ||
|
||
export const getDatasetConfig = (datasetId: string) => { | ||
const dataset = getDatasetOrThrow404(datasetId); | ||
return dataset.getConfig(); | ||
}; | ||
|
||
export const getDatasetLocations = (datasetId: string): fs.ReadStream => { | ||
const dataset = getDatasetOrThrow404(datasetId); | ||
return dataset.getLocations(); | ||
}; | ||
|
||
export const searchDataset = ( | ||
datasetId: string, | ||
filter?: string[], | ||
text?: string, | ||
): number[] => { | ||
const dataset = getDatasetOrThrow404(datasetId); | ||
return dataset.search(filter, text); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.