@openstax/ts-utils 1.24.6 → 1.25.1-pre2
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/dist/cjs/services/documentStore/unversioned/dynamodb.d.ts +3 -1
- package/dist/cjs/services/documentStore/unversioned/dynamodb.js +22 -11
- package/dist/cjs/services/documentStore/versioned/dynamodb.d.ts +4 -1
- package/dist/cjs/services/documentStore/versioned/dynamodb.js +13 -7
- package/dist/cjs/services/documentStore/versioned/file-system.d.ts +3 -1
- package/dist/cjs/services/documentStore/versioned/file-system.js +3 -3
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/documentStore/unversioned/dynamodb.d.ts +3 -1
- package/dist/esm/services/documentStore/unversioned/dynamodb.js +22 -11
- package/dist/esm/services/documentStore/versioned/dynamodb.d.ts +4 -1
- package/dist/esm/services/documentStore/versioned/dynamodb.js +13 -7
- package/dist/esm/services/documentStore/versioned/file-system.d.ts +3 -1
- package/dist/esm/services/documentStore/versioned/file-system.js +3 -3
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -5,7 +5,9 @@ interface Initializer<C> {
|
|
|
5
5
|
}
|
|
6
6
|
export declare const dynamoUnversionedDocumentStore: <C extends string = "dynamodb">(initializer?: Initializer<C> | undefined) => <T extends TDocument<T>>() => (configProvider: { [key in C]: {
|
|
7
7
|
tableName: import("../../../config").ConfigValueProvider<string>;
|
|
8
|
-
}; }) => <K extends keyof T>(_: {}, hashKey: K
|
|
8
|
+
}; }) => <K extends keyof T>(_: {}, hashKey: K, options?: {
|
|
9
|
+
afterWrite?: ((item: T) => void | Promise<void>) | undefined;
|
|
10
|
+
} | undefined) => {
|
|
9
11
|
loadAllDocumentsTheBadWay: () => Promise<T[]>;
|
|
10
12
|
batchGetItem: (ids: T[K][]) => Promise<T[]>;
|
|
11
13
|
getItem: (id: T[K]) => Promise<T | undefined>;
|
|
@@ -8,9 +8,9 @@ const errors_1 = require("../../../errors");
|
|
|
8
8
|
const guards_1 = require("../../../guards");
|
|
9
9
|
const dynamoEncoding_1 = require("../dynamoEncoding");
|
|
10
10
|
const dynamodb = (0, __1.once)(() => new client_dynamodb_1.DynamoDB({ apiVersion: '2012-08-10' }));
|
|
11
|
-
const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey) => {
|
|
12
|
-
const
|
|
13
|
-
const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(
|
|
11
|
+
const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
|
|
12
|
+
const init = (0, guards_1.ifDefined)(initializer, {});
|
|
13
|
+
const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(init.configSpace, 'dynamodb')].tableName));
|
|
14
14
|
return {
|
|
15
15
|
loadAllDocumentsTheBadWay: async () => {
|
|
16
16
|
const loadAllResults = async (ExclusiveStartKey) => {
|
|
@@ -57,14 +57,19 @@ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) =
|
|
|
57
57
|
ConditionExpression: 'attribute_exists(#k)',
|
|
58
58
|
ExpressionAttributeNames: { '#k': hashKey.toString(), '#f': field },
|
|
59
59
|
ExpressionAttributeValues: { ':one': { N: '1' } },
|
|
60
|
-
ReturnValues: '
|
|
60
|
+
ReturnValues: 'ALL_NEW',
|
|
61
61
|
});
|
|
62
|
-
return dynamodb().send(cmd).then((item) => {
|
|
63
|
-
var _a;
|
|
64
|
-
const result = (_a = item.Attributes) === null || _a === void 0 ? void 0 : _a[field]['N'];
|
|
65
|
-
if (!
|
|
62
|
+
return dynamodb().send(cmd).then(async (item) => {
|
|
63
|
+
var _a, _b, _c;
|
|
64
|
+
const result = (_b = (_a = item.Attributes) === null || _a === void 0 ? void 0 : _a[field]) === null || _b === void 0 ? void 0 : _b['N'];
|
|
65
|
+
if (!item.Attributes) {
|
|
66
66
|
throw new errors_1.NotFoundError(`Item with ${key} "${id}" does not exist`);
|
|
67
67
|
}
|
|
68
|
+
if (!result) {
|
|
69
|
+
throw new errors_1.NotFoundError(`Item with ${key} "${id}" did not produce field "${field}"`);
|
|
70
|
+
}
|
|
71
|
+
const updatedDoc = (0, dynamoEncoding_1.decodeDynamoDocument)(item.Attributes);
|
|
72
|
+
await ((_c = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _c === void 0 ? void 0 : _c.call(options, updatedDoc));
|
|
68
73
|
return parseFloat(result);
|
|
69
74
|
}).catch((error) => {
|
|
70
75
|
throw error.name === 'ConditionalCheckFailedException' ?
|
|
@@ -99,11 +104,14 @@ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) =
|
|
|
99
104
|
ExpressionAttributeValues: expressionAttributeValues,
|
|
100
105
|
ReturnValues: 'ALL_NEW',
|
|
101
106
|
});
|
|
102
|
-
return dynamodb().send(cmd).then((item) => {
|
|
107
|
+
return dynamodb().send(cmd).then(async (item) => {
|
|
108
|
+
var _a;
|
|
103
109
|
if (!item.Attributes) {
|
|
104
110
|
throw new errors_1.NotFoundError(`Item with ${key} "${id}" does not exist`);
|
|
105
111
|
}
|
|
106
|
-
|
|
112
|
+
const updatedDoc = (0, dynamoEncoding_1.decodeDynamoDocument)(item.Attributes);
|
|
113
|
+
await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
|
|
114
|
+
return updatedDoc;
|
|
107
115
|
}).catch((error) => {
|
|
108
116
|
throw error.name === 'ConditionalCheckFailedException' ?
|
|
109
117
|
new errors_1.NotFoundError(`Item with ${key} "${id}" does not exist`) : error;
|
|
@@ -111,11 +119,14 @@ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) =
|
|
|
111
119
|
},
|
|
112
120
|
/* replaces the entire document with the given data */
|
|
113
121
|
putItem: async (item) => {
|
|
122
|
+
var _a;
|
|
114
123
|
const cmd = new client_dynamodb_1.PutItemCommand({
|
|
115
124
|
TableName: await tableName(),
|
|
116
125
|
Item: (0, dynamoEncoding_1.encodeDynamoDocument)(item),
|
|
117
126
|
});
|
|
118
|
-
|
|
127
|
+
const updatedDoc = await dynamodb().send(cmd).then(() => item);
|
|
128
|
+
await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
|
|
129
|
+
return updatedDoc;
|
|
119
130
|
},
|
|
120
131
|
};
|
|
121
132
|
};
|
|
@@ -6,7 +6,10 @@ interface Initializer<C> {
|
|
|
6
6
|
}
|
|
7
7
|
export declare const dynamoVersionedDocumentStore: <C extends string = "dynamodb">(initializer?: Initializer<C> | undefined) => <T extends VersionedTDocument<T>>() => (configProvider: { [key in C]: {
|
|
8
8
|
tableName: import("../../../config").ConfigValueProvider<string>;
|
|
9
|
-
}; }) => <K extends keyof T, A extends ((...a: any[]) => Promise<VersionedDocumentAuthor>) | undefined>(_: {}, hashKey: K,
|
|
9
|
+
}; }) => <K extends keyof T, A extends ((...a: any[]) => Promise<VersionedDocumentAuthor>) | undefined>(_: {}, hashKey: K, options?: {
|
|
10
|
+
afterWrite?: ((item: T) => void | Promise<void>) | undefined;
|
|
11
|
+
getAuthor?: A | undefined;
|
|
12
|
+
} | undefined) => {
|
|
10
13
|
loadAllDocumentsTheBadWay: () => Promise<T[]>;
|
|
11
14
|
getVersions: (id: T[K], startVersion?: number | undefined) => Promise<{
|
|
12
15
|
items: T[];
|
|
@@ -8,9 +8,9 @@ const guards_1 = require("../../../guards");
|
|
|
8
8
|
const dynamoEncoding_1 = require("../dynamoEncoding");
|
|
9
9
|
const dynamodb = (0, __1.once)(() => new client_dynamodb_1.DynamoDB({ apiVersion: '2012-08-10' }));
|
|
10
10
|
// i'm not really excited about getAuthor being required, but ts is getting confused about the type when unspecified
|
|
11
|
-
const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey,
|
|
12
|
-
const
|
|
13
|
-
const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(
|
|
11
|
+
const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
|
|
12
|
+
const init = (0, guards_1.ifDefined)(initializer, {});
|
|
13
|
+
const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(init.configSpace, 'dynamodb')].tableName));
|
|
14
14
|
return {
|
|
15
15
|
loadAllDocumentsTheBadWay: async () => {
|
|
16
16
|
const loadAllResults = async (ExclusiveStartKey) => {
|
|
@@ -94,11 +94,12 @@ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) =>
|
|
|
94
94
|
* cannot be modified */
|
|
95
95
|
prepareItem: async (item, ...authorArgs) => {
|
|
96
96
|
// this getAuthor type is terrible
|
|
97
|
-
const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
|
|
97
|
+
const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
|
|
98
98
|
const timestamp = new Date().getTime();
|
|
99
99
|
return {
|
|
100
100
|
document: { ...item, timestamp, author },
|
|
101
101
|
save: async (changes) => {
|
|
102
|
+
var _a;
|
|
102
103
|
const document = {
|
|
103
104
|
...item,
|
|
104
105
|
...changes,
|
|
@@ -109,15 +110,18 @@ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) =>
|
|
|
109
110
|
TableName: await tableName(),
|
|
110
111
|
Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
|
|
111
112
|
});
|
|
112
|
-
|
|
113
|
+
const updatedDoc = await dynamodb().send(cmd)
|
|
113
114
|
.then(() => document);
|
|
115
|
+
await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
|
|
116
|
+
return updatedDoc;
|
|
114
117
|
}
|
|
115
118
|
};
|
|
116
119
|
},
|
|
117
120
|
/* saves a new version of a document with the given data */
|
|
118
121
|
putItem: async (item, ...authorArgs) => {
|
|
122
|
+
var _a;
|
|
119
123
|
// this getAuthor type is terrible
|
|
120
|
-
const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
|
|
124
|
+
const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
|
|
121
125
|
const document = {
|
|
122
126
|
...item,
|
|
123
127
|
timestamp: new Date().getTime(),
|
|
@@ -127,8 +131,10 @@ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) =>
|
|
|
127
131
|
TableName: await tableName(),
|
|
128
132
|
Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
|
|
129
133
|
});
|
|
130
|
-
|
|
134
|
+
const updatedDoc = await dynamodb().send(cmd)
|
|
131
135
|
.then(() => document);
|
|
136
|
+
await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
|
|
137
|
+
return updatedDoc;
|
|
132
138
|
},
|
|
133
139
|
};
|
|
134
140
|
};
|
|
@@ -8,7 +8,9 @@ interface Initializer<C> {
|
|
|
8
8
|
}
|
|
9
9
|
export declare const fileSystemVersionedDocumentStore: <C extends string = "fileSystem">(initializer: Initializer<C>) => <T extends VersionedTDocument<T>>() => (configProvider: { [key in C]: {
|
|
10
10
|
tableName: import("../../../config").ConfigValueProvider<string>;
|
|
11
|
-
}; }) => <K extends keyof T, A extends ((...a: any[]) => Promise<VersionedDocumentAuthor>) | undefined>(_: {}, hashKey: K,
|
|
11
|
+
}; }) => <K extends keyof T, A extends ((...a: any[]) => Promise<VersionedDocumentAuthor>) | undefined>(_: {}, hashKey: K, options?: {
|
|
12
|
+
getAuthor?: A | undefined;
|
|
13
|
+
} | undefined) => {
|
|
12
14
|
loadAllDocumentsTheBadWay: () => Promise<T[]>;
|
|
13
15
|
getVersions: (id: T[K], startVersion?: number | undefined) => Promise<{
|
|
14
16
|
items: T[];
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.fileSystemVersionedDocumentStore = void 0;
|
|
4
4
|
const file_system_1 = require("../unversioned/file-system");
|
|
5
5
|
const PAGE_LIMIT = 5;
|
|
6
|
-
const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey,
|
|
6
|
+
const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
|
|
7
7
|
const unversionedDocuments = (0, file_system_1.fileSystemUnversionedDocumentStore)(initializer)()(configProvider)({}, 'id');
|
|
8
8
|
return {
|
|
9
9
|
loadAllDocumentsTheBadWay: () => {
|
|
@@ -34,7 +34,7 @@ const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider)
|
|
|
34
34
|
},
|
|
35
35
|
prepareItem: async (item, ...authorArgs) => {
|
|
36
36
|
// this getAuthor type is terrible
|
|
37
|
-
const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
|
|
37
|
+
const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
|
|
38
38
|
const timestamp = new Date().getTime();
|
|
39
39
|
return {
|
|
40
40
|
document: { ...item, timestamp, author },
|
|
@@ -50,7 +50,7 @@ const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider)
|
|
|
50
50
|
},
|
|
51
51
|
putItem: async (item, ...authorArgs) => {
|
|
52
52
|
var _a;
|
|
53
|
-
const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
|
|
53
|
+
const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
|
|
54
54
|
const document = { ...item, timestamp: new Date().getTime(), author };
|
|
55
55
|
const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
|
|
56
56
|
const updated = { ...container, items: [...container.items, document] };
|