Skip to content

Commit

Permalink
✨ Feature: change inner db to @picgo/store
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Jul 31, 2022
1 parent 158be01 commit 0e90af3
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 68 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@types/image-size": "^0.0.29",
"@types/inquirer": "^0.0.42",
"@types/lodash": "^4.14.175",
"@types/lowdb": "^1.0.4",
"@types/md5": "^2.1.32",
"@types/mime-types": "^2.1.0",
"@types/minimatch": "^3.0.3",
Expand Down Expand Up @@ -93,6 +92,7 @@
},
"dependencies": {
"@picgo/i18n": "^1.0.0",
"@picgo/store": "^2.0.2",
"chalk": "^2.4.1",
"commander": "^8.1.0",
"comment-json": "^2.3.1",
Expand All @@ -106,8 +106,6 @@
"inquirer": "^6.0.0",
"is-wsl": "^2.2.0",
"lodash": "^4.17.21",
"lodash-id": "^0.14.0",
"lowdb": "^1.0.0",
"md5": "^2.2.1",
"mime-types": "2.1.33",
"minimatch": "^3.0.4",
Expand Down
2 changes: 1 addition & 1 deletion src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class PicGo extends EventEmitter implements IPicGo {

private initConfig (): void {
this.db = new DB(this)
this._config = this.db.read().value()
this._config = this.db.read(true) as IConfig
}

private init (): void {
Expand Down
45 changes: 18 additions & 27 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,57 @@
import lowdb from 'lowdb'
import lodashId from 'lodash-id'
import FileSync from 'lowdb/adapters/FileSync'
import json from 'comment-json'
import { IConfig, IPicGo } from '../types'
import { JSONStore } from '@picgo/store'
import { IJSON } from '@picgo/store/dist/types'

class DB {
private readonly ctx: IPicGo
private readonly db: lowdb.LowdbSync<any>
private readonly db: JSONStore
constructor (ctx: IPicGo) {
this.ctx = ctx
const adapter = new FileSync(this.ctx.configPath, {
serialize (obj: object): string {
return json.stringify(obj, null, 2)
},
deserialize: json.parse
})
this.db = lowdb(adapter)
this.db._.mixin(lodashId)
this.db = new JSONStore(this.ctx.configPath)

if (!this.db.has('picBed').value()) {
if (!this.db.has('picBed')) {
try {
this.db.set('picBed', {
uploader: 'smms',
current: 'smms'
}).write()
})
} catch (e: any) {
this.ctx.log.error(e)
throw e
}
}
if (!this.db.has('picgoPlugins').value()) {
if (!this.db.has('picgoPlugins')) {
try {
this.db.set('picgoPlugins', {}).write()
this.db.set('picgoPlugins', {})
} catch (e: any) {
this.ctx.log.error(e)
throw e
}
}
}

read (): any {
return this.db.read()
read (flush?: boolean): IJSON {
return this.db.read(flush)
}

get (key: string = ''): any {
return this.read().get(key).value()
this.read(true)
return this.db.get(key)
}

set (key: string, value: any): void {
return this.read().set(key, value).write()
this.read(true)
return this.db.set(key, value)
}

has (key: string): boolean {
return this.read().has(key).value()
}

insert (key: string, value: any): void {
return this.read().get(key).insert(value).write()
this.read(true)
return this.db.has(key)
}

unset (key: string, value: any): boolean {
return this.read().get(key).unset(value).write()
this.read(true)
return this.db.unset(key, value)
}

saveConfig (config: Partial<IConfig>): void {
Expand Down
132 changes: 95 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@
dependencies:
chalk "^4.0.0"

"@commonify/lowdb@^3.0.0":
version "3.0.0"
resolved "https://registry.npmmirror.com/@commonify/lowdb/-/lowdb-3.0.0.tgz#09c33ffd19057eab67440a1dc22847569407cafe"
integrity sha512-GwZq68zStvMENxEzN6EE8pacgY2Rlrs5L00BpvB6NvpDW96JUxIa8PJXd9T7qIdx07ro0ITBtw6HfSJw/qboGw==
dependencies:
"@commonify/steno" "2.1.0"

"@commonify/[email protected]", "@commonify/steno@^2.1.0":
version "2.1.0"
resolved "https://registry.npmmirror.com/@commonify/steno/-/steno-2.1.0.tgz#7d1e68a0e239bf4fd35abfd167006a3e56140b28"
integrity sha512-3W0LmYb84EU82Ky18M+D0tB33W66ccoC/Ot/8mm3uBQFuaTpiWaoxntQGBTL3+bIpV4e77ks53IE3sy9BRFBxA==

"@endemolshinegroup/cosmiconfig-typescript-loader@^3.0.2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@endemolshinegroup/cosmiconfig-typescript-loader/-/cosmiconfig-typescript-loader-3.0.2.tgz#eea4635828dde372838b0909693ebd9aafeec22d"
Expand Down Expand Up @@ -362,6 +374,22 @@
chalk "4.1.2"
tslib "^2.3.1"

"@picgo/store@^2.0.2":
version "2.0.2"
resolved "https://registry.npmmirror.com/@picgo/store/-/store-2.0.2.tgz#0b5050f5e8cef7043cf5463fa81ef3c3a19fffc7"
integrity sha512-/nZr6zeLNtlTG+g8iUd5xy5Vtl7iu7SHI3aY9a/+AIlBSs7Io/06MrxGyoAHSWVg9BsB80kJyrNeGyOWiOO5jw==
dependencies:
"@commonify/lowdb" "^3.0.0"
"@commonify/steno" "^2.1.0"
"@types/bson" "^4.0.1"
"@types/graceful-fs" "^4.1.3"
"@types/lodash" "^4.14.182"
comment-json "^4.1.0"
fflate "^0.7.3"
lodash "^4.17.21"
lodash-id "^0.14.0"
write-file-atomic "^4.0.1"

"@rollup/plugin-commonjs@^21.0.0":
version "21.0.0"
resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-21.0.0.tgz#b9e4342855ea20b5528f4587b9a90f642196a502"
Expand Down Expand Up @@ -429,6 +457,13 @@
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==

"@types/bson@^4.0.1":
version "4.2.0"
resolved "https://registry.npmmirror.com/@types/bson/-/bson-4.2.0.tgz#a2f71e933ff54b2c3bf267b67fa221e295a33337"
integrity sha512-ELCPqAdroMdcuxqwMgUpifQyRoTpyYCNr1V9xKyF40VsBobsj+BbWNRvwGchMgBPGqkw655ypkjj2MEF5ywVwg==
dependencies:
bson "*"

"@types/caseless@*":
version "0.12.2"
resolved "https://registry.yarnpkg.com/@types/caseless/-/caseless-0.12.2.tgz#f65d3d6389e01eeb458bd54dc8f52b95a9463bc8"
Expand Down Expand Up @@ -476,6 +511,13 @@
"@types/minimatch" "*"
"@types/node" "*"

"@types/graceful-fs@^4.1.3":
version "4.1.5"
resolved "https://registry.npmmirror.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
integrity sha512-anKkLmZZ+xm4p8JWBf4hElkM4XR+EZeA2M9BAkkTldmcyDY4mbdIJnRghDJH3Ov5ooY7/UAoENtmdMSkaAd7Cw==
dependencies:
"@types/node" "*"

"@types/image-size@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/image-size/-/image-size-0.0.29.tgz#0924d4ec95edc82f615b7f634cba31534b81c351"
Expand All @@ -501,17 +543,15 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@*", "@types/lodash@^4.14.175":
"@types/lodash@^4.14.175":
version "4.14.175"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.175.tgz#b78dfa959192b01fae0ad90e166478769b215f45"
integrity sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==

"@types/lowdb@^1.0.4":
version "1.0.11"
resolved "https://registry.yarnpkg.com/@types/lowdb/-/lowdb-1.0.11.tgz#d8336a635ea0dbd48a7f6f62fb9fccc5ec358ae3"
integrity sha512-h99VMxvTuz+VsXUVCCJo4dsps4vbkXwvU71TpmxDoiBU24bJ0VBygIHgmMm+UPoQIFihmV6euRik4z8J7XDJWg==
dependencies:
"@types/lodash" "*"
"@types/lodash@^4.14.182":
version "4.14.182"
resolved "https://registry.npmmirror.com/@types/lodash/-/lodash-4.14.182.tgz#05301a4d5e62963227eaafe0ce04dd77c54ea5c2"
integrity sha512-/THyiqyQAP9AfARo4pF+aCGcyiQ94tX/Is2I7HofNRqoYLgN1PBoOWu2/zTA5zMxzP5EFutMtWtGAFRKUe961Q==

"@types/md5@^2.1.32":
version "2.3.1"
Expand Down Expand Up @@ -915,6 +955,11 @@ array-includes@^3.1.3:
get-intrinsic "^1.1.1"
is-string "^1.0.7"

array-timsort@^1.0.3:
version "1.0.3"
resolved "https://registry.npmmirror.com/array-timsort/-/array-timsort-1.0.3.tgz#3c9e4199e54fb2b9c3fe5976396a21614ef0d926"
integrity sha512-/+3GRL7dDAGEfM6TseQk/U+mi18TU2Ms9I3UlLdUMhz2hbvGNTKdj9xniwXfUqgYhHxRx0+8UnKkvlNwVU+cWQ==

array-union@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/array-union/-/array-union-2.1.0.tgz#b798420adbeb1de828d84acd8a2e23d3efe85e8d"
Expand Down Expand Up @@ -1054,6 +1099,13 @@ braces@^3.0.1:
dependencies:
fill-range "^7.0.1"

bson@*:
version "4.6.5"
resolved "https://registry.npmmirror.com/bson/-/bson-4.6.5.tgz#1a410148c20eef4e40d484878a037a7036e840fb"
integrity sha512-uqrgcjyOaZsHfz7ea8zLRCLe1u+QGUSzMZmvXqO24CDW7DWoW1qiN9folSwa7hSneTSgM2ykDIzF5kcQQ8cwNw==
dependencies:
buffer "^5.6.0"

buffer-alloc-unsafe@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/buffer-alloc-unsafe/-/buffer-alloc-unsafe-1.1.0.tgz#bd7dc26ae2972d0eda253be061dba992349c19f0"
Expand Down Expand Up @@ -1082,7 +1134,7 @@ buffer-from@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.2.tgz#2b146a6fd72e80b4f55d255f35ed59a3a9a41bd5"
integrity sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==

buffer@^5.2.1:
buffer@^5.2.1, buffer@^5.6.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.7.1.tgz#ba62e7c13133053582197160851a8f648e99eed0"
integrity sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==
Expand Down Expand Up @@ -1330,6 +1382,17 @@ comment-json@^2.3.1:
has-own-prop "^2.0.0"
repeat-string "^1.6.1"

comment-json@^4.1.0:
version "4.2.2"
resolved "https://registry.npmmirror.com/comment-json/-/comment-json-4.2.2.tgz#5fae70a94e0c8f84a077bd31df5aa5269252f293"
integrity sha512-H8T+kl3nZesZu41zO2oNXIJWojNeK3mHxCLrsBNu6feksBXsgb+PtYz5daP5P86A0F3sz3840KVYehr04enISQ==
dependencies:
array-timsort "^1.0.3"
core-util-is "^1.0.3"
esprima "^4.0.1"
has-own-prop "^2.0.0"
repeat-string "^1.6.1"

commitizen@^4.0.3, commitizen@^4.2.3:
version "4.2.4"
resolved "https://registry.yarnpkg.com/commitizen/-/commitizen-4.2.4.tgz#a3e5b36bd7575f6bf6e7aa19dbbf06b0d8f37165"
Expand Down Expand Up @@ -1604,7 +1667,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=

core-util-is@^1.0.2, core-util-is@~1.0.0:
core-util-is@^1.0.2, core-util-is@^1.0.3, core-util-is@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85"
integrity sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==
Expand Down Expand Up @@ -2441,6 +2504,11 @@ fd-slicer@~1.1.0:
dependencies:
pend "~1.2.0"

fflate@^0.7.3:
version "0.7.3"
resolved "https://registry.npmmirror.com/fflate/-/fflate-0.7.3.tgz#288b034ff0e9c380eaa2feff48c787b8371b7fa5"
integrity sha512-0Zz1jOzJWERhyhsimS54VTqOteCNwRtIlh8isdL0AXLo0g7xNTfTL7oWrkmCnPhZGocKIkWHBistBrrpoNH3aw==

figures@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962"
Expand Down Expand Up @@ -3348,11 +3416,6 @@ is-plain-obj@^1.0.0, is-plain-obj@^1.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-1.1.0.tgz#71a50c8429dfca773c92a390a4a03b39fcd51d3e"
integrity sha1-caUMhCnfync8kqOQpKA7OfzVHT4=

is-promise@^2.1.0:
version "2.2.2"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.2.2.tgz#39ab959ccbf9a774cf079f7b40c7a26f763135f1"
integrity sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==

is-reference@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.2.1.tgz#8b2dac0b371f4bc994fdeaba9eb542d03002d0b7"
Expand Down Expand Up @@ -3688,16 +3751,16 @@ lodash.truncate@^4.4.2:
resolved "https://registry.yarnpkg.com/lodash.truncate/-/lodash.truncate-4.4.2.tgz#5a350da0b1113b837ecfffd5812cbe58d6eae193"
integrity sha1-WjUNoLERO4N+z//VgSy+WNbq4ZM=

lodash@4, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.2.1:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

[email protected]:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==

lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.2.1:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

log-symbols@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a"
Expand All @@ -3718,17 +3781,6 @@ loud-rejection@^1.0.0:
currently-unhandled "^0.4.1"
signal-exit "^3.0.0"

lowdb@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/lowdb/-/lowdb-1.0.0.tgz#5243be6b22786ccce30e50c9a33eac36b20c8064"
integrity sha512-2+x8esE/Wb9SQ1F9IHaYWfsC9FIecLOPrK4g17FGEayjUWH172H6nwicRovGvSE2CPZouc2MCIqCI7h9d+GftQ==
dependencies:
graceful-fs "^4.1.3"
is-promise "^2.1.0"
lodash "4"
pify "^3.0.0"
steno "^0.4.1"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-1.0.0.tgz#4e3366b39e7f5457e35f1324bdf6f88d0bfc7306"
Expand Down Expand Up @@ -5033,6 +5085,11 @@ signal-exit@^3.0.0, signal-exit@^3.0.2, signal-exit@^3.0.3:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.5.tgz#9e3e8cc0c75a99472b44321033a7702e7738252f"
integrity sha512-KWcOiKeQj6ZyXx7zq4YxSMgHRlod4czeBQZrPb8OKcohcqAXShm7E20kEMle9WBt26hFcAf0qLOcp5zmY7kOqQ==

signal-exit@^3.0.7:
version "3.0.7"
resolved "https://registry.npmmirror.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==

slash@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44"
Expand Down Expand Up @@ -5208,13 +5265,6 @@ stealthy-require@^1.1.1:
resolved "https://registry.yarnpkg.com/stealthy-require/-/stealthy-require-1.1.1.tgz#35b09875b4ff49f26a777e509b3090a3226bf24b"
integrity sha1-NbCYdbT/SfJqd35QmzCQoyJr8ks=

steno@^0.4.1:
version "0.4.4"
resolved "https://registry.yarnpkg.com/steno/-/steno-0.4.4.tgz#071105bdfc286e6615c0403c27e9d7b5dcb855cb"
integrity sha1-BxEFvfwobmYVwEA8J+nXtdy4Vcs=
dependencies:
graceful-fs "^4.1.3"

strict-uri-encode@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz#279b225df1d582b1f54e65addd4352e18faa0713"
Expand Down Expand Up @@ -5830,6 +5880,14 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

write-file-atomic@^4.0.1:
version "4.0.1"
resolved "https://registry.npmmirror.com/write-file-atomic/-/write-file-atomic-4.0.1.tgz#9faa33a964c1c85ff6f849b80b42a88c2c537c8f"
integrity sha512-nSKUxgAbyioruk6hU87QzVbY279oYT6uiwgDoujth2ju4mJ+TZau7SQBhtbTmUyuNYTuXnSyRn66FV0+eCgcrQ==
dependencies:
imurmurhash "^0.1.4"
signal-exit "^3.0.7"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/xregexp/-/xregexp-2.0.0.tgz#52a63e56ca0b84a7f3a5f3d61872f126ad7a5943"
Expand Down

0 comments on commit 0e90af3

Please sign in to comment.