@openstax/ts-utils 1.32.5 → 1.32.7
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/file-system.d.ts +1 -0
- package/dist/cjs/services/documentStore/unversioned/file-system.js +18 -8
- package/dist/cjs/services/documentStore/versioned/file-system.d.ts +1 -0
- package/dist/cjs/services/documentStore/versioned/file-system.js +11 -5
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/documentStore/unversioned/file-system.d.ts +1 -0
- package/dist/esm/services/documentStore/unversioned/file-system.js +18 -8
- package/dist/esm/services/documentStore/versioned/file-system.d.ts +1 -0
- package/dist/esm/services/documentStore/versioned/file-system.js +11 -5
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -8,6 +8,7 @@ interface Initializer<C> {
|
|
|
8
8
|
export declare const fileSystemUnversionedDocumentStore: <C extends string = "fileSystem">(initializer: Initializer<C>) => <T extends TDocument<T>>() => (configProvider: { [_key in C]: ConfigProviderForConfig<Config>; }) => <K extends keyof T>(_: {}, hashKey: K, options?: {
|
|
9
9
|
afterWrite?: (item: T) => void | Promise<void>;
|
|
10
10
|
batchAfterWrite?: (items: T[]) => void | Promise<void>;
|
|
11
|
+
skipAssert?: boolean;
|
|
11
12
|
}) => {
|
|
12
13
|
loadAllDocumentsTheBadWay: () => Promise<T[]>;
|
|
13
14
|
getItemsByField: (key: keyof T, value: T[K], pageKey?: string) => Promise<{
|
|
@@ -45,9 +45,11 @@ const errors_1 = require("../../../errors");
|
|
|
45
45
|
const guards_1 = require("../../../guards");
|
|
46
46
|
const fileSystemAssert_1 = require("../fileSystemAssert");
|
|
47
47
|
const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
|
|
48
|
+
var _a;
|
|
48
49
|
const tableName = (0, config_1.resolveConfigValue)(configProvider[initializer.configSpace || 'fileSystem'].tableName);
|
|
49
50
|
const tablePath = tableName.then((table) => path_1.default.join(initializer.dataDir, table));
|
|
50
51
|
const { mkdir, readdir, readFile, writeFile } = (0, guards_1.ifDefined)(initializer.fs, fsModule);
|
|
52
|
+
const skipAssert = (_a = options === null || options === void 0 ? void 0 : options.skipAssert) !== null && _a !== void 0 ? _a : false;
|
|
51
53
|
const mkTableDir = new Promise((resolve, reject) => tablePath.then((path) => mkdir(path, { recursive: true }, (err) => err && err.code !== 'EEXIST' ? reject(err) : resolve())));
|
|
52
54
|
const hashFilename = (value) => `${(0, __1.hashValue)(value)}.json`;
|
|
53
55
|
const filePath = async (filename) => path_1.default.join(await tablePath, filename);
|
|
@@ -82,7 +84,8 @@ const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvide
|
|
|
82
84
|
return {
|
|
83
85
|
loadAllDocumentsTheBadWay,
|
|
84
86
|
getItemsByField: async (key, value, pageKey) => {
|
|
85
|
-
|
|
87
|
+
if (!skipAssert)
|
|
88
|
+
(0, fileSystemAssert_1.assertNoUndefined)(value, [key]);
|
|
86
89
|
const pageSize = 10;
|
|
87
90
|
const items = await loadAllDocumentsTheBadWay();
|
|
88
91
|
const filteredItems = items.filter((item) => item[key] === value);
|
|
@@ -95,17 +98,20 @@ const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvide
|
|
|
95
98
|
},
|
|
96
99
|
batchGetItem: async (ids) => {
|
|
97
100
|
const items = await Promise.all(ids.map((id) => {
|
|
98
|
-
|
|
101
|
+
if (!skipAssert)
|
|
102
|
+
(0, fileSystemAssert_1.assertNoUndefined)(id, ['id']);
|
|
99
103
|
return load(hashFilename(id));
|
|
100
104
|
}));
|
|
101
105
|
return items.filter(guards_1.isDefined);
|
|
102
106
|
},
|
|
103
107
|
getItem: (id) => {
|
|
104
|
-
|
|
108
|
+
if (!skipAssert)
|
|
109
|
+
(0, fileSystemAssert_1.assertNoUndefined)(id, ['id']);
|
|
105
110
|
return load(hashFilename(id));
|
|
106
111
|
},
|
|
107
112
|
incrementItemAttribute: async (id, attribute) => {
|
|
108
|
-
|
|
113
|
+
if (!skipAssert)
|
|
114
|
+
(0, fileSystemAssert_1.assertNoUndefined)(id, ['id']);
|
|
109
115
|
const filename = hashFilename(id);
|
|
110
116
|
const path = await filePath(filename);
|
|
111
117
|
await mkTableDir;
|
|
@@ -136,13 +142,15 @@ const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvide
|
|
|
136
142
|
throw new errors_1.NotFoundError(`Item with ${hashKey.toString()} "${id}" does not exist`);
|
|
137
143
|
}
|
|
138
144
|
const newItem = { ...data, ...item };
|
|
139
|
-
|
|
145
|
+
if (!skipAssert)
|
|
146
|
+
(0, fileSystemAssert_1.assertNoUndefined)(newItem);
|
|
140
147
|
return new Promise((resolve, reject) => {
|
|
141
148
|
writeFile(path, JSON.stringify(newItem, null, 2), (err) => err ? reject(err) : resolve(newItem));
|
|
142
149
|
});
|
|
143
150
|
},
|
|
144
151
|
putItem: async (item) => {
|
|
145
|
-
|
|
152
|
+
if (!skipAssert)
|
|
153
|
+
(0, fileSystemAssert_1.assertNoUndefined)(item);
|
|
146
154
|
const path = await filePath(hashFilename(item[hashKey]));
|
|
147
155
|
await mkTableDir;
|
|
148
156
|
return new Promise((resolve, reject) => {
|
|
@@ -150,7 +158,8 @@ const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvide
|
|
|
150
158
|
});
|
|
151
159
|
},
|
|
152
160
|
createItem: async (item) => {
|
|
153
|
-
|
|
161
|
+
if (!skipAssert)
|
|
162
|
+
(0, fileSystemAssert_1.assertNoUndefined)(item);
|
|
154
163
|
const hashed = hashFilename(item[hashKey]);
|
|
155
164
|
const existingItem = await load(hashed);
|
|
156
165
|
if (existingItem) {
|
|
@@ -171,7 +180,8 @@ const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvide
|
|
|
171
180
|
// Process items sequentially to ensure consistent conflict detection
|
|
172
181
|
// Note: concurrency parameter is ignored for filesystem to avoid race conditions
|
|
173
182
|
for (const item of items) {
|
|
174
|
-
|
|
183
|
+
if (!skipAssert)
|
|
184
|
+
(0, fileSystemAssert_1.assertNoUndefined)(item);
|
|
175
185
|
try {
|
|
176
186
|
const hashed = hashFilename(item[hashKey]);
|
|
177
187
|
const existingItem = await load(hashed);
|
|
@@ -8,6 +8,7 @@ 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]: ConfigProviderForConfig<Config>; }) => <K extends keyof T, A extends undefined | ((...a: any[]) => Promise<VersionedDocumentAuthor>)>(_: {}, hashKey: K, options?: {
|
|
10
10
|
getAuthor?: A;
|
|
11
|
+
skipAssert?: boolean;
|
|
11
12
|
}) => {
|
|
12
13
|
loadAllDocumentsTheBadWay: () => Promise<T[]>;
|
|
13
14
|
getVersions: (id: T[K], startVersion?: number) => Promise<{
|
|
@@ -5,7 +5,9 @@ const fileSystemAssert_1 = require("../fileSystemAssert");
|
|
|
5
5
|
const file_system_1 = require("../unversioned/file-system");
|
|
6
6
|
const PAGE_LIMIT = 5;
|
|
7
7
|
const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
|
|
8
|
-
|
|
8
|
+
var _a;
|
|
9
|
+
const skipAssert = (_a = options === null || options === void 0 ? void 0 : options.skipAssert) !== null && _a !== void 0 ? _a : false;
|
|
10
|
+
const unversionedDocuments = (0, file_system_1.fileSystemUnversionedDocumentStore)(initializer)()(configProvider)({}, 'id', { skipAssert });
|
|
9
11
|
return {
|
|
10
12
|
loadAllDocumentsTheBadWay: () => {
|
|
11
13
|
return unversionedDocuments.loadAllDocumentsTheBadWay().then(documents => documents.map(document => {
|
|
@@ -13,7 +15,8 @@ const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider)
|
|
|
13
15
|
}));
|
|
14
16
|
},
|
|
15
17
|
getVersions: async (id, startVersion) => {
|
|
16
|
-
|
|
18
|
+
if (!skipAssert)
|
|
19
|
+
(0, fileSystemAssert_1.assertNoUndefined)(id, ['id']);
|
|
17
20
|
const item = await unversionedDocuments.getItem(id);
|
|
18
21
|
const versions = item === null || item === void 0 ? void 0 : item.items.reverse();
|
|
19
22
|
if (!versions) {
|
|
@@ -28,7 +31,8 @@ const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider)
|
|
|
28
31
|
};
|
|
29
32
|
},
|
|
30
33
|
getItem: async (id, timestamp) => {
|
|
31
|
-
|
|
34
|
+
if (!skipAssert)
|
|
35
|
+
(0, fileSystemAssert_1.assertNoUndefined)(id, ['id']);
|
|
32
36
|
const item = await unversionedDocuments.getItem(id);
|
|
33
37
|
if (timestamp) {
|
|
34
38
|
return item === null || item === void 0 ? void 0 : item.items.find(version => version.timestamp === timestamp);
|
|
@@ -44,7 +48,8 @@ const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider)
|
|
|
44
48
|
save: async (changes) => {
|
|
45
49
|
var _a;
|
|
46
50
|
const document = { ...item, ...changes, timestamp, author };
|
|
47
|
-
|
|
51
|
+
if (!skipAssert)
|
|
52
|
+
(0, fileSystemAssert_1.assertNoUndefined)(document);
|
|
48
53
|
const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
|
|
49
54
|
const updated = { ...container, items: [...container.items, document] };
|
|
50
55
|
await unversionedDocuments.putItem(updated);
|
|
@@ -56,7 +61,8 @@ const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider)
|
|
|
56
61
|
var _a;
|
|
57
62
|
const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
|
|
58
63
|
const document = { ...item, timestamp: new Date().getTime(), author };
|
|
59
|
-
|
|
64
|
+
if (!skipAssert)
|
|
65
|
+
(0, fileSystemAssert_1.assertNoUndefined)(document);
|
|
60
66
|
const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
|
|
61
67
|
const updated = { ...container, items: [...container.items, document] };
|
|
62
68
|
await unversionedDocuments.putItem(updated);
|