All files / src Decorators.ts

100% Statements 31/31
87.5% Branches 7/8
100% Functions 15/15
100% Lines 28/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104  9x                                                 9x   12x   12x   12x                         18x       20x     20x 20x                     9x 9x   9x   12x 12x         12x     9x 3x 3x 3x         9x     2x 2x   2x 2x   1x   2x   2x   2x    
import neo4js, { Model, ModelInstance } from "./index";
import { connectHelper } from "./utils";
import { RelationType } from "./Relation";
import { HasManyActions, HasOneActions } from "./Types";
 
export type lazyModel<P, M extends ModelInstance<P>> =
  | Model<P, M>
  | (() => Model<P, M> | null);
 
export type metaRelation = {
  from: lazyModel<any, any>;
  to: lazyModel<any, any>;
  via: string;
};
 
export type lazyMetaRelation = metaRelation | (() => metaRelation | null);
 
export type relationProperty = {
  dest: lazyModel<any, any>;
  relation: lazyMetaRelation;
  out?: boolean;
  any?: boolean;
  propertyName: string;
  many: boolean;
};
 
export const relation = {
  from: <P1, M1 extends ModelInstance<P1>>(from: lazyModel<P1, M1>) => {
    return {
      to: <P2, M2 extends ModelInstance<P2>>(to: lazyModel<P2, M2>) => {
        return {
          via: (via: string): metaRelation => {
            return {
              from,
              to,
              via,
            };
          },
        };
      },
    };
  },
};
 
function connectRelationToProp(many: boolean) {
  return <P, M extends ModelInstance<P>>(
    model: lazyModel<P, M>,
    relation: lazyMetaRelation,
    direction?: "in" | "out" | "any"
  ) => (target: any, key: string) => {
    //if (descriptor) descriptor.writable = true;
 
    if (!target._relations) target._relations = [];
    target._relations.push({
      dest: model,
      relation,
      out: direction ? direction === "out" : null,
      any: direction ? direction === "any" : null,
      propertyName: key,
      many,
    });
  };
}
 
export const hasMany = connectRelationToProp(true);
export const hasOne = connectRelationToProp(false);
 
export const model = <P, M extends ModelInstance<P>>(
  model: lazyModel<P, M>
) => (target: any) => {
  connectHelper.models.push({
    model,
    relations: target.prototype._relations,
    modelInstance: target,
  });
  connectHelper.tryInject();
};
 
export const defaultProps = (props: any) => {
  return (target: any) => {
    Eif (props) {
      target.prototype._defaultProps = props;
    }
  };
};
 
export function extendModelInstance<P, M extends ModelInstance<P>>(
  instance: new () => M
): new () => M {
  const i: any = instance;
  i.hasMany = (propertyName, ...args) =>
    // @ts-ignore
    hasMany(...args)(instance.prototype, propertyName, null);
  i.hasOne = (propertyName, ...args) =>
    // @ts-ignore
    hasOne(...args)(instance.prototype, propertyName, null);
  // @ts-ignore
  i.model = (...args) => model(...args)(instance, "");
  // @ts-ignore
  i.defaultProps = (...args) => defaultProps(...args)(instance, "");
 
  return i;
}