albatross 4.0.0-rc.2 → 4.0.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
@@ -11,6 +11,11 @@ type DeepReadonly<T> = T extends Date | RegExp | string | number | boolean | big
11
11
  type SpecificProjection<T, TProjection> = Omit<T, 'projection'> & { projection: TProjection }
12
12
  type WithoutProjection<T> = T & { fields?: undefined, projection?: undefined }
13
13
 
14
+ // The upstream types have misstyped the `hint` option
15
+ interface FindOneAndUpdateOptions extends Omit<mongodb.FindOneAndUpdateOptions, 'hint'> {
16
+ hint?: mongodb.Hint
17
+ }
18
+
14
19
  export interface Collection<TSchema extends { _id: any }> {
15
20
  readonly parent: Albatross
16
21
  id (hexString?: mongodb.ObjectId | string): mongodb.ObjectId
@@ -30,22 +35,22 @@ export interface Collection<TSchema extends { _id: any }> {
30
35
  distinct<TKey extends keyof mongodb.WithId<TSchema>> (key: TKey, query?: mongodb.Filter<TSchema>, options?: mongodb.DistinctOptions): Promise<Array<mongodb.Flatten<mongodb.WithId<TSchema>[TKey]>>>
31
36
  distinct (key: string, query?: mongodb.Filter<TSchema>, options?: mongodb.DistinctOptions): Promise<any[]>
32
37
 
33
- exists (query?: mongodb.Filter<TSchema>): Promise<boolean>
38
+ exists (query?: mongodb.Filter<TSchema>, options?: { hint?: mongodb.Hint }): Promise<boolean>
34
39
 
35
40
  insert (doc: DeepReadonly<mongodb.OptionalId<TSchema>>, options?: mongodb.InsertOneOptions): Promise<mongodb.WithId<TSchema>>
36
41
  insert (docs: DeepReadonly<mongodb.OptionalId<TSchema>>[], options?: mongodb.BulkWriteOptions): Promise<mongodb.WithId<TSchema>[]>
37
42
 
38
- findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: WithoutProjection<mongodb.FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }>): Promise<TSchema>
39
- findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options?: WithoutProjection<mongodb.FindOneAndUpdateOptions>): Promise<TSchema | null>
43
+ findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: WithoutProjection<FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }>): Promise<TSchema>
44
+ findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options?: WithoutProjection<FindOneAndUpdateOptions>): Promise<TSchema | null>
40
45
 
41
- findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey>>
42
- findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOptions, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
46
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey>>
47
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<FindOneAndUpdateOptions, { [key in TKey]: 1 | true } & { _id: 0 | false }>): Promise<Pick<TSchema, TKey> | null>
43
48
 
44
- findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey>>
45
- findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<mongodb.FindOneAndUpdateOptions, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
49
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey>>
50
+ findOneAndUpdate<TKey extends keyof TSchema> (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: SpecificProjection<FindOneAndUpdateOptions, { [key in TKey]: 1 | true } & { _id?: 1 | true }>): Promise<Pick<TSchema, '_id' | TKey> | null>
46
51
 
47
- findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: mongodb.FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }): Promise<object>
48
- findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options?: mongodb.FindOneAndUpdateOptions): Promise<object | null>
52
+ findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options: FindOneAndUpdateOptions & { returnDocument: 'after', upsert: true }): Promise<object>
53
+ findOneAndUpdate (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options?: FindOneAndUpdateOptions): Promise<object | null>
49
54
 
50
55
  updateOne (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema> | Partial<TSchema>, options?: mongodb.UpdateOptions): Promise<{ matched: 0 | 1, modified: 0 | 1 }>
51
56
  updateMany (filter: mongodb.Filter<TSchema>, update: mongodb.UpdateFilter<TSchema>, options?: mongodb.UpdateOptions): Promise<{ matched: number, modified: number }>
@@ -54,6 +59,8 @@ export interface Collection<TSchema extends { _id: any }> {
54
59
  deleteMany (filter: mongodb.Filter<TSchema>, options?: mongodb.DeleteOptions): Promise<number>
55
60
 
56
61
  aggregate (pipeline: object[], options?: mongodb.AggregateOptions): Promise<object[]>
62
+
63
+ createIndex (indexSpec: mongodb.IndexSpecification, options?: mongodb.CreateIndexesOptions): Promise<string>
57
64
  }
58
65
 
59
66
  export interface FileInfo {
package/lib/collection.js CHANGED
@@ -74,12 +74,16 @@ export default class Collection {
74
74
  }
75
75
  }
76
76
 
77
- async exists (query) {
77
+ async exists (query, opts) {
78
78
  const db = await this.parent.db()
79
79
 
80
80
  // https://blog.serverdensity.com/checking-if-a-document-exists-mongodb-slow-findone-vs-find/
81
81
  const cursor = db.collection(this.name).find(query, { projection: { _id: 1 }, limit: 1 })
82
82
 
83
+ if (opts && opts.hint) {
84
+ cursor.hint(opts.hint)
85
+ }
86
+
83
87
  try {
84
88
  this[kDebug]('exists(%o)', query)
85
89
  const res = await cursor.hasNext()
@@ -199,4 +203,18 @@ export default class Collection {
199
203
  throw err
200
204
  }
201
205
  }
206
+
207
+ async createIndex (indexSpec, opts) {
208
+ const db = await this.parent.db()
209
+
210
+ try {
211
+ this[kDebug]('createIndex(%o, %o)', indexSpec, opts)
212
+ const res = await db.collection(this.name).createIndex(indexSpec, opts)
213
+ this[kDebug]('reply OK')
214
+ return res
215
+ } catch (err) {
216
+ this[kDebug]('reply ERR')
217
+ throw err
218
+ }
219
+ }
202
220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "albatross",
3
- "version": "4.0.0-rc.2",
3
+ "version": "4.0.0",
4
4
  "license": "MIT",
5
5
  "repository": "LinusU/node-albatross",
6
6
  "type": "module",