Skip to content

Commit

Permalink
Fix up use of db getters and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Cassidy <[email protected]>
  • Loading branch information
stevecassidy committed Nov 8, 2023
1 parent bc06878 commit bbbfd37
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 105 deletions.
12 changes: 9 additions & 3 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,18 @@ api.get(

// export current versions of all records in this notebook as csv
api.get(
'/notebooks/:id/:viewid.csv',
'/notebooks/:id/:viewID.csv',
requireAuthenticationAPI,
async (req, res) => {
if (req.user && userHasPermission(req.user, req.params.id, 'read')) {
res.setHeader('Content-Type', 'text/csv');
streamNotebookRecordsAsCSV(req.params.id, req.params.viewid, res);
try {
res.setHeader('Content-Type', 'text/csv');
streamNotebookRecordsAsCSV(req.params.id, req.params.viewID, res);
} catch (err) {
console.log('Error streaming CSV', err);
res.json({error: 'error creating CSV'});
res.status(500).end();
}
} else {
res.json({error: 'notebook not found'});
res.status(404).end();
Expand Down
4 changes: 2 additions & 2 deletions src/buildconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function is_testing() {

function couchdb_internal_url(): string {
let couchdb = process.env.COUCHDB_INTERNAL_URL;
const couchdbDefault = 'http://localhost:5984/';
const couchdbDefault = 'http://localhost:5984';
if (couchdb === '' || couchdb === undefined) {
console.log('COUCHDB_INTERNAL_URL not set, using default');
return couchdbDefault;
Expand All @@ -103,7 +103,7 @@ function couchdb_internal_url(): string {

function couchdb_public_url(): string {
let couchdb = process.env.COUCHDB_PUBLIC_URL;
const couchdbDefault = 'http://localhost:5984/';
const couchdbDefault = 'http://localhost:5984';
if (couchdb === '' || couchdb === undefined) {
console.log('COUCHDB_PUBLIC_URL not set, using default');
return couchdbDefault;
Expand Down
28 changes: 7 additions & 21 deletions src/couchdb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const getDirectoryDB = (): PouchDB.Database | undefined => {
if (!_directoryDB) {
const pouch_options = pouchOptions();

const directorydb = COUCHDB_INTERNAL_URL + DIRECTORY_DB_NAME;
const directorydb = COUCHDB_INTERNAL_URL + '/' + DIRECTORY_DB_NAME;
try {
_directoryDB = new PouchDB(directorydb, pouch_options);
} catch (error) {
Expand All @@ -82,7 +82,7 @@ export const getUsersDB = (): PouchDB.Database | undefined => {
if (!_usersDB) {
const pouch_options = pouchOptions();

const dbName = COUCHDB_INTERNAL_URL + PEOPLE_DB_NAME;
const dbName = COUCHDB_INTERNAL_URL + '/' + PEOPLE_DB_NAME;
_usersDB = new PouchDB(dbName, pouch_options);
}

Expand All @@ -92,7 +92,7 @@ export const getUsersDB = (): PouchDB.Database | undefined => {
export const getProjectsDB = (): PouchDB.Database | undefined => {
if (!_projectsDB) {
const pouch_options = pouchOptions();
const dbName = COUCHDB_INTERNAL_URL + PROJECTS_DB_NAME;
const dbName = COUCHDB_INTERNAL_URL + '/' + PROJECTS_DB_NAME;
try {
_projectsDB = new PouchDB(dbName, pouch_options);
} catch (error) {
Expand All @@ -105,7 +105,7 @@ export const getProjectsDB = (): PouchDB.Database | undefined => {
export const getInvitesDB = (): PouchDB.Database | undefined => {
if (!_invitesDB) {
const pouch_options = pouchOptions();
const dbName = COUCHDB_INTERNAL_URL + INVITE_DB_NAME;
const dbName = COUCHDB_INTERNAL_URL + '/' + INVITE_DB_NAME;
try {
_invitesDB = new PouchDB(dbName, pouch_options);
} catch (error) {
Expand All @@ -115,21 +115,6 @@ export const getInvitesDB = (): PouchDB.Database | undefined => {
return _invitesDB;
};

export const createProjectDB = (
dbName: string
): PouchDB.Database | undefined => {
const pouch_options = pouchOptions();

try {
const db = new PouchDB(COUCHDB_INTERNAL_URL + dbName, pouch_options);
return db;
} catch (error) {
console.error('error creating project database');
console.error(error);
}
return undefined;
};

export const getProjectMetaDB = async (
projectID: ProjectID
): Promise<PouchDB.Database | undefined> => {
Expand All @@ -140,7 +125,8 @@ export const getProjectMetaDB = async (
projectID
)) as unknown as ProjectObject;
if (projectDoc.metadata_db) {
const dbname = COUCHDB_INTERNAL_URL + projectDoc.metadata_db.db_name;
const dbname =
COUCHDB_INTERNAL_URL + '/' + projectDoc.metadata_db.db_name;
const pouch_options = pouchOptions();

if (LOCAL_COUCHDB_AUTH !== undefined) {
Expand All @@ -166,7 +152,7 @@ export const getProjectDataDB = async (
projectID
)) as unknown as ProjectObject;
if (projectDoc.data_db) {
const dbname = COUCHDB_INTERNAL_URL + projectDoc.data_db.db_name;
const dbname = COUCHDB_INTERNAL_URL + '/' + projectDoc.data_db.db_name;
const pouch_options = pouchOptions();

if (LOCAL_COUCHDB_AUTH !== undefined) {
Expand Down
Loading

0 comments on commit bbbfd37

Please sign in to comment.