@wxn0brp/db 0.5.5 → 0.5.7

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.
@@ -0,0 +1,4 @@
1
+ import DataBaseRemote from "./client/database.js";
2
+ import { Remote } from "./client/remote.js";
3
+ import DataBase from "./database.js";
4
+ export declare function autoCreate(cfg: string | Remote): DataBase | DataBaseRemote;
@@ -0,0 +1,9 @@
1
+ import DataBaseRemote from "./client/database.js";
2
+ import DataBase from "./database.js";
3
+ export function autoCreate(cfg) {
4
+ if (typeof cfg === "object")
5
+ return new DataBaseRemote(cfg);
6
+ if (cfg.startsWith("http"))
7
+ return new DataBaseRemote(cfg);
8
+ return new DataBase(cfg);
9
+ }
@@ -12,7 +12,7 @@ import { Transaction } from "../types/transactions.js";
12
12
  */
13
13
  declare class DataBaseRemote {
14
14
  remote: Remote;
15
- constructor(remote: Remote);
15
+ constructor(remote: Remote | string);
16
16
  /**
17
17
  * Make a request to the remote database.
18
18
  */
@@ -9,7 +9,25 @@ import serializeFunctions from "./function.js";
9
9
  class DataBaseRemote {
10
10
  remote;
11
11
  constructor(remote) {
12
- this.remote = remote;
12
+ if (typeof remote === "string") {
13
+ const urlObj = new URL(remote);
14
+ const name = urlObj.username;
15
+ const auth = urlObj.password;
16
+ if (!name || !auth)
17
+ throw new Error("Invalid remote database");
18
+ urlObj.username = "";
19
+ urlObj.password = "";
20
+ const url = urlObj.toString().slice(0, -1);
21
+ this.remote = {
22
+ name,
23
+ url,
24
+ auth
25
+ };
26
+ }
27
+ else
28
+ this.remote = remote;
29
+ if (this.remote.url.endsWith("/"))
30
+ this.remote.url = this.remote.url.slice(0, -1);
13
31
  }
14
32
  /**
15
33
  * Make a request to the remote database.
@@ -21,7 +39,8 @@ class DataBaseRemote {
21
39
  params: processed.data,
22
40
  keys: processed.keys
23
41
  };
24
- const res = await ky.post(this.remote.url + "/db/" + type, {
42
+ const url = this.remote.url + "/db/" + type;
43
+ const res = await ky.post(url, {
25
44
  json: data,
26
45
  headers: {
27
46
  "Authorization": this.remote.auth
@@ -9,7 +9,7 @@ declare class GraphRemote {
9
9
  /**
10
10
  * Create a new database instance.
11
11
  */
12
- constructor(remote: Remote);
12
+ constructor(remote: Remote | string);
13
13
  /**
14
14
  * Make a request to the remote database.
15
15
  */
@@ -10,7 +10,25 @@ class GraphRemote {
10
10
  * Create a new database instance.
11
11
  */
12
12
  constructor(remote) {
13
- this.remote = remote;
13
+ if (typeof remote === "string") {
14
+ const urlObj = new URL(remote);
15
+ const name = urlObj.username;
16
+ const auth = urlObj.password;
17
+ if (!name || !auth)
18
+ throw new Error("Invalid remote database");
19
+ urlObj.username = "";
20
+ urlObj.password = "";
21
+ const url = urlObj.toString().slice(0, -1);
22
+ this.remote = {
23
+ name,
24
+ url,
25
+ auth
26
+ };
27
+ }
28
+ else
29
+ this.remote = remote;
30
+ if (this.remote.url.endsWith("/"))
31
+ this.remote.url = this.remote.url.slice(0, -1);
14
32
  }
15
33
  /**
16
34
  * Make a request to the remote database.
@@ -20,7 +38,8 @@ class GraphRemote {
20
38
  db: this.remote.name,
21
39
  params
22
40
  };
23
- const res = await ky.post(this.remote.url + "/db/" + type, {
41
+ const url = this.remote.url + "/db/" + type;
42
+ const res = await ky.post(url, {
24
43
  json: data,
25
44
  headers: {
26
45
  "Authorization": this.remote.auth
package/dist/index.d.ts CHANGED
@@ -6,7 +6,8 @@ import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
7
  import CustomFileCpu from "./file/customFileCpu.js";
8
8
  import ValtheraMemory, { createMemoryValthera } from "./memory.js";
9
- export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera };
9
+ import { autoCreate } from "./autoCreate.js";
10
+ export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera, autoCreate, };
10
11
  export type Id = import("./types/Id.js").Id;
11
12
  export declare namespace ValtheraTypes {
12
13
  type Arg = import("./types/arg.js").Arg;
package/dist/index.js CHANGED
@@ -6,4 +6,5 @@ import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
7
  import CustomFileCpu from "./file/customFileCpu.js";
8
8
  import ValtheraMemory, { createMemoryValthera } from "./memory.js";
9
- export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera };
9
+ import { autoCreate } from "./autoCreate.js";
10
+ export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera, autoCreate, };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.5.5",
3
+ "version": "0.5.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",