@tblabs/storage 5.9.1 → 5.10.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
@@ -2,23 +2,16 @@ 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";
6
- export declare class Dataset<T extends object> implements IRepo<T> {
7
- private _db;
8
- private _converter;
9
- private dataset;
5
+ import { DatasetBase } from "./DatasetBase";
6
+ export declare class Dataset<T extends object> extends DatasetBase implements IRepo<T> {
7
+ protected _db: OnlineStorage;
8
+ protected _converter: IConverter<any, T>;
9
+ protected dataset: dir;
10
10
  constructor(_db: OnlineStorage, _converter: IConverter<any, T>, dataset: dir);
11
- get Name(): string;
12
- get Database(): OnlineStorage;
13
- private get DatasetPath();
14
- CanUserDo(action: keyof IStorageUserPrivileges): boolean;
15
11
  GetOne(id: string): Promise<T>;
16
- Read(id: string, offset?: number, limit?: number): Promise<string>;
17
12
  GetAll(): Promise<T[]>;
18
13
  GetAllIds(): Promise<string[]>;
19
14
  Add(id: string, item: T): Promise<void>;
20
- Append(id: string, text: string): Promise<void>;
21
- AppendLine(id: string, line: string): Promise<void>;
22
15
  Update(id: string, item: T): Promise<void>;
23
16
  Delete(id: string): Promise<void>;
24
17
  Drop(): Promise<void>;
package/Dataset.js CHANGED
@@ -5,126 +5,83 @@ import { ListFilesQuery } from "./Messages/ListFilesQuery";
5
5
  import { GetFileQuery } from "./Messages/GetFileQuery";
6
6
  import { UpdateFileCommand } from "./Messages/UpdateFileCommand";
7
7
  import { ListFilesIdsQuery } from "./Messages/ListFilesIdsQuery";
8
- import { AppendCommand } from "./Messages/AppendCommand";
9
- import { ReadFileQuery } from "./Messages/ReadFileQuery";
10
- export class Dataset {
8
+ import { DatasetBase } from "./DatasetBase";
9
+ export class Dataset extends DatasetBase {
11
10
  _db;
12
11
  _converter;
13
12
  dataset;
14
13
  constructor(_db, _converter, dataset) {
14
+ super(_db, dataset);
15
15
  this._db = _db;
16
16
  this._converter = _converter;
17
17
  this.dataset = dataset;
18
18
  }
19
- get Name() {
20
- return this.dataset;
21
- }
22
- get Database() {
23
- return this._db;
24
- }
25
- get DatasetPath() {
26
- return this._db.DatabaseDir + "/Datasets/" + this.dataset;
27
- }
28
- CanUserDo(action) {
29
- if (!this._db.User.Privileges) {
30
- return false;
31
- }
32
- if (typeof this._db.User.Privileges[action] === "object" && this._db.User.Privileges[action]) {
33
- return (this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets?.includes(this.dataset)) ?? false;
34
- }
35
- if (typeof this._db.User.Privileges[action] === "boolean" && this._db.User.Privileges[action]) {
36
- return this._db.User.Privileges[action];
37
- }
38
- return false;
39
- }
40
19
  async GetOne(id) {
41
- this._db.Log(`Fetching "${id}"...`);
20
+ this.Log(`Fetching "${id}"...`);
42
21
  const queryResult = await this._db.Send(new GetFileQuery(this._db.DatabaseDir, this.dataset, id));
43
22
  if (!queryResult.IsSuccess) {
44
23
  throw new Error("Failed to get one item: " + queryResult.ErrorMessage);
45
24
  }
46
25
  const rawItem = queryResult.Result;
47
26
  const item = this._converter.FromRaw(rawItem);
48
- this._db.Log(`Fetched "${id}" from "${this.DatasetPath}".`);
27
+ this.Log(`Fetched "${id}" from "${this.DatasetPath}".`);
49
28
  return item;
50
29
  }
51
- async Read(id, offset = 0, limit = 0) {
52
- this._db.Log(`Reading "${id}" from "${this.DatasetPath}"...`);
53
- const queryResult = await this._db.Send(new ReadFileQuery(this._db.DatabaseDir, this.dataset, id, offset, limit));
54
- if (!queryResult.IsSuccess) {
55
- throw new Error("Failed to read: " + queryResult.ErrorMessage);
56
- }
57
- const content = queryResult.Result;
58
- this._db.Log(`Read ${content.length} characters from "${id}" in "${this.DatasetPath}".`);
59
- return content;
60
- }
61
30
  async GetAll() {
62
- this._db.Log(`Fetching all from "${this.DatasetPath}"...`);
31
+ this.Log(`Fetching all from "${this.DatasetPath}"...`);
63
32
  const queryResult = await this._db.Send(new ListFilesQuery(this._db.DatabaseDir, this.dataset));
64
33
  if (!queryResult.IsSuccess) {
65
34
  throw new Error("Failed to fetch all items: " + queryResult.ErrorMessage);
66
35
  }
67
36
  const rawItems = queryResult.Result;
68
37
  const items = rawItems.map((raw) => this._converter.FromRaw(raw));
69
- this._db.Log(`Fetched ${items.length} items from "${this.DatasetPath}".`);
38
+ this.Log(`Fetched ${items.length} items from "${this.DatasetPath}".`);
70
39
  return items;
71
40
  }
72
41
  async GetAllIds() {
73
- this._db.Log(`Fetching all files IDs from "${this.DatasetPath}"...`);
42
+ this.Log(`Fetching all files IDs from "${this.DatasetPath}"...`);
74
43
  const queryResult = await this._db.Send(new ListFilesIdsQuery(this._db.DatabaseDir, this.dataset));
75
44
  if (!queryResult.IsSuccess) {
76
45
  throw new Error("Failed to fetch all items ids: " + queryResult.ErrorMessage);
77
46
  }
78
47
  const ids = queryResult.Result;
79
- this._db.Log(`Found ${ids.length} files in "${this.DatasetPath}".`);
48
+ this.Log(`Found ${ids.length} files in "${this.DatasetPath}".`);
80
49
  return ids;
81
50
  }
82
51
  async Add(id, item) {
83
- this._db.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
52
+ this.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
84
53
  const rawItem = this._converter.ToRaw(item);
85
54
  const result = await this._db.Send(new AddFileCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
86
55
  if (!result.IsSuccess) {
87
56
  throw new Error("Failed to add item: " + result.ErrorMessage);
88
57
  }
89
- this._db.Log(`"${id}" added to "${this.DatasetPath}".`);
58
+ this.Log(`"${id}" added to "${this.DatasetPath}".`);
90
59
  }
91
60
  ;
92
- async Append(id, text) {
93
- this._db.Log(`Appending ${text.length} characters to "${id}" in "${this.DatasetPath}"...`);
94
- const result = await this._db.Send(new AppendCommand(this._db.DatabaseDir, this.dataset, id, text));
95
- if (!result.IsSuccess) {
96
- throw new Error("Failed to append: " + result.ErrorMessage);
97
- }
98
- this._db.Log(`${text.length} characters appended to "${id}" in "${this.DatasetPath}".`);
99
- }
100
- ;
101
- async AppendLine(id, line) {
102
- await this.Append(id, line + "\n");
103
- }
104
61
  async Update(id, item) {
105
- this._db.Log(`Updating "${id}"...`);
62
+ this.Log(`Updating "${id}"...`);
106
63
  const rawItem = this._converter.ToRaw(item);
107
64
  const result = await this._db.Send(new UpdateFileCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
108
65
  if (!result.IsSuccess) {
109
66
  throw new Error("Failed to update item: " + result.ErrorMessage);
110
67
  }
111
- this._db.Log(`"${id}" updated in "${this.DatasetPath}".`);
68
+ this.Log(`"${id}" updated in "${this.DatasetPath}".`);
112
69
  }
113
70
  ;
114
71
  async Delete(id) {
115
- this._db.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
72
+ this.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
116
73
  const result = await this._db.Send(new DeleteFileCommand(this._db.DatabaseDir, this.dataset, id));
117
74
  if (!result.IsSuccess) {
118
75
  throw new Error("Failed to delete item: " + result.ErrorMessage);
119
76
  }
120
- this._db.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
77
+ this.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
121
78
  }
122
79
  async Drop() {
123
- this._db.Log(`Dropping "${this.DatasetPath}"...`);
80
+ this.Log(`Dropping "${this.DatasetPath}"...`);
124
81
  const result = await this._db.Send(new DropDatasetCommand(this._db.DatabaseDir, this.dataset));
125
82
  if (!result.IsSuccess) {
126
83
  throw new Error("Failed to drop dataset: " + result.ErrorMessage);
127
84
  }
128
- this._db.Log(`"${this.DatasetPath}" dropped.`);
85
+ this.Log(`"${this.DatasetPath}" dropped.`);
129
86
  }
130
87
  }
@@ -0,0 +1,13 @@
1
+ import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
2
+ import { OnlineStorage } from "./OnlineStorage";
3
+ import { dir } from "./Types/dir";
4
+ export declare class DatasetBase {
5
+ protected _db: OnlineStorage;
6
+ protected dataset: dir;
7
+ constructor(_db: OnlineStorage, dataset: dir);
8
+ get Name(): string;
9
+ get Database(): OnlineStorage;
10
+ protected get DatasetPath(): string;
11
+ CanUserDo(action: keyof IStorageUserPrivileges): boolean;
12
+ protected Log(message: string): void;
13
+ }
package/DatasetBase.js ADDED
@@ -0,0 +1,32 @@
1
+ export class DatasetBase {
2
+ _db;
3
+ dataset;
4
+ constructor(_db, dataset) {
5
+ this._db = _db;
6
+ this.dataset = dataset;
7
+ }
8
+ get Name() {
9
+ return this.dataset;
10
+ }
11
+ get Database() {
12
+ return this._db;
13
+ }
14
+ get DatasetPath() {
15
+ return this._db.DatabaseDir + "/Datasets/" + this.dataset;
16
+ }
17
+ CanUserDo(action) {
18
+ if (!this._db.User.Privileges) {
19
+ return false;
20
+ }
21
+ if (typeof this._db.User.Privileges[action] === "object" && this._db.User.Privileges[action]) {
22
+ return (this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets?.includes(this.dataset)) ?? false;
23
+ }
24
+ if (typeof this._db.User.Privileges[action] === "boolean" && this._db.User.Privileges[action]) {
25
+ return this._db.User.Privileges[action];
26
+ }
27
+ return false;
28
+ }
29
+ Log(message) {
30
+ this._db.Log(message);
31
+ }
32
+ }
package/IRepo.d.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
2
- export interface IRepo<T> {
3
- CanUserDo(action: keyof IStorageUserPrivileges): boolean;
1
+ import { IRepoBase } from "./IRepoBase";
2
+ export interface IRepo<T> extends IRepoBase {
4
3
  Add(id: string, item: T): Promise<void>;
5
- GetAll(): Promise<T[]>;
6
4
  GetOne(id: string): Promise<T | null>;
5
+ GetAll(): Promise<T[]>;
6
+ GetAllIds(): Promise<string[]>;
7
7
  Update(id: string, item: T): Promise<void>;
8
8
  Delete(id: string): Promise<void>;
9
9
  Drop(): Promise<void>;
package/IRepoBase.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
2
+ import { OnlineStorage } from "./OnlineStorage";
3
+ export interface IRepoBase {
4
+ get Name(): string;
5
+ get Database(): OnlineStorage;
6
+ CanUserDo(action: keyof IStorageUserPrivileges): boolean;
7
+ }
package/IRepoBase.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/ITextRepo.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { IRepoBase } from "./IRepoBase";
2
+ export interface ITextRepo extends IRepoBase {
3
+ Create(id: string): Promise<void>;
4
+ Read(id: string, offset: number, limit: number): Promise<string[]>;
5
+ GetAllIds(): Promise<string[]>;
6
+ AppendText(id: string, text: string): Promise<void>;
7
+ AppendLine(id: string, line: string): Promise<void>;
8
+ Delete(id: string): Promise<void>;
9
+ Drop(): Promise<void>;
10
+ }
package/ITextRepo.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ import { dir } from '../Types/dir';
3
+ export declare class CreateFileCommand extends Message {
4
+ constructor(Database: dir, Dataset: dir, FileId: string);
5
+ }
@@ -0,0 +1,6 @@
1
+ import { Message } from '../MessageBus/Message';
2
+ export class CreateFileCommand extends Message {
3
+ constructor(Database, Dataset, FileId) {
4
+ super("CreateFile", { Database, Dataset, FileId });
5
+ }
6
+ }
@@ -9,6 +9,7 @@ import { IConverter } from "./IConverter";
9
9
  import { IStorageUserPrivileges } from "./IStorageUserPrivileges";
10
10
  import { DatabaseInfo } from "./DatabaseInfo";
11
11
  import { DatasetInfo } from "./DatasetInfo";
12
+ import { TextDataset } from "./TextDataset";
12
13
  export declare class OnlineStorage {
13
14
  private server;
14
15
  private database;
@@ -31,6 +32,7 @@ export declare class OnlineStorage {
31
32
  UpdateUser(user: IStorageUser): Promise<void>;
32
33
  DeleteUser(user: IStorageUser): Promise<void>;
33
34
  GetDataset<T extends object>(datasetName: string, converter: IConverter<any, T>): Dataset<T>;
35
+ GetTextDataset(datasetName: string): TextDataset;
34
36
  private IsBrowserEnvironment;
35
37
  private LoadUserFromLocalStorage;
36
38
  get User(): IStorageUser;
package/OnlineStorage.js CHANGED
@@ -17,6 +17,7 @@ import { ListDatabasesQuery } from "./ListDatabasesQuery";
17
17
  import { CreateDatabaseCommand } from "./CreateDatabaseCommand";
18
18
  import { ListDatasetsQuery } from "./Messages/ListDatabasesQuery";
19
19
  import { DeleteDatabaseCommand } from "./DeleteDatabaseCommand";
20
+ import { TextDataset } from "./TextDataset";
20
21
  export class OnlineStorage {
21
22
  server;
22
23
  database;
@@ -105,6 +106,9 @@ export class OnlineStorage {
105
106
  GetDataset(datasetName, converter) {
106
107
  return new Dataset(this, converter, datasetName);
107
108
  }
109
+ GetTextDataset(datasetName) {
110
+ return new TextDataset(this, datasetName);
111
+ }
108
112
  IsBrowserEnvironment() {
109
113
  return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
110
114
  }
@@ -0,0 +1,16 @@
1
+ import { DatasetBase } from "./DatasetBase";
2
+ import { ITextRepo } from "./ITextRepo";
3
+ import { OnlineStorage } from "./OnlineStorage";
4
+ import { dir } from "./Types/dir";
5
+ export declare class TextDataset extends DatasetBase implements ITextRepo {
6
+ protected _db: OnlineStorage;
7
+ protected dataset: dir;
8
+ constructor(_db: OnlineStorage, dataset: dir);
9
+ Read(id: string, offset?: number, limit?: number): Promise<string[]>;
10
+ GetAllIds(): Promise<string[]>;
11
+ Create(id: string): Promise<void>;
12
+ AppendText(id: string, text: string): Promise<void>;
13
+ AppendLine(id: string, line: string): Promise<void>;
14
+ Delete(id: string): Promise<void>;
15
+ Drop(): Promise<void>;
16
+ }
package/TextDataset.js ADDED
@@ -0,0 +1,73 @@
1
+ import { DatasetBase } from "./DatasetBase";
2
+ import { AppendCommand } from "./Messages/AppendCommand";
3
+ import { CreateFileCommand } from "./Messages/CreateFileCommand";
4
+ import { DeleteFileCommand } from "./Messages/DeleteFileCommand";
5
+ import { DropDatasetCommand } from "./Messages/DropDatasetCommand";
6
+ import { ListFilesIdsQuery } from "./Messages/ListFilesIdsQuery";
7
+ import { ReadFileQuery } from "./Messages/ReadFileQuery";
8
+ export class TextDataset extends DatasetBase {
9
+ _db;
10
+ dataset;
11
+ constructor(_db, dataset) {
12
+ super(_db, dataset);
13
+ this._db = _db;
14
+ this.dataset = dataset;
15
+ }
16
+ async Read(id, offset = 0, limit = 0) {
17
+ this.Log(`Reading "${id}" from "${this.DatasetPath}"...`);
18
+ const queryResult = await this._db.Send(new ReadFileQuery(this._db.DatabaseDir, this.dataset, id, offset, limit));
19
+ if (!queryResult.IsSuccess) {
20
+ throw new Error("Failed to read: " + queryResult.ErrorMessage);
21
+ }
22
+ const content = queryResult.Result;
23
+ this.Log(`Read ${content.length} characters from "${id}" in "${this.DatasetPath}".`);
24
+ return content;
25
+ }
26
+ async GetAllIds() {
27
+ this.Log(`Fetching all files IDs from "${this.DatasetPath}"...`);
28
+ const queryResult = await this._db.Send(new ListFilesIdsQuery(this._db.DatabaseDir, this.dataset));
29
+ if (!queryResult.IsSuccess) {
30
+ throw new Error("Failed to fetch all items ids: " + queryResult.ErrorMessage);
31
+ }
32
+ const ids = queryResult.Result;
33
+ this.Log(`Found ${ids.length} files in "${this.DatasetPath}".`);
34
+ return ids;
35
+ }
36
+ async Create(id) {
37
+ this.Log(`Creating "${id}" in "${this.DatasetPath}"...`);
38
+ const result = await this._db.Send(new CreateFileCommand(this._db.DatabaseDir, this.dataset, id));
39
+ if (!result.IsSuccess) {
40
+ throw new Error("Failed to create item: " + result.ErrorMessage);
41
+ }
42
+ this.Log(`"${id}" created in "${this.DatasetPath}".`);
43
+ }
44
+ ;
45
+ async AppendText(id, text) {
46
+ this.Log(`Appending ${text.length} characters to "${id}" in "${this.DatasetPath}"...`);
47
+ const result = await this._db.Send(new AppendCommand(this._db.DatabaseDir, this.dataset, id, text));
48
+ if (!result.IsSuccess) {
49
+ throw new Error("Failed to append: " + result.ErrorMessage);
50
+ }
51
+ this.Log(`${text.length} characters appended to "${id}" in "${this.DatasetPath}".`);
52
+ }
53
+ ;
54
+ async AppendLine(id, line) {
55
+ await this.AppendText(id, line + "\n");
56
+ }
57
+ async Delete(id) {
58
+ this.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
59
+ const result = await this._db.Send(new DeleteFileCommand(this._db.DatabaseDir, this.dataset, id));
60
+ if (!result.IsSuccess) {
61
+ throw new Error("Failed to delete item: " + result.ErrorMessage);
62
+ }
63
+ this.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
64
+ }
65
+ async Drop() {
66
+ this.Log(`Dropping "${this.DatasetPath}"...`);
67
+ const result = await this._db.Send(new DropDatasetCommand(this._db.DatabaseDir, this.dataset));
68
+ if (!result.IsSuccess) {
69
+ throw new Error("Failed to drop dataset: " + result.ErrorMessage);
70
+ }
71
+ this.Log(`"${this.DatasetPath}" dropped.`);
72
+ }
73
+ }
package/index.d.ts CHANGED
@@ -5,9 +5,12 @@ export * from "./IStorageUser";
5
5
  export * from "./IConverter";
6
6
  export * from "./Converter";
7
7
  export * from "./DummyConverter";
8
+ export * from "./IRepoBase";
8
9
  export * from "./IRepo";
10
+ export * from "./ITextRepo";
9
11
  export * from "./IRepos";
10
12
  export * from "./Dataset";
13
+ export * from "./TextDataset";
11
14
  export * from "./Datasets";
12
15
  export * from "./OnlineStorage";
13
16
  export * from "./Utils/OperationRepeater";
package/index.js CHANGED
@@ -5,9 +5,12 @@ export * from "./IStorageUser";
5
5
  export * from "./IConverter";
6
6
  export * from "./Converter";
7
7
  export * from "./DummyConverter";
8
+ export * from "./IRepoBase";
8
9
  export * from "./IRepo";
10
+ export * from "./ITextRepo";
9
11
  export * from "./IRepos";
10
12
  export * from "./Dataset";
13
+ export * from "./TextDataset";
11
14
  export * from "./Datasets";
12
15
  export * from "./OnlineStorage";
13
16
  export * from "./Utils/OperationRepeater";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.9.1",
3
+ "version": "5.10.0",
4
4
  "description": "online storage module with auth",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
Binary file
Binary file