@tblabs/storage 5.10.0 → 5.11.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/IRepo.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { IRepoBase } from "./IRepoBase";
2
2
  export interface IRepo<T> extends IRepoBase {
3
3
  Add(id: string, item: T): Promise<void>;
4
- GetOne(id: string): Promise<T | null>;
4
+ GetOne(id: string): Promise<T>;
5
5
  GetAll(): Promise<T[]>;
6
6
  GetAllIds(): Promise<string[]>;
7
7
  Update(id: string, item: T): Promise<void>;
@@ -0,0 +1,5 @@
1
+ import { TextConverter } from "./TextConverter";
2
+ export declare class JsonTextConverter<T> implements TextConverter<T> {
3
+ ToLine(entry: T): string;
4
+ ToEntry(line: string): T;
5
+ }
@@ -0,0 +1,8 @@
1
+ export class JsonTextConverter {
2
+ ToLine(entry) {
3
+ return JSON.stringify(entry);
4
+ }
5
+ ToEntry(line) {
6
+ return JSON.parse(line);
7
+ }
8
+ }
@@ -9,7 +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
+ import { RawTextDataset } from "./RawTextDataset";
13
13
  export declare class OnlineStorage {
14
14
  private server;
15
15
  private database;
@@ -32,7 +32,7 @@ export declare class OnlineStorage {
32
32
  UpdateUser(user: IStorageUser): Promise<void>;
33
33
  DeleteUser(user: IStorageUser): Promise<void>;
34
34
  GetDataset<T extends object>(datasetName: string, converter: IConverter<any, T>): Dataset<T>;
35
- GetTextDataset(datasetName: string): TextDataset;
35
+ GetTextDataset(datasetName: string): RawTextDataset;
36
36
  private IsBrowserEnvironment;
37
37
  private LoadUserFromLocalStorage;
38
38
  get User(): IStorageUser;
package/OnlineStorage.js CHANGED
@@ -17,7 +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
+ import { RawTextDataset } from "./RawTextDataset";
21
21
  export class OnlineStorage {
22
22
  server;
23
23
  database;
@@ -107,7 +107,7 @@ export class OnlineStorage {
107
107
  return new Dataset(this, converter, datasetName);
108
108
  }
109
109
  GetTextDataset(datasetName) {
110
- return new TextDataset(this, datasetName);
110
+ return new RawTextDataset(this, datasetName);
111
111
  }
112
112
  IsBrowserEnvironment() {
113
113
  return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
@@ -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 RawTextDataset 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
+ }
@@ -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 RawTextDataset 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
+ }
@@ -0,0 +1,4 @@
1
+ export interface TextConverter<T> {
2
+ ToLine(entry: T): string;
3
+ ToEntry(line: string): T;
4
+ }
@@ -0,0 +1 @@
1
+ export {};
package/TextDataset.d.ts CHANGED
@@ -1,16 +1,12 @@
1
- import { DatasetBase } from "./DatasetBase";
2
- import { ITextRepo } from "./ITextRepo";
3
1
  import { OnlineStorage } from "./OnlineStorage";
2
+ import { RawTextDataset } from "./RawTextDataset";
3
+ import { TextConverter } from "./TextConverter";
4
4
  import { dir } from "./Types/dir";
5
- export declare class TextDataset extends DatasetBase implements ITextRepo {
5
+ export declare class TextDataset<T> extends RawTextDataset {
6
6
  protected _db: OnlineStorage;
7
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>;
8
+ protected _converter: TextConverter<T>;
9
+ constructor(_db: OnlineStorage, dataset: dir, _converter?: TextConverter<T>);
10
+ Load(id: string, offset?: number, limit?: number): Promise<T[]>;
11
+ Append(id: string, entry: T): Promise<void>;
16
12
  }
package/TextDataset.js CHANGED
@@ -1,73 +1,21 @@
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 {
1
+ import { JsonTextConverter } from "./JsonTextConverter";
2
+ import { RawTextDataset } from "./RawTextDataset";
3
+ export class TextDataset extends RawTextDataset {
9
4
  _db;
10
5
  dataset;
11
- constructor(_db, dataset) {
6
+ _converter;
7
+ constructor(_db, dataset, _converter = new JsonTextConverter()) {
12
8
  super(_db, dataset);
13
9
  this._db = _db;
14
10
  this.dataset = dataset;
11
+ this._converter = _converter;
15
12
  }
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;
13
+ async Load(id, offset = 0, limit = 0) {
14
+ const lines = await super.Read(id, offset, limit);
15
+ return lines.map(line => this._converter.ToEntry(line));
25
16
  }
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.`);
17
+ async Append(id, entry) {
18
+ const line = this._converter.ToLine(entry);
19
+ await super.AppendLine(id, line);
72
20
  }
73
21
  }
package/index.d.ts CHANGED
@@ -5,11 +5,14 @@ export * from "./IStorageUser";
5
5
  export * from "./IConverter";
6
6
  export * from "./Converter";
7
7
  export * from "./DummyConverter";
8
+ export * from "./TextConverter";
9
+ export * from "./JsonTextConverter";
8
10
  export * from "./IRepoBase";
9
11
  export * from "./IRepo";
10
12
  export * from "./ITextRepo";
11
13
  export * from "./IRepos";
12
14
  export * from "./Dataset";
15
+ export * from "./RawTextDataset";
13
16
  export * from "./TextDataset";
14
17
  export * from "./Datasets";
15
18
  export * from "./OnlineStorage";
package/index.js CHANGED
@@ -5,11 +5,14 @@ export * from "./IStorageUser";
5
5
  export * from "./IConverter";
6
6
  export * from "./Converter";
7
7
  export * from "./DummyConverter";
8
+ export * from "./TextConverter";
9
+ export * from "./JsonTextConverter";
8
10
  export * from "./IRepoBase";
9
11
  export * from "./IRepo";
10
12
  export * from "./ITextRepo";
11
13
  export * from "./IRepos";
12
14
  export * from "./Dataset";
15
+ export * from "./RawTextDataset";
13
16
  export * from "./TextDataset";
14
17
  export * from "./Datasets";
15
18
  export * from "./OnlineStorage";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tblabs/storage",
3
- "version": "5.10.0",
3
+ "version": "5.11.0",
4
4
  "description": "online storage module with auth",
5
5
  "license": "beerware",
6
6
  "main": "index.js",
Binary file
Binary file