@tblabs/storage 5.2.7 → 5.2.8
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/Dataset.d.ts +0 -1
- package/Dataset.js +5 -51
- package/IRepo.d.ts +2 -1
- package/IStorageUserPrivileges.d.ts +3 -0
- package/Messages/FindQuery.d.ts +2 -2
- package/Messages/FindQuery.js +3 -3
- package/OnlineStorage.d.ts +2 -2
- package/OnlineStorage.js +33 -17
- package/package.json +1 -1
- package/tblabs-storage-5.2.8.tgz +0 -0
- package/tblabs-storage-5.2.7.tgz +0 -0
package/Dataset.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export declare class Dataset<T extends object> implements IRepo<T> {
|
|
|
12
12
|
private get DatasetPath();
|
|
13
13
|
CanUserDo(action: keyof IStorageUserPrivileges): boolean;
|
|
14
14
|
GetOne(id: string): Promise<T>;
|
|
15
|
-
FindOne(id: string): Promise<T | null>;
|
|
16
15
|
GetAll(): Promise<T[]>;
|
|
17
16
|
Add(id: string, item: T): Promise<void>;
|
|
18
17
|
Update(id: string, item: T): Promise<void>;
|
package/Dataset.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { AddCommand } from "./Messages/AddCommand";
|
|
2
2
|
import { DeleteCommand } from "./Messages/DeleteCommand";
|
|
3
3
|
import { DropCommand } from "./Messages/DropCommand";
|
|
4
|
-
import { FindQuery } from "./Messages/FindQuery";
|
|
5
4
|
import { ListQuery } from "./Messages/ListQuery";
|
|
6
5
|
import { GetQuery } from "./Messages/GetQuery";
|
|
7
6
|
import { UpdateCommand } from "./Messages/UpdateCommand";
|
|
@@ -21,14 +20,11 @@ export class Dataset {
|
|
|
21
20
|
return this._db.DatabaseDir + "/Datasets/" + this.dataset;
|
|
22
21
|
}
|
|
23
22
|
CanUserDo(action) {
|
|
24
|
-
// console.log("ACTION", action);
|
|
25
|
-
// console.log("PRIVILEGES", this._db.User.Privileges);
|
|
26
23
|
if (!this._db.User.Privileges) {
|
|
27
|
-
// console.warn("User not logged in (even if Anonymous)");
|
|
28
24
|
return false;
|
|
29
25
|
}
|
|
30
26
|
if (typeof this._db.User.Privileges[action] === "object" && this._db.User.Privileges[action]) {
|
|
31
|
-
return this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets
|
|
27
|
+
return (this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets?.includes(this.dataset)) ?? false;
|
|
32
28
|
}
|
|
33
29
|
if (typeof this._db.User.Privileges[action] === "boolean" && this._db.User.Privileges[action]) {
|
|
34
30
|
return this._db.User.Privileges[action];
|
|
@@ -38,83 +34,41 @@ export class Dataset {
|
|
|
38
34
|
async GetOne(id) {
|
|
39
35
|
this._db.Log(`Fetching "${id}"...`);
|
|
40
36
|
const queryResult = await this._db.Send(new GetQuery(this._db.DatabaseDir, this.dataset, id));
|
|
41
|
-
if (!queryResult.IsSuccess) {
|
|
42
|
-
throw new Error(`Could not fetch file: ${queryResult.ErrorMessage}`);
|
|
43
|
-
}
|
|
44
37
|
const rawItem = queryResult.Result;
|
|
45
38
|
const item = this._converter.FromRaw(rawItem);
|
|
46
39
|
this._db.Log(`Fetched "${id}" from "${this.DatasetPath}".`);
|
|
47
40
|
return item;
|
|
48
41
|
}
|
|
49
|
-
async FindOne(id) {
|
|
50
|
-
this._db.Log(`Searching for "${id}"...`);
|
|
51
|
-
const queryResult = await this._db.Send(new FindQuery(id, this._db.DatabaseDir));
|
|
52
|
-
if (!queryResult.IsSuccess) {
|
|
53
|
-
throw new Error(`Could not find file: ${queryResult.ErrorMessage}`);
|
|
54
|
-
}
|
|
55
|
-
const rawItem = queryResult.Result;
|
|
56
|
-
const item = this._converter.FromRaw(rawItem);
|
|
57
|
-
this._db.Log(`Found "${id}" in "${this.DatasetPath}".`);
|
|
58
|
-
return item;
|
|
59
|
-
}
|
|
60
42
|
async GetAll() {
|
|
61
43
|
this._db.Log(`Fetching all from "${this.DatasetPath}"...`);
|
|
62
44
|
const queryResult = await this._db.Send(new ListQuery(this._db.DatabaseDir, this.dataset));
|
|
63
|
-
if (!queryResult.IsSuccess) {
|
|
64
|
-
throw new Error(`Could not fetch items: ${queryResult.ErrorMessage}`);
|
|
65
|
-
}
|
|
66
45
|
const rawItems = queryResult.Result;
|
|
67
46
|
const items = rawItems.map((raw) => this._converter.FromRaw(raw));
|
|
68
47
|
this._db.Log(`Fetched ${items.length} items from "${this.DatasetPath}".`);
|
|
69
48
|
return items;
|
|
70
49
|
}
|
|
71
|
-
// public async GetAllIds(): Promise<string[]>
|
|
72
|
-
// {
|
|
73
|
-
// this._db.Log(`Fetching all ids from "${this.DatasetPath}"...`);
|
|
74
|
-
// const queryResult = await this._db.Send(new ListQuery(this.DatasetPath, false));
|
|
75
|
-
// if (!queryResult.IsSuccess)
|
|
76
|
-
// {
|
|
77
|
-
// this._db.Log(`Could not fetch all ids from "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
|
|
78
|
-
// return [];
|
|
79
|
-
// }
|
|
80
|
-
// const itemsIds = Object.values(queryResult.Result);
|
|
81
|
-
// this._db.Log(`Fetched ${itemsIds.length} items ids from "${this.DatasetPath}".`);
|
|
82
|
-
// return itemsIds as string[];
|
|
83
|
-
// }
|
|
84
50
|
async Add(id, item) {
|
|
85
51
|
this._db.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
|
|
86
52
|
const rawItem = this._converter.ToRaw(item);
|
|
87
|
-
|
|
88
|
-
if (!result.IsSuccess) {
|
|
89
|
-
throw new Error(`Could not add file: ${result.ErrorMessage}`);
|
|
90
|
-
}
|
|
53
|
+
await this._db.Send(new AddCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
|
|
91
54
|
this._db.Log(`"${id}" added to "${this.DatasetPath}".`);
|
|
92
55
|
}
|
|
93
56
|
;
|
|
94
57
|
async Update(id, item) {
|
|
95
58
|
this._db.Log(`Updating "${id}"...`);
|
|
96
59
|
const rawItem = this._converter.ToRaw(item);
|
|
97
|
-
|
|
98
|
-
if (!result.IsSuccess) {
|
|
99
|
-
throw new Error(`Could not update file: ${result.ErrorMessage}`);
|
|
100
|
-
}
|
|
60
|
+
await this._db.Send(new UpdateCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
|
|
101
61
|
this._db.Log(`"${id}" updated in "${this.DatasetPath}".`);
|
|
102
62
|
}
|
|
103
63
|
;
|
|
104
64
|
async Delete(id) {
|
|
105
65
|
this._db.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
|
|
106
|
-
|
|
107
|
-
if (!result.IsSuccess) {
|
|
108
|
-
throw new Error(`Could not delete file: ${result.ErrorMessage}`);
|
|
109
|
-
}
|
|
66
|
+
await this._db.Send(new DeleteCommand(this._db.DatabaseDir, this.dataset, id));
|
|
110
67
|
this._db.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
|
|
111
68
|
}
|
|
112
69
|
async Drop() {
|
|
113
70
|
this._db.Log(`Dropping "${this.DatasetPath}"...`);
|
|
114
|
-
|
|
115
|
-
if (!result.IsSuccess) {
|
|
116
|
-
throw new Error(`Could not drop dataset: ${result.ErrorMessage}`);
|
|
117
|
-
}
|
|
71
|
+
await this._db.Send(new DropCommand(this._db.DatabaseDir, this.dataset));
|
|
118
72
|
this._db.Log(`"${this.DatasetPath}" dropped.`);
|
|
119
73
|
}
|
|
120
74
|
}
|
package/IRepo.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
|
|
1
2
|
export interface IRepo<T> {
|
|
3
|
+
CanUserDo(action: keyof IStorageUserPrivileges): boolean;
|
|
2
4
|
Add(id: string, item: T): Promise<void>;
|
|
3
5
|
GetAll(): Promise<T[]>;
|
|
4
6
|
GetOne(id: string): Promise<T | null>;
|
|
5
|
-
FindOne(id: string): Promise<T | null>;
|
|
6
7
|
Update(id: string, item: T): Promise<void>;
|
|
7
8
|
Delete(id: string): Promise<void>;
|
|
8
9
|
Drop(): Promise<void>;
|
package/Messages/FindQuery.d.ts
CHANGED
package/Messages/FindQuery.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Message } from '../MessageBus/Message';
|
|
2
|
-
export class
|
|
3
|
-
constructor(
|
|
4
|
-
super("
|
|
2
|
+
export class SearchQuery extends Message {
|
|
3
|
+
constructor(Database, Id) {
|
|
4
|
+
super("Search", { Database, Id });
|
|
5
5
|
}
|
|
6
6
|
}
|
package/OnlineStorage.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { MessageBus } from "./MessageBus/MessageBus";
|
|
|
5
5
|
import { MessageBusResponse } from "./MessageBus/MessageBusResponse";
|
|
6
6
|
import { Dataset } from "./Dataset";
|
|
7
7
|
import { IStorageUser } from "./IStorageUser";
|
|
8
|
+
import { IConverter } from "./IConverter";
|
|
8
9
|
export declare class OnlineStorage {
|
|
9
10
|
private database;
|
|
10
11
|
private ANONYMOUS_USER;
|
|
@@ -15,8 +16,7 @@ export declare class OnlineStorage {
|
|
|
15
16
|
private readonly USER_LOCAL_STORAGE_KEY;
|
|
16
17
|
get DatabaseDir(): dir;
|
|
17
18
|
constructor(server: url, database: dir, sender?: ISender, bus?: MessageBus);
|
|
18
|
-
|
|
19
|
-
InitOnce(): Promise<void>;
|
|
19
|
+
GetDataset<T extends object>(datasetName: string, converter: IConverter<any, T>): Dataset<T>;
|
|
20
20
|
private IsBrowserEnvironment;
|
|
21
21
|
private LoadUserFromLocalStorage;
|
|
22
22
|
get User(): IStorageUser;
|
package/OnlineStorage.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { MessageBus } from "./MessageBus/MessageBus";
|
|
2
2
|
import { CreateCommand, ExistsCommand, RemoveCommand } from "./Messages/AddDirCommand";
|
|
3
|
+
import { Dataset } from "./Dataset";
|
|
3
4
|
import { MoveCommand } from "./Messages/MoveCommand";
|
|
4
5
|
import { LoginQuery } from "./Messages/LoginQuery";
|
|
5
6
|
export class OnlineStorage {
|
|
@@ -17,38 +18,50 @@ export class OnlineStorage {
|
|
|
17
18
|
this.database = database;
|
|
18
19
|
this._bus = bus || new MessageBus(server, sender);
|
|
19
20
|
this.USER_LOCAL_STORAGE_KEY = "OnlineStorage:User:" + this.database;
|
|
20
|
-
this.LoadUserFromLocalStorage(
|
|
21
|
-
|
|
22
|
-
WasAnonymousUserEverLoaded() {
|
|
23
|
-
// console.log( "aaaaAAA", this.user.Privileges)
|
|
24
|
-
return (this.User.Id === this.ANONYMOUS_USER.Id && this.user.Privileges !== undefined);
|
|
25
|
-
}
|
|
26
|
-
async InitOnce() {
|
|
27
|
-
// console.log("INIT", this.WasAnonymousUserEverLoaded());
|
|
28
|
-
if (this.WasAnonymousUserEverLoaded()) {
|
|
29
|
-
// console.log("INIT RETURN WasAnonymousUserEverLoaded()=true");
|
|
30
|
-
return;
|
|
21
|
+
if (this.LoadUserFromLocalStorage()) {
|
|
22
|
+
this.CallOnUserChanged(true);
|
|
31
23
|
}
|
|
32
|
-
this.user = await this.Login("Anonymous"); // TODO: Tutaj można my zrobić dedykowaną metode do pobierania ustawień użytkownika Anonymous
|
|
33
24
|
}
|
|
25
|
+
GetDataset(datasetName, converter) {
|
|
26
|
+
return new Dataset(this, converter, datasetName);
|
|
27
|
+
}
|
|
28
|
+
// private WasAnonymousUserEverLoaded(): boolean
|
|
29
|
+
// {
|
|
30
|
+
// return (this.User.Id === this.ANONYMOUS_USER.Id && this.user.Privileges !== undefined);
|
|
31
|
+
// }
|
|
32
|
+
// public SetAnonymousUserPrivileges(privileges: IStorageUserPrivileges): this
|
|
33
|
+
// {
|
|
34
|
+
// this.user.Privileges = privileges;
|
|
35
|
+
// return this;
|
|
36
|
+
// }
|
|
37
|
+
// public async InitOnce()
|
|
38
|
+
// {
|
|
39
|
+
// // console.log("INIT", this.WasAnonymousUserEverLoaded());
|
|
40
|
+
// if (this.WasAnonymousUserEverLoaded())
|
|
41
|
+
// {
|
|
42
|
+
// // console.log("INIT RETURN WasAnonymousUserEverLoaded()=true");
|
|
43
|
+
// return;
|
|
44
|
+
// }
|
|
45
|
+
// this.user = await this.Login("Anonymous"); // TODO: Tutaj można my zrobić dedykowaną metode do pobierania ustawień użytkownika Anonymous
|
|
46
|
+
// }
|
|
34
47
|
IsBrowserEnvironment() {
|
|
35
48
|
return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
|
|
36
49
|
}
|
|
37
|
-
LoadUserFromLocalStorage(
|
|
50
|
+
LoadUserFromLocalStorage() {
|
|
38
51
|
if (this.IsBrowserEnvironment()) {
|
|
39
52
|
const user = window.localStorage.getItem(this.USER_LOCAL_STORAGE_KEY);
|
|
40
53
|
if (user) {
|
|
41
54
|
this.user = JSON.parse(user);
|
|
42
55
|
this.SetUser(this.user || this.ANONYMOUS_USER);
|
|
43
|
-
|
|
56
|
+
return true;
|
|
44
57
|
}
|
|
45
58
|
}
|
|
59
|
+
return false;
|
|
46
60
|
}
|
|
47
61
|
get User() {
|
|
48
62
|
return this.user;
|
|
49
63
|
}
|
|
50
64
|
async Login(password) {
|
|
51
|
-
// console.log("LOGIN", password);
|
|
52
65
|
const result = await this._bus.SendMessage(new LoginQuery(this.DatabaseDir, password));
|
|
53
66
|
if (!result.IsSuccess) {
|
|
54
67
|
throw new Error(`Could not login: ${result.ErrorMessage}`);
|
|
@@ -57,7 +70,6 @@ export class OnlineStorage {
|
|
|
57
70
|
this.SetUser(this.user);
|
|
58
71
|
this.SaveUserInLocalStorage();
|
|
59
72
|
this.CallOnUserChanged(false);
|
|
60
|
-
// console.log("USER LOGIN", this.user);
|
|
61
73
|
return this.user;
|
|
62
74
|
}
|
|
63
75
|
SaveUserInLocalStorage() {
|
|
@@ -123,7 +135,11 @@ export class OnlineStorage {
|
|
|
123
135
|
return await this._bus.Ping();
|
|
124
136
|
}
|
|
125
137
|
async Send(message) {
|
|
126
|
-
|
|
138
|
+
const result = await this._bus.SendMessage(message);
|
|
139
|
+
if (!result.IsSuccess) {
|
|
140
|
+
throw new Error(`Message resulted with error: ${result.ErrorMessage}`);
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
127
143
|
}
|
|
128
144
|
async CreateDataset(datasetName) {
|
|
129
145
|
this.Log(`Creating dataset "${datasetName}"...`);
|
package/package.json
CHANGED
|
Binary file
|
package/tblabs-storage-5.2.7.tgz
DELETED
|
Binary file
|