albatross 3.4.2 → 3.7.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 CHANGED
@@ -10,6 +10,26 @@ type FlattenIfArray<T> = T extends Array<infer R> ? R : T
10
10
  type SpecificProjection<T, TProjection> = Omit<T, 'projection'> & { projection: TProjection }
11
11
  type WithoutProjection<T> = T & { fields?: undefined, projection?: undefined }
12
12
 
13
+ interface FindOneOptions<T> extends Omit<mongodb.FindOneOptions<T>, 'hint'> {
14
+ hint?: string | object
15
+ }
16
+
17
+ interface FindOneAndUpdateOption<T> extends Omit<mongodb.FindOneAndUpdateOption<T>, 'hint'> {
18
+ hint?: string | object
19
+ }
20
+
21
+ interface UpdateOneOptions extends Omit<mongodb.UpdateOneOptions, 'hint'> {
22
+ hint?: string | object
23
+ }
24
+
25
+ interface UpdateManyOptions extends Omit<mongodb.UpdateManyOptions, 'hint'> {
26
+ hint?: string | object
27
+ }
28
+
29
+ interface FindOneAndDeleteOption<T> extends Omit<mongodb.FindOneAndDeleteOption<T>, 'hint'> {
30
+ hint?: string | object
31
+ }
32
+
13
33
  declare function albatross (uri: string): albatross.Albatross
14
34
 
15
35
  declare namespace albatross {
@@ -17,45 +37,52 @@ declare namespace albatross {
17
37
  readonly parent: Albatross
18
38
  id (hexString?: mongodb.ObjectId | string): mongodb.ObjectId
19
39
 
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>
40
+ findOne (filter: mongodb.FilterQuery<TSchema>, options?: WithoutProjection<FindOneOptions<TSchema>>): Promise<TSchema | null>
41
+ findOne<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, options: SpecificProjection<FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
42
+ findOne<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, options: SpecificProjection<FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
43
+ findOne (filter: mongodb.FilterQuery<TSchema>, options: FindOneOptions<TSchema>): Promise<object | null>
24
44
 
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[]>
45
+ find (query: mongodb.FilterQuery<TSchema>, options?: WithoutProjection<FindOneOptions<TSchema>>): Promise<TSchema[]>
46
+ find<TKey extends keyof TSchema> (query: mongodb.FilterQuery<TSchema>, options: SpecificProjection<FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Array<Pick<TSchema, TKey>>>
47
+ find<TKey extends keyof TSchema> (query: mongodb.FilterQuery<TSchema>, options: SpecificProjection<FindOneOptions<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Array<Pick<TSchema, '_id' | TKey>>>
48
+ find (query: mongodb.FilterQuery<TSchema>, options: FindOneOptions<TSchema>): Promise<object[]>
29
49
 
30
50
  count (query?: mongodb.FilterQuery<TSchema>, options?: mongodb.MongoCountPreferences): Promise<number>
31
51
 
32
52
  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]>>>
33
53
  distinct (key: string, query?: mongodb.FilterQuery<TSchema>, options?: { readPreference?: mongodb.ReadPreference | string, maxTimeMS?: number, session?: mongodb.ClientSession }): Promise<any[]>
34
54
 
35
- exists (query?: mongodb.FilterQuery<TSchema>): Promise<boolean>
55
+ exists (query?: mongodb.FilterQuery<TSchema>, options?: { hint?: string | object }): Promise<boolean>
36
56
 
37
57
  insert (doc: DeepReadonly<mongodb.OptionalId<TSchema>>, options?: mongodb.CollectionInsertOneOptions): Promise<mongodb.WithId<TSchema>>
38
58
  insert (docs: DeepReadonly<mongodb.OptionalId<TSchema>>[], options?: mongodb.CollectionInsertManyOptions): Promise<mongodb.WithId<TSchema>[]>
39
59
 
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>
60
+ findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: WithoutProjection<FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }>): Promise<TSchema>
61
+ findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: WithoutProjection<FindOneAndUpdateOption<TSchema>>): Promise<TSchema | null>
62
+
63
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey>>
64
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<FindOneAndUpdateOption<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
42
65
 
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>
66
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey>>
67
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: SpecificProjection<FindOneAndUpdateOption<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
45
68
 
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>
69
+ findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options: FindOneAndUpdateOption<TSchema> & { returnOriginal: false, upsert: true }): Promise<object>
70
+ findOneAndUpdate (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: FindOneAndUpdateOption<TSchema>): Promise<object | null>
48
71
 
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>
72
+ updateOne (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: UpdateOneOptions): Promise<{ matched: 0 | 1, modified: 0 | 1 }>
73
+ updateMany (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: UpdateManyOptions): Promise<{ matched: number, modified: number }>
51
74
 
52
- updateOne (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: mongodb.UpdateOneOptions): Promise<{ matched: 0 | 1, modified: 0 | 1 }>
53
- updateMany (filter: mongodb.FilterQuery<TSchema>, update: mongodb.UpdateQuery<TSchema> | Partial<TSchema>, options?: mongodb.UpdateManyOptions): Promise<{ matched: number, modified: number }>
75
+ deleteOne (filter: mongodb.FilterQuery<TSchema>, options?: mongodb.CommonOptions & { bypassDocumentValidation?: boolean, hint?: string | object }): Promise<0 | 1>
76
+ deleteMany (filter: mongodb.FilterQuery<TSchema>, options?: mongodb.CommonOptions & { hint?: string | object }): Promise<number>
54
77
 
55
- deleteOne (filter: mongodb.FilterQuery<TSchema>, options?: mongodb.CommonOptions & { bypassDocumentValidation?: boolean }): Promise<0 | 1>
56
- deleteMany (filter: mongodb.FilterQuery<TSchema>, options?: mongodb.CommonOptions): Promise<number>
78
+ findOneAndDelete (filter: mongodb.FilterQuery<TSchema>, options?: WithoutProjection<FindOneAndDeleteOption<TSchema>>): Promise<TSchema | null>
79
+ findOneAndDelete<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, options: SpecificProjection<FindOneAndDeleteOption<TSchema>, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
80
+ findOneAndDelete<TKey extends keyof TSchema> (filter: mongodb.FilterQuery<TSchema>, options: SpecificProjection<FindOneAndDeleteOption<TSchema>, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
81
+ findOneAndDelete (filter: mongodb.FilterQuery<TSchema>, options?: FindOneAndDeleteOption<TSchema>): Promise<object | null>
57
82
 
58
83
  aggregate (pipeline: object[], options?: mongodb.CollectionAggregationOptions): Promise<object[]>
84
+
85
+ createIndex (fieldOrSpec: string | any, options?: mongodb.IndexOptions): Promise<string>
59
86
  }
60
87
 
61
88
  interface FileInfo {
@@ -82,6 +109,7 @@ declare namespace albatross {
82
109
  collection<TSchema extends { _id: any }> (name: string): Collection<TSchema>
83
110
  grid (name?: string): Grid
84
111
  ping (timeout?: number): Promise<void>
112
+ transaction<T> (fn: (session: mongodb.ClientSession) => PromiseLike<T>): Promise<T>
85
113
  close (force?: boolean): Promise<void>
86
114
  }
87
115
 
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/lib/collection.js CHANGED
@@ -76,12 +76,16 @@ class Collection {
76
76
  }
77
77
  }
78
78
 
79
- async exists (query) {
79
+ async exists (query, opts) {
80
80
  const db = await this.parent.db()
81
81
 
82
82
  // https://blog.serverdensity.com/checking-if-a-document-exists-mongodb-slow-findone-vs-find/
83
83
  const cursor = db.collection(this.name).find(query, { projection: { _id: 1 }, limit: 1 })
84
84
 
85
+ if (opts && opts.hint) {
86
+ cursor.hint(opts.hint)
87
+ }
88
+
85
89
  try {
86
90
  this[kDebug]('exists(%o)', query)
87
91
  const res = await cursor.hasNext()
@@ -186,6 +190,20 @@ class Collection {
186
190
  }
187
191
  }
188
192
 
193
+ async findOneAndDelete (filter, opts) {
194
+ const db = await this.parent.db()
195
+
196
+ try {
197
+ this[kDebug]('findOneAndDelete(%o)', filter)
198
+ const res = await db.collection(this.name).findOneAndDelete(filter, opts)
199
+ this[kDebug](`reply OK ${res.value == null ? 0 : 1}`)
200
+ return res.value
201
+ } catch (err) {
202
+ this[kDebug]('reply ERR')
203
+ throw err
204
+ }
205
+ }
206
+
189
207
  async aggregate (pipeline, opts) {
190
208
  const db = await this.parent.db()
191
209
 
@@ -199,6 +217,20 @@ class Collection {
199
217
  throw err
200
218
  }
201
219
  }
220
+
221
+ async createIndex (fieldOrSpec, opts) {
222
+ const db = await this.parent.db()
223
+
224
+ try {
225
+ this[kDebug]('createIndex(%o, %o)', fieldOrSpec, opts)
226
+ const res = await db.collection(this.name).createIndex(fieldOrSpec, opts)
227
+ this[kDebug]('reply OK')
228
+ return res
229
+ } catch (err) {
230
+ this[kDebug]('reply ERR')
231
+ throw err
232
+ }
233
+ }
202
234
  }
203
235
 
204
236
  module.exports = Collection
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "albatross",
3
- "version": "3.4.2",
3
+ "version": "3.7.0",
4
4
  "license": "MIT",
5
5
  "author": "Linus Unnebäck <linus@folkdatorn.se>",
6
6
  "repository": "LinusU/node-albatross",
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.
@@ -165,6 +185,10 @@ Deletes a single document matching `filter`. Resolves with the number of documen
165
185
 
166
186
  Deletes multiple documents matching `filter`. Resolves with the number of documents deleted.
167
187
 
188
+ #### `findOneAndDelete(filter[, opts]): Promise<object>`
189
+
190
+ Finds a document and deletes it in one atomic operation.
191
+
168
192
  #### `aggregate(pipeline[, opts]): Promise<object[]>`
169
193
 
170
194
  Executes an aggregation framework pipeline against the collection. Resolves with the aggregated objects.