You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
I tried to convert the plugin to typescript. Leaving it here for you to use if of value. Its missing the hashing overrides for now as I did not need them in my project as yet.
importHashIdfrom'hashids/cjs'importmemoizefrom'nano-memoize'import{Model}from"objection"constmemoizeHashId=memoize((salt:string,minLength: number,alphabet?:string,seps?:string)=>newHashId(salt,minLength,alphabet,seps))interfaceIConstructorextendsFunction{
_hashIdInstance: {
encode: (id: number)=>string;
decode: (hashId: string)=>number;};
hashIdField: string;
hashedFields: string[];}classHashedQueryBuilder<MextendsModel,R=M[]>extendsModel.QueryBuilder<M,R>{findByHashId(hashId: string){constid=(this.modelClass()asunknownasIConstructor)._hashIdInstance.decode(hashId)constcompoundPK=Array.isArray(this.modelClass().idColumn)returnthis.findById(compoundPK ? id : id[0])}}exportdefault<MextendstypeofModel>(ModelClass: typeofModel): M=>{returnclassextendsModelClass{staticgethashIdSalt(){returnthis.name}staticgethashIdMinLength(){return6}staticget_hashIdInstance(){returnmemoizeHashId(this.hashIdSalt,this.hashIdMinLength)}gethashId(){constconf=this.constructorasIConstructor;returnconf._hashIdInstance.encode(this.$id())}gethashid(){returnthis.hashId}// This field indicates what property the hashid is written under.// By default, it overwrites id, but if you're using Algolia, for example,// it may be useful to set a custom id field as "ObjectID".staticgethashIdField(){return'id'}// These are all of the **non-id** fields that are hashed.// Useful if you have FK references that points to an id that is hashed.staticgethashedFields(){return[]}$formatJson(obj: {}){letformattedJson=super.$formatJson(obj)constconf=this.constructorasIConstructor;// inject the hashed PK into the resulting JSON - a reminder// that hashId/hashid fields are virtual and do not get written to JSON.if(conf?.hashIdField){formattedJson[conf.hashIdField]=this.hashId}// hash the rest of the fieldsconf.hashedFields.forEach(field=>{formattedJson[field]=conf._hashIdInstance.encode(formattedJson[field])})returnformattedJson}QueryBuilderType!: HashedQueryBuilder<this>staticQueryBuilder=HashedQueryBuilder}asunknownasM}
The text was updated successfully, but these errors were encountered:
I tried to convert the plugin to typescript. Leaving it here for you to use if of value. Its missing the hashing overrides for now as I did not need them in my project as yet.
The text was updated successfully, but these errors were encountered: