@tblabs/storage 1.1.0 → 2.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.
package/Converter.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { IConverter } from "./IConverter";
2
+ export declare abstract class Converter<R extends object, T extends object> implements IConverter<R, T> {
3
+ protected abstract ConvertToRaw(obj: T): R;
4
+ protected abstract ConvertFromRaw(raw: R): T;
5
+ ToRaw(target: T): R;
6
+ FromRaw(raw: R): T;
7
+ protected GetArray<R, T>(value: R[], creator: (raw: R) => T): Array<T>;
8
+ protected GetString(value: any): string;
9
+ protected GetNumber(value: any): number;
10
+ protected GetBool(value: any): boolean;
11
+ protected GetDate(date: any): Date;
12
+ }
package/Converter.js ADDED
@@ -0,0 +1,36 @@
1
+ export class Converter {
2
+ ToRaw(target) {
3
+ try {
4
+ return this.ConvertToRaw(target);
5
+ }
6
+ catch (ex) {
7
+ console.error(`Conversion from object to raw problem: ${ex.message}\n\nStack trace: ${ex.stack}\n\nObject:`, target);
8
+ throw ex;
9
+ }
10
+ }
11
+ FromRaw(raw) {
12
+ try {
13
+ return this.ConvertFromRaw(raw);
14
+ }
15
+ catch (ex) {
16
+ console.error(`Conversion from raw to object problem: ${ex.message}\n\nStack trace: ${ex.stack}\n\nRaw:`, raw);
17
+ throw ex;
18
+ }
19
+ }
20
+ GetArray(value, creator) {
21
+ const items = value?.map(x => creator(x));
22
+ return items || [];
23
+ }
24
+ GetString(value) {
25
+ return value || "";
26
+ }
27
+ GetNumber(value) {
28
+ return +value || 0;
29
+ }
30
+ GetBool(value) {
31
+ return !!value || false;
32
+ }
33
+ GetDate(date) {
34
+ return date ? new Date(date) : new Date(0);
35
+ }
36
+ }
package/Dataset.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { OnlineStorage } from "./OnlineStorage";
2
+ import { IConverter } from "./IConverter";
3
+ export declare class Dataset<T extends object> {
4
+ private _db;
5
+ private _converter;
6
+ private folder;
7
+ constructor(_db: OnlineStorage, _converter: IConverter<any, T>, folder: string);
8
+ private get FolderPath();
9
+ GetOne(id: string): Promise<T | null>;
10
+ FindOne(id: string): Promise<T | null>;
11
+ GetAll(): Promise<T[]>;
12
+ GetAllIds(): Promise<string[]>;
13
+ Add(id: string, item: T): Promise<void>;
14
+ Update(id: string, item: T): Promise<void>;
15
+ Delete(id: string): Promise<void>;
16
+ Drop(): Promise<void>;
17
+ Move(id: string, targetDataset: Dataset<any>): Promise<void>;
18
+ }
package/Dataset.js ADDED
@@ -0,0 +1,111 @@
1
+ import { AddCommand } from "./Messages/AddCommand";
2
+ import { DeleteCommand } from "./Messages/DeleteCommand";
3
+ import { DropCommand } from "./Messages/DropCommand";
4
+ import { FindQuery } from "./Messages/FindQuery";
5
+ import { ListQuery } from "./Messages/ListQuery";
6
+ import { MoveCommand } from "./Messages/MoveCommand";
7
+ import { ReadQuery } from "./Messages/ReadQuery";
8
+ import { UpdateCommand } from "./Messages/UpdateCommand";
9
+ export class Dataset {
10
+ _db;
11
+ _converter;
12
+ folder;
13
+ constructor(_db, _converter, folder) {
14
+ this._db = _db;
15
+ this._converter = _converter;
16
+ this.folder = folder;
17
+ }
18
+ get FolderPath() {
19
+ return this._db.DatabaseDir + "/" + this.folder;
20
+ }
21
+ async GetOne(id) {
22
+ this._db.Log(`Fetching "${id}"...`);
23
+ const queryResult = await this._db.Send(new ReadQuery(id, this.FolderPath));
24
+ if (!queryResult.IsSuccess) {
25
+ this._db.Log(`Not found "${id}" in "${this.FolderPath}".`);
26
+ return null;
27
+ }
28
+ this._db.Log(`Found "${id}" in "${this.FolderPath}".`);
29
+ const rawItem = queryResult.Result;
30
+ const item = this._converter.FromRaw(rawItem);
31
+ return item;
32
+ }
33
+ async FindOne(id) {
34
+ this._db.Log(`Searching for "${id}"...`);
35
+ const queryResult = await this._db.Send(new FindQuery(id, this._db.DatabaseDir));
36
+ if (!queryResult.IsSuccess) {
37
+ this._db.Log(`Not found "${id}".`);
38
+ return null;
39
+ }
40
+ this._db.Log(`Found "${id}".`);
41
+ const rawItem = queryResult.Result;
42
+ const item = this._converter.FromRaw(rawItem);
43
+ return item;
44
+ }
45
+ async GetAll() {
46
+ this._db.Log(`Fetching all from "${this._db.DatabaseDir}"...`);
47
+ const queryResult = await this._db.Send(new ListQuery(this.FolderPath, true));
48
+ if (!queryResult.IsSuccess) {
49
+ this._db.Log(`Could not fetch all items from "${this.FolderPath}": ${queryResult.ErrorMessage}`);
50
+ return [];
51
+ }
52
+ const rawItems = queryResult.Result;
53
+ const items = rawItems.map((raw) => this._converter.FromRaw(raw));
54
+ this._db.Log(`Fetched ${items.length} items from "${this.FolderPath}".`);
55
+ return items;
56
+ }
57
+ async GetAllIds() {
58
+ this._db.Log(`Fetching all ids from "${this.FolderPath}"...`);
59
+ const queryResult = await this._db.Send(new ListQuery(this.FolderPath, false));
60
+ if (!queryResult.IsSuccess) {
61
+ this._db.Log(`Could not fetch all ids from "${this.FolderPath}": ${queryResult.ErrorMessage}`);
62
+ return [];
63
+ }
64
+ const itemsIds = Object.values(queryResult.Result);
65
+ this._db.Log(`Fetched ${itemsIds.length} items ids from "${this.FolderPath}".`);
66
+ return itemsIds;
67
+ }
68
+ async Add(id, item) {
69
+ this._db.Log(`Adding "${id}" to "${this.FolderPath}"...`);
70
+ const result = await this._db.Send(new AddCommand(id, this.FolderPath, item));
71
+ if (!result.IsSuccess) {
72
+ throw new Error(`Could not add "${id}" to "${this.FolderPath}": ${result.ErrorMessage}`);
73
+ }
74
+ this._db.Log(`"${id}" added to "${this.FolderPath}".`);
75
+ }
76
+ ;
77
+ async Update(id, item) {
78
+ this._db.Log(`Updating "${id}"...`);
79
+ const result = await this._db.Send(new UpdateCommand(id, this.FolderPath, item));
80
+ if (!result.IsSuccess) {
81
+ throw new Error(`Could not update "${id}" in "${this.FolderPath}": ${result.ErrorMessage}`);
82
+ }
83
+ this._db.Log(`"${id}" updated in "${this.FolderPath}".`);
84
+ }
85
+ ;
86
+ async Delete(id) {
87
+ this._db.Log(`Deleting "${id}" from "${this.FolderPath}"...`);
88
+ const result = await this._db.Send(new DeleteCommand(id, this.FolderPath));
89
+ if (!result.IsSuccess) {
90
+ throw new Error(`Could not delete "${id}" from "${this.FolderPath}": ${result.ErrorMessage}`);
91
+ }
92
+ this._db.Log(`Deleted "${id}" from "${this.FolderPath}".`);
93
+ }
94
+ async Drop() {
95
+ this._db.Log(`Dropping "${this.FolderPath}"...`);
96
+ const result = await this._db.Send(new DropCommand(this.FolderPath));
97
+ if (!result.IsSuccess) {
98
+ throw new Error(`Could not drop "${this.FolderPath}": ${result.ErrorMessage}`);
99
+ }
100
+ this._db.Log(`Dropped "${this.FolderPath}".`);
101
+ }
102
+ async Move(id, targetDataset) {
103
+ const source = this.FolderPath;
104
+ const target = targetDataset.FolderPath;
105
+ this._db.Log(`Moving "${id}" from "${source}" to "${target}"...`);
106
+ const result = await this._db.Send(new MoveCommand(id, source, target));
107
+ if (!result.IsSuccess)
108
+ throw new Error(`Could not move "${id}" from "${source}" to "${target}": ${result.ErrorMessage}`);
109
+ this._db.Log(`Moved "${id}" from "${source}" to "${target}".`);
110
+ }
111
+ }
@@ -0,0 +1,4 @@
1
+ export interface IConverter<R extends object, T extends object> {
2
+ FromRaw(raw: R): T;
3
+ ToRaw(item: T): R;
4
+ }
package/IConverter.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import { IMessage } from "./MessageBus/IMessage";
2
+ import { ISender } from "./MessageBus/ISender";
3
+ import { MessageBusResponse } from "./MessageBus/MessageBusResponse";
4
+ export declare class OnlineStorage {
5
+ private databaseFolder;
6
+ private access;
7
+ private logEnabled;
8
+ private readonly _bus;
9
+ get DatabaseDir(): string;
10
+ constructor(serverUrl: string, databaseFolder: string, access: ISender);
11
+ EnableLog(): void;
12
+ DisableLog(): void;
13
+ Log(...message: any[]): void;
14
+ Ping(): Promise<boolean>;
15
+ Send(message: IMessage): Promise<MessageBusResponse>;
16
+ AddDataset(datasetName: string): Promise<void>;
17
+ }
@@ -0,0 +1,41 @@
1
+ import { MessageBus } from "./MessageBus/MessageBus";
2
+ import { AddDirCommand } from "./Messages/AddDirCommand";
3
+ export class OnlineStorage {
4
+ databaseFolder;
5
+ access;
6
+ logEnabled = false;
7
+ _bus;
8
+ get DatabaseDir() {
9
+ return this.databaseFolder;
10
+ }
11
+ constructor(serverUrl, databaseFolder, access) {
12
+ this.databaseFolder = databaseFolder;
13
+ this.access = access;
14
+ this._bus = new MessageBus(serverUrl, access);
15
+ }
16
+ EnableLog() {
17
+ this.logEnabled = true;
18
+ }
19
+ DisableLog() {
20
+ this.logEnabled = false;
21
+ }
22
+ Log(...message) {
23
+ if (this.logEnabled)
24
+ console.log('[OnlineRepo]', ...message);
25
+ }
26
+ async Ping() {
27
+ return await this._bus.Ping();
28
+ }
29
+ async Send(message) {
30
+ return await this._bus.SendMessage(message);
31
+ }
32
+ async AddDataset(datasetName) {
33
+ const target = this.DatabaseDir + "/" + datasetName;
34
+ this.Log(`Creating dataset directory "${target}"...`);
35
+ const result = await this._bus.SendMessage(new AddDirCommand(target));
36
+ if (!result.IsSuccess) {
37
+ throw new Error(`Could not add directory "${target}": ${result.ErrorMessage}`);
38
+ }
39
+ this.Log(`Directory "${target}" added.`);
40
+ }
41
+ }
package/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  export * from "./IRepo";
2
- export * from "./OnlineRepo";
2
+ export * from "./IConverter";
3
+ export * from "./OnlineStorage";
3
4
  export * from "./Utils/OperationRepeater";
5
+ export * from "./Converter";
6
+ export * from "./Dataset";
package/index.js CHANGED
@@ -1,3 +1,6 @@
1
1
  export * from "./IRepo";
2
- export * from "./OnlineRepo";
2
+ export * from "./IConverter";
3
+ export * from "./OnlineStorage";
3
4
  export * from "./Utils/OperationRepeater";
5
+ export * from "./Converter";
6
+ export * from "./Dataset";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "online storage module",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
@@ -9,7 +9,7 @@
9
9
  "pack": "cd ./bin && npm pack",
10
10
  "clean": "rm ./bin -rf && rm *.tgz -rf",
11
11
  "build": "npm run clean && tsc ./src/index.ts --declaration --outDir ./bin && cp ./package.json ./bin/package.json",
12
- "publish": "npm login && npm run build && npm run pack && cd ./bin && npm publish --access public"
12
+ "publish:npm": "npm login && npm run build && npm run pack && cd ./bin && npm publish --access public"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/jest": "^30.0.0",
Binary file
package/OnlineRepo.d.ts DELETED
@@ -1,22 +0,0 @@
1
- import { IRepo } from "./IRepo";
2
- import { ISender } from "./MessageBus/ISender";
3
- export declare class OnlineRepo<T extends object> implements IRepo<T> {
4
- private server;
5
- private databaseFolder;
6
- private user;
7
- LOG: boolean;
8
- private readonly _bus;
9
- constructor(server: string, databaseFolder: string, user: ISender);
10
- private Log;
11
- Ping(): Promise<boolean>;
12
- GetOne(folder: string, id: string): Promise<T | null>;
13
- FindOne(id: string): Promise<T | null>;
14
- GetAll(folder: string): Promise<T[]>;
15
- GetAllIds(folder: string): Promise<string[]>;
16
- Add(folder: string, id: string, item: T): Promise<void>;
17
- Update(folder: string, id: string, item: T): Promise<void>;
18
- Delete(folder: string, id: string): Promise<void>;
19
- Drop(): Promise<void>;
20
- Move(id: string, sourceFolder: string, targetFolder: string): Promise<void>;
21
- AddDir(targetFolder: string): Promise<void>;
22
- }
package/OnlineRepo.js DELETED
@@ -1,127 +0,0 @@
1
- import { DeleteCommand } from "./Messages/DeleteCommand";
2
- import { DropCommand } from "./Messages/DropCommand";
3
- import { FindQuery } from "./Messages/FindQuery";
4
- import { ListQuery } from "./Messages/ListQuery";
5
- import { MoveCommand } from "./Messages/MoveCommand";
6
- import { ReadQuery } from "./Messages/ReadQuery";
7
- import { UpdateCommand } from "./Messages/UpdateCommand";
8
- import { MessageBus } from "./MessageBus/MessageBus";
9
- import { AddCommand } from "./Messages/AddCommand";
10
- import { AddDirCommand } from "./Messages/AddDirCommand";
11
- export class OnlineRepo {
12
- server;
13
- databaseFolder;
14
- user;
15
- LOG = false;
16
- _bus;
17
- constructor(server, databaseFolder, user) {
18
- this.server = server;
19
- this.databaseFolder = databaseFolder;
20
- this.user = user;
21
- this._bus = new MessageBus(server, user);
22
- }
23
- Log(...message) {
24
- if (this.LOG)
25
- console.log('[OnlineRepo]', ...message);
26
- }
27
- async Ping() {
28
- return await this._bus.Ping();
29
- }
30
- async GetOne(folder, id) {
31
- this.Log(`Fetching "${id}"...`);
32
- const folderPath = this.databaseFolder + "/" + folder;
33
- const queryResult = await this._bus.SendMessage(new ReadQuery(id, folderPath));
34
- if (queryResult.IsSuccess) {
35
- const item = queryResult.Result;
36
- this.Log(`Found "${id}" in "${folderPath}".`);
37
- return item;
38
- }
39
- this.Log(`Not found "${id}" in "${folderPath}".`);
40
- return null;
41
- }
42
- async FindOne(id) {
43
- this.Log(`Fetching "${id}"...`);
44
- const queryResult = await this._bus.SendMessage(new FindQuery(id, this.databaseFolder));
45
- if (queryResult.IsSuccess) {
46
- const item = queryResult.Result;
47
- this.Log(`Found "${id}".`);
48
- return item;
49
- }
50
- this.Log(`Not found "${id}".`);
51
- return null;
52
- }
53
- async GetAll(folder) {
54
- this.Log(`Fetching all from "${this.databaseFolder}"...`);
55
- const folderPath = this.databaseFolder + "/" + folder;
56
- const queryResult = await this._bus.SendMessage(new ListQuery(folderPath, true));
57
- if (queryResult.IsSuccess) {
58
- const items = queryResult.Result;
59
- this.Log(`Fetched ${items.length} items from "${folderPath}".`);
60
- return items;
61
- }
62
- else
63
- throw new Error(`Could not fetch all from "${folder}": ${queryResult.ErrorMessage}`);
64
- }
65
- async GetAllIds(folder) {
66
- this.Log(`Fetching all from "${this.databaseFolder}"...`);
67
- const folderPath = this.databaseFolder + "/" + folder;
68
- const queryResult = await this._bus.SendMessage(new ListQuery(folderPath, false));
69
- if (queryResult.IsSuccess) {
70
- const itemsIds = Object.values(queryResult.Result);
71
- this.Log(`Fetched ${itemsIds.length} items ids from "${folderPath}".`);
72
- return itemsIds;
73
- }
74
- else
75
- throw new Error(`Could not fetch all ids from "${folder}": ${queryResult.ErrorMessage}`);
76
- }
77
- async Add(folder, id, item) {
78
- this.Log(`Adding "${id}" to "${folder}"...`);
79
- const folderPath = this.databaseFolder + "/" + folder;
80
- const result = await this._bus.SendMessage(new AddCommand(id, folderPath, item));
81
- if (!result.IsSuccess)
82
- throw new Error(`Could not add "${id}" to "${folderPath}": ${result.ErrorMessage}`);
83
- this.Log(`"${id}" added to "${folderPath}".`);
84
- }
85
- ;
86
- async Update(folder, id, item) {
87
- this.Log(`Updating "${id}"...`);
88
- const folderPath = this.databaseFolder + "/" + folder;
89
- const result = await this._bus.SendMessage(new UpdateCommand(id, folderPath, item));
90
- if (!result.IsSuccess)
91
- throw new Error(`Could not update "${id}" in "${folderPath}": ${result.ErrorMessage}`);
92
- this.Log(`"${id}" updated in "${folderPath}".`);
93
- }
94
- ;
95
- async Delete(folder, id) {
96
- const folderPath = this.databaseFolder + "/" + folder;
97
- this.Log(`Deleting "${id}" from "${folderPath}"...`);
98
- const result = await this._bus.SendMessage(new DeleteCommand(id, folderPath));
99
- if (!result.IsSuccess)
100
- throw new Error(`Could not delete "${id}" from "${folderPath}": ${result.ErrorMessage}`);
101
- this.Log(`Deleted "${id}" from "${folderPath}".`);
102
- }
103
- async Drop() {
104
- this.Log(`Dropping "${this.databaseFolder}"...`);
105
- const result = await this._bus.SendMessage(new DropCommand(this.databaseFolder));
106
- if (!result.IsSuccess)
107
- throw new Error(`Could not drop "${this.databaseFolder}": ${result.ErrorMessage}`);
108
- this.Log(`Dropped "${this.databaseFolder}".`);
109
- }
110
- async Move(id, sourceFolder, targetFolder) {
111
- const source = this.databaseFolder + "/" + sourceFolder;
112
- const target = this.databaseFolder + "/" + targetFolder;
113
- this.Log(`Moving "${id}" from "${source}" to "${target}"...`);
114
- const result = await this._bus.SendMessage(new MoveCommand(id, source, target));
115
- if (!result.IsSuccess)
116
- throw new Error(`Could not move "${id}" from "${source}" to "${target}": ${result.ErrorMessage}`);
117
- this.Log(`Moved "${id}" from "${source}" to "${target}".`);
118
- }
119
- async AddDir(targetFolder) {
120
- const target = this.databaseFolder + "/" + targetFolder;
121
- this.Log(`Adding directory "${target}"...`);
122
- const result = await this._bus.SendMessage(new AddDirCommand(target));
123
- if (!result.IsSuccess)
124
- throw new Error(`Could not add directory "${target}": ${result.ErrorMessage}`);
125
- this.Log(`Added directory "${target}".`);
126
- }
127
- }
Binary file