couchset 0.2.7 → 0.3.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 (108) hide show
  1. package/README.md +284 -153
  2. package/dist/connection/connection.d.ts +68 -0
  3. package/dist/connection/connection.js +485 -0
  4. package/dist/connection/connection.js.map +1 -0
  5. package/dist/connection/env.d.ts +2 -0
  6. package/dist/connection/env.js +16 -0
  7. package/dist/connection/env.js.map +1 -0
  8. package/dist/connection/index.d.ts +3 -0
  9. package/dist/connection/index.js +25 -0
  10. package/dist/connection/index.js.map +1 -0
  11. package/dist/connection/types.d.ts +25 -0
  12. package/dist/connection/types.js +3 -0
  13. package/dist/connection/types.js.map +1 -0
  14. package/dist/database.d.ts +9 -0
  15. package/dist/database.js +196 -0
  16. package/dist/database.js.map +1 -0
  17. package/dist/index.d.ts +18 -6
  18. package/dist/index.js +53 -8
  19. package/dist/index.js.map +1 -1
  20. package/dist/legacy/connection.d.ts +2 -0
  21. package/dist/legacy/connection.js +24 -0
  22. package/dist/legacy/connection.js.map +1 -0
  23. package/dist/legacy/index.d.ts +10 -0
  24. package/dist/legacy/index.js +93 -0
  25. package/dist/legacy/index.js.map +1 -0
  26. package/dist/legacy/model.d.ts +55 -0
  27. package/dist/legacy/model.js +282 -0
  28. package/dist/legacy/model.js.map +1 -0
  29. package/dist/legacy/pagination/index.d.ts +1 -0
  30. package/dist/legacy/pagination/index.js +18 -0
  31. package/dist/legacy/pagination/index.js.map +1 -0
  32. package/dist/legacy/pagination/pagination.d.ts +12 -0
  33. package/dist/legacy/pagination/pagination.js +85 -0
  34. package/dist/legacy/pagination/pagination.js.map +1 -0
  35. package/dist/legacy/search/customQuery.d.ts +13 -0
  36. package/dist/legacy/search/customQuery.js +73 -0
  37. package/dist/legacy/search/customQuery.js.map +1 -0
  38. package/dist/legacy/search/index.d.ts +1 -0
  39. package/dist/legacy/search/index.js +18 -0
  40. package/dist/legacy/search/index.js.map +1 -0
  41. package/dist/model/default-scope.d.ts +10 -0
  42. package/dist/model/default-scope.js +47 -0
  43. package/dist/model/default-scope.js.map +1 -0
  44. package/dist/model/hydrated-document.d.ts +30 -0
  45. package/dist/model/hydrated-document.js +118 -0
  46. package/dist/model/hydrated-document.js.map +1 -0
  47. package/dist/model/include.d.ts +33 -0
  48. package/dist/model/include.js +250 -0
  49. package/dist/model/include.js.map +1 -0
  50. package/dist/model/index.d.ts +137 -51
  51. package/dist/model/index.js +519 -174
  52. package/dist/model/index.js.map +1 -1
  53. package/dist/model/indexes.d.ts +16 -0
  54. package/dist/model/indexes.js +155 -0
  55. package/dist/model/indexes.js.map +1 -0
  56. package/dist/model/keyspace.d.ts +9 -0
  57. package/dist/model/keyspace.js +28 -0
  58. package/dist/model/keyspace.js.map +1 -0
  59. package/dist/model/read-helpers.d.ts +36 -0
  60. package/dist/model/read-helpers.js +275 -0
  61. package/dist/model/read-helpers.js.map +1 -0
  62. package/dist/model/safe-query.d.ts +23 -0
  63. package/dist/model/safe-query.js +138 -0
  64. package/dist/model/safe-query.js.map +1 -0
  65. package/dist/model/ttl.d.ts +9 -0
  66. package/dist/model/ttl.js +73 -0
  67. package/dist/model/ttl.js.map +1 -0
  68. package/dist/model/validation.d.ts +5 -0
  69. package/dist/model/validation.js +104 -0
  70. package/dist/model/validation.js.map +1 -0
  71. package/dist/model/write-helpers.d.ts +31 -0
  72. package/dist/model/write-helpers.js +220 -0
  73. package/dist/model/write-helpers.js.map +1 -0
  74. package/dist/pagination/index.d.ts +1 -0
  75. package/dist/pagination/index.js +1 -0
  76. package/dist/pagination/index.js.map +1 -1
  77. package/dist/pagination/pagination.d.ts +1 -8
  78. package/dist/pagination/pagination.js +16 -29
  79. package/dist/pagination/pagination.js.map +1 -1
  80. package/dist/pagination/safe-pagination.d.ts +15 -0
  81. package/dist/pagination/safe-pagination.js +221 -0
  82. package/dist/pagination/safe-pagination.js.map +1 -0
  83. package/dist/pagination/types.d.ts +11 -0
  84. package/dist/pagination/types.js +3 -0
  85. package/dist/pagination/types.js.map +1 -0
  86. package/dist/search/customQuery.d.ts +10 -3
  87. package/dist/search/customQuery.js +17 -18
  88. package/dist/search/customQuery.js.map +1 -1
  89. package/dist/shared/common.model.js +2 -2
  90. package/dist/shared/common.model.js.map +1 -1
  91. package/dist/timeseries/index.d.ts +1 -0
  92. package/dist/timeseries/index.js +18 -0
  93. package/dist/timeseries/index.js.map +1 -0
  94. package/dist/timeseries/time-series-model.d.ts +60 -0
  95. package/dist/timeseries/time-series-model.js +267 -0
  96. package/dist/timeseries/time-series-model.js.map +1 -0
  97. package/dist/utils/awaitTo.d.ts +1 -1
  98. package/dist/utils/awaitTo.js +1 -1
  99. package/dist/utils/awaitTo.js.map +1 -1
  100. package/docs/beta-migration.md +175 -0
  101. package/legacy/index.js +1 -0
  102. package/legacy/package.json +4 -0
  103. package/next/index.js +1 -0
  104. package/next/package.json +4 -0
  105. package/package.json +13 -8
  106. package/dist/connection.d.ts +0 -42
  107. package/dist/connection.js +0 -156
  108. package/dist/connection.js.map +0 -1
@@ -1,7 +1,19 @@
1
- import type { Collection, GetOptions, RemoveOptions, ReplaceOptions, UpsertOptions } from 'couchbase';
2
- import { CustomQueryPagination } from '../search';
1
+ import type { Collection, 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
+ export type { ModelPageResult, ModelReadArgs } from './read-helpers';
10
+ export type { QueryLogger, QueryPageOptions, QueryPageResult, QueryParameters, SafeQueryOptions, } from './safe-query';
11
+ export type { FindByIdWithMetaResult, PatchByIdArgs } from './write-helpers';
12
+ export type { Hydrated } from './hydrated-document';
13
+ export type { IncludeDefinition, IncludeType } from './include';
14
+ export type { EnsureIndexOptions, ModelIndexDefinition } from './indexes';
15
+ export type { TtlOptions } from './ttl';
16
+ export type { ParseHook, ValidationHook } from './validation';
5
17
  export interface AutoModelFields {
6
18
  id: string;
7
19
  createdAt: Date;
@@ -13,91 +25,165 @@ export interface AutoModelFields {
13
25
  export interface UpdateOptions extends ReplaceOptions {
14
26
  silent?: boolean;
15
27
  }
28
+ export interface DeleteByIdOptions extends RemoveOptions {
29
+ hard?: boolean;
30
+ }
16
31
  export interface ModalOptions {
17
32
  scope?: string;
33
+ collection?: string;
18
34
  schema?: Record<string, SchemaTypes>;
19
- graphqlType?: any;
35
+ softDelete?: boolean;
36
+ defaultWhere?: any;
37
+ validateCreate?: ValidationHook;
38
+ validateReplace?: ValidationHook;
39
+ parse?: ParseHook;
40
+ dateFields?: string[];
41
+ indexes?: ModelIndexDefinition[];
20
42
  }
21
43
  export declare class Model {
44
+ private static registeredModels;
22
45
  collection: Collection;
23
46
  collectionName: string;
24
47
  scope: string;
48
+ private scopeName?;
49
+ private collectionTargetName?;
50
+ private defaultWhere?;
51
+ private defaultWhereMode;
52
+ private softDelete;
53
+ private validateCreateHook?;
54
+ private validateReplaceHook?;
55
+ private parseHook?;
56
+ private dateFields?;
57
+ private indexes;
25
58
  schema: Record<string, SchemaTypes>;
26
- graphqlType: any;
27
59
  constructor(name: string, options?: ModalOptions);
28
60
  /**
29
- * Set setGraphqlType
30
- * setGraphqlType
61
+ * Ensure indexes for every constructed model with index declarations.
31
62
  */
32
- setGraphqlType(gqlType: any): Model;
63
+ static ensureIndexes(options?: EnsureIndexOptions): Promise<string[]>;
33
64
  /**
34
65
  * Refresh and get default collection from CouchbaseConnection
35
66
  * Because CouchbaseConnection is a singleton, sometimes it might be undefined depending when model was created
36
67
  * So we have to call it from all model methods
37
68
  * to avoid error `Cannot read property 'defaultCollection' of null`
38
69
  */
39
- fresh(): void;
70
+ private fresh;
71
+ private prepare;
72
+ private withConnection;
40
73
  /** Get this collection
41
74
  * getCollection
42
75
  */
43
- getBucketName(): string;
76
+ private getBucketName;
44
77
  /** Get this collection
45
78
  * getCollection
46
79
  */
47
- couchbaseConnection(): CouchbaseConnection;
80
+ private couchbaseConnection;
48
81
  /** Get this collection
49
82
  * getCollection
50
83
  */
51
84
  getCollection(): Collection;
85
+ private collectionTarget;
86
+ private queryResultKey;
87
+ private readContext;
88
+ private readArgs;
89
+ private writeContext;
90
+ private cloneWithDefaultWhereMode;
91
+ /**
92
+ * Return the active bucket as a SQL++ keyspace identifier.
93
+ */
94
+ bucket(): string;
95
+ /**
96
+ * Return this model's SQL++ keyspace target.
97
+ */
98
+ keyspace(): string;
99
+ /**
100
+ * Return a SQL++ FROM target for this model's current bucket.
101
+ */
102
+ from(alias?: string): string;
103
+ /**
104
+ * Return CREATE INDEX statements for this model without executing them.
105
+ */
106
+ indexStatements(options?: EnsureIndexOptions): string[];
52
107
  /**
53
- * create
108
+ * Create this model's declared indexes if Couchbase does not already have them.
54
109
  */
55
- create<T>(data: T, options?: UpsertOptions): Promise<T & AutoModelFields>;
110
+ ensureIndexes(options?: EnsureIndexOptions): Promise<string[]>;
56
111
  /**
57
- * findById
112
+ * Run a SQL++ query and return rows directly.
58
113
  */
59
- findById(id: string, options?: GetOptions): Promise<any & AutoModelFields>;
114
+ queryRows<T>(query: string, params?: QueryParameters, options?: SafeQueryOptions): Promise<T[]>;
60
115
  /**
61
- * update
116
+ * Run a SQL++ query and return the first row or null.
62
117
  */
63
- updateById<T>(id: string, data: T, opt?: UpdateOptions): Promise<T>;
118
+ queryOne<T>(query: string, params?: QueryParameters, options?: SafeQueryOptions): Promise<T | null>;
64
119
  /**
65
- * save
120
+ * Run a SQL++ query using limit + 1 pagination.
66
121
  */
67
- save<T>(data: T & {
122
+ queryPage<T>(query: string, params?: QueryParameters, options?: QueryPageOptions): Promise<QueryPageResult<T>>;
123
+ /**
124
+ * Find many model-scoped documents.
125
+ */
126
+ findMany<T>(args?: ModelReadArgs): Promise<T[]>;
127
+ /**
128
+ * Find the first matching model-scoped document.
129
+ */
130
+ findOne<T>(args?: ModelReadArgs): Promise<T | null>;
131
+ /**
132
+ * Check whether at least one model-scoped document matches.
133
+ */
134
+ exists(args?: ModelReadArgs): Promise<boolean>;
135
+ /**
136
+ * Count model-scoped documents.
137
+ */
138
+ count(args?: ModelReadArgs): Promise<number>;
139
+ /**
140
+ * Fetch a limit + 1 page of model-scoped documents.
141
+ */
142
+ page<T>(args?: ModelReadArgs): Promise<ModelPageResult<T>>;
143
+ withDeleted(): Model;
144
+ onlyDeleted(): Model;
145
+ withoutDefaultWhere(): Model;
146
+ /**
147
+ * Insert a new document and fail if the id already exists.
148
+ */
149
+ insert<T>(data: T, options?: InsertWriteOptions): Promise<T & AutoModelFields>;
150
+ /**
151
+ * Explicit insert-or-replace write.
152
+ */
153
+ upsert<T>(data: T, options?: UpsertWriteOptions): Promise<T & AutoModelFields>;
154
+ getById<T>(id: string, options?: GetOptions): Promise<T & AutoModelFields>;
155
+ findDocById<T extends {
156
+ id: string;
157
+ }>(id: string, options?: GetOptions): Promise<Hydrated<T>>;
158
+ /**
159
+ * Find a document with SDK metadata.
160
+ */
161
+ findByIdWithMeta<T>(id: string, options?: GetOptions): Promise<FindByIdWithMetaResult<T>>;
162
+ /**
163
+ * Explicit full-document replace.
164
+ */
165
+ replaceById<T>(id: string, data: T, options?: UpdateOptions): Promise<T & AutoModelFields>;
166
+ /**
167
+ * Partial document mutation using common patch operators.
168
+ */
169
+ patchById<T>(id: string, patch: PatchByIdArgs, options?: MutateInOptions): Promise<T & AutoModelFields>;
170
+ /**
171
+ * Couchbase subdocument mutation escape hatch.
172
+ */
173
+ mutateById(id: string, specs: any[], options?: MutateInOptions): Promise<any>;
174
+ /**
175
+ * Atomic numeric field increment.
176
+ */
177
+ incrementById<T>(id: string, field: string, delta: number, options?: MutateInOptions): Promise<T & AutoModelFields>;
178
+ softDeleteById<T>(id: string, options?: MutateInOptions): Promise<T & AutoModelFields>;
179
+ restoreById<T>(id: string, options?: MutateInOptions): Promise<T & AutoModelFields>;
180
+ deleteById<T>(id: string, options?: DeleteByIdOptions): Promise<boolean | (T & AutoModelFields)>;
181
+ findDocOne<T extends {
182
+ id: string;
183
+ }>(args?: ModelReadArgs): Promise<Hydrated<T> | null>;
184
+ hydrate<T extends {
68
185
  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]>;
186
+ }>(data: T & AutoModelFields): Hydrated<T>;
101
187
  parse<T>(data: T): T;
102
188
  }
103
189
  export default Model;