@tblabs/storage 5.2.7 → 5.3.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/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.includes(this.dataset);
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
- const result = await this._db.Send(new AddCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
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
- const result = await this._db.Send(new UpdateCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
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
- const result = await this._db.Send(new DeleteCommand(this._db.DatabaseDir, this.dataset, id));
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
- const result = await this._db.Send(new DropCommand(this._db.DatabaseDir, this.dataset));
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>;
@@ -34,5 +34,8 @@ export interface IStorageUserPrivileges {
34
34
  Remove: {
35
35
  Datasets: IEntitiesSet;
36
36
  };
37
+ Search: {
38
+ Datasets: IEntitiesSet;
39
+ };
37
40
  Register: boolean;
38
41
  }
@@ -0,0 +1,5 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ import { dir } from '../Types/dir';
3
+ export declare class ListDatasetsQuery extends Message {
4
+ constructor(Database: dir);
5
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class ListDatasetsQuery extends Message {
3
+ constructor(Database) {
4
+ super("ListDatasets", { Database });
5
+ }
6
+ }
@@ -0,0 +1,5 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ import { dir } from '../Types/dir';
3
+ export declare class ListUsersQuery extends Message {
4
+ constructor(Database: dir);
5
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class ListUsersQuery extends Message {
3
+ constructor(Database) {
4
+ super("ListUsers", { Database });
5
+ }
6
+ }
@@ -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,9 @@ 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
- private WasAnonymousUserEverLoaded;
19
- InitOnce(): Promise<void>;
19
+ ListDatasets(): Promise<string[]>;
20
+ ListUsers(): Promise<IStorageUser[]>;
21
+ GetDataset<T extends object>(datasetName: string, converter: IConverter<any, T>): Dataset<T>;
20
22
  private IsBrowserEnvironment;
21
23
  private LoadUserFromLocalStorage;
22
24
  get User(): IStorageUser;
package/OnlineStorage.js CHANGED
@@ -1,7 +1,10 @@
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";
6
+ import { ListUsersQuery } from "./Messages/ListUsersQuery";
7
+ import { ListDatasetsQuery } from "./Messages/ListDatabasesQuery";
5
8
  export class OnlineStorage {
6
9
  database;
7
10
  ANONYMOUS_USER = { Id: "Anonymous" };
@@ -17,38 +20,64 @@ export class OnlineStorage {
17
20
  this.database = database;
18
21
  this._bus = bus || new MessageBus(server, sender);
19
22
  this.USER_LOCAL_STORAGE_KEY = "OnlineStorage:User:" + this.database;
20
- this.LoadUserFromLocalStorage(true);
23
+ if (this.LoadUserFromLocalStorage()) {
24
+ this.CallOnUserChanged(true);
25
+ }
21
26
  }
22
- WasAnonymousUserEverLoaded() {
23
- // console.log( "aaaaAAA", this.user.Privileges)
24
- return (this.User.Id === this.ANONYMOUS_USER.Id && this.user.Privileges !== undefined);
27
+ async ListDatasets() {
28
+ const result = await this._bus.SendMessage(new ListDatasetsQuery(this.DatabaseDir));
29
+ if (!result.IsSuccess) {
30
+ throw new Error(`Could not list datasets: ${result.ErrorMessage}`);
31
+ }
32
+ return result.Result;
25
33
  }
26
- async InitOnce() {
27
- // console.log("INIT", this.WasAnonymousUserEverLoaded());
28
- if (this.WasAnonymousUserEverLoaded()) {
29
- // console.log("INIT RETURN WasAnonymousUserEverLoaded()=true");
30
- return;
34
+ async ListUsers() {
35
+ const result = await this._bus.SendMessage(new ListUsersQuery(this.DatabaseDir));
36
+ if (!result.IsSuccess) {
37
+ throw new Error(`Could not list users: ${result.ErrorMessage}`);
31
38
  }
32
- this.user = await this.Login("Anonymous"); // TODO: Tutaj można my zrobić dedykowaną metode do pobierania ustawień użytkownika Anonymous
39
+ return result.Result;
40
+ }
41
+ GetDataset(datasetName, converter) {
42
+ return new Dataset(this, converter, datasetName);
33
43
  }
44
+ // private WasAnonymousUserEverLoaded(): boolean
45
+ // {
46
+ // return (this.User.Id === this.ANONYMOUS_USER.Id && this.user.Privileges !== undefined);
47
+ // }
48
+ // public SetAnonymousUserPrivileges(privileges: IStorageUserPrivileges): this
49
+ // {
50
+ // this.user.Privileges = privileges;
51
+ // return this;
52
+ // }
53
+ // public async InitOnce()
54
+ // {
55
+ // // console.log("INIT", this.WasAnonymousUserEverLoaded());
56
+ // if (this.WasAnonymousUserEverLoaded())
57
+ // {
58
+ // // console.log("INIT RETURN WasAnonymousUserEverLoaded()=true");
59
+ // return;
60
+ // }
61
+ // this.user = await this.Login("Anonymous"); // TODO: Tutaj można my zrobić dedykowaną metode do pobierania ustawień użytkownika Anonymous
62
+ // }
34
63
  IsBrowserEnvironment() {
35
64
  return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
36
65
  }
37
- LoadUserFromLocalStorage(appStart) {
66
+ LoadUserFromLocalStorage() {
38
67
  if (this.IsBrowserEnvironment()) {
39
68
  const user = window.localStorage.getItem(this.USER_LOCAL_STORAGE_KEY);
40
69
  if (user) {
41
70
  this.user = JSON.parse(user);
42
71
  this.SetUser(this.user || this.ANONYMOUS_USER);
43
- this.CallOnUserChanged(appStart);
72
+ return true;
44
73
  }
45
74
  }
75
+ return false;
46
76
  }
47
77
  get User() {
48
78
  return this.user;
49
79
  }
50
80
  async Login(password) {
51
- // console.log("LOGIN", password);
52
81
  const result = await this._bus.SendMessage(new LoginQuery(this.DatabaseDir, password));
53
82
  if (!result.IsSuccess) {
54
83
  throw new Error(`Could not login: ${result.ErrorMessage}`);
@@ -57,7 +86,6 @@ export class OnlineStorage {
57
86
  this.SetUser(this.user);
58
87
  this.SaveUserInLocalStorage();
59
88
  this.CallOnUserChanged(false);
60
- // console.log("USER LOGIN", this.user);
61
89
  return this.user;
62
90
  }
63
91
  SaveUserInLocalStorage() {
@@ -123,7 +151,11 @@ export class OnlineStorage {
123
151
  return await this._bus.Ping();
124
152
  }
125
153
  async Send(message) {
126
- return await this._bus.SendMessage(message);
154
+ const result = await this._bus.SendMessage(message);
155
+ if (!result.IsSuccess) {
156
+ throw new Error(`Message resulted with error: ${result.ErrorMessage}`);
157
+ }
158
+ return result;
127
159
  }
128
160
  async CreateDataset(datasetName) {
129
161
  this.Log(`Creating dataset "${datasetName}"...`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.2.7",
3
+ "version": "5.3.0",
4
4
  "description": "online storage module with auth",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
Binary file
@@ -1,4 +0,0 @@
1
- import { Message } from '../MessageBus/Message';
2
- export declare class FindQuery extends Message {
3
- constructor(Id: string, Folder: string);
4
- }
@@ -1,6 +0,0 @@
1
- import { Message } from '../MessageBus/Message';
2
- export class FindQuery extends Message {
3
- constructor(Id, Folder) {
4
- super("Find", { Id, Folder });
5
- }
6
- }
@@ -1,6 +0,0 @@
1
- import { IStorageUserPrivileges } from "../IStorageUserPrivileges";
2
- import { Message } from "../MessageBus/Message";
3
- import { dir } from "../Types/dir";
4
- export declare class RegisterCommand extends Message {
5
- constructor(Database: dir, Id: string, Role: string, Name: string, Login: string, Password: string, Email: string, Privileges: IStorageUserPrivileges, Icon?: string, Tags?: string[], CreatedAt?: Date);
6
- }
@@ -1,6 +0,0 @@
1
- import { Message } from "../MessageBus/Message";
2
- export class RegisterCommand extends Message {
3
- constructor(Database, Id, Role, Name, Login, Password, Email, Privileges, Icon, Tags, CreatedAt) {
4
- super("Register", { Database, Id, Role, Name, Login, Password, Email, Privileges, Icon, Tags, CreatedAt });
5
- }
6
- }
Binary file