@tblabs/storage 5.9.2 → 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,24 +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
- Create(id: string): Promise<void>;
20
14
  Add(id: string, item: T): Promise<void>;
21
- AppendText(id: string, text: string): Promise<void>;
22
- AppendLine(id: string, line: string): Promise<void>;
23
15
  Update(id: string, item: T): Promise<void>;
24
16
  Delete(id: string): Promise<void>;
25
17
  Drop(): Promise<void>;
package/Dataset.js CHANGED
@@ -5,136 +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
- import { CreateFileCommand } from "./Messages/CreateFileCommand";
11
- export class Dataset {
8
+ import { DatasetBase } from "./DatasetBase";
9
+ export class Dataset extends DatasetBase {
12
10
  _db;
13
11
  _converter;
14
12
  dataset;
15
13
  constructor(_db, _converter, dataset) {
14
+ super(_db, dataset);
16
15
  this._db = _db;
17
16
  this._converter = _converter;
18
17
  this.dataset = dataset;
19
18
  }
20
- get Name() {
21
- return this.dataset;
22
- }
23
- get Database() {
24
- return this._db;
25
- }
26
- get DatasetPath() {
27
- return this._db.DatabaseDir + "/Datasets/" + this.dataset;
28
- }
29
- CanUserDo(action) {
30
- if (!this._db.User.Privileges) {
31
- return false;
32
- }
33
- if (typeof this._db.User.Privileges[action] === "object" && this._db.User.Privileges[action]) {
34
- return (this._db.User.Privileges[action].Datasets == "all" || this._db.User.Privileges[action].Datasets?.includes(this.dataset)) ?? false;
35
- }
36
- if (typeof this._db.User.Privileges[action] === "boolean" && this._db.User.Privileges[action]) {
37
- return this._db.User.Privileges[action];
38
- }
39
- return false;
40
- }
41
19
  async GetOne(id) {
42
- this._db.Log(`Fetching "${id}"...`);
20
+ this.Log(`Fetching "${id}"...`);
43
21
  const queryResult = await this._db.Send(new GetFileQuery(this._db.DatabaseDir, this.dataset, id));
44
22
  if (!queryResult.IsSuccess) {
45
23
  throw new Error("Failed to get one item: " + queryResult.ErrorMessage);
46
24
  }
47
25
  const rawItem = queryResult.Result;
48
26
  const item = this._converter.FromRaw(rawItem);
49
- this._db.Log(`Fetched "${id}" from "${this.DatasetPath}".`);
27
+ this.Log(`Fetched "${id}" from "${this.DatasetPath}".`);
50
28
  return item;
51
29
  }
52
- async Read(id, offset = 0, limit = 0) {
53
- this._db.Log(`Reading "${id}" from "${this.DatasetPath}"...`);
54
- const queryResult = await this._db.Send(new ReadFileQuery(this._db.DatabaseDir, this.dataset, id, offset, limit));
55
- if (!queryResult.IsSuccess) {
56
- throw new Error("Failed to read: " + queryResult.ErrorMessage);
57
- }
58
- const content = queryResult.Result;
59
- this._db.Log(`Read ${content.length} characters from "${id}" in "${this.DatasetPath}".`);
60
- return content;
61
- }
62
30
  async GetAll() {
63
- this._db.Log(`Fetching all from "${this.DatasetPath}"...`);
31
+ this.Log(`Fetching all from "${this.DatasetPath}"...`);
64
32
  const queryResult = await this._db.Send(new ListFilesQuery(this._db.DatabaseDir, this.dataset));
65
33
  if (!queryResult.IsSuccess) {
66
34
  throw new Error("Failed to fetch all items: " + queryResult.ErrorMessage);
67
35
  }
68
36
  const rawItems = queryResult.Result;
69
37
  const items = rawItems.map((raw) => this._converter.FromRaw(raw));
70
- this._db.Log(`Fetched ${items.length} items from "${this.DatasetPath}".`);
38
+ this.Log(`Fetched ${items.length} items from "${this.DatasetPath}".`);
71
39
  return items;
72
40
  }
73
41
  async GetAllIds() {
74
- this._db.Log(`Fetching all files IDs from "${this.DatasetPath}"...`);
42
+ this.Log(`Fetching all files IDs from "${this.DatasetPath}"...`);
75
43
  const queryResult = await this._db.Send(new ListFilesIdsQuery(this._db.DatabaseDir, this.dataset));
76
44
  if (!queryResult.IsSuccess) {
77
45
  throw new Error("Failed to fetch all items ids: " + queryResult.ErrorMessage);
78
46
  }
79
47
  const ids = queryResult.Result;
80
- this._db.Log(`Found ${ids.length} files in "${this.DatasetPath}".`);
48
+ this.Log(`Found ${ids.length} files in "${this.DatasetPath}".`);
81
49
  return ids;
82
50
  }
83
- async Create(id) {
84
- this._db.Log(`Creating "${id}" in "${this.DatasetPath}"...`);
85
- const result = await this._db.Send(new CreateFileCommand(this._db.DatabaseDir, this.dataset, id));
86
- if (!result.IsSuccess) {
87
- throw new Error("Failed to create item: " + result.ErrorMessage);
88
- }
89
- this._db.Log(`"${id}" created in "${this.DatasetPath}".`);
90
- }
91
- ;
92
51
  async Add(id, item) {
93
- this._db.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
52
+ this.Log(`Adding "${id}" to "${this.DatasetPath}"...`);
94
53
  const rawItem = this._converter.ToRaw(item);
95
54
  const result = await this._db.Send(new AddFileCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
96
55
  if (!result.IsSuccess) {
97
56
  throw new Error("Failed to add item: " + result.ErrorMessage);
98
57
  }
99
- this._db.Log(`"${id}" added to "${this.DatasetPath}".`);
58
+ this.Log(`"${id}" added to "${this.DatasetPath}".`);
100
59
  }
101
60
  ;
102
- async AppendText(id, text) {
103
- this._db.Log(`Appending ${text.length} characters to "${id}" in "${this.DatasetPath}"...`);
104
- const result = await this._db.Send(new AppendCommand(this._db.DatabaseDir, this.dataset, id, text));
105
- if (!result.IsSuccess) {
106
- throw new Error("Failed to append: " + result.ErrorMessage);
107
- }
108
- this._db.Log(`${text.length} characters appended to "${id}" in "${this.DatasetPath}".`);
109
- }
110
- ;
111
- async AppendLine(id, line) {
112
- await this.AppendText(id, line + "\n");
113
- }
114
61
  async Update(id, item) {
115
- this._db.Log(`Updating "${id}"...`);
62
+ this.Log(`Updating "${id}"...`);
116
63
  const rawItem = this._converter.ToRaw(item);
117
64
  const result = await this._db.Send(new UpdateFileCommand(this._db.DatabaseDir, this.dataset, id, rawItem));
118
65
  if (!result.IsSuccess) {
119
66
  throw new Error("Failed to update item: " + result.ErrorMessage);
120
67
  }
121
- this._db.Log(`"${id}" updated in "${this.DatasetPath}".`);
68
+ this.Log(`"${id}" updated in "${this.DatasetPath}".`);
122
69
  }
123
70
  ;
124
71
  async Delete(id) {
125
- this._db.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
72
+ this.Log(`Deleting "${id}" from "${this.DatasetPath}"...`);
126
73
  const result = await this._db.Send(new DeleteFileCommand(this._db.DatabaseDir, this.dataset, id));
127
74
  if (!result.IsSuccess) {
128
75
  throw new Error("Failed to delete item: " + result.ErrorMessage);
129
76
  }
130
- this._db.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
77
+ this.Log(`Deleted "${id}" from "${this.DatasetPath}".`);
131
78
  }
132
79
  async Drop() {
133
- this._db.Log(`Dropping "${this.DatasetPath}"...`);
80
+ this.Log(`Dropping "${this.DatasetPath}"...`);
134
81
  const result = await this._db.Send(new DropDatasetCommand(this._db.DatabaseDir, this.dataset));
135
82
  if (!result.IsSuccess) {
136
83
  throw new Error("Failed to drop dataset: " + result.ErrorMessage);
137
84
  }
138
- this._db.Log(`"${this.DatasetPath}" dropped.`);
85
+ this.Log(`"${this.DatasetPath}" dropped.`);
139
86
  }
140
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 {};
@@ -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.2",
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