diff --git a/LICENSE b/LICENSE index ddb280e..529d3db 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Björn Hase, me@herr-hase.wtf +Copyright (c) 2024 Björn Hase, herrhase@node001.net Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/README.md b/README.md index b678ea8..270b9ca 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Nano Sqlite -Functions and Classes for handle Sqlite in [Bun](https://bun.sh/). +Functions and Classes for handle Sqlite with Better-Sqlite3 ## API ### getOrCreateSqlite(options = {}) -As options the default Parameters from [Bun](https://bun.sh/docs/api/sqlite) are available. The uri of the Sqlite can be set in the .env-file, +The uri of the Sqlite can be set in the .env-file, ``` NANO_SQLITE_PATH="./../storage/db.sqlite" @@ -41,18 +41,18 @@ class Store extends Store { } ``` -#### findOneById(id: integer) +#### findOneById(id) Getting single row by id. -#### create(data: object) +#### create(data) Create a new row in a Table. -#### update(id: integer, data: object) +#### update(id, data) Update single row by id. -#### remove(id: integer) +#### remove(id) Remove single row by id. diff --git a/bunfig.toml b/bunfig.toml deleted file mode 100644 index f6b3dcd..0000000 --- a/bunfig.toml +++ /dev/null @@ -1,2 +0,0 @@ -[install.scopes] -"@nano" = "https://git.node001.net/api/packages/nano/npm/" diff --git a/index.js b/index.js new file mode 100644 index 0000000..186ba27 --- /dev/null +++ b/index.js @@ -0,0 +1,9 @@ +import Store from './src/store.js' +import runMigrationSqlite from './src/migration.js' +import getOrCreateSqlite from './src/sqlite.js' + +export { + Store, + runMigrationSqlite, + getOrCreateSqlite +} diff --git a/index.ts b/index.ts deleted file mode 100644 index 7f83458..0000000 --- a/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import Store from './src/store.ts' -import runMigrationSqlite from './src/migration.ts' -import getOrCreateSqlite from './src/sqlite.ts' - -export { - Store, - runMigrationSqlite, - getOrCreateSqlite -} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e4b3d72 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,468 @@ +{ + "name": "@nano/sqlite", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "@nano/sqlite", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "better-sqlite3": "12.6.*" + } + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/better-sqlite3": { + "version": "12.6.2", + "resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-12.6.2.tgz", + "integrity": "sha512-8VYKM3MjCa9WcaSAI3hzwhmyHVlH8tiGFwf0RlTsZPWJ1I5MkzjiudCo4KC4DxOaL/53A5B1sI/IbldNFDbsKA==", + "hasInstallScript": true, + "license": "MIT", + "dependencies": { + "bindings": "^1.5.0", + "prebuild-install": "^7.1.1" + }, + "engines": { + "node": "20.x || 22.x || 23.x || 24.x || 25.x" + } + }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "license": "MIT", + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "license": "MIT", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, + "node_modules/chownr": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz", + "integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==", + "license": "ISC" + }, + "node_modules/decompress-response": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz", + "integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==", + "license": "MIT", + "dependencies": { + "mimic-response": "^3.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/deep-extend": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz", + "integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==", + "license": "MIT", + "engines": { + "node": ">=4.0.0" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/end-of-stream": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz", + "integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==", + "license": "MIT", + "dependencies": { + "once": "^1.4.0" + } + }, + "node_modules/expand-template": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz", + "integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==", + "license": "(MIT OR WTFPL)", + "engines": { + "node": ">=6" + } + }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "license": "MIT" + }, + "node_modules/fs-constants": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz", + "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", + "license": "MIT" + }, + "node_modules/github-from-package": { + "version": "0.0.0", + "resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz", + "integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==", + "license": "MIT" + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "BSD-3-Clause" + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "license": "ISC" + }, + "node_modules/ini": { + "version": "1.3.8", + "resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz", + "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==", + "license": "ISC" + }, + "node_modules/mimic-response": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz", + "integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==", + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/mkdirp-classic": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz", + "integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==", + "license": "MIT" + }, + "node_modules/napi-build-utils": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz", + "integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==", + "license": "MIT" + }, + "node_modules/node-abi": { + "version": "3.94.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.94.0.tgz", + "integrity": "sha512-W5ZNO5KRPB5TkYmGVD9F6YqhsglXJzE6etpbmT+f6EQElhiX/UTG551cnsRGvLG3fyZEg9HwaDmNmj5nwJ4z9g==", + "license": "MIT", + "dependencies": { + "semver": "^7.3.5" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", + "license": "ISC", + "dependencies": { + "wrappy": "1" + } + }, + "node_modules/prebuild-install": { + "version": "7.1.3", + "resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz", + "integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==", + "deprecated": "No longer maintained. Please contact the author of the relevant native addon; alternatives are available.", + "license": "MIT", + "dependencies": { + "detect-libc": "^2.0.0", + "expand-template": "^2.0.3", + "github-from-package": "0.0.0", + "minimist": "^1.2.3", + "mkdirp-classic": "^0.5.3", + "napi-build-utils": "^2.0.0", + "node-abi": "^3.3.0", + "pump": "^3.0.0", + "rc": "^1.2.7", + "simple-get": "^4.0.0", + "tar-fs": "^2.0.0", + "tunnel-agent": "^0.6.0" + }, + "bin": { + "prebuild-install": "bin.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/pump": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.4.tgz", + "integrity": "sha512-VS7sjc6KR7e1ukRFhQSY5LM2uBWAUPiOPa/A3mkKmiMwSmRFUITt0xuj+/lesgnCv+dPIEYlkzrcyXgquIHMcA==", + "license": "MIT", + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, + "node_modules/rc": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz", + "integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==", + "license": "(BSD-2-Clause OR MIT OR Apache-2.0)", + "dependencies": { + "deep-extend": "^0.6.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" + }, + "bin": { + "rc": "cli.js" + } + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "license": "MIT", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/semver": { + "version": "7.8.5", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.5.tgz", + "integrity": "sha512-Y7/KDsb8LjooZpwaqGyulO6DQlksgCncchHGk+sZIY4SBvUocMBEFH5Ur1fI4dV+Jvl0w6cjvucaIi40puRioA==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/simple-concat": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz", + "integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/simple-get": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz", + "integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "decompress-response": "^6.0.0", + "once": "^1.3.1", + "simple-concat": "^1.0.0" + } + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/strip-json-comments": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", + "integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==", + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/tar-fs": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.5.tgz", + "integrity": "sha512-OboTd8mmMhZDNPV+UjQcK9yKAatXu2aJ+r1w4im1Otd4M4fl2hwvdoXUxIYHFTHWK/3y3FarBP70v3vwmGlOxw==", + "license": "MIT", + "dependencies": { + "chownr": "^1.1.1", + "mkdirp-classic": "^0.5.2", + "pump": "^3.0.0", + "tar-stream": "^2.1.4" + } + }, + "node_modules/tar-stream": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz", + "integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==", + "license": "MIT", + "dependencies": { + "bl": "^4.0.3", + "end-of-stream": "^1.4.1", + "fs-constants": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.1.1" + }, + "engines": { + "node": ">=6" + } + }, + "node_modules/tunnel-agent": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz", + "integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==", + "license": "Apache-2.0", + "dependencies": { + "safe-buffer": "^5.0.1" + }, + "engines": { + "node": "*" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "license": "MIT" + }, + "node_modules/wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", + "license": "ISC" + } + } +} diff --git a/package.json b/package.json index 7673f07..610ba40 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,18 @@ { "name": "@nano/sqlite", - "version": "0.2.1", - "description": "Functions and Classes for handle sqlite in Bun", + "version": "1.0.0", + "description": "Functions and Classes for handle Better-Sqlite3 in NodeJS", "repository": { "type": "git", "url": "git@git.node001.net:nano/sqlite.git" }, - "author": "Björn Hase ", + "author": "Björn Hase ", "license": "MIT", "type": "module", "scripts": { - "test": "bun test ./test/*" + "test": "node --test ./test/*" + }, + "dependencies": { + "better-sqlite3": "12.6.*" } } diff --git a/resources/itemStore.ts b/resources/itemStore.js similarity index 90% rename from resources/itemStore.ts rename to resources/itemStore.js index 7e6fcdd..3714208 100644 --- a/resources/itemStore.ts +++ b/resources/itemStore.js @@ -1,4 +1,4 @@ -import Store from './../src/Store.ts' +import Store from './../src/store.js' /** * store - abstract class diff --git a/src/migration.ts b/src/migration.js similarity index 73% rename from src/migration.ts rename to src/migration.js index 961e141..20af14c 100644 --- a/src/migration.ts +++ b/src/migration.js @@ -1,4 +1,4 @@ -import { readdir } from 'node:fs/promises' +import { readdir, readFile } from 'node:fs/promises' import path from 'path' /** @@ -8,7 +8,7 @@ import path from 'path' * @param {object} db * */ -async function runMigrationSqlite(filePath: string, db: object): void { +async function runMigrationSqlite(filePath, db) { const SQL_EXTENSION = '.sql' @@ -22,16 +22,14 @@ async function runMigrationSqlite(filePath: string, db: object): void { continue } - const file = Bun.file(filePath + '/' + fileName) + const sql = await readFile(filePath + '/' + fileName, 'utf8') try { - const sql = await file.text() - if (!sql.trim()) { throw new Error(fileName + ' is empty!') } - db.query(sql.trim()).run() + db.prepare(sql.trim()).run() console.log('migrated ' + path.basename(fileName, SQL_EXTENSION)) } catch(error) { diff --git a/src/sqlite.ts b/src/sqlite.js similarity index 59% rename from src/sqlite.ts rename to src/sqlite.js index 54a827f..5818431 100644 --- a/src/sqlite.ts +++ b/src/sqlite.js @@ -1,15 +1,15 @@ /** -* db -* -* @author Björn Hase -* @license http://opensource.org/licenses/MIT The MIT License -* @link https://gitea.node001.net/HerrHase/urban-bucket.git + * db + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://gitea.node001.net/HerrHase/urban-bucket.git * */ -import { Database } from 'bun:sqlite' +import Database from 'better-sqlite3' -function getOrCreateSqlite(options = {}): Database { +function getOrCreateSqlite(options = {}) { let db @@ -32,7 +32,7 @@ function getOrCreateSqlite(options = {}): Database { } if (wal) { - db.exec('PRAGMA journal_mode = WAL;') + db.pragma('journal_mode = WAL') } return db diff --git a/src/store.js b/src/store.js new file mode 100644 index 0000000..36312c0 --- /dev/null +++ b/src/store.js @@ -0,0 +1,116 @@ +/** + * store - abstract class + * + * @author Björn Hase + * @license http://opensource.org/licenses/MIT The MIT License + * @link https://gitea.node001.net/HerrHase/urban-filehub.git + * + */ + +class Store { + + /** + * + * + * @param {tableName} + * + */ + constructor(db, tableName) { + this._db = db + this._tableName = tableName + } + + /** + * create row + * + */ + findOneById(id) { + return this._db + .prepare('SELECT * FROM ' + this._tableName + ' WHERE id = ?') + .get(id) + } + + /** + * create row + * + */ + remove(id) { + return this._db + .prepare('DELETE FROM ' + this._tableName + ' WHERE id = ?') + .run(id) + } + + /** + * + * + */ + update(id, data) { + return this._db + .prepare('UPDATE ' + this._tableName + ' SET ' + this._prepareUpdateBinding(data) + ' WHERE id = ?') + .run(data, id) + } + + /** + * + * + */ + create(data) { + const result = this._db + .prepare('INSERT INTO ' + this._tableName + ' (' + Object.keys(data).join(', ') + ') VALUES (' + this._prepareInsertBinding(data) + ')') + .run(data) + + if (!result.lastInsertRowid) { + throw new Error('Store / missing id after created data') + } + + return result.lastInsertRowid + } + + /** + * run through object and add key to string for + * binding parameters for insert + * + * @param {object} data + * @return {string} + */ + _prepareInsertBinding(data) { + let result = '' + let length = Object.keys(data).length + let index = 0 + + for (const key in data) { + result += '@' + key + + if (index++ < (length - 1)) { + result += ', ' + } + } + + return result + } + + /** + * run through object and add key to string for + * binding parameters for update + * + * @param {object} data + * @return {string} + */ + _prepareUpdateBinding(data) { + let result = '' + let length = Object.keys(data).length + let index = 0 + + for (const key in data) { + result += key + ' = @' + key + + if (index++ < (length - 1)) { + result += ', ' + } + } + + return result + } +} + +export default Store diff --git a/src/store.ts b/src/store.ts deleted file mode 100644 index 1aad02f..0000000 --- a/src/store.ts +++ /dev/null @@ -1,122 +0,0 @@ -/** - * store - abstract class - * - * @author Björn Hase - * @license http://opensource.org/licenses/MIT The MIT License - * @link https://gitea.node001.net/HerrHase/urban-filehub.git - * - */ - -abstract class Store { - - protected db: string - protected tableName: string - - /** - * - * - * @param {tableName} - * - */ - public constructor(db: object, tableName?: string) { - this.db = db - this.tableName = tableName - } - - /** - * create row - * - */ - public findOneById(id: integer): object { - return this.db - .query('SELECT * FROM ' + this.tableName + ' WHERE id = $id') - .get({ - '$id': id - }) - } - - /** - * create row - * - */ - public create(data: object): integer { - this.db - .query('INSERT INTO ' + this.tableName + ' (' + Object.keys(data).join(', ') + ') VALUES (' + this.prepareInsertBinding(data) + ')') - .run(this.prepareData(data)) - - return this.db.query('SELECT last_insert_rowid()').get()['last_insert_rowid()'] - } - - /** - * update row - * - */ - public update(id: integer, data: object): void { - this.db - .query('UPDATE ' + this.tableName + ' SET ' + this.prepareUpdateBinding(data) + ' WHERE id = $id') - .run(this.prepareData(Object.assign(data, { - 'id': id - }))) - } - - /** - * create row - * - */ - public remove(id: integer): void { - this.db - .query('DELETE FROM ' + this.tableName + ' WHERE id = $id') - .run({ - $id: id - }) - } - - /** - * - */ - protected prepareData(data: object) { - const results = {} - - for (const key in data) { - results['$' + key] = data[key] - } - - return results - } - - /** - * run through object and add key to string for - * binding parameters for insert - * - * @param {object} data - * @return {string} - */ - protected prepareInsertBinding(data: array) { - let result = [] - - for (const key in data) { - result.push('$' + key) - } - - return result.join(', ') - } - - /** - * run through object and add key to string for - * binding parameters for update - * - * @param {object} data - * @return {string} - */ - protected prepareUpdateBinding(data: array) { - let result = [] - - for (const key in data) { - result.push(key + ' = $' + key) - } - - return result.join(', ') - } -} - -export default Store diff --git a/test/migration.test.js b/test/migration.test.js new file mode 100644 index 0000000..338bb9f --- /dev/null +++ b/test/migration.test.js @@ -0,0 +1,16 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' + +import path from 'path' +import runMigrationSqlite from './../src/migration.js' +import getOrCreateSqlite from './../src/sqlite.js' + +test('migration', async () => { + const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true }) + await runMigrationSqlite(path.join(path.resolve(), './resources'), db) + + // check for boxes + const results = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='items'").get() + + assert.equal(results.name, 'items') +}) diff --git a/test/migration.test.ts b/test/migration.test.ts deleted file mode 100644 index 27b6f05..0000000 --- a/test/migration.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { expect, test } from 'bun:test' -import path from 'path' -import runMigrationSqlite from './../src/migration.ts' -import getOrCreateSqlite from './../src/sqlite.ts' - -import { Database } from 'bun:sqlite' - -test('migration', async () => { - const db = getOrCreateSqlite({ 'uri': ':memory:', 'create': true, 'readwrite': true }) - await runMigrationSqlite(path.join(__dirname, './../resources'), db) - - // check for boxes - const results = db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='items'").get() - - expect(results).toEqual({ name: 'items' }) -}) diff --git a/test/sqlite.test.js b/test/sqlite.test.js new file mode 100644 index 0000000..2ecc446 --- /dev/null +++ b/test/sqlite.test.js @@ -0,0 +1,9 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' + +import getOrCreateSqlite from './../src/sqlite.js' + +test('sqlite', () => { + const db = getOrCreateSqlite({ 'uri': ':memory:' }) + assert.ok(typeof db === 'object'); +}) diff --git a/test/sqlite.test.ts b/test/sqlite.test.ts deleted file mode 100644 index a46206f..0000000 --- a/test/sqlite.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { expect, test } from 'bun:test' -import getOrCreateSqlite from './../src/sqlite.ts' - -test('sqlite', () => { - const db = getOrCreateSqlite({ 'uri': ':memory:' }) - expect(typeof db === 'object').toBeTruthy(); -}) diff --git a/test/store.test.ts b/test/store.test.js similarity index 51% rename from test/store.test.ts rename to test/store.test.js index e071919..27304f4 100644 --- a/test/store.test.ts +++ b/test/store.test.js @@ -1,48 +1,49 @@ -import { expect, test } from 'bun:test' -import path from 'path' +import { test } from 'node:test' +import assert from 'node:assert/strict' -import ItemStore from './../resources/itemStore.ts' -import runMigrationSqlite from './../src/migration.ts' -import getOrCreateSqlite from './../src/sqlite.ts' +import path from 'path' +import ItemStore from './../resources/itemStore.js' +import runMigrationSqlite from './../src/migration.js' +import getOrCreateSqlite from './../src/sqlite.js' test('store', () => { const db = getOrCreateSqlite({ 'uri': ':memory:' }) const itemStore = new ItemStore(db) - expect(typeof itemStore === 'object').toBeTruthy(); + assert.ok(typeof itemStore === 'object') }) test('store / insert', async () => { const db = getOrCreateSqlite({ 'uri': ':memory:' }) - await runMigrationSqlite(path.join(__dirname, './../resources'), db) + await runMigrationSqlite(path.join(path.resolve(), './resources'), db) const itemStore = new ItemStore(db) const id = itemStore.create({ 'name': 'Lorem ipsum dolor', 'description': 'lore' }) const result = itemStore.findOneById(id) - expect(result).toEqual({ 'id': 1, 'name': 'Lorem ipsum dolor', 'description': 'lore', 'date_created_at': null, 'date_upated_at': null }) + assert.deepEqual(result, { 'id': 1, 'name': 'Lorem ipsum dolor', 'description': 'lore', 'date_created_at': null, 'date_upated_at': null }) }) test('store / remove', async () => { const db = getOrCreateSqlite({ 'uri': ':memory:' }) - await runMigrationSqlite(path.join(__dirname, './../resources'), db) + await runMigrationSqlite(path.join(path.resolve(), './resources'), db) const itemStore = new ItemStore(db) itemStore.create({ 'name': 'Lorem ipsum dolor', 'description': 'lore' }) - const result = db.query("SELECT * FROM items").get() + const result = db.prepare("SELECT * FROM items").get() itemStore.remove(result.id) - const emptyResult = db.query("SELECT * FROM items").get() + const emptyResult = db.prepare("SELECT * FROM items").get() - expect(emptyResult).toEqual(null) + assert.equal(emptyResult, undefined) }) test('store / update', async () => { const db = getOrCreateSqlite({ 'uri': ':memory:' }) - await runMigrationSqlite(path.join(__dirname, './../resources'), db) + await runMigrationSqlite(path.join(path.resolve(), './resources'), db) const itemStore = new ItemStore(db) const id = itemStore.create({ 'name': 'Lorem ipsum dolor', 'description': 'lore' }) @@ -50,5 +51,5 @@ test('store / update', async () => { const result = itemStore.findOneById(id) - expect(result).toEqual({ 'id': 1, 'name': 'lore', 'description': 'Lorem ipsum dolor', 'date_created_at': null, 'date_upated_at': null }) + assert.deepEqual(result, { 'id': 1, 'name': 'lore', 'description': 'Lorem ipsum dolor', 'date_created_at': null, 'date_upated_at': null }) })