@tblabs/storage 5.0.1 → 5.0.2

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.js CHANGED
@@ -24,7 +24,7 @@ export class Dataset {
24
24
  this._db.Log(`Fetching "${id}"...`);
25
25
  const queryResult = await this._db.Send(new GetQuery(this._db.DatabaseDir, this.dataset, id));
26
26
  if (!queryResult.IsSuccess) {
27
- throw new Error(`Could not fetch "${id}" from "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
27
+ throw new Error(`Could not fetch file: ${queryResult.ErrorMessage}`);
28
28
  }
29
29
  const rawItem = queryResult.Result;
30
30
  const item = this._converter.FromRaw(rawItem);
@@ -35,7 +35,7 @@ export class Dataset {
35
35
  this._db.Log(`Searching for "${id}"...`);
36
36
  const queryResult = await this._db.Send(new FindQuery(id, this._db.DatabaseDir));
37
37
  if (!queryResult.IsSuccess) {
38
- throw new Error(`Could not find "${id}" in "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
38
+ throw new Error(`Could not find file: ${queryResult.ErrorMessage}`);
39
39
  }
40
40
  const rawItem = queryResult.Result;
41
41
  const item = this._converter.FromRaw(rawItem);
@@ -46,7 +46,7 @@ export class Dataset {
46
46
  this._db.Log(`Fetching all from "${this.DatasetPath}"...`);
47
47
  const queryResult = await this._db.Send(new ListQuery(this._db.DatabaseDir, this.dataset));
48
48
  if (!queryResult.IsSuccess) {
49
- throw new Error(`Could not fetch items from "${this.DatasetPath}": ${queryResult.ErrorMessage}`);
49
+ throw new Error(`Could not fetch items: ${queryResult.ErrorMessage}`);
50
50
  }
51
51
  const rawItems = queryResult.Result;
52
52
  const items = rawItems.map((raw) => this._converter.FromRaw(raw));
@@ -70,7 +70,7 @@ export class Dataset {
70
70
  this._db.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
71
71
  const result = await this._db.Send(new AddCommand(this._db.DatabaseDir, this.dataset, id, item));
72
72
  if (!result.IsSuccess) {
73
- throw new Error(`Could not add "${id}" to "${this.DatasetPath}": ${result.ErrorMessage}`);
73
+ throw new Error(`Could not add file: ${result.ErrorMessage}`);
74
74
  }
75
75
  this._db.Log(`"${id}" added to "${this.DatasetPath}".`);
76
76
  }
@@ -79,7 +79,7 @@ export class Dataset {
79
79
  this._db.Log(`Updating "${id}"...`);
80
80
  const result = await this._db.Send(new UpdateCommand(this._db.DatabaseDir, this.dataset, id, item));
81
81
  if (!result.IsSuccess) {
82
- throw new Error(`Could not update "${id}" in "${this.DatasetPath}": ${result.ErrorMessage}`);
82
+ throw new Error(`Could not update file: ${result.ErrorMessage}`);
83
83
  }
84
84
  this._db.Log(`"${id}" updated in "${this.DatasetPath}".`);
85
85
  }
@@ -88,7 +88,7 @@ export class Dataset {
88
88
  this._db.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
89
89
  const result = await this._db.Send(new DeleteCommand(this._db.DatabaseDir, this.dataset, id));
90
90
  if (!result.IsSuccess) {
91
- throw new Error(`Could not delete "${id}" from "${this.DatasetPath}": ${result.ErrorMessage}`);
91
+ throw new Error(`Could not delete file: ${result.ErrorMessage}`);
92
92
  }
93
93
  this._db.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
94
94
  }
@@ -96,7 +96,7 @@ export class Dataset {
96
96
  this._db.Log(`Dropping "${this.DatasetPath}"...`);
97
97
  const result = await this._db.Send(new DropCommand(this._db.DatabaseDir, this.dataset));
98
98
  if (!result.IsSuccess) {
99
- throw new Error(`Could not drop "${this.DatasetPath}": ${result.ErrorMessage}`);
99
+ throw new Error(`Could not drop dataset: ${result.ErrorMessage}`);
100
100
  }
101
101
  this._db.Log(`"${this.DatasetPath}" dropped.`);
102
102
  }
@@ -0,0 +1,50 @@
1
+ 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
+ Files: IEntitiesSet;
13
+ };
14
+ Delete: {
15
+ Files: IEntitiesSet;
16
+ };
17
+ List: {
18
+ Datasets: IEntitiesSet;
19
+ };
20
+ Get: {
21
+ Files: IEntitiesSet;
22
+ };
23
+ Drop: {
24
+ Datasets: IEntitiesSet;
25
+ };
26
+ Move: {
27
+ Datasets: IEntitiesSet;
28
+ Files: IEntitiesSet;
29
+ };
30
+ Create: {
31
+ Datasets: IEntitiesSet;
32
+ };
33
+ Exists: {
34
+ Datasets: IEntitiesSet;
35
+ };
36
+ Remove: {
37
+ Datasets: IEntitiesSet;
38
+ };
39
+ Register: boolean;
40
+ }
41
+ export interface IStorageUser extends ISender {
42
+ Id: string;
43
+ Database: string;
44
+ Name: string;
45
+ Role: string;
46
+ Icon: string;
47
+ Password: string;
48
+ Privileges: IStorageUserPrivileges;
49
+ Tags: string[];
50
+ }
@@ -0,0 +1,2 @@
1
+ ;
2
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Message } from "../MessageBus/Message";
2
+ import { dir } from "../Types/dir";
3
+ export declare class LoginQuery extends Message {
4
+ constructor(Database: dir, Password: string);
5
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from "../MessageBus/Message";
2
+ export class LoginQuery extends Message {
3
+ constructor(Database, Password) {
4
+ super("Login", { Database, Password });
5
+ }
6
+ }
@@ -0,0 +1,6 @@
1
+ import { IStorageUserPrivileges } from "../IStorageUser";
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, Password: string, Privileges: IStorageUserPrivileges, Icon?: string, Tags?: string[]);
6
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from "../MessageBus/Message";
2
+ export class RegisterCommand extends Message {
3
+ constructor(Database, Id, Role, Name, Password, Privileges, Icon, Tags) {
4
+ super("Register", { Database, Id, Role, Icon, Name, Password, Privileges, Tags });
5
+ }
6
+ }
@@ -3,68 +3,20 @@ import { IMessage } from "./MessageBus/IMessage";
3
3
  import { ISender } from "./MessageBus/ISender";
4
4
  import { MessageBus } from "./MessageBus/MessageBus";
5
5
  import { MessageBusResponse } from "./MessageBus/MessageBusResponse";
6
- import { Message } from "./MessageBus/Message";
7
6
  import { Dataset } from "./Dataset";
8
- export type IEntitiesSet = string[] | "all" | "none";
9
- export interface IUser {
10
- Id: string;
11
- Name: string;
12
- Database: string;
13
- Privileges: {
14
- Add: {
15
- Datasets: IEntitiesSet;
16
- };
17
- Update: {
18
- Datasets: IEntitiesSet;
19
- Files: IEntitiesSet;
20
- };
21
- Delete: {
22
- Datasets: IEntitiesSet;
23
- Files: IEntitiesSet;
24
- };
25
- List: {
26
- Datasets: IEntitiesSet;
27
- };
28
- Get: {
29
- Files: IEntitiesSet;
30
- };
31
- Drop: {
32
- Datasets: IEntitiesSet;
33
- };
34
- Move: {
35
- Datasets: IEntitiesSet;
36
- Files: IEntitiesSet;
37
- };
38
- Backup: {
39
- Datasets: IEntitiesSet;
40
- };
41
- Find: {
42
- Files: IEntitiesSet;
43
- };
44
- Create: {
45
- Datasets: IEntitiesSet;
46
- };
47
- Exists: {
48
- Datasets: IEntitiesSet;
49
- };
50
- Remove: {
51
- Datasets: IEntitiesSet;
52
- };
53
- };
54
- }
55
- export declare class AuthQuery extends Message {
56
- constructor(Database: dir, Password: string);
57
- }
7
+ import { IStorageUser, IStorageUserPrivileges } from "./IStorageUser";
58
8
  export declare class OnlineStorage {
59
9
  private database;
60
10
  private logEnabled;
61
11
  private readonly _bus;
62
12
  get DatabaseDir(): dir;
63
13
  constructor(server: url, database: dir, sender?: ISender, bus?: MessageBus);
64
- Login(password: string): Promise<IUser>;
14
+ Login(password: string): Promise<IStorageUser>;
15
+ Register(id: string, role: string, name: string, password: string, privileges: IStorageUserPrivileges, icon?: string, tags?: string[]): Promise<IStorageUser>;
65
16
  Logout(): this;
17
+ Init(): Promise<void>;
66
18
  IsAuthorized(): boolean;
67
- User(access: ISender): this;
19
+ User(sender: ISender): this;
68
20
  EnableLog(): this;
69
21
  DisableLog(): this;
70
22
  Log(...message: any[]): void;
package/OnlineStorage.js CHANGED
@@ -1,12 +1,8 @@
1
1
  import { MessageBus } from "./MessageBus/MessageBus";
2
2
  import { CreateCommand, ExistsCommand, RemoveCommand } from "./Messages/AddDirCommand";
3
- import { Message } from "./MessageBus/Message";
4
3
  import { MoveCommand } from "./Messages/MoveCommand";
5
- export class AuthQuery extends Message {
6
- constructor(Database, Password) {
7
- super("LoginUser", { Database, Password });
8
- }
9
- }
4
+ import { LoginQuery } from "./Messages/LoginQuery";
5
+ import { RegisterCommand } from "./Messages/RegisterCommand";
10
6
  export class OnlineStorage {
11
7
  database;
12
8
  logEnabled = false;
@@ -19,7 +15,7 @@ export class OnlineStorage {
19
15
  this._bus = bus || new MessageBus(server, sender);
20
16
  }
21
17
  async Login(password) {
22
- const result = await this._bus.SendMessage(new AuthQuery(this.DatabaseDir, password));
18
+ const result = await this._bus.SendMessage(new LoginQuery(this.DatabaseDir, password));
23
19
  if (!result.IsSuccess) {
24
20
  throw new Error(`Could not login: ${result.ErrorMessage}`);
25
21
  }
@@ -27,15 +23,26 @@ export class OnlineStorage {
27
23
  this.User({ Id: user?.Id ?? "Anonymous" });
28
24
  return user;
29
25
  }
26
+ async Register(id, role, name, password, privileges, icon, tags) {
27
+ const result = await this._bus.SendMessage(new RegisterCommand(this.DatabaseDir, id, role, name, password, privileges, icon, tags));
28
+ if (!result.IsSuccess) {
29
+ throw new Error(`Could not register: ${result.ErrorMessage}`);
30
+ }
31
+ const user = result.Result;
32
+ return user;
33
+ }
30
34
  Logout() {
31
35
  this.User({ Id: "Anonymous" });
32
36
  return this;
33
37
  }
38
+ async Init() {
39
+ // TODO: Setup Anonymous user and Admin
40
+ }
34
41
  IsAuthorized() {
35
42
  return this._bus.Sender?.Id !== "Anonymous";
36
43
  }
37
- User(access) {
38
- this._bus.SetSender(access);
44
+ User(sender) {
45
+ this._bus.SetSender(sender);
39
46
  return this;
40
47
  }
41
48
  EnableLog() {
@@ -61,7 +68,7 @@ export class OnlineStorage {
61
68
  this.Log(`Creating dataset "${datasetName}"...`);
62
69
  const result = await this._bus.SendMessage(new CreateCommand(this.DatabaseDir, datasetName));
63
70
  if (!result.IsSuccess) {
64
- throw new Error(`Could not add dataset "${datasetName}": ${result.ErrorMessage}`);
71
+ throw new Error(`Could not add dataset: ${result.ErrorMessage}`);
65
72
  }
66
73
  this.Log(`Dataset "${datasetName}" created.`);
67
74
  }
@@ -69,7 +76,7 @@ export class OnlineStorage {
69
76
  this.Log(`Checking if dataset "${datasetName}" exists...`);
70
77
  const result = await this._bus.SendMessage(new ExistsCommand(this.DatabaseDir, datasetName));
71
78
  if (!result.IsSuccess) {
72
- throw new Error(`Could not check if dataset "${datasetName}" exists: ${result.ErrorMessage}`);
79
+ throw new Error(`Could not check if dataset exists: ${result.ErrorMessage}`);
73
80
  }
74
81
  this.Log(`Dataset "${datasetName}" ${result.Result ? "exists" : "does not exist"}.`);
75
82
  return result.Result;
@@ -78,7 +85,7 @@ export class OnlineStorage {
78
85
  this.Log(`Removing dataset "${datasetName}"...`);
79
86
  const result = await this._bus.SendMessage(new RemoveCommand(this.DatabaseDir, datasetName));
80
87
  if (!result.IsSuccess) {
81
- throw new Error(`Could not remove dataset "${datasetName}": ${result.ErrorMessage}`);
88
+ throw new Error(`Could not remove dataset: ${result.ErrorMessage}`);
82
89
  }
83
90
  this.Log(`Dataset "${datasetName}" removed.`);
84
91
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "online storage module with auth",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
Binary file
Binary file