From cb11d662e6ee14bdc5c42b570baee9bee125037b Mon Sep 17 00:00:00 2001 From: Jane Jeon Date: Wed, 4 Sep 2019 10:50:50 -0400 Subject: [PATCH] I'm not sure how to handle the hashid --- .circleci/config.yml | 34 +++++++++++++++++++++++++++++ LICENSE | 2 +- README.md | 5 +++++ index.js | 47 ++++++++++++++++++++++++++++++++++++++++ index.test.js | 0 package.json | 51 ++++++++++++++++++++++---------------------- yarn.lock | 5 +++++ 7 files changed, 118 insertions(+), 26 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 README.md create mode 100644 index.js create mode 100644 index.test.js diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..f48c1a7 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,34 @@ +version: 2 + +jobs: + build: + docker: + - image: circleci/node + + working_directory: ~/repo + + steps: + - checkout + + # Download and cache dependencies + - restore_cache: + keys: + - v1-dependencies-{{ checksum "yarn.lock" }} + # fallback to using the latest cache if no exact match is found + - v1-dependencies- + + - run: yarn --frozen-lockfile + + - save_cache: + paths: + - node_modules + key: v1-dependencies-{{ checksum "yarn.lock" }} + + - run: yarn lint + + - run: yarn test --coverage --ci --runInBand --reporters=default --reporters=jest-junit --forceExit + + - store_test_results: + path: junit.xml + + - run: yarn coverage diff --git a/LICENSE b/LICENSE index 4c4b811..c78e6a8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Sungil Ahn +Copyright (c) 2019 Jane Jeon Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md new file mode 100644 index 0000000..30666bc --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Objection-HashId + +[![CircleCI](https://img.shields.io/circleci/build/github/JaneJeon/objection-hashid)](https://circleci.com/gh/JaneJeon/objection-hashid) [![Coverage Status](https://coveralls.io/repos/github/JaneJeon/objection-hashid/badge.svg?branch=master)](https://coveralls.io/github/JaneJeon/objection-hashid?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/78bae22810143ad84ef1/maintainability)](https://codeclimate.com/github/JaneJeon/objection-hashid/maintainability) [![Version](https://img.shields.io/npm/v/objection-hashid)](https://www.npmjs.com/package/objection-hashid) [![Downloads](https://img.shields.io/npm/dt/objection-hashid)](https://www.npmjs.com/package/objection-hashid) [![install size](https://packagephobia.now.sh/badge?p=objection-hashid)](https://packagephobia.now.sh/result?p=objection-hashid) [![Dependencies](https://img.shields.io/david/JaneJeon/objection-hashid)](https://david-dm.org/JaneJeon/objection-hashid) [![Known Vulnerabilities](https://snyk.io//test/github/JaneJeon/objection-hashid/badge.svg?targetFile=package.json)](https://snyk.io//test/github/JaneJeon/objection-hashid?targetFile=package.json) [![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=JaneJeon/objection-hashid)](https://dependabot.com) [![License](https://img.shields.io/npm/l/objection-hashid)](https://github.com/JaneJeon/objection-hashid/blob/master/LICENSE) [![Docs](https://img.shields.io/badge/docs-github-blue)](https://janejeon.github.io/objection-hashid) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) + +Use objection-visibility! diff --git a/index.js b/index.js new file mode 100644 index 0000000..ddd6617 --- /dev/null +++ b/index.js @@ -0,0 +1,47 @@ +const HashId = require('hashids/cjs') +const memoize = require('lodash.memoize') + +const memoizeHashId = memoize( + (salt, minLength, alphabet, seps) => + new HashId(salt, minLength, alphabet, seps) +) + +// you can override any of the hashid properties (minus the salt) by passing thru opt +module.exports = (opts = {hashId = 'id'}) => { + return Model => { + // you can also override the hashid properties on a per-model basis using model properties + return class extends Model { + static get hashIdSalt() { + return this.name + } + + static get hashIdMinLength() { + return opts.minLength + } + + static get hashIdAlphabet() { + return opts.alphabet + } + + static get hashIdSeps() { + return opts.seps + } + + // you can even override the hashId instance! + static get hashIdInstance() { + return memoizeHashId( + this.hashIdSalt, + this.hashIdMinLength, + this.hashIdAlphabet, + this.hashIdSeps + ) + } + + static get QueryBuilder() { + return class extends Model.QueryBuilder { + // TODO: + } + } + } + } +} diff --git a/index.test.js b/index.test.js new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index ae5ab9a..de7076c 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,11 @@ { "name": "objection-hashid", "version": "0.1.0", - "description": "Obfuscate your Objection.js model ids using hashids!", + "description": "Objection plugin to automatically obfuscate model ids using hashids!", "main": "index.js", - "repository": "https://github.com/JaneJeon/objection-hashid", + "repository": "JaneJeon/objection-hashid", "author": "Jane Jeon ", "license": "MIT", - "keywords": [ - "objection", - "objection.js", - "knex", - "knexjs", - "knex.js", - "orm", - "plugin", - "id", - "ids", - "hashids", - "hashid", - "hash", - "obfuscation", - "youtube", - "bitly", - "encode", - "decode", - "encrypt", - "decrypt" - ], "scripts": { "test": "jest", "test:watch": "yarn test --watch", @@ -38,7 +17,8 @@ "pre-push": "yarn test" }, "dependencies": { - "hashids": "^2.0.0" + "hashids": "^2.0.0", + "lodash.memoize": "^4.1.2" }, "devDependencies": { "@types/jest": "^24.0.18", @@ -51,5 +31,26 @@ "objection": "^1.6.9", "sqlite3": "^4.1.0", "standard": "^14.1.0" - } + }, + "keywords": [ + "objection", + "objection.js", + "knex", + "knexjs", + "knex.js", + "orm", + "plugin", + "id", + "ids", + "hashids", + "hashid", + "hash", + "obfuscation", + "youtube", + "bitly", + "encode", + "decode", + "encrypt", + "decrypt" + ] } diff --git a/yarn.lock b/yarn.lock index f6276b5..ea572a8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2854,6 +2854,11 @@ locate-path@^5.0.0: dependencies: p-locate "^4.1.0" +lodash.memoize@^4.1.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" + integrity sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4= + lodash.sortby@^4.7.0: version "4.7.0" resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"