hide-a-bed 5.0.4 → 5.1.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/README.md CHANGED
@@ -40,9 +40,22 @@ A convience method to bind the config, so you dont need to pass it in.
40
40
  ```
41
41
  import { bindConfig } from 'hide-a-bed'
42
42
  const db = bindConfig(process.env)
43
- const doc = db.get('doc-123')
43
+ const doc = await db.get('doc-123')
44
44
  ```
45
45
 
46
+ ##### Config Overrides
47
+
48
+ You also can quickly change one (or more) config settings for a particular call with the db.config(options)
49
+
50
+ eg
51
+
52
+ ```
53
+ const doc = await db.config({ throwOnGetNotFound: true }).get('doc-id')
54
+ ```
55
+
56
+ You can pass any of [Config Options](#advanced-config-options) to db.config to override the original bindings.
57
+
58
+
46
59
  ### Document Operations
47
60
 
48
61
  #### get
@@ -412,32 +425,47 @@ const result = await query(config, view, options)
412
425
  // }
413
426
  ```
414
427
 
428
+ Some notes on the keys. Use native js things for arrays keys, rather then strings. Eg
429
+
430
+ - ```{ startkey: ['ryan'], endkey: ['ryan', {}] }```
431
+ - ```{ startkey: [47, null], endkey: [48, null] }```
432
+ - ```{ startkey: [customerIdVar], endkey: [customerIdVar, {}] }```
433
+ - ```{ startkey: [teamId, userId, startTimestamp], endkey: [teamId, userId, endTimestamp] }```
434
+
435
+
436
+
415
437
  #### createQuery()
416
- Create a query builder to help construct view queries with a fluent interface.
438
+ Create a query builder to help construct view queries with a fluent interface. Note we have stuck to couch naming conventions and not camel case.
439
+
417
440
  - Returns: QueryBuilder instance with methods:
418
441
  - `key(value)`: Set exact key match
419
- - `startKey(value)`: Set range start key
420
- - `endKey(value)`: Set range end key
442
+ - `startkey(value)`: Set range start key
443
+ - `endkey(value)`: Set range end key
421
444
  - `descending(bool)`: Set descending sort order
422
445
  - `skip(number)`: Set number of results to skip
423
446
  - `limit(number)`: Set max number of results
424
- - `includeDocs(bool)`: Include full documents
447
+ - `include_docs(bool)`: Include full documents
425
448
  - `reduce(bool)`: Enable/disable reduce
426
449
  - `group(bool)`: Enable/disable grouping
427
- - `groupLevel(number)`: Set group level
450
+ - `group_level(number)`: Set group level
428
451
  - `build()`: Return the constructed query options object
429
452
 
430
453
  ```javascript
431
454
  const options = createQuery()
432
- .startKey('A')
433
- .endKey('B')
434
- .includeDocs(true)
455
+ .startkey('A')
456
+ .endkey('B')
457
+ .include_docs(true)
435
458
  .limit(10)
436
459
  .build()
437
460
 
438
461
  const result = await query(config, view, options)
439
462
  ```
440
463
 
464
+ Again, use js types for array keys
465
+
466
+ - ```.startkey([teamId, userId]).endkey([teamId, userId, {}])```
467
+ - ```.startkey([teamId, userId, startTimestamp]).endkey([teamId, userId, endTimestamp])```
468
+
441
469
  #### queryStream
442
470
 
443
471
  Use Cases *Streaming Data*
@@ -41,6 +41,14 @@ class QueryBuilder {
41
41
  this.#options.startkey = startkey;
42
42
  return this;
43
43
  }
44
+ /**
45
+ * @param {any} startkey
46
+ * @returns {QueryBuilder}
47
+ */
48
+ startkey(startkey) {
49
+ this.#options.startkey = startkey;
50
+ return this;
51
+ }
44
52
  /**
45
53
  * @param {any} endkey
46
54
  * @returns {QueryBuilder}
@@ -49,6 +57,14 @@ class QueryBuilder {
49
57
  this.#options.endkey = endkey;
50
58
  return this;
51
59
  }
60
+ /**
61
+ * @param {any} endkey
62
+ * @returns {QueryBuilder}
63
+ */
64
+ endkey(endkey) {
65
+ this.#options.endkey = endkey;
66
+ return this;
67
+ }
52
68
  /**
53
69
  * @param {boolean} reduce
54
70
  * @returns {QueryBuilder}
@@ -73,6 +89,30 @@ class QueryBuilder {
73
89
  this.#options.group_level = level;
74
90
  return this;
75
91
  }
92
+ /**
93
+ * @param {number} level
94
+ * @returns {QueryBuilder}
95
+ */
96
+ group_level(level) {
97
+ this.#options.group_level = level;
98
+ return this;
99
+ }
100
+ /**
101
+ * @param {boolean} includeDocs
102
+ * @returns {QueryBuilder}
103
+ */
104
+ includeDocs(includeDocs = true) {
105
+ this.#options.include_docs = includeDocs;
106
+ return this;
107
+ }
108
+ /**
109
+ * @param {boolean} includeDocs
110
+ * @returns {QueryBuilder}
111
+ */
112
+ include_docs(includeDocs = true) {
113
+ this.#options.include_docs = includeDocs;
114
+ return this;
115
+ }
76
116
  /**
77
117
  * @param {string} stale
78
118
  * @returns {QueryBuilder}
package/cjs/index.cjs CHANGED
@@ -86,13 +86,13 @@ const schema = {
86
86
  ChangesOptions: import_changes2.ChangesOptions,
87
87
  ChangesResponse: import_changes2.ChangesResponse
88
88
  };
89
- const bindConfig = import_bind.Bind.implement((config) => {
89
+ function doBind(config) {
90
90
  const retryOptions = {
91
91
  maxRetries: config.maxRetries ?? 10,
92
92
  initialDelay: config.initialDelay ?? 1e3,
93
93
  backoffFactor: config.backoffFactor ?? 2
94
94
  };
95
- return {
95
+ const result = {
96
96
  get: config.bindWithRetry ? (0, import_retry.withRetry)(import_crud.get.bind(null, config), retryOptions) : import_crud.get.bind(null, config),
97
97
  getAtRev: config.bindWithRetry ? (0, import_retry.withRetry)(import_crud.getAtRev.bind(null, config), retryOptions) : import_crud.getAtRev.bind(null, config),
98
98
  put: config.bindWithRetry ? (0, import_retry.withRetry)(import_crud.put.bind(null, config), retryOptions) : import_crud.put.bind(null, config),
@@ -112,4 +112,15 @@ const bindConfig = import_bind.Bind.implement((config) => {
112
112
  watchDocs: import_watch.watchDocs.bind(null, config),
113
113
  changes: import_changes.changes.bind(null, config)
114
114
  };
115
+ return result;
116
+ }
117
+ const bindConfig = import_bind.Bind.implement((config) => {
118
+ const parsedConfig = import_config.CouchConfig.parse(config);
119
+ const funcs = doBind(parsedConfig);
120
+ const reconfig = (_overrides) => {
121
+ const newConfig = { ...config, ..._overrides };
122
+ return bindConfig(newConfig);
123
+ };
124
+ const all = { ...funcs, options: reconfig };
125
+ return all;
115
126
  });
@@ -18,7 +18,9 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var bind_exports = {};
20
20
  __export(bind_exports, {
21
- Bind: () => Bind
21
+ Bind: () => Bind,
22
+ BindBase: () => BindBase,
23
+ BindReturns: () => BindReturns
22
24
  });
23
25
  module.exports = __toCommonJS(bind_exports);
24
26
  var import_zod = require("zod");
@@ -31,7 +33,7 @@ var import_stream = require("./stream.cjs");
31
33
  var import_lock = require("./sugar/lock.cjs");
32
34
  var import_changes = require("./changes.cjs");
33
35
  var import_watch = require("./sugar/watch.cjs");
34
- const BindReturns = import_zod.z.object({
36
+ const BindBase = import_zod.z.object({
35
37
  bulkGet: import_bulk.BulkGetBound,
36
38
  bulkSave: import_bulk.BulkSaveBound,
37
39
  bulkRemove: import_bulk.BulkRemoveBound,
@@ -48,4 +50,8 @@ const BindReturns = import_zod.z.object({
48
50
  changes: import_changes.ChangesBound,
49
51
  watchDocs: import_watch.WatchDocsBound
50
52
  });
53
+ const RebindOptions = import_config.CouchConfig.omit({ couch: true });
54
+ const BindReturns = BindBase.extend({
55
+ options: import_zod.z.function().args(RebindOptions).returns(BindBase)
56
+ });
51
57
  const Bind = import_zod.z.function().args(import_config.CouchConfig).returns(BindReturns);
@@ -6,6 +6,7 @@
6
6
  * @property {boolean} [reduce] - Whether to use reduce function
7
7
  * @property {boolean} [group] - Whether to group results
8
8
  * @property {number} [group_level] - Level at which to group
9
+ * @property {boolean} [include_docs] - Whether to include full couch docs
9
10
  * @property {string} [stale] - Stale parameter value
10
11
  * @property {number} [limit] - Max number of results
11
12
  */
@@ -20,11 +21,21 @@ export class QueryBuilder {
20
21
  * @returns {QueryBuilder}
21
22
  */
22
23
  startKey(startkey: any): QueryBuilder;
24
+ /**
25
+ * @param {any} startkey
26
+ * @returns {QueryBuilder}
27
+ */
28
+ startkey(startkey: any): QueryBuilder;
23
29
  /**
24
30
  * @param {any} endkey
25
31
  * @returns {QueryBuilder}
26
32
  */
27
33
  endKey(endkey: any): QueryBuilder;
34
+ /**
35
+ * @param {any} endkey
36
+ * @returns {QueryBuilder}
37
+ */
38
+ endkey(endkey: any): QueryBuilder;
28
39
  /**
29
40
  * @param {boolean} reduce
30
41
  * @returns {QueryBuilder}
@@ -40,6 +51,21 @@ export class QueryBuilder {
40
51
  * @returns {QueryBuilder}
41
52
  */
42
53
  groupLevel(level: number): QueryBuilder;
54
+ /**
55
+ * @param {number} level
56
+ * @returns {QueryBuilder}
57
+ */
58
+ group_level(level: number): QueryBuilder;
59
+ /**
60
+ * @param {boolean} includeDocs
61
+ * @returns {QueryBuilder}
62
+ */
63
+ includeDocs(includeDocs?: boolean): QueryBuilder;
64
+ /**
65
+ * @param {boolean} includeDocs
66
+ * @returns {QueryBuilder}
67
+ */
68
+ include_docs(includeDocs?: boolean): QueryBuilder;
43
69
  /**
44
70
  * @param {string} stale
45
71
  * @returns {QueryBuilder}
@@ -82,6 +108,10 @@ export type QueryOptions = {
82
108
  * - Level at which to group
83
109
  */
84
110
  group_level?: number | undefined;
111
+ /**
112
+ * - Whether to include full couch docs
113
+ */
114
+ include_docs?: boolean | undefined;
85
115
  /**
86
116
  * - Stale parameter value
87
117
  */
@@ -1 +1 @@
1
- {"version":3,"file":"queryBuilder.d.mts","sourceRoot":"","sources":["queryBuilder.mjs"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AAEH;IAIE;;;OAGG;IACH,SAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,mBAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,eAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,gBAHW,OAAO,GACL,YAAY,CAKxB;IAED;;;OAGG;IACH,cAHW,OAAO,GACL,YAAY,CAKxB;IAED;;;OAGG;IACH,kBAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;;OAGG;IACH,aAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;;OAGG;IACH,aAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;OAEG;IACH,SAFa,YAAY,CAIxB;;CACF;AAEM,4CAA4C;;;;;UA9FrC,GAAG;;;;eACH,GAAG;;;;aACH,GAAG"}
1
+ {"version":3,"file":"queryBuilder.d.mts","sourceRoot":"","sources":["queryBuilder.mjs"],"names":[],"mappings":"AAEA;;;;;;;;;;;GAWG;AAEH;IAIE;;;OAGG;IACH,SAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,mBAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,mBAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,eAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,eAHW,GAAG,GACD,YAAY,CAKxB;IAED;;;OAGG;IACH,gBAHW,OAAO,GACL,YAAY,CAKxB;IAED;;;OAGG;IACH,cAHW,OAAO,GACL,YAAY,CAKxB;IAED;;;OAGG;IACH,kBAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;;OAGG;IACH,mBAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;;OAGG;IACH,0BAHW,OAAO,GACL,YAAY,CAKxB;IAED;;;OAGG;IACH,2BAHW,OAAO,GACL,YAAY,CAKxB;IAED;;;OAGG;IACH,aAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;;OAGG;IACH,aAHW,MAAM,GACJ,YAAY,CAKxB;IAED;;OAEG;IACH,SAFa,YAAY,CAIxB;;CACF;AAEM,4CAA4C;;;;;UA5IrC,GAAG;;;;eACH,GAAG;;;;aACH,GAAG"}
@@ -8,6 +8,7 @@
8
8
  * @property {boolean} [reduce] - Whether to use reduce function
9
9
  * @property {boolean} [group] - Whether to group results
10
10
  * @property {number} [group_level] - Level at which to group
11
+ * @property {boolean} [include_docs] - Whether to include full couch docs
11
12
  * @property {string} [stale] - Stale parameter value
12
13
  * @property {number} [limit] - Max number of results
13
14
  */
@@ -34,6 +35,15 @@ export class QueryBuilder {
34
35
  return this
35
36
  }
36
37
 
38
+ /**
39
+ * @param {any} startkey
40
+ * @returns {QueryBuilder}
41
+ */
42
+ startkey (startkey) {
43
+ this.#options.startkey = startkey
44
+ return this
45
+ }
46
+
37
47
  /**
38
48
  * @param {any} endkey
39
49
  * @returns {QueryBuilder}
@@ -43,6 +53,15 @@ export class QueryBuilder {
43
53
  return this
44
54
  }
45
55
 
56
+ /**
57
+ * @param {any} endkey
58
+ * @returns {QueryBuilder}
59
+ */
60
+ endkey (endkey) {
61
+ this.#options.endkey = endkey
62
+ return this
63
+ }
64
+
46
65
  /**
47
66
  * @param {boolean} reduce
48
67
  * @returns {QueryBuilder}
@@ -70,6 +89,33 @@ export class QueryBuilder {
70
89
  return this
71
90
  }
72
91
 
92
+ /**
93
+ * @param {number} level
94
+ * @returns {QueryBuilder}
95
+ */
96
+ group_level (level) {
97
+ this.#options.group_level = level
98
+ return this
99
+ }
100
+
101
+ /**
102
+ * @param {boolean} includeDocs
103
+ * @returns {QueryBuilder}
104
+ */
105
+ includeDocs (includeDocs = true) {
106
+ this.#options.include_docs = includeDocs
107
+ return this
108
+ }
109
+
110
+ /**
111
+ * @param {boolean} includeDocs
112
+ * @returns {QueryBuilder}
113
+ */
114
+ include_docs (includeDocs = true) {
115
+ this.#options.include_docs = includeDocs
116
+ return this
117
+ }
118
+
73
119
  /**
74
120
  * @param {string} stale
75
121
  * @returns {QueryBuilder}
package/index.d.mts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["index.mjs"],"names":[],"mappings":"oBAEmC,iBAAiB;yBAAjB,iBAAiB;oBAAjB,iBAAiB;wBADkC,iBAAiB;yBAAjB,iBAAiB;sBAMjF,kBAAkB;4BACZ,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;sBAJP,kBAAkB;iCAAlB,kBAAkB;2BAH4B,iBAAiB;kCAAjB,iBAAiB;oCAAjB,iBAAiB;AAkDvG,uDAAuD;AACvD,yBADY,OAAO,mBAAmB,EAAE,UAAU,CA+BhD;0BAxEwB,kBAAkB;4BADhB,yBAAyB;2BAJd,uBAAuB;2BAAvB,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AAzB,yBAAyB;wBAH1B,sBAAsB;+BAAtB,sBAAsB;gCAAtB,sBAAsB"}
package/index.mjs CHANGED
@@ -48,20 +48,19 @@ const schema = {
48
48
  ChangesOptions,
49
49
  ChangesResponse
50
50
  }
51
-
52
- /** @type { import('./schema/bind.mjs').BindSchema } */
53
- const bindConfig = Bind.implement((
54
- /** @type { import('./schema/config.mjs').CouchConfigSchema } */
55
- config
56
- ) => {
51
+ /**
52
+ * @param {import('./schema/config.mjs').CouchConfigSchema } config
53
+ */
54
+ function doBind(config) {
57
55
  // Default retry options
58
56
  const retryOptions = {
59
57
  maxRetries: config.maxRetries ?? 10,
60
58
  initialDelay: config.initialDelay ?? 1000,
61
59
  backoffFactor: config.backoffFactor ?? 2
62
60
  }
63
-
64
- return {
61
+
62
+ // Create the object without the config property first
63
+ const result = {
65
64
  get: config.bindWithRetry ? withRetry(get.bind(null, config), retryOptions) : get.bind(null, config),
66
65
  getAtRev: config.bindWithRetry ? withRetry(getAtRev.bind(null, config), retryOptions) : getAtRev.bind(null, config),
67
66
  put: config.bindWithRetry ? withRetry(put.bind(null, config), retryOptions) : put.bind(null, config),
@@ -80,6 +79,33 @@ const bindConfig = Bind.implement((
80
79
  watchDocs: watchDocs.bind(null, config),
81
80
  changes: changes.bind(null, config)
82
81
  }
82
+
83
+ return result;
84
+ }
85
+
86
+ /** @type { import('./schema/bind.mjs').BindSchema } */
87
+ const bindConfig = Bind.implement((
88
+ /** @type { import('./schema/config.mjs').CouchConfigSchema } */
89
+ config
90
+ ) => {
91
+ const parsedConfig = CouchConfig.parse(config)
92
+
93
+ /** @type { import('./schema/bind.mjs').BindBaseSchema } funcs */
94
+ const funcs = doBind(parsedConfig)
95
+
96
+ // Add the options function that returns a new bound instance
97
+ // this allows the user to override some options
98
+ const reconfig = (
99
+ /** @type any */
100
+ _overrides
101
+ ) => {
102
+ // override the config and return doBind again
103
+ const newConfig = { ...config, ..._overrides }
104
+ return bindConfig(newConfig)
105
+ }
106
+ /** @type { import('./schema/bind.mjs').BindReturnsSchema } */
107
+ const all = { ...funcs, options: reconfig }
108
+ return all
83
109
  })
84
110
 
85
111
  export {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hide-a-bed",
3
- "version": "5.0.4",
3
+ "version": "5.1.0",
4
4
  "description": "An abstraction over couchdb calls that includes easy mock/stubs with pouchdb",
5
5
  "module": "index.mjs",
6
6
  "main": "cjs/index.cjs",
@@ -57,4 +57,4 @@
57
57
  "node": "20.17.0",
58
58
  "npm": "10.8.2"
59
59
  }
60
- }
60
+ }