@wxn0brp/db-storage-csv 0.110.0 → 0.120.1

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/README.md CHANGED
@@ -34,4 +34,4 @@ const singleFileDB = createCsvValthera({
34
34
 
35
35
  ## License
36
36
 
37
- MIT
37
+ MIT
package/dist/action.d.ts CHANGED
@@ -5,6 +5,7 @@ export interface Opts {
5
5
  }
6
6
  export declare class DbStorageCsv extends CustomActionsBase {
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[]>;
package/dist/action.js CHANGED
@@ -2,8 +2,10 @@ import { CustomFileCpu } from "@wxn0brp/db-core";
2
2
  import { CustomActionsBase } from "@wxn0brp/db-core/base/custom";
3
3
  import { parse, stringify } from "csv/sync";
4
4
  import { access, mkdir, readdir, readFile, rm, writeFile } from "fs/promises";
5
+ import { version } from "./version.js";
5
6
  export class DbStorageCsv extends CustomActionsBase {
6
7
  opts;
8
+ version = version;
7
9
  constructor(opts) {
8
10
  super();
9
11
  this.opts = opts;
@@ -20,10 +22,37 @@ export class DbStorageCsv extends CustomActionsBase {
20
22
  async read(file) {
21
23
  const path = this._getPath(file);
22
24
  const data = await readFile(path, "utf-8");
23
- return parse(data, { columns: true });
25
+ const records = parse(data, {
26
+ columns: true,
27
+ });
28
+ for (const record of records) {
29
+ for (const key of Object.keys(record)) {
30
+ const val = record[key];
31
+ if (val === "" || val === undefined || val === null) {
32
+ delete record[key];
33
+ continue;
34
+ }
35
+ try {
36
+ record[key] = JSON.parse(val);
37
+ }
38
+ catch {
39
+ // keep as string if not valid JSON
40
+ }
41
+ }
42
+ }
43
+ return records;
24
44
  }
25
45
  async write(file, data) {
26
- const string = stringify(data, { header: true });
46
+ const transformed = data.map((row) => {
47
+ const newRow = {};
48
+ for (const [k, v] of Object.entries(row)) {
49
+ newRow[k] = v === undefined ? "" : JSON.stringify(v);
50
+ }
51
+ return newRow;
52
+ });
53
+ const string = stringify(transformed, {
54
+ header: true,
55
+ });
27
56
  const path = this._getPath(file);
28
57
  await writeFile(path, string);
29
58
  }
@@ -32,6 +61,7 @@ export class DbStorageCsv extends CustomActionsBase {
32
61
  if (this.opts.file) {
33
62
  try {
34
63
  await access(path);
64
+ return false;
35
65
  }
36
66
  catch (error) {
37
67
  await writeFile(path, "");
@@ -40,9 +70,12 @@ export class DbStorageCsv extends CustomActionsBase {
40
70
  else if (this.opts.dir) {
41
71
  try {
42
72
  await access(path);
73
+ return false;
43
74
  }
44
75
  catch (error) {
45
- await mkdir(path.split("/").slice(0, -1).join("/"), { recursive: true });
76
+ await mkdir(path.split("/").slice(0, -1).join("/"), {
77
+ recursive: true,
78
+ });
46
79
  await writeFile(path, "");
47
80
  }
48
81
  }
@@ -60,10 +93,20 @@ export class DbStorageCsv extends CustomActionsBase {
60
93
  }
61
94
  async getCollections() {
62
95
  if (this.opts.file) {
63
- return [this.opts.file];
96
+ return [
97
+ this.opts.file,
98
+ ];
64
99
  }
65
100
  else if (this.opts.dir) {
66
- return await readdir(this.opts.dir, { recursive: true });
101
+ try {
102
+ const files = await readdir(this.opts.dir, {
103
+ recursive: true,
104
+ });
105
+ return files.map(f => (f.endsWith(".csv") ? f.slice(0, -4) : f));
106
+ }
107
+ catch {
108
+ return [];
109
+ }
67
110
  }
68
111
  else {
69
112
  return [];
package/dist/index.js CHANGED
@@ -3,6 +3,6 @@ import { DbStorageCsv } from "./action.js";
3
3
  export function createCsvValthera(opts) {
4
4
  const csvStorage = new DbStorageCsv(opts);
5
5
  return new ValtheraClass({
6
- dbAction: csvStorage
6
+ adapter: csvStorage,
7
7
  });
8
8
  }
@@ -0,0 +1 @@
1
+ export declare const version = "0.120.1";
@@ -0,0 +1 @@
1
+ export const version = "0.120.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-csv",
3
- "version": "0.110.0",
3
+ "version": "0.120.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "ValtheraDB Storage CSV",
@@ -18,17 +18,15 @@
18
18
  "vdb-adapter",
19
19
  "csv"
20
20
  ],
21
- "peerDependencies": {
22
- "@wxn0brp/db-core": ">=0.11.0"
23
- },
24
21
  "dependencies": {
25
- "csv": "^6.5.1"
22
+ "@wxn0brp/db-core": "^0.12.2",
23
+ "csv": "^6.6.3"
26
24
  },
27
25
  "devDependencies": {
28
26
  "@types/bun": "*",
29
- "@wxn0brp/db-core": "^0.11.0",
27
+ "@wxn0brp/biome": "*",
30
28
  "tsc-alias": "^1",
31
- "typescript": "^6"
29
+ "typescript": "^7"
32
30
  },
33
31
  "files": [
34
32
  "dist"