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.
- package/CHANGELOG.md +41 -0
- package/README.md +292 -152
- package/dist/connection/connection.d.ts +68 -0
- package/dist/connection/connection.js +495 -0
- package/dist/connection/connection.js.map +1 -0
- package/dist/connection/env.d.ts +2 -0
- package/dist/connection/env.js +16 -0
- package/dist/connection/env.js.map +1 -0
- package/dist/connection/index.d.ts +3 -0
- package/dist/connection/index.js +25 -0
- package/dist/connection/index.js.map +1 -0
- package/dist/connection/types.d.ts +25 -0
- package/dist/connection/types.js +3 -0
- package/dist/connection/types.js.map +1 -0
- package/dist/database.d.ts +9 -0
- package/dist/database.js +196 -0
- package/dist/database.js.map +1 -0
- package/dist/index.d.ts +20 -6
- package/dist/index.js +55 -8
- package/dist/index.js.map +1 -1
- package/dist/legacy/connection.d.ts +2 -0
- package/dist/legacy/connection.js +24 -0
- package/dist/legacy/connection.js.map +1 -0
- package/dist/legacy/index.d.ts +10 -0
- package/dist/legacy/index.js +93 -0
- package/dist/legacy/index.js.map +1 -0
- package/dist/legacy/model.d.ts +55 -0
- package/dist/legacy/model.js +282 -0
- package/dist/legacy/model.js.map +1 -0
- package/dist/legacy/pagination/index.d.ts +1 -0
- package/dist/legacy/pagination/index.js +18 -0
- package/dist/legacy/pagination/index.js.map +1 -0
- package/dist/legacy/pagination/pagination.d.ts +12 -0
- package/dist/legacy/pagination/pagination.js +85 -0
- package/dist/legacy/pagination/pagination.js.map +1 -0
- package/dist/legacy/search/customQuery.d.ts +13 -0
- package/dist/legacy/search/customQuery.js +73 -0
- package/dist/legacy/search/customQuery.js.map +1 -0
- package/dist/legacy/search/index.d.ts +1 -0
- package/dist/legacy/search/index.js +18 -0
- package/dist/legacy/search/index.js.map +1 -0
- package/dist/model/default-scope.d.ts +10 -0
- package/dist/model/default-scope.js +47 -0
- package/dist/model/default-scope.js.map +1 -0
- package/dist/model/hydrated-document.d.ts +30 -0
- package/dist/model/hydrated-document.js +118 -0
- package/dist/model/hydrated-document.js.map +1 -0
- package/dist/model/include.d.ts +33 -0
- package/dist/model/include.js +250 -0
- package/dist/model/include.js.map +1 -0
- package/dist/model/index.d.ts +192 -52
- package/dist/model/index.js +646 -172
- package/dist/model/index.js.map +1 -1
- package/dist/model/indexes.d.ts +18 -0
- package/dist/model/indexes.js +155 -0
- package/dist/model/indexes.js.map +1 -0
- package/dist/model/keyspace.d.ts +9 -0
- package/dist/model/keyspace.js +28 -0
- package/dist/model/keyspace.js.map +1 -0
- package/dist/model/read-helpers.d.ts +36 -0
- package/dist/model/read-helpers.js +275 -0
- package/dist/model/read-helpers.js.map +1 -0
- package/dist/model/safe-query.d.ts +23 -0
- package/dist/model/safe-query.js +138 -0
- package/dist/model/safe-query.js.map +1 -0
- package/dist/model/ttl.d.ts +9 -0
- package/dist/model/ttl.js +73 -0
- package/dist/model/ttl.js.map +1 -0
- package/dist/model/validation.d.ts +5 -0
- package/dist/model/validation.js +104 -0
- package/dist/model/validation.js.map +1 -0
- package/dist/model/write-helpers.d.ts +39 -0
- package/dist/model/write-helpers.js +245 -0
- package/dist/model/write-helpers.js.map +1 -0
- package/dist/next.d.ts +204 -0
- package/dist/next.js +1190 -0
- package/dist/next.js.map +1 -0
- package/dist/next.types.d.ts +1 -0
- package/dist/next.types.js +38 -0
- package/dist/next.types.js.map +1 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +1 -0
- package/dist/pagination/index.js.map +1 -1
- package/dist/pagination/pagination.d.ts +1 -8
- package/dist/pagination/pagination.js +16 -29
- package/dist/pagination/pagination.js.map +1 -1
- package/dist/pagination/safe-pagination.d.ts +15 -0
- package/dist/pagination/safe-pagination.js +221 -0
- package/dist/pagination/safe-pagination.js.map +1 -0
- package/dist/pagination/types.d.ts +11 -0
- package/dist/pagination/types.js +3 -0
- package/dist/pagination/types.js.map +1 -0
- package/dist/search/customQuery.d.ts +10 -3
- package/dist/search/customQuery.js +17 -18
- package/dist/search/customQuery.js.map +1 -1
- package/dist/shared/common.model.js +2 -2
- package/dist/shared/common.model.js.map +1 -1
- package/dist/timeseries/index.d.ts +1 -0
- package/dist/timeseries/index.js +18 -0
- package/dist/timeseries/index.js.map +1 -0
- package/dist/timeseries/time-series-model.d.ts +60 -0
- package/dist/timeseries/time-series-model.js +267 -0
- package/dist/timeseries/time-series-model.js.map +1 -0
- package/dist/utils/awaitTo.d.ts +1 -1
- package/dist/utils/awaitTo.js +1 -1
- package/dist/utils/awaitTo.js.map +1 -1
- package/docs/beta-migration.md +175 -0
- package/docs/document-modeling.md +133 -0
- package/docs/next-primitives.md +81 -0
- package/legacy/index.js +1 -0
- package/legacy/package.json +4 -0
- package/next/index.js +1 -0
- package/next/package.json +4 -0
- package/package.json +18 -10
- package/dist/connection.d.ts +0 -42
- package/dist/connection.js +0 -156
- package/dist/connection.js.map +0 -1
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.parseDateFields = exports.schemaFromDateFields = exports.applyValidation = void 0;
|
|
51
|
+
var applyValidation = function (document, hook) { return __awaiter(void 0, void 0, void 0, function () {
|
|
52
|
+
var result;
|
|
53
|
+
return __generator(this, function (_a) {
|
|
54
|
+
switch (_a.label) {
|
|
55
|
+
case 0:
|
|
56
|
+
if (!hook) {
|
|
57
|
+
return [2 /*return*/, document];
|
|
58
|
+
}
|
|
59
|
+
return [4 /*yield*/, hook(document)];
|
|
60
|
+
case 1:
|
|
61
|
+
result = _a.sent();
|
|
62
|
+
return [2 /*return*/, result === undefined ? document : result];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}); };
|
|
66
|
+
exports.applyValidation = applyValidation;
|
|
67
|
+
var schemaFromDateFields = function (dateFields) {
|
|
68
|
+
return (dateFields || []).reduce(function (schema, field) {
|
|
69
|
+
schema[field] = 'date';
|
|
70
|
+
return schema;
|
|
71
|
+
}, {});
|
|
72
|
+
};
|
|
73
|
+
exports.schemaFromDateFields = schemaFromDateFields;
|
|
74
|
+
var parseDateValue = function (value) {
|
|
75
|
+
if (value === undefined || value === null || value instanceof Date) {
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
return new Date(value);
|
|
79
|
+
};
|
|
80
|
+
var parseDateFields = function (document, dateFields) {
|
|
81
|
+
if (!dateFields || !dateFields.length || !document) {
|
|
82
|
+
return document;
|
|
83
|
+
}
|
|
84
|
+
var cloned = __assign({}, document);
|
|
85
|
+
dateFields.forEach(function (field) {
|
|
86
|
+
var parts = field.split('.');
|
|
87
|
+
var target = cloned;
|
|
88
|
+
for (var index = 0; index < parts.length - 1; index += 1) {
|
|
89
|
+
var part = parts[index];
|
|
90
|
+
if (!target[part]) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
target[part] = __assign({}, target[part]);
|
|
94
|
+
target = target[part];
|
|
95
|
+
}
|
|
96
|
+
var leaf = parts[parts.length - 1];
|
|
97
|
+
if (Object.prototype.hasOwnProperty.call(target, leaf)) {
|
|
98
|
+
target[leaf] = parseDateValue(target[leaf]);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
return cloned;
|
|
102
|
+
};
|
|
103
|
+
exports.parseDateFields = parseDateFields;
|
|
104
|
+
//# sourceMappingURL=validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/model/validation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGO,IAAM,eAAe,GAAG,UAAU,QAAW,EAAE,IAAwB;;;;;gBAC1E,IAAI,CAAC,IAAI,EAAE,CAAC;oBACR,sBAAO,QAAQ,EAAC;gBACpB,CAAC;gBAEc,qBAAM,IAAI,CAAC,QAAQ,CAAC,EAAA;;gBAA7B,MAAM,GAAG,SAAoB;gBAEnC,sBAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAE,MAAY,EAAC;;;KAC1D,CAAC;AARW,QAAA,eAAe,mBAQ1B;AAEK,IAAM,oBAAoB,GAAG,UAAC,UAAqB;IACtD,OAAO,CAAC,UAAU,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,UAAC,MAAM,EAAE,KAAK;QAC3C,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC;QAEvB,OAAO,MAAM,CAAC;IAClB,CAAC,EAAE,EAA4B,CAAC,CAAC;AACrC,CAAC,CAAC;AANW,QAAA,oBAAoB,wBAM/B;AAEF,IAAM,cAAc,GAAG,UAAC,KAAU;IAC9B,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,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3BW,QAAA,eAAe,mBA2B1B","sourcesContent":["export type ValidationHook<T = any> = (document: T) => T | void | Promise<T | void>;\nexport type ParseHook<T = any> = (document: T) => T;\n\nexport const applyValidation = async <T>(document: T, hook?: ValidationHook<T>): Promise<T> => {\n if (!hook) {\n return document;\n }\n\n const result = await hook(document);\n\n return result === undefined ? document : (result as T);\n};\n\nexport const schemaFromDateFields = (dateFields?: string[]): Record<string, 'date'> => {\n return (dateFields || []).reduce((schema, field) => {\n schema[field] = 'date';\n\n return schema;\n }, {} as Record<string, 'date'>);\n};\n\nconst parseDateValue = (value: any): any => {\n if (value === undefined || value === null || value instanceof Date) {\n return value;\n }\n\n return new Date(value);\n};\n\nexport const parseDateFields = <T>(document: T, dateFields?: string[]): T => {\n if (!dateFields || !dateFields.length || !document) {\n return document;\n }\n\n const cloned: any = {...(document as any)};\n\n dateFields.forEach((field) => {\n const parts = field.split('.');\n let target = cloned;\n\n for (let index = 0; index < parts.length - 1; index += 1) {\n const part = parts[index];\n if (!target[part]) {\n return;\n }\n target[part] = {...target[part]};\n target = target[part];\n }\n\n const leaf = parts[parts.length - 1];\n if (Object.prototype.hasOwnProperty.call(target, leaf)) {\n target[leaf] = parseDateValue(target[leaf]);\n }\n });\n\n return cloned;\n};\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Collection, GetOptions, InsertOptions, MutateInOptions, UpsertOptions } from 'couchbase';
|
|
2
|
+
import { TtlOptions } from './ttl';
|
|
3
|
+
import { ValidationHook } from './validation';
|
|
4
|
+
import type { AutoModelFields, UpdateOptions } from './index';
|
|
5
|
+
export interface ModelWriteContext {
|
|
6
|
+
collection: Collection;
|
|
7
|
+
collectionName: string;
|
|
8
|
+
scope: string;
|
|
9
|
+
parse: <T>(data: T) => T;
|
|
10
|
+
serialize?: <T>(data: T) => T;
|
|
11
|
+
serializeField?: (path: string, value: any) => any;
|
|
12
|
+
validateCreate?: ValidationHook;
|
|
13
|
+
validateReplace?: ValidationHook;
|
|
14
|
+
}
|
|
15
|
+
export interface PatchByIdArgs {
|
|
16
|
+
$set?: Record<string, any>;
|
|
17
|
+
$unset?: string[] | Record<string, any>;
|
|
18
|
+
$inc?: Record<string, number>;
|
|
19
|
+
}
|
|
20
|
+
export interface FindByIdWithMetaResult<T> {
|
|
21
|
+
content: T & AutoModelFields;
|
|
22
|
+
cas: any;
|
|
23
|
+
expiryTime?: any;
|
|
24
|
+
}
|
|
25
|
+
export type InsertWriteOptions = InsertOptions & TtlOptions;
|
|
26
|
+
export type UpsertWriteOptions = UpsertOptions & TtlOptions;
|
|
27
|
+
export declare const modelDocument: <T>(context: ModelWriteContext, data: T, createdAt?: Date) => T & AutoModelFields;
|
|
28
|
+
export declare const replacementDocument: <T>(context: ModelWriteContext, id: string, data: T, options?: UpdateOptions) => T & AutoModelFields;
|
|
29
|
+
/** Prepare an insert using the same metadata, validation, and codec pipeline as Model.insert. */
|
|
30
|
+
export declare const prepareInsertDocument: <T>(context: ModelWriteContext, data: T) => Promise<T & AutoModelFields>;
|
|
31
|
+
/** Prepare a replace using the same metadata, validation, and codec pipeline as Model.replaceById. */
|
|
32
|
+
export declare const prepareReplacementDocument: <T>(context: ModelWriteContext, id: string, data: T, options?: UpdateOptions) => Promise<T & AutoModelFields>;
|
|
33
|
+
export declare const insert: <T>(context: ModelWriteContext, data: T, options?: InsertWriteOptions) => Promise<T & AutoModelFields>;
|
|
34
|
+
export declare const upsert: <T>(context: ModelWriteContext, data: T, options?: UpsertWriteOptions) => Promise<T & AutoModelFields>;
|
|
35
|
+
export declare const replaceById: <T>(context: ModelWriteContext, id: string, data: T, options?: UpdateOptions) => Promise<T & AutoModelFields>;
|
|
36
|
+
export declare const mutateById: (context: ModelWriteContext, id: string, specs: any[], options?: MutateInOptions) => Promise<any>;
|
|
37
|
+
export declare const patchById: <T>(context: ModelWriteContext, id: string, patch: PatchByIdArgs, options?: MutateInOptions) => Promise<T & AutoModelFields>;
|
|
38
|
+
export declare const incrementById: <T>(context: ModelWriteContext, id: string, field: string, delta: number, options?: MutateInOptions) => Promise<T & AutoModelFields>;
|
|
39
|
+
export declare const findByIdWithMeta: <T>(context: ModelWriteContext, id: string, options?: GetOptions) => Promise<FindByIdWithMetaResult<T>>;
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
24
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
50
|
+
var t = {};
|
|
51
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
52
|
+
t[p] = s[p];
|
|
53
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
54
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
55
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
56
|
+
t[p[i]] = s[p[i]];
|
|
57
|
+
}
|
|
58
|
+
return t;
|
|
59
|
+
};
|
|
60
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
61
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
62
|
+
if (!m) return o;
|
|
63
|
+
var i = m.call(o), r, ar = [], e;
|
|
64
|
+
try {
|
|
65
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
66
|
+
}
|
|
67
|
+
catch (error) { e = { error: error }; }
|
|
68
|
+
finally {
|
|
69
|
+
try {
|
|
70
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
71
|
+
}
|
|
72
|
+
finally { if (e) throw e.error; }
|
|
73
|
+
}
|
|
74
|
+
return ar;
|
|
75
|
+
};
|
|
76
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
77
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
78
|
+
if (ar || !(i in from)) {
|
|
79
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
80
|
+
ar[i] = from[i];
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
84
|
+
};
|
|
85
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
86
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
87
|
+
};
|
|
88
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
|
+
exports.findByIdWithMeta = exports.incrementById = exports.patchById = exports.mutateById = exports.replaceById = exports.upsert = exports.insert = exports.prepareReplacementDocument = exports.prepareInsertDocument = exports.replacementDocument = exports.modelDocument = void 0;
|
|
90
|
+
var couchbase_1 = __importDefault(require("../couchbase"));
|
|
91
|
+
var uuid_1 = require("../uuid");
|
|
92
|
+
var ttl_1 = require("./ttl");
|
|
93
|
+
var validation_1 = require("./validation");
|
|
94
|
+
var now = function () { return new Date(); };
|
|
95
|
+
var modelDocument = function (context, data, createdAt) {
|
|
96
|
+
var id = (0, uuid_1.generateUUID)();
|
|
97
|
+
var timestamp = now();
|
|
98
|
+
return __assign(__assign({ id: id }, data), { createdAt: createdAt || timestamp, updatedAt: timestamp, _type: context.collectionName, _scope: context.scope });
|
|
99
|
+
};
|
|
100
|
+
exports.modelDocument = modelDocument;
|
|
101
|
+
var replacementDocument = function (context, id, data, options) {
|
|
102
|
+
var document = __assign(__assign({}, data), { id: id, _type: context.collectionName, _scope: context.scope });
|
|
103
|
+
if (!options || !options.silent) {
|
|
104
|
+
document.updatedAt = now();
|
|
105
|
+
}
|
|
106
|
+
return document;
|
|
107
|
+
};
|
|
108
|
+
exports.replacementDocument = replacementDocument;
|
|
109
|
+
var unsetPaths = function (value) {
|
|
110
|
+
if (!value) {
|
|
111
|
+
return [];
|
|
112
|
+
}
|
|
113
|
+
return Array.isArray(value) ? value : Object.keys(value);
|
|
114
|
+
};
|
|
115
|
+
/** Prepare an insert using the same metadata, validation, and codec pipeline as Model.insert. */
|
|
116
|
+
var prepareInsertDocument = function (context, data) { return __awaiter(void 0, void 0, void 0, function () {
|
|
117
|
+
var validated;
|
|
118
|
+
return __generator(this, function (_a) {
|
|
119
|
+
switch (_a.label) {
|
|
120
|
+
case 0: return [4 /*yield*/, (0, validation_1.applyValidation)((0, exports.modelDocument)(context, data), context.validateCreate)];
|
|
121
|
+
case 1:
|
|
122
|
+
validated = _a.sent();
|
|
123
|
+
return [2 /*return*/, context.serialize ? context.serialize(validated) : validated];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}); };
|
|
127
|
+
exports.prepareInsertDocument = prepareInsertDocument;
|
|
128
|
+
/** Prepare a replace using the same metadata, validation, and codec pipeline as Model.replaceById. */
|
|
129
|
+
var prepareReplacementDocument = function (context, id, data, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
130
|
+
var validated;
|
|
131
|
+
return __generator(this, function (_a) {
|
|
132
|
+
switch (_a.label) {
|
|
133
|
+
case 0: return [4 /*yield*/, (0, validation_1.applyValidation)((0, exports.replacementDocument)(context, id, data, options), context.validateReplace)];
|
|
134
|
+
case 1:
|
|
135
|
+
validated = _a.sent();
|
|
136
|
+
return [2 /*return*/, context.serialize ? context.serialize(validated) : validated];
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}); };
|
|
140
|
+
exports.prepareReplacementDocument = prepareReplacementDocument;
|
|
141
|
+
var insert = function (context, data, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
142
|
+
var stored;
|
|
143
|
+
return __generator(this, function (_a) {
|
|
144
|
+
switch (_a.label) {
|
|
145
|
+
case 0: return [4 /*yield*/, (0, exports.prepareInsertDocument)(context, data)];
|
|
146
|
+
case 1:
|
|
147
|
+
stored = _a.sent();
|
|
148
|
+
return [4 /*yield*/, context.collection.insert(stored.id, stored, (0, ttl_1.applyTtlOptions)(options))];
|
|
149
|
+
case 2:
|
|
150
|
+
_a.sent();
|
|
151
|
+
return [2 /*return*/, context.parse(stored)];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}); };
|
|
155
|
+
exports.insert = insert;
|
|
156
|
+
var upsert = function (context, data, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
157
|
+
var stored;
|
|
158
|
+
return __generator(this, function (_a) {
|
|
159
|
+
switch (_a.label) {
|
|
160
|
+
case 0: return [4 /*yield*/, (0, exports.prepareInsertDocument)(context, data)];
|
|
161
|
+
case 1:
|
|
162
|
+
stored = _a.sent();
|
|
163
|
+
return [4 /*yield*/, context.collection.upsert(stored.id, stored, (0, ttl_1.applyTtlOptions)(options))];
|
|
164
|
+
case 2:
|
|
165
|
+
_a.sent();
|
|
166
|
+
return [2 /*return*/, context.parse(stored)];
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}); };
|
|
170
|
+
exports.upsert = upsert;
|
|
171
|
+
var replaceById = function (context, id, data, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
172
|
+
var _a, silent, replaceOptions, stored;
|
|
173
|
+
return __generator(this, function (_b) {
|
|
174
|
+
switch (_b.label) {
|
|
175
|
+
case 0:
|
|
176
|
+
_a = options || {}, silent = _a.silent, replaceOptions = __rest(_a, ["silent"]);
|
|
177
|
+
return [4 /*yield*/, (0, exports.prepareReplacementDocument)(context, id, data, { silent: silent })];
|
|
178
|
+
case 1:
|
|
179
|
+
stored = _b.sent();
|
|
180
|
+
return [4 /*yield*/, context.collection.replace(id, stored, replaceOptions)];
|
|
181
|
+
case 2:
|
|
182
|
+
_b.sent();
|
|
183
|
+
return [2 /*return*/, context.parse(stored)];
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}); };
|
|
187
|
+
exports.replaceById = replaceById;
|
|
188
|
+
var mutateById = function (context, id, specs, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
189
|
+
return __generator(this, function (_a) {
|
|
190
|
+
if (!Array.isArray(specs) || !specs.length) {
|
|
191
|
+
throw new Error('mutateById requires at least one mutation spec');
|
|
192
|
+
}
|
|
193
|
+
return [2 /*return*/, context.collection.mutateIn(id, specs, options)];
|
|
194
|
+
});
|
|
195
|
+
}); };
|
|
196
|
+
exports.mutateById = mutateById;
|
|
197
|
+
var patchById = function (context, id, patch, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
198
|
+
var specs, data;
|
|
199
|
+
return __generator(this, function (_a) {
|
|
200
|
+
switch (_a.label) {
|
|
201
|
+
case 0:
|
|
202
|
+
specs = __spreadArray(__spreadArray(__spreadArray(__spreadArray([], __read(Object.keys(patch.$set || {}).map(function (path) {
|
|
203
|
+
return couchbase_1.default.MutateInSpec.upsert(path, context.serializeField
|
|
204
|
+
? context.serializeField(path, patch.$set[path])
|
|
205
|
+
: patch.$set[path]);
|
|
206
|
+
})), false), __read(unsetPaths(patch.$unset).map(function (path) { return couchbase_1.default.MutateInSpec.remove(path); })), false), __read(Object.keys(patch.$inc || {}).map(function (path) {
|
|
207
|
+
return couchbase_1.default.MutateInSpec.increment(path, patch.$inc[path]);
|
|
208
|
+
})), false), [
|
|
209
|
+
couchbase_1.default.MutateInSpec.upsert('updatedAt', now()),
|
|
210
|
+
], false);
|
|
211
|
+
return [4 /*yield*/, (0, exports.mutateById)(context, id, specs, options)];
|
|
212
|
+
case 1:
|
|
213
|
+
_a.sent();
|
|
214
|
+
return [4 /*yield*/, context.collection.get(id)];
|
|
215
|
+
case 2:
|
|
216
|
+
data = _a.sent();
|
|
217
|
+
return [2 /*return*/, context.parse(data.content)];
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}); };
|
|
221
|
+
exports.patchById = patchById;
|
|
222
|
+
var incrementById = function (context, id, field, delta, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
223
|
+
var _a;
|
|
224
|
+
return __generator(this, function (_b) {
|
|
225
|
+
return [2 /*return*/, (0, exports.patchById)(context, id, { $inc: (_a = {}, _a[field] = delta, _a) }, options)];
|
|
226
|
+
});
|
|
227
|
+
}); };
|
|
228
|
+
exports.incrementById = incrementById;
|
|
229
|
+
var findByIdWithMeta = function (context, id, options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
230
|
+
var data;
|
|
231
|
+
return __generator(this, function (_a) {
|
|
232
|
+
switch (_a.label) {
|
|
233
|
+
case 0: return [4 /*yield*/, context.collection.get(id, options)];
|
|
234
|
+
case 1:
|
|
235
|
+
data = _a.sent();
|
|
236
|
+
return [2 /*return*/, {
|
|
237
|
+
content: context.parse(data.content),
|
|
238
|
+
cas: data.cas,
|
|
239
|
+
expiryTime: data.expiryTime,
|
|
240
|
+
}];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}); };
|
|
244
|
+
exports.findByIdWithMeta = findByIdWithMeta;
|
|
245
|
+
//# sourceMappingURL=write-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"write-helpers.js","sourceRoot":"","sources":["../../src/model/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vC,IAAI,GAAG,SAAgC;gBAE7C,sBAAO,OAAO,CAAC,KAAK,CAAsB,IAAI,CAAC,OAAO,CAAC,EAAC;;;KAC3D,CAAC;AA1BW,QAAA,SAAS,aA0BpB;AAEK,IAAM,aAAa,GAAG,UACzB,OAA0B,EAC1B,EAAU,EACV,KAAa,EACb,KAAa,EACb,OAAyB;;;QAEzB,sBAAO,IAAA,iBAAS,EAAI,OAAO,EAAE,EAAE,EAAE,EAAC,IAAI,YAAG,GAAC,KAAK,IAAG,KAAK,KAAC,EAAC,EAAE,OAAO,CAAC,EAAC;;KACvE,CAAC;AARW,QAAA,aAAa,iBAQxB;AAEK,IAAM,gBAAgB,GAAG,UAC5B,OAA0B,EAC1B,EAAU,EACV,OAAoB;;;;oBAEP,qBAAM,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,EAAE,OAAO,CAAC,EAAA;;gBAAhD,IAAI,GAAG,SAAyC;gBAEtD,sBAAO;wBACH,OAAO,EAAE,OAAO,CAAC,KAAK,CAAsB,IAAI,CAAC,OAAO,CAAC;wBACzD,GAAG,EAAE,IAAI,CAAC,GAAG;wBACb,UAAU,EAAE,IAAI,CAAC,UAAU;qBAC9B,EAAC;;;KACL,CAAC;AAZW,QAAA,gBAAgB,oBAY3B","sourcesContent":["import type {\n Collection,\n GetOptions,\n InsertOptions,\n MutateInOptions,\n ReplaceOptions,\n UpsertOptions,\n} from 'couchbase';\n\nimport couchbase from '../couchbase';\nimport {generateUUID} from '../uuid';\n\nimport {applyTtlOptions, TtlOptions} from './ttl';\nimport {applyValidation, ValidationHook} from './validation';\n\nimport type {AutoModelFields, UpdateOptions} from './index';\n\nexport interface ModelWriteContext {\n collection: Collection;\n collectionName: string;\n scope: string;\n parse: <T>(data: T) => T;\n serialize?: <T>(data: T) => T;\n serializeField?: (path: string, value: any) => any;\n validateCreate?: ValidationHook;\n validateReplace?: ValidationHook;\n}\n\nexport interface PatchByIdArgs {\n $set?: Record<string, any>;\n $unset?: string[] | Record<string, any>;\n $inc?: Record<string, number>;\n}\n\nexport interface FindByIdWithMetaResult<T> {\n content: T & AutoModelFields;\n cas: any;\n expiryTime?: any;\n}\n\nexport type InsertWriteOptions = InsertOptions & TtlOptions;\nexport type UpsertWriteOptions = UpsertOptions & TtlOptions;\n\nconst now = (): Date => new Date();\n\nexport const modelDocument = <T>(\n context: ModelWriteContext,\n data: T,\n createdAt?: Date\n): T & AutoModelFields => {\n const id = generateUUID();\n const timestamp = now();\n\n return {\n id,\n ...data,\n createdAt: createdAt || timestamp,\n updatedAt: timestamp,\n _type: context.collectionName,\n _scope: context.scope,\n } as T & AutoModelFields;\n};\n\nexport const replacementDocument = <T>(\n context: ModelWriteContext,\n id: string,\n data: T,\n options?: UpdateOptions\n): T & AutoModelFields => {\n const document: any = {\n ...data,\n id,\n _type: context.collectionName,\n _scope: context.scope,\n };\n\n if (!options || !options.silent) {\n document.updatedAt = now();\n }\n\n return document;\n};\n\nconst unsetPaths = (value?: string[] | Record<string, any>): string[] => {\n if (!value) {\n return [];\n }\n\n return Array.isArray(value) ? value : Object.keys(value);\n};\n\n/** Prepare an insert using the same metadata, validation, and codec pipeline as Model.insert. */\nexport const prepareInsertDocument = async <T>(\n context: ModelWriteContext,\n data: T\n): Promise<T & AutoModelFields> => {\n const validated = await applyValidation(\n modelDocument<T>(context, data),\n context.validateCreate\n );\n return context.serialize ? context.serialize(validated) : validated;\n};\n\n/** Prepare a replace using the same metadata, validation, and codec pipeline as Model.replaceById. */\nexport const prepareReplacementDocument = async <T>(\n context: ModelWriteContext,\n id: string,\n data: T,\n options?: UpdateOptions\n): Promise<T & AutoModelFields> => {\n const validated = await applyValidation(\n replacementDocument<T>(context, id, data, options),\n context.validateReplace\n );\n return context.serialize ? context.serialize(validated) : validated;\n};\n\nexport const insert = async <T>(\n context: ModelWriteContext,\n data: T,\n options?: InsertWriteOptions\n): Promise<T & AutoModelFields> => {\n const stored = await prepareInsertDocument<T>(context, data);\n await context.collection.insert(stored.id, stored, applyTtlOptions(options));\n\n return context.parse<T & AutoModelFields>(stored);\n};\n\nexport const upsert = async <T>(\n context: ModelWriteContext,\n data: T,\n options?: UpsertWriteOptions\n): Promise<T & AutoModelFields> => {\n const stored = await prepareInsertDocument<T>(context, data);\n await context.collection.upsert(stored.id, stored, applyTtlOptions(options));\n\n return context.parse<T & AutoModelFields>(stored);\n};\n\nexport const replaceById = async <T>(\n context: ModelWriteContext,\n id: string,\n data: T,\n options?: UpdateOptions\n): Promise<T & AutoModelFields> => {\n const {silent, ...replaceOptions} = options || {};\n const stored = await prepareReplacementDocument<T>(context, id, data, {silent});\n await context.collection.replace(id, stored, replaceOptions as ReplaceOptions);\n\n return context.parse<T & AutoModelFields>(stored);\n};\n\nexport const mutateById = async (\n context: ModelWriteContext,\n id: string,\n specs: any[],\n options?: MutateInOptions\n): Promise<any> => {\n if (!Array.isArray(specs) || !specs.length) {\n throw new Error('mutateById requires at least one mutation spec');\n }\n\n return context.collection.mutateIn(id, specs, options);\n};\n\nexport const patchById = async <T>(\n context: ModelWriteContext,\n id: string,\n patch: PatchByIdArgs,\n options?: MutateInOptions\n): Promise<T & AutoModelFields> => {\n const specs = [\n ...Object.keys(patch.$set || {}).map((path) =>\n couchbase.MutateInSpec.upsert(\n path,\n context.serializeField\n ? context.serializeField(path, patch.$set[path])\n : patch.$set[path]\n )\n ),\n ...unsetPaths(patch.$unset).map((path) => couchbase.MutateInSpec.remove(path)),\n ...Object.keys(patch.$inc || {}).map((path) =>\n couchbase.MutateInSpec.increment(path, patch.$inc[path])\n ),\n couchbase.MutateInSpec.upsert('updatedAt', now()),\n ];\n\n await mutateById(context, id, specs, options);\n const data = await context.collection.get(id);\n\n return context.parse<T & AutoModelFields>(data.content);\n};\n\nexport const incrementById = async <T>(\n context: ModelWriteContext,\n id: string,\n field: string,\n delta: number,\n options?: MutateInOptions\n): Promise<T & AutoModelFields> => {\n return patchById<T>(context, id, {$inc: {[field]: delta}}, options);\n};\n\nexport const findByIdWithMeta = async <T>(\n context: ModelWriteContext,\n id: string,\n options?: GetOptions\n): Promise<FindByIdWithMetaResult<T>> => {\n const data = await context.collection.get(id, options);\n\n return {\n content: context.parse<T & AutoModelFields>(data.content),\n cas: data.cas,\n expiryTime: data.expiryTime,\n };\n};\n"]}
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { Bucket, Cluster } from 'couchbase';
|
|
2
|
+
import type { CouchsetArgs } from './connection';
|
|
3
|
+
import { AutoModelFields, FieldCodec, Model, ModelConnection, ModalOptions } from './model';
|
|
4
|
+
import type { ModelIndexDefinition } from './model';
|
|
5
|
+
import type { SortType } from './query/interface/query.types';
|
|
6
|
+
export type { FieldCodec } from './model';
|
|
7
|
+
/** A JSON-friendly Date codec for models declared with defineModel. */
|
|
8
|
+
export declare const dateCodec: FieldCodec<Date, string>;
|
|
9
|
+
export interface ModelCollectionSettings {
|
|
10
|
+
maxExpiry?: number;
|
|
11
|
+
history?: boolean;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A model definition has no connection and performs no I/O. Definitions may be
|
|
15
|
+
* reused safely across clients and are the unit the client registers.
|
|
16
|
+
*/
|
|
17
|
+
export type ModelField<T> = Extract<keyof T, string>;
|
|
18
|
+
export type TypedIndexField<T> = ModelField<T> | Partial<Record<ModelField<T>, SortType>>;
|
|
19
|
+
export type ModelCodecs<T> = Partial<{
|
|
20
|
+
[Field in ModelField<T>]: FieldCodec<T[Field], any>;
|
|
21
|
+
}> & Partial<Record<`${string}.${string}`, FieldCodec<any, any>>>;
|
|
22
|
+
export interface TypedModelIndexDefinition<T> extends Omit<ModelIndexDefinition, 'fields'> {
|
|
23
|
+
fields?: TypedIndexField<T>[];
|
|
24
|
+
}
|
|
25
|
+
export interface ModelDefinition<T = any> extends Omit<ModalOptions, 'codecs' | 'indexes'> {
|
|
26
|
+
name: string;
|
|
27
|
+
collectionSettings?: ModelCollectionSettings;
|
|
28
|
+
codecs?: ModelCodecs<T>;
|
|
29
|
+
indexes?: TypedModelIndexDefinition<T>[];
|
|
30
|
+
}
|
|
31
|
+
/** User fields are distinct from CouchSet's generated persistence metadata. */
|
|
32
|
+
export type ModelCreateInput<T> = Omit<T, keyof AutoModelFields> & Partial<Pick<T, Extract<keyof T, keyof AutoModelFields>>>;
|
|
33
|
+
export interface TypedModel<T> extends Omit<Model, 'getById' | 'getWithCas' | 'insert' | 'replaceById' | 'replaceIfCas'> {
|
|
34
|
+
getById(id: string, options?: any): Promise<T & AutoModelFields>;
|
|
35
|
+
getWithCas(id: string, options?: any): Promise<{
|
|
36
|
+
content: T & AutoModelFields;
|
|
37
|
+
cas: any;
|
|
38
|
+
expiryTime?: any;
|
|
39
|
+
}>;
|
|
40
|
+
insert(data: ModelCreateInput<T>, options?: any): Promise<T & AutoModelFields>;
|
|
41
|
+
replaceById(id: string, data: T, options?: any): Promise<T & AutoModelFields>;
|
|
42
|
+
replaceIfCas(id: string, data: T, cas: any, options?: any): Promise<T & AutoModelFields>;
|
|
43
|
+
}
|
|
44
|
+
export declare const defineModel: <T = any>(definition: ModelDefinition<T>) => ModelDefinition<T>;
|
|
45
|
+
export interface CouchsetClientDependencies {
|
|
46
|
+
/** Supply a fully controlled runtime for tests or an application-owned connection. */
|
|
47
|
+
connection?: ModelConnection;
|
|
48
|
+
/** Supply an already connected SDK cluster/bucket without using couchbase.connect. */
|
|
49
|
+
cluster?: Cluster;
|
|
50
|
+
bucket?: Bucket;
|
|
51
|
+
/** Override connection construction; useful for dependency injection and test doubles. */
|
|
52
|
+
connect?: (settings: CouchsetArgs) => Promise<Cluster>;
|
|
53
|
+
}
|
|
54
|
+
export interface CouchsetClientOptions extends Partial<CouchsetArgs> {
|
|
55
|
+
models?: Array<ModelDefinition<any>>;
|
|
56
|
+
dependencies?: CouchsetClientDependencies;
|
|
57
|
+
}
|
|
58
|
+
export interface EnsureCollectionsOptions {
|
|
59
|
+
models?: Array<ModelDefinition<any>>;
|
|
60
|
+
timeoutMs?: number;
|
|
61
|
+
pollIntervalMs?: number;
|
|
62
|
+
}
|
|
63
|
+
export interface ProvisionOptions extends EnsureCollectionsOptions {
|
|
64
|
+
collections?: boolean;
|
|
65
|
+
indexes?: boolean;
|
|
66
|
+
waitForIndexes?: boolean;
|
|
67
|
+
}
|
|
68
|
+
export interface RegisterModelOptions extends ProvisionOptions {
|
|
69
|
+
/** Explicitly provision this dynamic model. Constructors and CRUD never do this. */
|
|
70
|
+
provision?: boolean | {
|
|
71
|
+
collections?: boolean;
|
|
72
|
+
indexes?: boolean;
|
|
73
|
+
waitForIndexes?: boolean;
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export interface ProvisionedTarget {
|
|
77
|
+
scope: string;
|
|
78
|
+
collection: string;
|
|
79
|
+
createdScope: boolean;
|
|
80
|
+
createdCollection: boolean;
|
|
81
|
+
}
|
|
82
|
+
export interface IndexPlanItem {
|
|
83
|
+
status: 'create' | 'matching' | 'replace';
|
|
84
|
+
definition: ModelIndexDefinition;
|
|
85
|
+
keyspace: string;
|
|
86
|
+
target: CollectionTarget;
|
|
87
|
+
/** The physical index CouchSet will create. Replacement names are versioned. */
|
|
88
|
+
createAs: string;
|
|
89
|
+
/** Only populated for an explicit or detected safe replacement. */
|
|
90
|
+
replaces?: string;
|
|
91
|
+
reason?: string;
|
|
92
|
+
}
|
|
93
|
+
export interface IndexPlan {
|
|
94
|
+
items: IndexPlanItem[];
|
|
95
|
+
}
|
|
96
|
+
export interface ApplyIndexPlanOptions {
|
|
97
|
+
/** Opt-in only: drop old indexes after the replacement is online. */
|
|
98
|
+
dropReplaced?: boolean;
|
|
99
|
+
timeoutMs?: number;
|
|
100
|
+
}
|
|
101
|
+
interface CollectionTarget {
|
|
102
|
+
scope: string;
|
|
103
|
+
collection: string;
|
|
104
|
+
settings?: ModelCollectionSettings;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* A client owns its model-definition registry and connection. It intentionally
|
|
108
|
+
* does not alter the legacy singleton or register models globally.
|
|
109
|
+
*/
|
|
110
|
+
export declare class CouchsetClient {
|
|
111
|
+
private readonly connection;
|
|
112
|
+
private readonly registered;
|
|
113
|
+
private readonly settings;
|
|
114
|
+
constructor(options?: CouchsetClientOptions);
|
|
115
|
+
ready(): Promise<this>;
|
|
116
|
+
shutdown(): Promise<void>;
|
|
117
|
+
definitions(): ModelDefinition<any>[];
|
|
118
|
+
/** Bind and register a definition without provisioning or index DDL. */
|
|
119
|
+
model<T>(definition: ModelDefinition<T>): TypedModel<T>;
|
|
120
|
+
/**
|
|
121
|
+
* Async registration for dynamic models. DDL is performed only when the
|
|
122
|
+
* caller explicitly opts into provision; ordinary model() is side-effect-free.
|
|
123
|
+
*/
|
|
124
|
+
registerModel<T>(definition: ModelDefinition<T>, options?: RegisterModelOptions): Promise<TypedModel<T>>;
|
|
125
|
+
/** Create only missing scopes/collections. Existing resources are never altered. */
|
|
126
|
+
ensureCollections(options?: EnsureCollectionsOptions): Promise<ProvisionedTarget[]>;
|
|
127
|
+
/** Explicit bootstrap: provisioning occurs before index creation. */
|
|
128
|
+
provision(options?: ProvisionOptions): Promise<{
|
|
129
|
+
collections: ProvisionedTarget[];
|
|
130
|
+
indexes: string[];
|
|
131
|
+
}>;
|
|
132
|
+
/** Normal ensureIndexes stays create-only. It never checks, drops, or changes an existing index. */
|
|
133
|
+
ensureIndexes(options?: {
|
|
134
|
+
models?: Array<ModelDefinition<any>>;
|
|
135
|
+
deferred?: boolean;
|
|
136
|
+
waitForIndexes?: boolean;
|
|
137
|
+
timeoutMs?: number;
|
|
138
|
+
}): Promise<string[]>;
|
|
139
|
+
/** Report missing, matching, and drifted indexes; this method has no DDL side effects. */
|
|
140
|
+
planIndexes(options?: {
|
|
141
|
+
models?: Array<ModelDefinition<any>>;
|
|
142
|
+
}): Promise<IndexPlan>;
|
|
143
|
+
/**
|
|
144
|
+
* Apply a reviewed plan by creating versioned replacements and waiting for
|
|
145
|
+
* them. Dropping old indexes is deliberately an explicit second opt-in.
|
|
146
|
+
*/
|
|
147
|
+
applyIndexPlan(plan: IndexPlan, options?: ApplyIndexPlanOptions): Promise<string[]>;
|
|
148
|
+
/**
|
|
149
|
+
* Run the callback through Couchbase's transaction retry loop. The callback
|
|
150
|
+
* may execute more than once; do not perform external side effects inside it.
|
|
151
|
+
* A commit-ambiguous error is rethrown and must be resolved by the caller via
|
|
152
|
+
* an idempotency key/outbox, never by blindly retrying a side effect.
|
|
153
|
+
*/
|
|
154
|
+
transaction<T>(callback: (transaction: CouchsetTransaction) => Promise<T>): Promise<T>;
|
|
155
|
+
keyspace(definition: ModelDefinition<any>): string;
|
|
156
|
+
getConnection(): ModelConnection;
|
|
157
|
+
private targets;
|
|
158
|
+
private waitForCollectionManifest;
|
|
159
|
+
private waitForIndexes;
|
|
160
|
+
private indexCatalog;
|
|
161
|
+
}
|
|
162
|
+
/** A model bound strictly to a Couchbase transaction attempt. */
|
|
163
|
+
export declare class TransactionModel<T = any> {
|
|
164
|
+
private readonly model;
|
|
165
|
+
private readonly attempt;
|
|
166
|
+
constructor(model: TypedModel<T>, attempt: any);
|
|
167
|
+
get(id: string): Promise<TransactionDocument<T> | null>;
|
|
168
|
+
getById(id: string): Promise<TransactionDocument<T> | null>;
|
|
169
|
+
insert(id: string, data: ModelCreateInput<T>): Promise<TransactionDocument<T>>;
|
|
170
|
+
/** Replace requires the prior transactional read, as required by the SDK. */
|
|
171
|
+
replace(document: TransactionDocument<T>, data: T): Promise<TransactionDocument<T>>;
|
|
172
|
+
replaceById(id: string, data: T): Promise<TransactionDocument<T>>;
|
|
173
|
+
remove(document: TransactionDocument<T>): Promise<void>;
|
|
174
|
+
query<TRow = any>(statement: string, options?: any): Promise<{
|
|
175
|
+
rows: TRow[];
|
|
176
|
+
}>;
|
|
177
|
+
}
|
|
178
|
+
export interface TransactionDocument<T> {
|
|
179
|
+
content: T & AutoModelFields;
|
|
180
|
+
/** Opaque transaction SDK result; only pass it back to replace/remove. */
|
|
181
|
+
document: any;
|
|
182
|
+
}
|
|
183
|
+
export declare class CouchsetTransaction {
|
|
184
|
+
private readonly client;
|
|
185
|
+
private readonly attempt;
|
|
186
|
+
constructor(client: CouchsetClient, attempt: any);
|
|
187
|
+
model<T>(definition: ModelDefinition<T>): TransactionModel<T>;
|
|
188
|
+
query<TRow = any>(statement: string, options?: any): Promise<{
|
|
189
|
+
rows: TRow[];
|
|
190
|
+
}>;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Creates a short-lived scoped fixture and a cleanup function. The caller must
|
|
194
|
+
* invoke cleanup in after/afterEach; no normal client operation ever drops data.
|
|
195
|
+
*/
|
|
196
|
+
export declare const createCouchsetTestFixture: <T = any>(client: CouchsetClient, definition: Omit<ModelDefinition<T>, "scope" | "collection"> & {
|
|
197
|
+
scope?: string;
|
|
198
|
+
collection?: string;
|
|
199
|
+
}) => Promise<{
|
|
200
|
+
definition: ModelDefinition<T>;
|
|
201
|
+
model: TypedModel<T>;
|
|
202
|
+
cleanup: () => Promise<void>;
|
|
203
|
+
}>;
|
|
204
|
+
export declare const createCouchsetClient: (options?: CouchsetClientOptions) => CouchsetClient;
|