@wxn0brp/db-storage-csv 0.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 wxn0brP
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @wxn0brp/db-storage-csv
2
+
3
+ A CSV storage adapter for the `@wxn0brp/db-core` library. This package allows you to use CSV files as a simple database backend.
4
+
5
+ ## Usage
6
+
7
+ ### Basic Setup
8
+
9
+ ```typescript
10
+ import { createCsvValthera } from "@wxn0brp/db-storage-csv";
11
+
12
+ // Initialize the CSV ValtheraDB
13
+ const csvStorageDB = createCsvValthera({
14
+ dir: './data' // Directory where CSV files will be stored
15
+ });
16
+
17
+ // Or, for a single file
18
+ const singleFileDB = createCsvValthera({
19
+ file: './data.csv' // Path to a single CSV file
20
+ });
21
+ // Note: all collections redirect to the same file, supporting single collection
22
+ ```
23
+
24
+ ### Options
25
+
26
+ - `dir`: Path to a directory where CSV files for each collection will be stored.
27
+ - `file`: Path to a single CSV file to be used for storage (mutually exclusive with `dir`).
28
+
29
+ ## How it works
30
+
31
+ - When a collection is accessed, the adapter reads the corresponding CSV file.
32
+ - Data is parsed into JavaScript objects using the `csv` library.
33
+ - Writes stringify the data back into CSV format.
34
+
35
+ ## License
36
+
37
+ MIT
@@ -0,0 +1,16 @@
1
+ import { CustomActionsBase } from "@wxn0brp/db-core/base/custom";
2
+ import { VQuery } from "@wxn0brp/db-core/types/query";
3
+ export interface Opts {
4
+ file?: string;
5
+ dir?: string;
6
+ }
7
+ export declare class DbStorageCsv extends CustomActionsBase {
8
+ opts: Opts;
9
+ constructor(opts: Opts);
10
+ _getPath(config: VQuery): string;
11
+ read(file: string): Promise<unknown[]>;
12
+ write(file: string, data: any): Promise<void>;
13
+ ensureCollection(config: VQuery): Promise<boolean>;
14
+ issetCollection(config: VQuery): Promise<boolean>;
15
+ getCollections(): Promise<string[]>;
16
+ }
package/dist/action.js ADDED
@@ -0,0 +1,73 @@
1
+ import { CustomFileCpu } from "@wxn0brp/db-core";
2
+ import { CustomActionsBase } from "@wxn0brp/db-core/base/custom";
3
+ import { access, mkdir, readdir } from "fs/promises";
4
+ import { parse, stringify } from "csv/sync";
5
+ import { readFile, writeFile } from "fs/promises";
6
+ export class DbStorageCsv extends CustomActionsBase {
7
+ opts;
8
+ constructor(opts) {
9
+ super();
10
+ this.opts = opts;
11
+ this.fileCpu = new CustomFileCpu(this.read.bind(this), this.write.bind(this));
12
+ }
13
+ _getPath(config) {
14
+ if (this.opts.file) {
15
+ return this.opts.file;
16
+ }
17
+ else if (this.opts.dir) {
18
+ return this.opts.dir + "/" + config.collection + ".csv";
19
+ }
20
+ }
21
+ async read(file) {
22
+ const path = this._getPath({ collection: file });
23
+ const data = await readFile(path, "utf-8");
24
+ return parse(data, { columns: true });
25
+ }
26
+ async write(file, data) {
27
+ const string = stringify(data, { header: true });
28
+ const path = this._getPath({ collection: file });
29
+ await writeFile(path, string);
30
+ }
31
+ async ensureCollection(config) {
32
+ const path = this._getPath(config);
33
+ if (this.opts.file) {
34
+ try {
35
+ await access(path);
36
+ }
37
+ catch (error) {
38
+ await writeFile(path, "");
39
+ }
40
+ }
41
+ else if (this.opts.dir) {
42
+ try {
43
+ await access(path);
44
+ }
45
+ catch (error) {
46
+ await mkdir(path.split("/").slice(0, -1).join("/"), { recursive: true });
47
+ await writeFile(path, "");
48
+ }
49
+ }
50
+ return true;
51
+ }
52
+ async issetCollection(config) {
53
+ const path = this._getPath(config);
54
+ try {
55
+ await access(path);
56
+ return true;
57
+ }
58
+ catch (error) {
59
+ return false;
60
+ }
61
+ }
62
+ async getCollections() {
63
+ if (this.opts.file) {
64
+ return [this.opts.file];
65
+ }
66
+ else if (this.opts.dir) {
67
+ return await readdir(this.opts.dir, { recursive: true });
68
+ }
69
+ else {
70
+ return [];
71
+ }
72
+ }
73
+ }
@@ -0,0 +1,3 @@
1
+ import { ValtheraClass } from "@wxn0brp/db-core";
2
+ import { Opts } from "./action.js";
3
+ export declare function createCsvValthera(opts: Opts): ValtheraClass;
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import { ValtheraClass } from "@wxn0brp/db-core";
2
+ import { DbStorageCsv } from "./action.js";
3
+ export function createCsvValthera(opts) {
4
+ const csvStorage = new DbStorageCsv(opts);
5
+ return new ValtheraClass({
6
+ dbAction: csvStorage
7
+ });
8
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@wxn0brp/db-storage-csv",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "description": "ValtheraDB Storage CSV",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/wxn0brp/ValtheraDB-storage-csv.git"
10
+ },
11
+ "homepage": "https://github.com/wxn0brp/ValtheraDB-storage-csv",
12
+ "author": "wxn0brP",
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "scripts": {
16
+ "build": "tsc && tsc-alias",
17
+ "bun": "bun run src/test.ts"
18
+ },
19
+ "peerDependencies": {
20
+ "@wxn0brp/db-core": ">=0.3.0"
21
+ },
22
+ "dependencies": {
23
+ "csv": "^6.4.1"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "*",
27
+ "@wxn0brp/db-core": "^0.3.0",
28
+ "tsc-alias": "*",
29
+ "typescript": "*"
30
+ },
31
+ "files": [
32
+ "dist"
33
+ ],
34
+ "exports": {
35
+ ".": {
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/index.js",
38
+ "default": "./dist/index.js"
39
+ },
40
+ "./*": {
41
+ "types": "./dist/*.d.ts",
42
+ "import": "./dist/*.js",
43
+ "default": "./dist/*.js"
44
+ }
45
+ }
46
+ }