Skip to content

Commit

Permalink
feat: update to db-lib@9
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jan 17, 2024
1 parent 4934890 commit d48719f
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 337 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"prepare": "husky install"
},
"dependencies": {
"@naturalcycles/db-lib": "^8.0.1",
"@naturalcycles/db-lib": "^9.0.0",
"@naturalcycles/js-lib": "^14.0.0",
"@naturalcycles/nodejs-lib": "^13.1.3",
"mongodb": "^6.0.0"
Expand Down
2 changes: 1 addition & 1 deletion scripts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
//
{
"extends": "@naturalcycles/dev-lib/scripts/tsconfig.json",
"exclude": ["**/__exclude"]
"exclude": ["**/__exclude"],
}
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import { MongoDB, MongoDBCfg, MongoObject } from './mongo.db'

export type { MongoObject, MongoDBCfg }

export { MongoDB }
export * from './mongo.db'
66 changes: 38 additions & 28 deletions src/mongo.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Transform } from 'node:stream'
import {
BaseCommonDB,
CommonDB,
commonDBFullSupport,
CommonDBOptions,
CommonDBSaveOptions,
CommonDBSupport,
DBQuery,
DBTransaction,
mergeDBOperations,
RunQueryResult,
} from '@naturalcycles/db-lib'
import {
Expand Down Expand Up @@ -43,6 +43,16 @@ export interface MongoDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjec
export interface MongoDBOptions extends CommonDBOptions, CommandOperationOptions {}

export class MongoDB extends BaseCommonDB implements CommonDB, AsyncDisposable {
override support: CommonDBSupport = {
...commonDBFullSupport,
bufferValues: false,
insertSaveMethod: false,
updateSaveMethod: false,
tableSchemas: false,
transactions: false,
updateByQuery: false,
}

constructor(cfg: MongoDBCfg) {
super()

Expand Down Expand Up @@ -141,7 +151,7 @@ export class MongoDB extends BaseCommonDB implements CommonDB, AsyncDisposable {

override async getByIds<ROW extends ObjectWithId>(
table: string,
ids: ROW['id'][],
ids: string[],
_opt?: CommonDBOptions,
): Promise<ROW[]> {
if (!ids.length) return []
Expand All @@ -159,9 +169,9 @@ export class MongoDB extends BaseCommonDB implements CommonDB, AsyncDisposable {
return items.map(i => this.mapFromMongo(i))
}

async deleteByIds<ROW extends ObjectWithId>(
override async deleteByIds(
table: string,
ids: ROW['id'][],
ids: string[],
opt: MongoDBOptions = {},
): Promise<number> {
if (!ids.length) return 0
Expand Down Expand Up @@ -272,27 +282,27 @@ export class MongoDB extends BaseCommonDB implements CommonDB, AsyncDisposable {
/**
* https://docs.mongodb.com/manual/core/transactions/
*/
override async commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void> {
const client = await this.client()
const session = client.startSession()
const ops = mergeDBOperations(tx.ops)

try {
await session.withTransaction(async () => {
for await (const op of ops) {
if (op.type === 'saveBatch') {
// Important: You must pass the session to the operations
await this.saveBatch(op.table, op.rows, { ...opt, session })
} else if (op.type === 'deleteByIds') {
await this.deleteByIds(op.table, op.ids, { ...opt, session })
} else {
throw new Error(`DBOperation not supported: ${(op as any).type}`)
}
}
})
} finally {
await session.endSession()
}
// todo: is catch/revert needed?
}
// override async commitTransaction(tx: DBTransaction, opt?: CommonDBSaveOptions): Promise<void> {
// const client = await this.client()
// const session = client.startSession()
// const ops = mergeDBOperations(tx.ops)
//
// try {
// await session.withTransaction(async () => {
// for await (const op of ops) {
// if (op.type === 'saveBatch') {
// // Important: You must pass the session to the operations
// await this.saveBatch(op.table, op.rows, { ...opt, session })
// } else if (op.type === 'deleteByIds') {
// await this.deleteByIds(op.table, op.ids, { ...opt, session })
// } else {
// throw new Error(`DBOperation not supported: ${(op as any).type}`)
// }
// }
// })
// } finally {
// await session.endSession()
// }
// // todo: is catch/revert needed?
// }
}
10 changes: 1 addition & 9 deletions src/test/mongo.manual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,7 @@ afterAll(async () => {
await mongoDB.close()
})

describe('runCommonDBTest', () =>
runCommonDBTest(mongoDB, {
bufferSupport: false,
insert: false,
update: false,
tableSchemas: false,
transactions: false,
updateByQuery: false,
}))
describe('runCommonDBTest', () => runCommonDBTest(mongoDB))

describe('runCommonDaoTest', () => runCommonDaoTest(mongoDB))

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "@naturalcycles/dev-lib/cfg/tsconfig.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
},
"include": ["src"],
"exclude": ["**/__exclude"]
"exclude": ["**/__exclude"],
}
Loading

0 comments on commit d48719f

Please sign in to comment.