@tblabs/storage 5.2.6 → 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 CHANGED
@@ -2,6 +2,7 @@ import { OnlineStorage } from "./OnlineStorage";
2
2
  import { IConverter } from "./IConverter";
3
3
  import { IRepo } from "./IRepo";
4
4
  import { dir } from "./Types/dir";
5
+ import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
5
6
  export declare class Dataset<T extends object> implements IRepo<T> {
6
7
  private _db;
7
8
  private _converter;
@@ -9,8 +10,8 @@ export declare class Dataset<T extends object> implements IRepo<T> {
9
10
  get Name(): string;
10
11
  constructor(_db: OnlineStorage, _converter: IConverter<any, T>, dataset: dir);
11
12
  private get DatasetPath();
13
+ CanUserDo(action: keyof IStorageUserPrivileges): boolean;
12
14
  GetOne(id: string): Promise<T>;
13
- FindOne(id: string): Promise<T | null>;
14
15
  GetAll(): Promise<T[]>;
15
16
  Add(id: string, item: T): Promise<void>;
16
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";
@@ -20,86 +19,56 @@ export class Dataset {
20
19
  get DatasetPath() {
21
20
  return this._db.DatabaseDir + "/Datasets/" + this.dataset;
22
21
  }
22
+ CanUserDo(action) {
23
+ if (!this._db.User.Privileges) {
24
+ return false;
25
+ }
26
+ if (typeof this._db.User.Privileges[action] === "object" && this._db.User.Privileges[action]) {
27
+ return (this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets?.includes(this.dataset)) ?? false;
28
+ }
29
+ if (typeof this._db.User.Privileges[action] === "boolean" && this._db.User.Privileges[action]) {
30
+ return this._db.User.Privileges[action];
31
+ }
32
+ return false;
33
+ }
23
34
  async GetOne(id) {
24
35
  this._db.Log(`Fetching "${id}"...`);
25
36
  const queryResult = await this._db.Send(new GetQuery(this._db.DatabaseDir, this.dataset, id));
26
- if (!queryResult.IsSuccess) {
27
- throw new Error(`Could not fetch file: ${queryResult.ErrorMessage}`);
28
- }
29
37
  const rawItem = queryResult.Result;
30
38
  const item = this._converter.FromRaw(rawItem);
31
39
  this._db.Log(`Fetched "${id}" from "${this.DatasetPath}".`);
32
40
  return item;
33
41
  }
34
- async FindOne(id) {
35
- this._db.Log(`Searching for "${id}"...`);
36
- const queryResult = await this._db.Send(new FindQuery(id, this._db.DatabaseDir));
37
- if (!queryResult.IsSuccess) {
38
- throw new Error(`Could not find file: ${queryResult.ErrorMessage}`);
39
- }
40
- const rawItem = queryResult.Result;
41
- const item = this._converter.FromRaw(rawItem);
42
- this._db.Log(`Found "${id}" in "${this.DatasetPath}".`);
43
- return item;
44
- }
45
42
  async GetAll() {
46
43
  this._db.Log(`Fetching all from "${this.DatasetPath}"...`);
47
44
  const queryResult = await this._db.Send(new ListQuery(this._db.DatabaseDir, this.dataset));
48
- if (!queryResult.IsSuccess) {
49
- throw new Error(`Could not fetch items: ${queryResult.ErrorMessage}`);
50
- }
51
45
  const rawItems = queryResult.Result;
52
46
  const items = rawItems.map((raw) => this._converter.FromRaw(raw));
53
47
  this._db.Log(`Fetched ${items.length} items from "${this.DatasetPath}".`);
54
48
  return items;
55
49
  }
56
- // public async GetAllIds(): Promise<string[]>
57
- // {
58
- // this._db.Log(`Fetching all ids from "${this.DatasetPath}"...`);
59
- // const queryResult = await this._db.Send(new ListQuery(this.DatasetPath, false));
60
- // if (!queryResult.IsSuccess)
61
- // {
62
- // this._db.Log(`Could not fetch all ids from "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
63
- // return [];
64
- // }
65
- // const itemsIds = Object.values(queryResult.Result);
66
- // this._db.Log(`Fetched ${itemsIds.length} items ids from "${this.DatasetPath}".`);
67
- // return itemsIds as string[];
68
- // }
69
50
  async Add(id, item) {
70
51
  this._db.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
71
52
  const rawItem = this._converter.ToRaw(item);
72
- const result = await this._db.Send(new AddCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
73
- if (!result.IsSuccess) {
74
- throw new Error(`Could not add file: ${result.ErrorMessage}`);
75
- }
53
+ await this._db.Send(new AddCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
76
54
  this._db.Log(`"${id}" added to "${this.DatasetPath}".`);
77
55
  }
78
56
  ;
79
57
  async Update(id, item) {
80
58
  this._db.Log(`Updating "${id}"...`);
81
59
  const rawItem = this._converter.ToRaw(item);
82
- const result = await this._db.Send(new UpdateCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
83
- if (!result.IsSuccess) {
84
- throw new Error(`Could not update file: ${result.ErrorMessage}`);
85
- }
60
+ await this._db.Send(new UpdateCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
86
61
  this._db.Log(`"${id}" updated in "${this.DatasetPath}".`);
87
62
  }
88
63
  ;
89
64
  async Delete(id) {
90
65
  this._db.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
91
- const result = await this._db.Send(new DeleteCommand(this._db.DatabaseDir, this.dataset, id));
92
- if (!result.IsSuccess) {
93
- throw new Error(`Could not delete file: ${result.ErrorMessage}`);
94
- }
66
+ await this._db.Send(new DeleteCommand(this._db.DatabaseDir, this.dataset, id));
95
67
  this._db.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
96
68
  }
97
69
  async Drop() {
98
70
  this._db.Log(`Dropping "${this.DatasetPath}"...`);
99
- const result = await this._db.Send(new DropCommand(this._db.DatabaseDir, this.dataset));
100
- if (!result.IsSuccess) {
101
- throw new Error(`Could not drop dataset: ${result.ErrorMessage}`);
102
- }
71
+ await this._db.Send(new DropCommand(this._db.DatabaseDir, this.dataset));
103
72
  this._db.Log(`"${this.DatasetPath}" dropped.`);
104
73
  }
105
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/IStorageUser.d.ts CHANGED
@@ -1,46 +1,5 @@
1
+ import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
1
2
  import { ISender } from "./MessageBus/ISender";
2
- export type IEntitiesSet = string[] | "all" | "none";
3
- export type ICommandPrivilege = {
4
- Datasets?: IEntitiesSet;
5
- Files?: IEntitiesSet;
6
- };
7
- export interface IStorageUserPrivileges {
8
- Add: {
9
- Datasets: IEntitiesSet;
10
- };
11
- Update: {
12
- Datasets: IEntitiesSet;
13
- Files: IEntitiesSet;
14
- };
15
- Delete: {
16
- Datasets: IEntitiesSet;
17
- Files: IEntitiesSet;
18
- };
19
- List: {
20
- Datasets: IEntitiesSet;
21
- };
22
- Get: {
23
- Datasets: IEntitiesSet;
24
- Files: IEntitiesSet;
25
- };
26
- Drop: {
27
- Datasets: IEntitiesSet;
28
- };
29
- Move: {
30
- Datasets: IEntitiesSet;
31
- Files: IEntitiesSet;
32
- };
33
- Create: {
34
- Datasets: IEntitiesSet;
35
- };
36
- Exists: {
37
- Datasets: IEntitiesSet;
38
- };
39
- Remove: {
40
- Datasets: IEntitiesSet;
41
- };
42
- Register: boolean;
43
- }
44
3
  export interface IStorageUser extends ISender {
45
4
  Id: string;
46
5
  Database: string;
package/IStorageUser.js CHANGED
@@ -1,2 +1 @@
1
- ;
2
1
  export {};
@@ -0,0 +1,41 @@
1
+ export type IEntitiesSet = string[] | "all" | "none";
2
+ export interface IStorageUserPrivileges {
3
+ Add: {
4
+ Datasets: IEntitiesSet;
5
+ };
6
+ Update: {
7
+ Datasets: IEntitiesSet;
8
+ Files: IEntitiesSet;
9
+ };
10
+ Delete: {
11
+ Datasets: IEntitiesSet;
12
+ Files: IEntitiesSet;
13
+ };
14
+ List: {
15
+ Datasets: IEntitiesSet;
16
+ };
17
+ Get: {
18
+ Datasets: IEntitiesSet;
19
+ Files: IEntitiesSet;
20
+ };
21
+ Drop: {
22
+ Datasets: IEntitiesSet;
23
+ };
24
+ Move: {
25
+ Datasets: IEntitiesSet;
26
+ Files: IEntitiesSet;
27
+ };
28
+ Create: {
29
+ Datasets: IEntitiesSet;
30
+ };
31
+ Exists: {
32
+ Datasets: IEntitiesSet;
33
+ };
34
+ Remove: {
35
+ Datasets: IEntitiesSet;
36
+ };
37
+ Search: {
38
+ Datasets: IEntitiesSet;
39
+ };
40
+ Register: boolean;
41
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
1
  import { Message } from '../MessageBus/Message';
2
- export declare class FindQuery extends Message {
3
- constructor(Id: string, Folder: string);
2
+ export declare class SearchQuery extends Message {
3
+ constructor(Database: string, Id: string);
4
4
  }
@@ -1,6 +1,6 @@
1
1
  import { Message } from '../MessageBus/Message';
2
- export class FindQuery extends Message {
3
- constructor(Id, Folder) {
4
- super("Find", { Id, Folder });
2
+ export class SearchQuery extends Message {
3
+ constructor(Database, Id) {
4
+ super("Search", { Database, Id });
5
5
  }
6
6
  }
@@ -1,4 +1,4 @@
1
- import { IStorageUserPrivileges } from "../IStorageUser";
1
+ import { IStorageUserPrivileges } from "../IStorageUserPrivileges";
2
2
  import { Message } from "../MessageBus/Message";
3
3
  import { dir } from "../Types/dir";
4
4
  export declare class RegisterCommand extends Message {
@@ -4,7 +4,8 @@ import { ISender } from "./MessageBus/ISender";
4
4
  import { MessageBus } from "./MessageBus/MessageBus";
5
5
  import { MessageBusResponse } from "./MessageBus/MessageBusResponse";
6
6
  import { Dataset } from "./Dataset";
7
- import { IStorageUser, IStorageUserPrivileges } from "./IStorageUser";
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,13 +16,13 @@ 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);
19
+ GetDataset<T extends object>(datasetName: string, converter: IConverter<any, T>): Dataset<T>;
18
20
  private IsBrowserEnvironment;
19
21
  private LoadUserFromLocalStorage;
20
22
  get User(): IStorageUser;
21
23
  Login(password: string): Promise<IStorageUser>;
22
24
  private SaveUserInLocalStorage;
23
25
  OnUserChange(handler: (user: IStorageUser, isAuthorized: boolean, appStart: boolean) => void): this;
24
- Register(id: string, role: string, name: string, login: string, password: string, email: string, privileges: IStorageUserPrivileges, icon?: string, tags?: string[]): Promise<IStorageUser>;
25
26
  Logout(): this;
26
27
  private CallOnUserChanged;
27
28
  get IsAuthorized(): boolean;
package/OnlineStorage.js CHANGED
@@ -1,8 +1,8 @@
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
- import { RegisterCommand } from "./Messages/RegisterCommand";
6
6
  export class OnlineStorage {
7
7
  database;
8
8
  ANONYMOUS_USER = { Id: "Anonymous" };
@@ -18,20 +18,45 @@ export class OnlineStorage {
18
18
  this.database = database;
19
19
  this._bus = bus || new MessageBus(server, sender);
20
20
  this.USER_LOCAL_STORAGE_KEY = "OnlineStorage:User:" + this.database;
21
- this.LoadUserFromLocalStorage(true);
21
+ if (this.LoadUserFromLocalStorage()) {
22
+ this.CallOnUserChanged(true);
23
+ }
22
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
+ // }
23
47
  IsBrowserEnvironment() {
24
48
  return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
25
49
  }
26
- LoadUserFromLocalStorage(appStart) {
50
+ LoadUserFromLocalStorage() {
27
51
  if (this.IsBrowserEnvironment()) {
28
52
  const user = window.localStorage.getItem(this.USER_LOCAL_STORAGE_KEY);
29
53
  if (user) {
30
54
  this.user = JSON.parse(user);
31
55
  this.SetUser(this.user || this.ANONYMOUS_USER);
32
- this.CallOnUserChanged(appStart);
56
+ return true;
33
57
  }
34
58
  }
59
+ return false;
35
60
  }
36
61
  get User() {
37
62
  return this.user;
@@ -57,15 +82,25 @@ export class OnlineStorage {
57
82
  handler(this.user, this.IsAuthorized, true);
58
83
  return this;
59
84
  }
60
- async Register(id, role, name, login, password, email, privileges, icon, tags) {
61
- const createdAt = new Date();
62
- const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, login, password, email, privileges, icon, tags, createdAt));
63
- if (!result.IsSuccess) {
64
- throw new Error(`Could not register: ${result.ErrorMessage}`);
65
- }
66
- const user = result.Result;
67
- return user;
68
- }
85
+ //
86
+ // UWAGA!!!!!!!!!! TAK NIE MOŻNA REJESTROWAĆ Privileges!!!!!! TO MUSI SIĘ ODBYWAĆ PRZEZ INNĄ METODE ALBO INNY MECHANIZM!!!!!!!
87
+ // Użytkownik nie może sam sobie nadawać uprawnień xddd
88
+ //
89
+ //
90
+ // public async Register(id: string, role: string, name: string,
91
+ // login: string, password: string, email: string,
92
+ // privileges: IStorageUserPrivileges,
93
+ // icon?: string, tags?: string[]): Promise<IStorageUser>
94
+ // {
95
+ // const createdAt = new Date();
96
+ // const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, login, password, email, privileges, icon, tags, createdAt));
97
+ // if (!result.IsSuccess)
98
+ // {
99
+ // throw new Error(`Could not register: ${result.ErrorMessage}`);
100
+ // }
101
+ // const user = result.Result as IStorageUser;
102
+ // return user;
103
+ // }
69
104
  Logout() {
70
105
  this.SetUser(this.ANONYMOUS_USER);
71
106
  this.SaveUserInLocalStorage();
@@ -100,7 +135,11 @@ export class OnlineStorage {
100
135
  return await this._bus.Ping();
101
136
  }
102
137
  async Send(message) {
103
- return await this._bus.SendMessage(message);
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;
104
143
  }
105
144
  async CreateDataset(datasetName) {
106
145
  this.Log(`Creating dataset "${datasetName}"...`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.2.6",
3
+ "version": "5.2.8",
4
4
  "description": "online storage module with auth",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
Binary file
Binary file