couchset 0.2.7 → 0.4.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.
Files changed (117) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +292 -152
  3. package/dist/connection/connection.d.ts +68 -0
  4. package/dist/connection/connection.js +495 -0
  5. package/dist/connection/connection.js.map +1 -0
  6. package/dist/connection/env.d.ts +2 -0
  7. package/dist/connection/env.js +16 -0
  8. package/dist/connection/env.js.map +1 -0
  9. package/dist/connection/index.d.ts +3 -0
  10. package/dist/connection/index.js +25 -0
  11. package/dist/connection/index.js.map +1 -0
  12. package/dist/connection/types.d.ts +25 -0
  13. package/dist/connection/types.js +3 -0
  14. package/dist/connection/types.js.map +1 -0
  15. package/dist/database.d.ts +9 -0
  16. package/dist/database.js +196 -0
  17. package/dist/database.js.map +1 -0
  18. package/dist/index.d.ts +20 -6
  19. package/dist/index.js +55 -8
  20. package/dist/index.js.map +1 -1
  21. package/dist/legacy/connection.d.ts +2 -0
  22. package/dist/legacy/connection.js +24 -0
  23. package/dist/legacy/connection.js.map +1 -0
  24. package/dist/legacy/index.d.ts +10 -0
  25. package/dist/legacy/index.js +93 -0
  26. package/dist/legacy/index.js.map +1 -0
  27. package/dist/legacy/model.d.ts +55 -0
  28. package/dist/legacy/model.js +282 -0
  29. package/dist/legacy/model.js.map +1 -0
  30. package/dist/legacy/pagination/index.d.ts +1 -0
  31. package/dist/legacy/pagination/index.js +18 -0
  32. package/dist/legacy/pagination/index.js.map +1 -0
  33. package/dist/legacy/pagination/pagination.d.ts +12 -0
  34. package/dist/legacy/pagination/pagination.js +85 -0
  35. package/dist/legacy/pagination/pagination.js.map +1 -0
  36. package/dist/legacy/search/customQuery.d.ts +13 -0
  37. package/dist/legacy/search/customQuery.js +73 -0
  38. package/dist/legacy/search/customQuery.js.map +1 -0
  39. package/dist/legacy/search/index.d.ts +1 -0
  40. package/dist/legacy/search/index.js +18 -0
  41. package/dist/legacy/search/index.js.map +1 -0
  42. package/dist/model/default-scope.d.ts +10 -0
  43. package/dist/model/default-scope.js +47 -0
  44. package/dist/model/default-scope.js.map +1 -0
  45. package/dist/model/hydrated-document.d.ts +30 -0
  46. package/dist/model/hydrated-document.js +118 -0
  47. package/dist/model/hydrated-document.js.map +1 -0
  48. package/dist/model/include.d.ts +33 -0
  49. package/dist/model/include.js +250 -0
  50. package/dist/model/include.js.map +1 -0
  51. package/dist/model/index.d.ts +192 -52
  52. package/dist/model/index.js +646 -172
  53. package/dist/model/index.js.map +1 -1
  54. package/dist/model/indexes.d.ts +18 -0
  55. package/dist/model/indexes.js +155 -0
  56. package/dist/model/indexes.js.map +1 -0
  57. package/dist/model/keyspace.d.ts +9 -0
  58. package/dist/model/keyspace.js +28 -0
  59. package/dist/model/keyspace.js.map +1 -0
  60. package/dist/model/read-helpers.d.ts +36 -0
  61. package/dist/model/read-helpers.js +275 -0
  62. package/dist/model/read-helpers.js.map +1 -0
  63. package/dist/model/safe-query.d.ts +23 -0
  64. package/dist/model/safe-query.js +138 -0
  65. package/dist/model/safe-query.js.map +1 -0
  66. package/dist/model/ttl.d.ts +9 -0
  67. package/dist/model/ttl.js +73 -0
  68. package/dist/model/ttl.js.map +1 -0
  69. package/dist/model/validation.d.ts +5 -0
  70. package/dist/model/validation.js +104 -0
  71. package/dist/model/validation.js.map +1 -0
  72. package/dist/model/write-helpers.d.ts +39 -0
  73. package/dist/model/write-helpers.js +245 -0
  74. package/dist/model/write-helpers.js.map +1 -0
  75. package/dist/next.d.ts +204 -0
  76. package/dist/next.js +1190 -0
  77. package/dist/next.js.map +1 -0
  78. package/dist/next.types.d.ts +1 -0
  79. package/dist/next.types.js +38 -0
  80. package/dist/next.types.js.map +1 -0
  81. package/dist/pagination/index.d.ts +1 -0
  82. package/dist/pagination/index.js +1 -0
  83. package/dist/pagination/index.js.map +1 -1
  84. package/dist/pagination/pagination.d.ts +1 -8
  85. package/dist/pagination/pagination.js +16 -29
  86. package/dist/pagination/pagination.js.map +1 -1
  87. package/dist/pagination/safe-pagination.d.ts +15 -0
  88. package/dist/pagination/safe-pagination.js +221 -0
  89. package/dist/pagination/safe-pagination.js.map +1 -0
  90. package/dist/pagination/types.d.ts +11 -0
  91. package/dist/pagination/types.js +3 -0
  92. package/dist/pagination/types.js.map +1 -0
  93. package/dist/search/customQuery.d.ts +10 -3
  94. package/dist/search/customQuery.js +17 -18
  95. package/dist/search/customQuery.js.map +1 -1
  96. package/dist/shared/common.model.js +2 -2
  97. package/dist/shared/common.model.js.map +1 -1
  98. package/dist/timeseries/index.d.ts +1 -0
  99. package/dist/timeseries/index.js +18 -0
  100. package/dist/timeseries/index.js.map +1 -0
  101. package/dist/timeseries/time-series-model.d.ts +60 -0
  102. package/dist/timeseries/time-series-model.js +267 -0
  103. package/dist/timeseries/time-series-model.js.map +1 -0
  104. package/dist/utils/awaitTo.d.ts +1 -1
  105. package/dist/utils/awaitTo.js +1 -1
  106. package/dist/utils/awaitTo.js.map +1 -1
  107. package/docs/beta-migration.md +175 -0
  108. package/docs/document-modeling.md +133 -0
  109. package/docs/next-primitives.md +81 -0
  110. package/legacy/index.js +1 -0
  111. package/legacy/package.json +4 -0
  112. package/next/index.js +1 -0
  113. package/next/package.json +4 -0
  114. package/package.json +18 -10
  115. package/dist/connection.d.ts +0 -42
  116. package/dist/connection.js +0 -156
  117. package/dist/connection.js.map +0 -1
@@ -1,7 +1,36 @@
1
- import type { Collection, GetOptions, RemoveOptions, ReplaceOptions, UpsertOptions } from 'couchbase';
2
- import { CustomQueryPagination } from '../search';
1
+ import type { Collection, Cluster, GetOptions, MutateInOptions, RemoveOptions, ReplaceOptions } from 'couchbase';
3
2
  import { SchemaTypes } from '../utils';
4
- import CouchbaseConnection from '../connection';
3
+ import type { ModelPageResult, ModelReadArgs } from './read-helpers';
4
+ import type { QueryPageOptions, QueryPageResult, QueryParameters, SafeQueryOptions } from './safe-query';
5
+ import type { FindByIdWithMetaResult, InsertWriteOptions, PatchByIdArgs, UpsertWriteOptions } from './write-helpers';
6
+ import { Hydrated } from './hydrated-document';
7
+ import { EnsureIndexOptions, ModelIndexDefinition } from './indexes';
8
+ import type { ParseHook, ValidationHook } from './validation';
9
+ /** A small connection surface used by client-bound models. */
10
+ export interface ModelConnection {
11
+ bucket?: any;
12
+ bucketName?: string;
13
+ cluster: Cluster;
14
+ getBucket(): string;
15
+ getCollection(scopeName?: string, collectionName?: string): Collection;
16
+ isConnected(): boolean;
17
+ ready(): Promise<any>;
18
+ shouldReconnect(error: any): boolean;
19
+ markDisconnected(error: any): void;
20
+ }
21
+ /** Converts a field between the application's value and Couchbase JSON. */
22
+ export interface FieldCodec<T = any, TDatabase = any> {
23
+ toDatabase(value: T): TDatabase;
24
+ fromDatabase(value: TDatabase): T;
25
+ }
26
+ export type { ModelPageResult, ModelReadArgs } from './read-helpers';
27
+ export type { QueryLogger, QueryPageOptions, QueryPageResult, QueryParameters, SafeQueryOptions, } from './safe-query';
28
+ export type { FindByIdWithMetaResult, PatchByIdArgs } from './write-helpers';
29
+ export type { Hydrated } from './hydrated-document';
30
+ export type { IncludeDefinition, IncludeType } from './include';
31
+ export type { EnsureIndexOptions, ModelIndexDefinition } from './indexes';
32
+ export type { TtlOptions } from './ttl';
33
+ export type { ParseHook, ValidationHook } from './validation';
5
34
  export interface AutoModelFields {
6
35
  id: string;
7
36
  createdAt: Date;
@@ -13,91 +42,202 @@ export interface AutoModelFields {
13
42
  export interface UpdateOptions extends ReplaceOptions {
14
43
  silent?: boolean;
15
44
  }
45
+ export interface DeleteByIdOptions extends RemoveOptions {
46
+ hard?: boolean;
47
+ }
16
48
  export interface ModalOptions {
17
49
  scope?: string;
50
+ collection?: string;
18
51
  schema?: Record<string, SchemaTypes>;
19
- graphqlType?: any;
52
+ softDelete?: boolean;
53
+ defaultWhere?: any;
54
+ validateCreate?: ValidationHook;
55
+ validateReplace?: ValidationHook;
56
+ parse?: ParseHook;
57
+ dateFields?: string[];
58
+ /**
59
+ * Explicit field transforms. A field cannot also be a dateFields/schema date
60
+ * transform, since that makes the parse order ambiguous.
61
+ */
62
+ codecs?: Record<string, FieldCodec<any, any>>;
63
+ indexes?: ModelIndexDefinition[];
20
64
  }
65
+ export type ConsumeOnceResult = {
66
+ status: 'consumed';
67
+ } | {
68
+ status: 'missing';
69
+ } | {
70
+ status: 'conflict';
71
+ };
21
72
  export declare class Model {
73
+ private static registeredModels;
22
74
  collection: Collection;
23
75
  collectionName: string;
24
76
  scope: string;
77
+ private scopeName?;
78
+ private collectionTargetName?;
79
+ private defaultWhere?;
80
+ private defaultWhereMode;
81
+ private softDelete;
82
+ private validateCreateHook?;
83
+ private validateReplaceHook?;
84
+ private parseHook?;
85
+ private dateFields?;
86
+ private codecs;
87
+ private readonly connection?;
88
+ private indexes;
25
89
  schema: Record<string, SchemaTypes>;
26
- graphqlType: any;
27
- constructor(name: string, options?: ModalOptions);
90
+ constructor(name: string, options?: ModalOptions, connection?: ModelConnection);
28
91
  /**
29
- * Set setGraphqlType
30
- * setGraphqlType
92
+ * Ensure indexes for every constructed model with index declarations.
31
93
  */
32
- setGraphqlType(gqlType: any): Model;
94
+ static ensureIndexes(options?: EnsureIndexOptions): Promise<string[]>;
33
95
  /**
34
96
  * Refresh and get default collection from CouchbaseConnection
35
97
  * Because CouchbaseConnection is a singleton, sometimes it might be undefined depending when model was created
36
98
  * So we have to call it from all model methods
37
99
  * to avoid error `Cannot read property 'defaultCollection' of null`
38
100
  */
39
- fresh(): void;
101
+ private fresh;
102
+ private prepare;
103
+ private withConnection;
40
104
  /** Get this collection
41
105
  * getCollection
42
106
  */
43
- getBucketName(): string;
107
+ private getBucketName;
44
108
  /** Get this collection
45
109
  * getCollection
46
110
  */
47
- couchbaseConnection(): CouchbaseConnection;
111
+ private couchbaseConnection;
48
112
  /** Get this collection
49
113
  * getCollection
50
114
  */
51
115
  getCollection(): Collection;
116
+ private collectionTarget;
117
+ private queryResultKey;
118
+ private readContext;
119
+ private readArgs;
120
+ private writeContext;
121
+ private cloneWithDefaultWhereMode;
122
+ /**
123
+ * Return the active bucket as a SQL++ keyspace identifier.
124
+ */
125
+ bucket(): string;
126
+ /**
127
+ * Return this model's SQL++ keyspace target.
128
+ */
129
+ keyspace(): string;
130
+ /**
131
+ * Return a SQL++ FROM target for this model's current bucket.
132
+ */
133
+ from(alias?: string): string;
52
134
  /**
53
- * create
135
+ * Return CREATE INDEX statements for this model without executing them.
54
136
  */
55
- create<T>(data: T, options?: UpsertOptions): Promise<T & AutoModelFields>;
137
+ indexStatements(options?: EnsureIndexOptions): string[];
56
138
  /**
57
- * findById
139
+ * Create this model's declared indexes if Couchbase does not already have them.
58
140
  */
59
- findById(id: string, options?: GetOptions): Promise<any & AutoModelFields>;
141
+ ensureIndexes(options?: EnsureIndexOptions): Promise<string[]>;
60
142
  /**
61
- * update
143
+ * Run a SQL++ query and return rows directly.
62
144
  */
63
- updateById<T>(id: string, data: T, opt?: UpdateOptions): Promise<T>;
145
+ queryRows<T>(query: string, params?: QueryParameters, options?: SafeQueryOptions): Promise<T[]>;
64
146
  /**
65
- * save
147
+ * Run a SQL++ query and return the first row or null.
66
148
  */
67
- save<T>(data: T & {
149
+ queryOne<T>(query: string, params?: QueryParameters, options?: SafeQueryOptions): Promise<T | null>;
150
+ /**
151
+ * Run a SQL++ query using limit + 1 pagination.
152
+ */
153
+ queryPage<T>(query: string, params?: QueryParameters, options?: QueryPageOptions): Promise<QueryPageResult<T>>;
154
+ /**
155
+ * Find many model-scoped documents.
156
+ */
157
+ findMany<T>(args?: ModelReadArgs): Promise<T[]>;
158
+ /**
159
+ * Find the first matching model-scoped document.
160
+ */
161
+ findOne<T>(args?: ModelReadArgs): Promise<T | null>;
162
+ /**
163
+ * Check whether at least one model-scoped document matches.
164
+ */
165
+ exists(args?: ModelReadArgs): Promise<boolean>;
166
+ /**
167
+ * Count model-scoped documents.
168
+ */
169
+ count(args?: ModelReadArgs): Promise<number>;
170
+ /**
171
+ * Fetch a limit + 1 page of model-scoped documents.
172
+ */
173
+ page<T>(args?: ModelReadArgs): Promise<ModelPageResult<T>>;
174
+ withDeleted(): Model;
175
+ onlyDeleted(): Model;
176
+ withoutDefaultWhere(): Model;
177
+ /**
178
+ * Insert a new document and fail if the id already exists.
179
+ */
180
+ insert<T>(data: T, options?: InsertWriteOptions): Promise<T & AutoModelFields>;
181
+ /** @internal Prepare a transaction insert without issuing a normal SDK write. */
182
+ prepareInsert<T>(data: T): Promise<T & AutoModelFields>;
183
+ /**
184
+ * Explicit insert-or-replace write.
185
+ */
186
+ upsert<T>(data: T, options?: UpsertWriteOptions): Promise<T & AutoModelFields>;
187
+ getById<T>(id: string, options?: GetOptions): Promise<T & AutoModelFields>;
188
+ findDocById<T extends {
189
+ id: string;
190
+ }>(id: string, options?: GetOptions): Promise<Hydrated<T>>;
191
+ /**
192
+ * Find a document with SDK metadata.
193
+ */
194
+ findByIdWithMeta<T>(id: string, options?: GetOptions): Promise<FindByIdWithMetaResult<T>>;
195
+ /** Ergonomic alias for findByIdWithMeta, intended for CAS workflows. */
196
+ getWithCas<T>(id: string, options?: GetOptions): Promise<FindByIdWithMetaResult<T>>;
197
+ /**
198
+ * Explicit full-document replace.
199
+ */
200
+ replaceById<T>(id: string, data: T, options?: UpdateOptions): Promise<T & AutoModelFields>;
201
+ /** @internal Prepare a transaction replace without issuing a normal SDK write. */
202
+ prepareReplace<T>(id: string, data: T, options?: UpdateOptions): Promise<T & AutoModelFields>;
203
+ /**
204
+ * Replace only when the supplied CAS still describes the current document.
205
+ * CouchSet deliberately performs one SDK operation and never re-reads/retries
206
+ * with a new CAS, because doing so would defeat the caller's concurrency check.
207
+ */
208
+ replaceIfCas<T>(id: string, data: T, cas: any, options?: UpdateOptions): Promise<T & AutoModelFields>;
209
+ /**
210
+ * Remove a one-time document with exactly the CAS supplied by the caller.
211
+ * Missing and CAS-mismatch cases are normal outcomes; transport and ambiguous
212
+ * failures are rethrown so callers never mistake an unknown commit for success.
213
+ */
214
+ consumeOnce(id: string, cas: any): Promise<ConsumeOnceResult>;
215
+ /**
216
+ * Partial document mutation using common patch operators.
217
+ */
218
+ patchById<T>(id: string, patch: PatchByIdArgs, options?: MutateInOptions): Promise<T & AutoModelFields>;
219
+ /**
220
+ * Couchbase subdocument mutation escape hatch.
221
+ */
222
+ mutateById(id: string, specs: any[], options?: MutateInOptions): Promise<any>;
223
+ /**
224
+ * Atomic numeric field increment.
225
+ */
226
+ incrementById<T>(id: string, field: string, delta: number, options?: MutateInOptions): Promise<T & AutoModelFields>;
227
+ softDeleteById<T>(id: string, options?: MutateInOptions): Promise<T & AutoModelFields>;
228
+ restoreById<T>(id: string, options?: MutateInOptions): Promise<T & AutoModelFields>;
229
+ deleteById<T>(id: string, options?: DeleteByIdOptions): Promise<boolean | (T & AutoModelFields)>;
230
+ findDocOne<T extends {
231
+ id: string;
232
+ }>(args?: ModelReadArgs): Promise<Hydrated<T> | null>;
233
+ hydrate<T extends {
68
234
  id: string;
69
- }, opt?: UpdateOptions): Promise<T>;
70
- delete(id: string, options?: RemoveOptions): Promise<boolean>;
71
- /**
72
- * Pagination
73
- *
74
- select = ['id', 'createdAt']
75
- where = {
76
- where: { owner: { $eq: "stoqey" }, _type: { $eq: "Trade" } },
77
- },
78
- page = 0,
79
- limit = 10,
80
- orderBy = { createdAt: "DESC" },
81
- * @param args PaginationArgs
82
- */
83
- pagination({ select, where, orderBy, limit, page, customQuery, }: {
84
- select?: any[] | string;
85
- where?: any;
86
- orderBy?: any;
87
- limit?: number;
88
- page?: number;
89
- customQuery?: any;
90
- }): Promise<any[]>;
91
- /**
92
- * Run a custom query with model parsing
93
- * @param { select, query }
94
- * @returns
95
- */
96
- customQuery<T>({ params, limit, query, }: {
97
- params: any;
98
- limit: number;
99
- query: string;
100
- }): Promise<[T[], CustomQueryPagination]>;
235
+ }>(data: T & AutoModelFields): Hydrated<T>;
101
236
  parse<T>(data: T): T;
237
+ /** @internal Used by the typed client before a document reaches the SDK. */
238
+ serialize<T>(data: T): T;
239
+ /** @internal Used by patch helpers and transaction-bound models. */
240
+ serializeField(path: string, value: any): any;
241
+ private deserialize;
102
242
  }
103
243
  export default Model;