@belopash/typeorm-store 0.0.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.
@@ -0,0 +1,74 @@
1
+ export enum UpdateType {
2
+ Insert,
3
+ Upsert,
4
+ Remove,
5
+ }
6
+
7
+ export interface Update {
8
+ id: string
9
+ type: UpdateType
10
+ }
11
+
12
+ export class UpdateMap {
13
+ private map: Map<string, UpdateType> = new Map()
14
+
15
+ get(id: string) {
16
+ return this.map.get(id)
17
+ }
18
+
19
+ insert(id: string) {
20
+ const prevType = this.map.get(id)
21
+
22
+ switch (prevType) {
23
+ case UpdateType.Insert:
24
+ case UpdateType.Upsert:
25
+ throw new Error(`Can not add id ${id} because it is already marked as insert or upsert`)
26
+ case UpdateType.Remove:
27
+ this.map.set(id, UpdateType.Upsert)
28
+ break
29
+ case undefined:
30
+ this.map.set(id, UpdateType.Insert)
31
+ break
32
+ }
33
+ }
34
+
35
+ upsert(id: string) {
36
+ const prevType = this.map.get(id)
37
+
38
+ switch (prevType) {
39
+ case UpdateType.Insert:
40
+ break
41
+ case UpdateType.Upsert:
42
+ case UpdateType.Remove:
43
+ case undefined:
44
+ this.map.set(id, UpdateType.Upsert)
45
+ break
46
+ }
47
+ }
48
+
49
+ remove(id: string) {
50
+ const prevType = this.map.get(id)
51
+
52
+ switch (prevType) {
53
+ case UpdateType.Insert:
54
+ case UpdateType.Upsert:
55
+ case UpdateType.Remove:
56
+ case undefined:
57
+ this.map.set(id, UpdateType.Remove)
58
+ break
59
+ }
60
+ }
61
+
62
+ clear() {
63
+ this.map.clear()
64
+ }
65
+
66
+ *[Symbol.iterator](): IterableIterator<Update> {
67
+ for (const [id, type] of this.map) {
68
+ yield {
69
+ id,
70
+ type,
71
+ }
72
+ }
73
+ }
74
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,49 @@
1
+ export function* splitIntoBatches<T>(list: T[], maxBatchSize: number): Generator<T[]> {
2
+ if (list.length <= maxBatchSize) {
3
+ yield list
4
+ } else {
5
+ let offset = 0
6
+ while (list.length - offset > maxBatchSize) {
7
+ yield list.slice(offset, offset + maxBatchSize)
8
+ offset += maxBatchSize
9
+ }
10
+ yield list.slice(offset)
11
+ }
12
+ }
13
+
14
+ export function copy<T>(obj: T): T {
15
+ if (typeof obj !== 'object' || obj == null) return obj
16
+ else if (obj instanceof Date) {
17
+ return new Date(obj) as any
18
+ } else if (Array.isArray(obj)) {
19
+ return copyArray(obj) as any
20
+ } else if (obj instanceof Map) {
21
+ return new Map(copyArray(Array.from(obj))) as any
22
+ } else if (obj instanceof Set) {
23
+ return new Set(copyArray(Array.from(obj))) as any
24
+ } else if (ArrayBuffer.isView(obj)) {
25
+ return copyBuffer(obj)
26
+ } else {
27
+ const clone = Object.create(Object.getPrototypeOf(obj))
28
+ for (var k in obj) {
29
+ clone[k] = copy(obj[k])
30
+ }
31
+ return clone
32
+ }
33
+ }
34
+
35
+ function copyBuffer(buf: any) {
36
+ if (buf instanceof Buffer) {
37
+ return Buffer.from(buf)
38
+ }
39
+
40
+ return new buf.constructor(buf.buffer.slice(), buf.byteOffset, buf.length)
41
+ }
42
+
43
+ function copyArray(arr: any[]) {
44
+ const clone = new Array(arr.length)
45
+ for (let i = 0; i < arr.length; i++) {
46
+ clone[i] = copy(clone[i])
47
+ }
48
+ return clone
49
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "es2020",
5
+ "outDir": "lib",
6
+ "rootDir": "src",
7
+ "allowJs": true,
8
+ "strict": true,
9
+ "declaration": true,
10
+ "declarationMap": true,
11
+ "sourceMap": true,
12
+ "esModuleInterop": true,
13
+ "experimentalDecorators": true,
14
+ "emitDecoratorMetadata": true,
15
+ "skipLibCheck": true
16
+ },
17
+ "include": ["src"],
18
+ "exclude": [
19
+ "node_modules"
20
+ ]
21
+ }