@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.
@@ -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<{
@@ -6,9 +6,11 @@ import { ConflictError, NotFoundError } from '../../../errors';
6
6
  import { ifDefined, isDefined } from '../../../guards';
7
7
  import { assertNoUndefined } from '../fileSystemAssert';
8
8
  export const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
9
+ var _a;
9
10
  const tableName = resolveConfigValue(configProvider[initializer.configSpace || 'fileSystem'].tableName);
10
11
  const tablePath = tableName.then((table) => path.join(initializer.dataDir, table));
11
12
  const { mkdir, readdir, readFile, writeFile } = ifDefined(initializer.fs, fsModule);
13
+ const skipAssert = (_a = options === null || options === void 0 ? void 0 : options.skipAssert) !== null && _a !== void 0 ? _a : false;
12
14
  const mkTableDir = new Promise((resolve, reject) => tablePath.then((path) => mkdir(path, { recursive: true }, (err) => err && err.code !== 'EEXIST' ? reject(err) : resolve())));
13
15
  const hashFilename = (value) => `${hashValue(value)}.json`;
14
16
  const filePath = async (filename) => path.join(await tablePath, filename);
@@ -43,7 +45,8 @@ export const fileSystemUnversionedDocumentStore = (initializer) => () => (config
43
45
  return {
44
46
  loadAllDocumentsTheBadWay,
45
47
  getItemsByField: async (key, value, pageKey) => {
46
- assertNoUndefined(value, [key]);
48
+ if (!skipAssert)
49
+ assertNoUndefined(value, [key]);
47
50
  const pageSize = 10;
48
51
  const items = await loadAllDocumentsTheBadWay();
49
52
  const filteredItems = items.filter((item) => item[key] === value);
@@ -56,17 +59,20 @@ export const fileSystemUnversionedDocumentStore = (initializer) => () => (config
56
59
  },
57
60
  batchGetItem: async (ids) => {
58
61
  const items = await Promise.all(ids.map((id) => {
59
- assertNoUndefined(id, ['id']);
62
+ if (!skipAssert)
63
+ assertNoUndefined(id, ['id']);
60
64
  return load(hashFilename(id));
61
65
  }));
62
66
  return items.filter(isDefined);
63
67
  },
64
68
  getItem: (id) => {
65
- assertNoUndefined(id, ['id']);
69
+ if (!skipAssert)
70
+ assertNoUndefined(id, ['id']);
66
71
  return load(hashFilename(id));
67
72
  },
68
73
  incrementItemAttribute: async (id, attribute) => {
69
- assertNoUndefined(id, ['id']);
74
+ if (!skipAssert)
75
+ assertNoUndefined(id, ['id']);
70
76
  const filename = hashFilename(id);
71
77
  const path = await filePath(filename);
72
78
  await mkTableDir;
@@ -97,13 +103,15 @@ export const fileSystemUnversionedDocumentStore = (initializer) => () => (config
97
103
  throw new NotFoundError(`Item with ${hashKey.toString()} "${id}" does not exist`);
98
104
  }
99
105
  const newItem = { ...data, ...item };
100
- assertNoUndefined(newItem);
106
+ if (!skipAssert)
107
+ assertNoUndefined(newItem);
101
108
  return new Promise((resolve, reject) => {
102
109
  writeFile(path, JSON.stringify(newItem, null, 2), (err) => err ? reject(err) : resolve(newItem));
103
110
  });
104
111
  },
105
112
  putItem: async (item) => {
106
- assertNoUndefined(item);
113
+ if (!skipAssert)
114
+ assertNoUndefined(item);
107
115
  const path = await filePath(hashFilename(item[hashKey]));
108
116
  await mkTableDir;
109
117
  return new Promise((resolve, reject) => {
@@ -111,7 +119,8 @@ export const fileSystemUnversionedDocumentStore = (initializer) => () => (config
111
119
  });
112
120
  },
113
121
  createItem: async (item) => {
114
- assertNoUndefined(item);
122
+ if (!skipAssert)
123
+ assertNoUndefined(item);
115
124
  const hashed = hashFilename(item[hashKey]);
116
125
  const existingItem = await load(hashed);
117
126
  if (existingItem) {
@@ -132,7 +141,8 @@ export const fileSystemUnversionedDocumentStore = (initializer) => () => (config
132
141
  // Process items sequentially to ensure consistent conflict detection
133
142
  // Note: concurrency parameter is ignored for filesystem to avoid race conditions
134
143
  for (const item of items) {
135
- assertNoUndefined(item);
144
+ if (!skipAssert)
145
+ assertNoUndefined(item);
136
146
  try {
137
147
  const hashed = hashFilename(item[hashKey]);
138
148
  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<{
@@ -2,7 +2,9 @@ import { assertNoUndefined } from '../fileSystemAssert';
2
2
  import { fileSystemUnversionedDocumentStore } from '../unversioned/file-system';
3
3
  const PAGE_LIMIT = 5;
4
4
  export const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
5
- const unversionedDocuments = fileSystemUnversionedDocumentStore(initializer)()(configProvider)({}, 'id');
5
+ var _a;
6
+ const skipAssert = (_a = options === null || options === void 0 ? void 0 : options.skipAssert) !== null && _a !== void 0 ? _a : false;
7
+ const unversionedDocuments = fileSystemUnversionedDocumentStore(initializer)()(configProvider)({}, 'id', { skipAssert });
6
8
  return {
7
9
  loadAllDocumentsTheBadWay: () => {
8
10
  return unversionedDocuments.loadAllDocumentsTheBadWay().then(documents => documents.map(document => {
@@ -10,7 +12,8 @@ export const fileSystemVersionedDocumentStore = (initializer) => () => (configPr
10
12
  }));
11
13
  },
12
14
  getVersions: async (id, startVersion) => {
13
- assertNoUndefined(id, ['id']);
15
+ if (!skipAssert)
16
+ assertNoUndefined(id, ['id']);
14
17
  const item = await unversionedDocuments.getItem(id);
15
18
  const versions = item === null || item === void 0 ? void 0 : item.items.reverse();
16
19
  if (!versions) {
@@ -25,7 +28,8 @@ export const fileSystemVersionedDocumentStore = (initializer) => () => (configPr
25
28
  };
26
29
  },
27
30
  getItem: async (id, timestamp) => {
28
- assertNoUndefined(id, ['id']);
31
+ if (!skipAssert)
32
+ assertNoUndefined(id, ['id']);
29
33
  const item = await unversionedDocuments.getItem(id);
30
34
  if (timestamp) {
31
35
  return item === null || item === void 0 ? void 0 : item.items.find(version => version.timestamp === timestamp);
@@ -41,7 +45,8 @@ export const fileSystemVersionedDocumentStore = (initializer) => () => (configPr
41
45
  save: async (changes) => {
42
46
  var _a;
43
47
  const document = { ...item, ...changes, timestamp, author };
44
- assertNoUndefined(document);
48
+ if (!skipAssert)
49
+ assertNoUndefined(document);
45
50
  const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
46
51
  const updated = { ...container, items: [...container.items, document] };
47
52
  await unversionedDocuments.putItem(updated);
@@ -53,7 +58,8 @@ export const fileSystemVersionedDocumentStore = (initializer) => () => (configPr
53
58
  var _a;
54
59
  const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
55
60
  const document = { ...item, timestamp: new Date().getTime(), author };
56
- assertNoUndefined(document);
61
+ if (!skipAssert)
62
+ assertNoUndefined(document);
57
63
  const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
58
64
  const updated = { ...container, items: [...container.items, document] };
59
65
  await unversionedDocuments.putItem(updated);