albatross 3.3.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +35 -10
- package/lib/albatross.js +17 -0
- package/package.json +4 -4
- package/readme.md +20 -0
package/index.d.ts
CHANGED
|
@@ -1,29 +1,53 @@
|
|
|
1
1
|
import * as mongodb from 'mongodb'
|
|
2
2
|
|
|
3
|
+
type DeepReadonly<T> = T extends Date | RegExp | string | number | boolean | bigint | symbol | undefined | null
|
|
4
|
+
? T
|
|
5
|
+
: T extends {}
|
|
6
|
+
? { readonly [K in keyof T]: DeepReadonly<T[K]> }
|
|
7
|
+
: Readonly<T>
|
|
8
|
+
|
|
3
9
|
type FlattenIfArray<T> = T extends Array<infer R> ? R : T
|
|
10
|
+
type SpecificProjection<T, TProjection> = Omit<T, 'projection'> & { projection: TProjection }
|
|
11
|
+
type WithoutProjection<T> = T & { fields?: undefined, projection?: undefined }
|
|
4
12
|
|
|
5
13
|
declare function albatross (uri: string): albatross.Albatross
|
|
6
14
|
|
|
7
15
|
declare namespace albatross {
|
|
8
|
-
interface Collection<TSchema
|
|
16
|
+
interface Collection<TSchema extends { _id: any }> {
|
|
9
17
|
readonly parent: Albatross
|
|
10
18
|
id (hexString?: mongodb.ObjectId | string): mongodb.ObjectId
|
|
11
19
|
|
|
12
|
-
findOne
|
|
13
|
-
|
|
20
|
+
findOne (filter: mongodb.FilterQuery<TSchema>, options?: WithoutProjection<mongodb.FindOneOptions<TSchema>>): Promise<TSchema | null>
|
|
21
|
+
findOne<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, options: SpecificProjection<mongodb.FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
|
|
22
|
+
findOne<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, options: SpecificProjection<mongodb.FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
|
|
23
|
+
findOne (filter: mongodb.FilterQuery<TSchema>, options: mongodb.FindOneOptions<TSchema>): Promise<object | null>
|
|
24
|
+
|
|
25
|
+
find (query: mongodb.FilterQuery<TSchema>, options?: WithoutProjection<mongodb.FindOneOptions<TSchema>>): Promise<TSchema[]>
|
|
26
|
+
find<TKey extends keyof TSchema> (query: mongodb.FilterQuery<TSchema>, options: SpecificProjection<mongodb.FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Array<Pick<TSchema, TKey>>>
|
|
27
|
+
find<TKey extends keyof TSchema> (query: mongodb.FilterQuery<TSchema>, options: SpecificProjection<mongodb.FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Array<Pick<TSchema, '_id' | TKey>>>
|
|
28
|
+
find (query: mongodb.FilterQuery<TSchema>, options: mongodb.FindOneOptions<TSchema>): Promise<object[]>
|
|
14
29
|
|
|
15
30
|
count (query?: mongodb.FilterQuery<TSchema>, options?: mongodb.MongoCountPreferences): Promise<number>
|
|
16
31
|
|
|
17
|
-
distinct<
|
|
32
|
+
distinct<TKey extends keyof mongodb.WithId<TSchema>> (key: TKey, query?: mongodb.FilterQuery<TSchema>, options?: { readPreference?: mongodb.ReadPreference | string, maxTimeMS?: number, session?: mongodb.ClientSession }): Promise<Array<FlattenIfArray<mongodb.WithId<TSchema>[TKey]>>>
|
|
18
33
|
distinct (key: string, query?: mongodb.FilterQuery<TSchema>, options?: { readPreference?: mongodb.ReadPreference | string, maxTimeMS?: number, session?: mongodb.ClientSession }): Promise<any[]>
|
|
19
34
|
|
|
20
35
|
exists (query?: mongodb.FilterQuery<TSchema>): Promise<boolean>
|
|
21
36
|
|
|
22
|
-
insert (doc: mongodb.OptionalId<TSchema
|
|
23
|
-
insert (docs: mongodb.OptionalId<TSchema
|
|
37
|
+
insert (doc: DeepReadonly<mongodb.OptionalId<TSchema>>, options?: mongodb.CollectionInsertOneOptions): Promise<mongodb.WithId<TSchema>>
|
|
38
|
+
insert (docs: DeepReadonly<mongodb.OptionalId<TSchema>>[], options?: mongodb.CollectionInsertManyOptions): Promise<mongodb.WithId<TSchema>[]>
|
|
39
|
+
|
|
40
|
+
findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: WithoutProjection<mongodb.FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }>): Promise<TSchema>
|
|
41
|
+
findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: WithoutProjection<mongodb.FindOneAndUpdateOption<TSchema>>): Promise<TSchema | null>
|
|
42
|
+
|
|
43
|
+
findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey>>
|
|
44
|
+
findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOption<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
|
|
45
|
+
|
|
46
|
+
findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey>>
|
|
47
|
+
findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOption<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
|
|
24
48
|
|
|
25
|
-
findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: mongodb.FindOneAndUpdateOption & { returnOriginal: false, upsert: true }): Promise<
|
|
26
|
-
findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: mongodb.FindOneAndUpdateOption): Promise<
|
|
49
|
+
findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: mongodb.FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }): Promise<object>
|
|
50
|
+
findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: mongodb.FindOneAndUpdateOption<TSchema>): Promise<object | null>
|
|
27
51
|
|
|
28
52
|
updateOne (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: mongodb.UpdateOneOptions): Promise<{ matched: 0 | 1, modified: 0 | 1 }>
|
|
29
53
|
updateMany (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: mongodb.UpdateManyOptions): Promise<{ matched: number, modified: number }>
|
|
@@ -31,7 +55,7 @@ declare namespace albatross {
|
|
|
31
55
|
deleteOne (filter: mongodb.FilterQuery<TSchema>, options?: mongodb.CommonOptions & { bypassDocumentValidation?: boolean }): Promise<0 | 1>
|
|
32
56
|
deleteMany (filter: mongodb.FilterQuery<TSchema>, options?: mongodb.CommonOptions): Promise<number>
|
|
33
57
|
|
|
34
|
-
aggregate
|
|
58
|
+
aggregate (pipeline: object[], options?: mongodb.CollectionAggregationOptions): Promise<object[]>
|
|
35
59
|
}
|
|
36
60
|
|
|
37
61
|
interface FileInfo {
|
|
@@ -55,9 +79,10 @@ declare namespace albatross {
|
|
|
55
79
|
|
|
56
80
|
interface Albatross {
|
|
57
81
|
id (hexString?: mongodb.ObjectId | string): mongodb.ObjectId
|
|
58
|
-
collection<TSchema> (name: string): Collection<TSchema>
|
|
82
|
+
collection<TSchema extends { _id: any }> (name: string): Collection<TSchema>
|
|
59
83
|
grid (name?: string): Grid
|
|
60
84
|
ping (timeout?: number): Promise<void>
|
|
85
|
+
transaction<T> (fn: (session: mongodb.ClientSession) => PromiseLike<T>): Promise<T>
|
|
61
86
|
close (force?: boolean): Promise<void>
|
|
62
87
|
}
|
|
63
88
|
|
package/lib/albatross.js
CHANGED
|
@@ -99,6 +99,23 @@ class Albatross {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
async transaction (fn) {
|
|
103
|
+
const client = await this[kConnect]()
|
|
104
|
+
const session = client.startSession()
|
|
105
|
+
|
|
106
|
+
let result
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
await session.withTransaction(async (...args) => {
|
|
110
|
+
result = await fn(...args)
|
|
111
|
+
})
|
|
112
|
+
} finally {
|
|
113
|
+
session.endSession()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return result
|
|
117
|
+
}
|
|
118
|
+
|
|
102
119
|
async close (force) {
|
|
103
120
|
if (this[kClient] == null) return
|
|
104
121
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "albatross",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Linus Unnebäck <linus@folkdatorn.se>",
|
|
6
6
|
"repository": "LinusU/node-albatross",
|
|
@@ -15,10 +15,10 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@types/bson": "1.0.11",
|
|
18
|
-
"@types/mongodb": "3.
|
|
19
|
-
"bson": "^1.1.
|
|
18
|
+
"@types/mongodb": "3.6.12",
|
|
19
|
+
"bson": "^1.1.4",
|
|
20
20
|
"debug": "^4.1.1",
|
|
21
|
-
"mongodb": "^3.
|
|
21
|
+
"mongodb": "^3.6.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/mocha": "^5.2.7",
|
package/readme.md
CHANGED
|
@@ -95,6 +95,26 @@ Send the `ping` command to the server, to check that the connection is still int
|
|
|
95
95
|
|
|
96
96
|
Optionally accepts a timeout in milliseconds.
|
|
97
97
|
|
|
98
|
+
#### `.transaction(fn): Promise`
|
|
99
|
+
|
|
100
|
+
Runs a provided function within a transaction, retrying either the commit operation or entire transaction as needed (and when the error permits) to better ensure that the transaction can complete successfully.
|
|
101
|
+
|
|
102
|
+
Example:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
const user = db.collection('user')
|
|
106
|
+
|
|
107
|
+
const result = await db.transaction(async (session) => {
|
|
108
|
+
await user.insert({ name: 'Linus', born: 1992 }, { session })
|
|
109
|
+
await user.insert({ name: 'Steve', born: 1955 }, { session })
|
|
110
|
+
|
|
111
|
+
return await user.findOne({ born: 1992 }, { session })
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
console.log('Hello ' + result.name)
|
|
115
|
+
//=> Hello Linus
|
|
116
|
+
```
|
|
117
|
+
|
|
98
118
|
#### `.close(): Promise<void>`
|
|
99
119
|
|
|
100
120
|
Closes the connection to the server.
|