Skip to content

Commit

Permalink
fix: added use of databse to fix typing plus some minor fixes after r…
Browse files Browse the repository at this point in the history
…efactoring db
  • Loading branch information
fernqvist committed Dec 4, 2024
1 parent 7887042 commit 153f917
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/api_productions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let dbManager;
const dbUrl = new URL(DB_CONNECTION_STRING);
if (dbUrl.protocol === 'mongodb:') {
dbManager = new DbManagerMongoDb(dbUrl);
} else if (dbUrl.protocol === 'couchdb:') {
} else if (dbUrl.protocol === 'http:' || dbUrl.protocol === 'https:') {
dbManager = new DbManagerCouchDb(dbUrl);
} else {
throw new Error('Unsupported database protocol');
Expand Down
36 changes: 23 additions & 13 deletions src/db/couchdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export class DbManagerCouchDb implements DbManager {
private nanoDb;

constructor(dbConnectionUrl: URL) {
this.client = nano(dbConnectionUrl.toString());
this.nanoDb = this.client.db.use(dbConnectionUrl.pathname);
const server = new URL('/', dbConnectionUrl).toString();
this.client = nano(server);
this.nanoDb = this.client.db.use(
dbConnectionUrl.pathname.replace(/^\//, '')
);
}

async connect(): Promise<void> {
Expand All @@ -22,26 +25,27 @@ export class DbManagerCouchDb implements DbManager {

private async getNextSequence(collectionName: string): Promise<number> {
const counterDocId = `counter_${collectionName}`;
let counterDoc;
interface CounterDoc {
_id: string;
_rev?: string;
value: string;
}
let counterDoc: CounterDoc;

try {
counterDoc = await this.nanoDb.get(counterDocId);
counterDoc = (await this.nanoDb.get(counterDocId)) as CounterDoc;
counterDoc.value = (parseInt(counterDoc.value) + 1).toString();
} catch (error) {
// assert.strictEqual(error.statusCode, 404, 'Unexpected error getting counter document');
counterDoc = { _id: counterDocId, value: '1' };
}
await this.nanoDb.insert(counterDoc);
return counterDoc.value;
return parseInt(counterDoc.value, 10);
}

/** Get all productions from the database in reverse natural order, limited by the limit parameter */
async getProductions(limit: number, offset: number): Promise<Production[]> {

Check warning on line 46 in src/db/couchdb.ts

View workflow job for this annotation

GitHub Actions / lint

'limit' is defined but never used

Check warning on line 46 in src/db/couchdb.ts

View workflow job for this annotation

GitHub Actions / lint

'offset' is defined but never used
const productions: Production[] = [];
const response = await this.nanoDb.list({
limit: limit,
skip: offset,
sort: [{ name: 'desc' }],
include_docs: true
});
response.rows.forEach((row: any) => {

Check warning on line 51 in src/db/couchdb.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
Expand All @@ -65,7 +69,11 @@ export class DbManagerCouchDb implements DbManager {
production: Production
): Promise<Production | undefined> {
const existingProduction = await this.nanoDb.get(production._id.toString());
const updatedProduction = { ...existingProduction, ...production };
const updatedProduction = {
...existingProduction,
...production,
_id: production._id.toString()
};
const response = await this.nanoDb.insert(updatedProduction);
return response.ok ? production : undefined;
}
Expand All @@ -75,16 +83,18 @@ export class DbManagerCouchDb implements DbManager {
if (_id === -1) {
throw new Error('Failed to get next sequence');
}
const production = { name, lines, _id };
const response = await this.nanoDb.insert(production);
const insertProduction = { name, lines, _id: _id.toString() };
const response = await this.nanoDb.insert(
insertProduction as unknown as nano.MaybeDocument
);
if (!response.ok) throw new Error('Failed to insert production');
return { name, lines, _id } as Production;
}

async deleteProduction(productionId: number): Promise<boolean> {
const production = await this.nanoDb.get(productionId.toString());
const response = await this.nanoDb.destroy(production._id, production._rev);
return response.ok;

}

async setLineConferenceId(
Expand Down

0 comments on commit 153f917

Please sign in to comment.