@wxn0brp/db-storage-csv 0.120.0 → 0.130.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/dist/{action.d.ts → adapter.d.ts} +3 -2
- package/dist/{action.js → adapter.js} +24 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/package.json +3 -6
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CustomAdapterBase } from "@wxn0brp/db-core/base/customAdapter";
|
|
2
2
|
export interface Opts {
|
|
3
3
|
file?: string;
|
|
4
4
|
dir?: string;
|
|
5
5
|
}
|
|
6
|
-
export declare class DbStorageCsv extends
|
|
6
|
+
export declare class DbStorageCsv extends CustomAdapterBase {
|
|
7
7
|
opts: Opts;
|
|
8
|
+
version: string;
|
|
8
9
|
constructor(opts: Opts);
|
|
9
10
|
_getPath(collection: string): string;
|
|
10
11
|
read(file: string): Promise<unknown[]>;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { CustomFileCpu } from "@wxn0brp/db-core";
|
|
2
|
-
import {
|
|
2
|
+
import { CustomAdapterBase } from "@wxn0brp/db-core/base/customAdapter";
|
|
3
|
+
import { pathRepair } from "@wxn0brp/db-core/base/customFileCpu";
|
|
3
4
|
import { parse, stringify } from "csv/sync";
|
|
4
5
|
import { access, mkdir, readdir, readFile, rm, writeFile } from "fs/promises";
|
|
5
|
-
|
|
6
|
+
import { version } from "./version.js";
|
|
7
|
+
export class DbStorageCsv extends CustomAdapterBase {
|
|
6
8
|
opts;
|
|
9
|
+
version = version;
|
|
7
10
|
constructor(opts) {
|
|
8
11
|
super();
|
|
9
12
|
this.opts = opts;
|
|
@@ -14,7 +17,7 @@ export class DbStorageCsv extends CustomActionsBase {
|
|
|
14
17
|
return this.opts.file;
|
|
15
18
|
}
|
|
16
19
|
else if (this.opts.dir) {
|
|
17
|
-
return this.opts.dir + "/" + collection + ".csv";
|
|
20
|
+
return this.opts.dir + "/" + pathRepair(collection) + ".csv";
|
|
18
21
|
}
|
|
19
22
|
}
|
|
20
23
|
async read(file) {
|
|
@@ -22,27 +25,33 @@ export class DbStorageCsv extends CustomActionsBase {
|
|
|
22
25
|
const data = await readFile(path, "utf-8");
|
|
23
26
|
const records = parse(data, {
|
|
24
27
|
columns: true,
|
|
25
|
-
cast: true,
|
|
26
28
|
});
|
|
27
29
|
for (const record of records) {
|
|
28
30
|
for (const key of Object.keys(record)) {
|
|
29
31
|
const val = record[key];
|
|
30
|
-
if (val === "") {
|
|
32
|
+
if (val === "" || val === undefined || val === null) {
|
|
31
33
|
delete record[key];
|
|
34
|
+
continue;
|
|
32
35
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
catch { }
|
|
36
|
+
try {
|
|
37
|
+
record[key] = JSON.parse(val);
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// keep as string if not valid JSON
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
}
|
|
42
44
|
return records;
|
|
43
45
|
}
|
|
44
46
|
async write(file, data) {
|
|
45
|
-
const
|
|
47
|
+
const transformed = data.map((row) => {
|
|
48
|
+
const newRow = {};
|
|
49
|
+
for (const [k, v] of Object.entries(row)) {
|
|
50
|
+
newRow[k] = v === undefined ? "" : JSON.stringify(v);
|
|
51
|
+
}
|
|
52
|
+
return newRow;
|
|
53
|
+
});
|
|
54
|
+
const string = stringify(transformed, {
|
|
46
55
|
header: true,
|
|
47
56
|
});
|
|
48
57
|
const path = this._getPath(file);
|
|
@@ -105,7 +114,9 @@ export class DbStorageCsv extends CustomActionsBase {
|
|
|
105
114
|
}
|
|
106
115
|
}
|
|
107
116
|
async removeCollection(collection) {
|
|
108
|
-
await rm(this._getPath(collection)
|
|
117
|
+
await rm(this._getPath(collection), {
|
|
118
|
+
force: true,
|
|
119
|
+
});
|
|
109
120
|
return true;
|
|
110
121
|
}
|
|
111
122
|
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const version = "0.130.0";
|
package/dist/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const version = "0.130.0";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-storage-csv",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.130.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"description": "ValtheraDB Storage CSV",
|
|
@@ -18,16 +18,13 @@
|
|
|
18
18
|
"vdb-adapter",
|
|
19
19
|
"csv"
|
|
20
20
|
],
|
|
21
|
-
"peerDependencies": {
|
|
22
|
-
"@wxn0brp/db-core": "^0.12.0"
|
|
23
|
-
},
|
|
24
21
|
"dependencies": {
|
|
25
|
-
"
|
|
22
|
+
"@wxn0brp/db-core": "^0.13.0",
|
|
23
|
+
"csv": "^6.6.4"
|
|
26
24
|
},
|
|
27
25
|
"devDependencies": {
|
|
28
26
|
"@types/bun": "*",
|
|
29
27
|
"@wxn0brp/biome": "*",
|
|
30
|
-
"@wxn0brp/db-core": "^0.12.0",
|
|
31
28
|
"tsc-alias": "^1",
|
|
32
29
|
"typescript": "^7"
|
|
33
30
|
},
|