├── docs ├── .nojekyll ├── index.html ├── README.md ├── _sidebar.md ├── types.md ├── schema.md ├── model.md └── style.css ├── .gitignore ├── .npmignore ├── lib ├── types.js.map ├── types.js ├── helpers │ ├── types.d.ts │ ├── typemap.d.ts │ ├── tokenmap.d.ts │ ├── methods.d.ts │ ├── tokenmap.js.map │ ├── methods.js.map │ ├── types.js.map │ ├── tokenmap.js │ ├── methods.js │ ├── typemap.js.map │ ├── utility.d.ts │ ├── typemap.js │ ├── types.js │ ├── utility.js.map │ └── utility.js ├── schema.js.map ├── connection.js.map ├── schema.d.ts ├── connection.d.ts ├── schema.js ├── types.d.ts ├── index.js.map ├── connection.js ├── query.d.ts ├── index.d.ts ├── model.d.ts ├── query.js.map ├── index.js ├── query.js └── model.js.map ├── tsconfig.json ├── src ├── helpers │ ├── tokenmap.ts │ ├── typemap.ts │ ├── methods.ts │ ├── types.ts │ └── utility.ts ├── schema.ts ├── types.ts ├── connection.ts ├── index.ts ├── query.ts └── model.ts ├── package.json ├── LICENSE ├── README.md └── example └── index.ts /docs/.nojekyll: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | **/.DS_Store 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .* 2 | **/.DS_Store 3 | npm-debug.log 4 | example 5 | src 6 | node_modules -------------------------------------------------------------------------------- /lib/types.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""} -------------------------------------------------------------------------------- /lib/types.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | //# sourceMappingURL=types.js.map -------------------------------------------------------------------------------- /lib/helpers/types.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * types 3 | * 4 | * dgraph-orm field types 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | import { TypesType } from "../types"; 9 | declare const Types: TypesType; 10 | export default Types; 11 | -------------------------------------------------------------------------------- /lib/helpers/typemap.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * typemap 3 | * 4 | * dgraph-orm type map 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | declare const typemap: { 9 | [index: string]: Array; 10 | }; 11 | export default typemap; 12 | -------------------------------------------------------------------------------- /lib/helpers/tokenmap.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * tokenmap 3 | * 4 | * dgraph-orm token maps 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | declare const tokenmap: { 9 | [index: string]: Array; 10 | }; 11 | export default tokenmap; 12 | -------------------------------------------------------------------------------- /lib/helpers/methods.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * methods 3 | * 4 | * dgraph-orm Model methods 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | import { MethodsType } from "../types"; 9 | declare const Methods: MethodsType; 10 | export default Methods; 11 | -------------------------------------------------------------------------------- /lib/helpers/tokenmap.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"tokenmap.js","sourceRoot":"","sources":["../../src/helpers/tokenmap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;AAEH,kDAA4B;AAE5B,IAAM,QAAQ,GAAqC,EAAE,CAAC;AAEtD,QAAQ,CAAC,eAAK,CAAC,MAAM,CAAC,GAAG;IACvB,OAAO;IACP,MAAM;IACN,MAAM;IACN,UAAU;IACV,SAAS;CACV,CAAC;AAEF,QAAQ,CAAC,eAAK,CAAC,QAAQ,CAAC,GAAG;IACzB,MAAM;IACN,OAAO;IACP,KAAK;IACL,MAAM;CACP,CAAC;AAEF,kBAAe,QAAQ,CAAC"} -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "esModuleInterop": true, 5 | "target": "es5", 6 | "noImplicitAny": true, 7 | "moduleResolution": "node", 8 | "sourceMap": true, 9 | "outDir": "lib", 10 | "baseUrl": ".", 11 | "paths": { 12 | "*": [ 13 | "node_modules/*" 14 | ] 15 | } 16 | }, 17 | "include": [ 18 | "src/**/*" 19 | ] 20 | } -------------------------------------------------------------------------------- /src/helpers/tokenmap.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * tokenmap 3 | * 4 | * dgraph-orm token maps 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | import types from './types'; 10 | 11 | const tokenmap: {[index: string]: Array} = {}; 12 | 13 | tokenmap[types.STRING] = [ 14 | 'exact', 15 | 'hash', 16 | 'term', 17 | 'fulltext', 18 | 'trigram' 19 | ]; 20 | 21 | tokenmap[types.DATETIME] = [ 22 | 'year', 23 | 'month', 24 | 'day', 25 | 'hour' 26 | ]; 27 | 28 | export default tokenmap; -------------------------------------------------------------------------------- /lib/helpers/methods.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"methods.js","sourceRoot":"","sources":["../../src/helpers/methods.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAIH,IAAM,EAAE,GAAW,IAAI,CAAC;AAExB,IAAM,GAAG,GAAW,KAAK,CAAC;AAE1B,IAAM,UAAU,GAAW,YAAY,CAAC;AAExC,IAAM,UAAU,GAAW,YAAY,CAAC;AAExC,IAAM,MAAM,GAAW,QAAQ,CAAC;AAEhC,IAAM,SAAS,GAAW,WAAW,CAAC;AAEtC,IAAM,SAAS,GAAW,WAAW,CAAC;AAEtC,IAAM,GAAG,GAAW,KAAK,CAAC;AAE1B,IAAM,IAAI,GAAW,MAAM,CAAC;AAE5B,IAAM,QAAQ,GAAW,UAAU,CAAC;AAEpC,IAAM,OAAO,GAAgB;IAC3B,EAAE,IAAA;IACF,GAAG,KAAA;IACH,UAAU,YAAA;IACV,UAAU,YAAA;IACV,MAAM,QAAA;IACN,SAAS,WAAA;IACT,SAAS,WAAA;IACT,GAAG,KAAA;IACH,IAAI,MAAA;IACJ,QAAQ,UAAA;CACT,CAAA;AACD,kBAAe,OAAO,CAAC"} -------------------------------------------------------------------------------- /lib/helpers/types.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/helpers/types.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAIH;;;;GAIG;AACH,IAAM,GAAG,GAAW,KAAK,CAAC;AAE1B;;;;GAIG;AACH,IAAM,KAAK,GAAW,OAAO,CAAC;AAE9B;;;;GAIG;AACH,IAAM,MAAM,GAAW,QAAQ,CAAC;AAEhC;;;;GAIG;AACH,IAAM,IAAI,GAAW,MAAM,CAAC;AAE5B;;;;GAIG;AACH,IAAM,QAAQ,GAAW,UAAU,CAAC;AAEpC;;;;GAIG;AACH,IAAM,GAAG,GAAW,KAAK,CAAC;AAE1B;;;;GAIG;AACH,IAAM,QAAQ,GAAW,UAAU,CAAC;AAEpC;;;;GAIG;AACH,IAAM,GAAG,GAAW,KAAK,CAAC;AAE1B,IAAM,KAAK,GAAc;IACvB,GAAG,KAAA;IACH,KAAK,OAAA;IACL,MAAM,QAAA;IACN,IAAI,MAAA;IACJ,QAAQ,UAAA;IACR,GAAG,KAAA;IACH,QAAQ,UAAA;IACR,GAAG,KAAA;CACJ,CAAA;AAED,kBAAe,KAAK,CAAC"} -------------------------------------------------------------------------------- /lib/helpers/tokenmap.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * tokenmap 4 | * 5 | * dgraph-orm token maps 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | var __importDefault = (this && this.__importDefault) || function (mod) { 10 | return (mod && mod.__esModule) ? mod : { "default": mod }; 11 | }; 12 | Object.defineProperty(exports, "__esModule", { value: true }); 13 | var types_1 = __importDefault(require("./types")); 14 | var tokenmap = {}; 15 | tokenmap[types_1.default.STRING] = [ 16 | 'exact', 17 | 'hash', 18 | 'term', 19 | 'fulltext', 20 | 'trigram' 21 | ]; 22 | tokenmap[types_1.default.DATETIME] = [ 23 | 'year', 24 | 'month', 25 | 'day', 26 | 'hour' 27 | ]; 28 | exports.default = tokenmap; 29 | //# sourceMappingURL=tokenmap.js.map -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | dgraph-orm - An ORM for Dgraph 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/helpers/typemap.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * typemap 3 | * 4 | * dgraph-orm type map 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | import types from './types'; 10 | 11 | const typemap: {[index: string]: Array} = {}; 12 | 13 | typemap[types.STRING] = ['list', 'count', 'lang', 'index', 'upsert', 'token', 'unique']; 14 | typemap[types.INT] = ['list', 'count', 'index', 'upsert', 'unique']; 15 | typemap[types.FLOAT] = ['list', 'count', 'index', 'upsert', 'unique']; 16 | typemap[types.BOOL] = ['list', 'count', 'index', 'upsert']; 17 | typemap[types.DATETIME] = ['list', 'count', 'index', 'upsert', 'token']; 18 | typemap[types.GEO] = ['list', 'count', 'index', 'upsert']; 19 | typemap[types.PASSWORD] = []; 20 | typemap[types.UID] = ['count', 'reverse', 'model', 'replace']; 21 | 22 | export default typemap; -------------------------------------------------------------------------------- /lib/helpers/methods.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * methods 4 | * 5 | * dgraph-orm Model methods 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | Object.defineProperty(exports, "__esModule", { value: true }); 10 | var eq = 'eq'; 11 | var uid = 'uid'; 12 | var allofterms = 'allofterms'; 13 | var anyofterms = 'anyofterms'; 14 | var regexp = 'regexp'; 15 | var anyoftext = 'anyoftext'; 16 | var alloftext = 'alloftext'; 17 | var has = 'has'; 18 | var near = 'near'; 19 | var contains = 'contains'; 20 | var Methods = { 21 | eq: eq, 22 | uid: uid, 23 | allofterms: allofterms, 24 | anyofterms: anyofterms, 25 | regexp: regexp, 26 | anyoftext: anyoftext, 27 | alloftext: alloftext, 28 | has: has, 29 | near: near, 30 | contains: contains 31 | }; 32 | exports.default = Methods; 33 | //# sourceMappingURL=methods.js.map -------------------------------------------------------------------------------- /src/helpers/methods.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * methods 3 | * 4 | * dgraph-orm Model methods 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | import { MethodsType } from "../types"; 10 | 11 | const eq: string = 'eq'; 12 | 13 | const uid: string = 'uid'; 14 | 15 | const allofterms: string = 'allofterms'; 16 | 17 | const anyofterms: string = 'anyofterms'; 18 | 19 | const regexp: string = 'regexp'; 20 | 21 | const anyoftext: string = 'anyoftext'; 22 | 23 | const alloftext: string = 'alloftext'; 24 | 25 | const has: string = 'has'; 26 | 27 | const near: string = 'near'; 28 | 29 | const contains: string = 'contains'; 30 | 31 | const Methods: MethodsType = { 32 | eq, 33 | uid, 34 | allofterms, 35 | anyofterms, 36 | regexp, 37 | anyoftext, 38 | alloftext, 39 | has, 40 | near, 41 | contains 42 | } 43 | export default Methods; -------------------------------------------------------------------------------- /lib/schema.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;AAEH;;GAEG;AACH,6CAAgE;AAIhE;;;;GAIG;AACH;IAuBE;;;;OAIG;IACH,gBAAY,IAAY,EAAE,QAAsB;QAC9C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QAEzB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;OAMG;IACK,uBAAM,GAAd,UAAe,IAAY,EAAE,MAA2B;QACtD,sBAAY,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAC3B,OAAO,uBAAa,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC;IAED;;;;;;OAMG;IACK,0BAAS,GAAjB,UAAkB,IAAY,EAAE,QAAsB;QAAtD,iBAOC;QANC,IAAM,OAAO,GAAkB,EAAE,CAAC;QAClC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,UAAC,GAAW;YACxC,OAAO,CAAC,IAAI,CAAC,IAAI,GAAG,GAAG,GAAG,KAAI,CAAC,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC;IACH,aAAC;AAAD,CAAC,AA9DD,IA8DC;AAED,kBAAe,MAAM,CAAC"} -------------------------------------------------------------------------------- /lib/helpers/typemap.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"typemap.js","sourceRoot":"","sources":["../../src/helpers/typemap.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;AAEH,kDAA4B;AAE5B,IAAM,OAAO,GAAqC,EAAE,CAAC;AAErD,OAAO,CAAC,eAAK,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AACxF,OAAO,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACpE,OAAO,CAAC,eAAK,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACtE,OAAO,CAAC,eAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC3D,OAAO,CAAC,eAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;AACxE,OAAO,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;AAC1D,OAAO,CAAC,eAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;AAC7B,OAAO,CAAC,eAAK,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,SAAS,CAAC,CAAC;AAE9D,kBAAe,OAAO,CAAC"} -------------------------------------------------------------------------------- /lib/helpers/utility.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * utility 3 | * 4 | * dgraph-orm utilities 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | import { FieldProps } from '../types'; 9 | /** 10 | * checkOptions 11 | * @param name {string} 12 | * @param options {string | FieldProps} 13 | * 14 | * @returns void 15 | */ 16 | export declare const checkOptions: (name: string, options: string | FieldProps) => void; 17 | /** 18 | * prepareSchema 19 | * @param name {string} 20 | * @param options {string | FieldProps} 21 | * 22 | * @returns string 23 | */ 24 | export declare const prepareSchema: (name: string, options: string | FieldProps) => string; 25 | /** 26 | * pluck 27 | * @param arr Array 28 | * @param key string 29 | * 30 | * @returns Array 31 | */ 32 | export declare const pluck: (arr: any[], key: string) => any[]; 33 | /** 34 | * merge 35 | * 36 | * @param data {any} 37 | * @param keys {Array} 38 | * 39 | * @returns any 40 | */ 41 | export declare const merge: Function; 42 | -------------------------------------------------------------------------------- /lib/connection.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"connection.js","sourceRoot":"","sources":["../src/connection.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;;;;;;;;;AAEH;;;;GAIG;AACH,gDAAoC;AAEpC;;;;GAIG;AACH,yCAA6B;AAI7B;;;;GAIG;AACH,IAAM,OAAO,GAAqB;IAChC,IAAI,EAAE,WAAW;IACjB,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IACZ,WAAW,EAAE,IAAI,CAAC,WAAW,CAAC,cAAc,EAAE;CAC/C,CAAA;AAED;;;;GAIG;AACH;IA8BE;;;;OAIG;IACH,oBAAY,MAA6B,EAAE,MAA8B;QAA7D,uBAAA,EAAA,WAA6B;QAAE,uBAAA,EAAA,SAAmB,OAAO,CAAC,GAAG;QAZzE;;;;WAIG;QACH,WAAM,GAAQ,MAAM,CAAC;QASnB,IAAI,CAAC,MAAM,yBACN,OAAO,GACP,MAAM,CACV,CAAA;QAED,IAAI;YACF,IAAI,CAAC,UAAU,GAAG,IAAI,MAAM,CAAC,gBAAgB,CACxC,IAAI,CAAC,MAAM,CAAC,IAAI,SAAI,IAAI,CAAC,MAAM,CAAC,IAAM,EACzC,IAAI,CAAC,MAAM,CAAC,WAAW,CACxB,CAAC;YAEF,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAEvD,IAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE;gBACpB,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;aAChC;SACF;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,CAAC,KAAK,CAAC,CAAC;SACf;IACH,CAAC;IAED;;;;OAIG;IACH,0BAAK,GAAL;QACE,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;IACH,iBAAC;AAAD,CAAC,AAlED,IAkEC"} -------------------------------------------------------------------------------- /lib/helpers/typemap.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * typemap 4 | * 5 | * dgraph-orm type map 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | var __importDefault = (this && this.__importDefault) || function (mod) { 10 | return (mod && mod.__esModule) ? mod : { "default": mod }; 11 | }; 12 | Object.defineProperty(exports, "__esModule", { value: true }); 13 | var types_1 = __importDefault(require("./types")); 14 | var typemap = {}; 15 | typemap[types_1.default.STRING] = ['list', 'count', 'lang', 'index', 'upsert', 'token', 'unique']; 16 | typemap[types_1.default.INT] = ['list', 'count', 'index', 'upsert', 'unique']; 17 | typemap[types_1.default.FLOAT] = ['list', 'count', 'index', 'upsert', 'unique']; 18 | typemap[types_1.default.BOOL] = ['list', 'count', 'index', 'upsert']; 19 | typemap[types_1.default.DATETIME] = ['list', 'count', 'index', 'upsert', 'token']; 20 | typemap[types_1.default.GEO] = ['list', 'count', 'index', 'upsert']; 21 | typemap[types_1.default.PASSWORD] = []; 22 | typemap[types_1.default.UID] = ['count', 'reverse', 'model', 'replace']; 23 | exports.default = typemap; 24 | //# sourceMappingURL=typemap.js.map -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dgraph-orm", 3 | "version": "2.0.0", 4 | "description": "Simplified schema creation, queries and mutations for Dgraph.", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "prepare": "tsc -d", 9 | "start": "nodemon -e ts example --exec ts-node" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/ashokvishwakarma/dgraph-orm.git" 14 | }, 15 | "keywords": [ 16 | "dgraph-orm", 17 | "dgraph-schema" 18 | ], 19 | "author": "Ashok Vishwakarma ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/ashokvishwakarma/dgraph-orm/issues" 23 | }, 24 | "homepage": "https://ashokvishwakarma.github.io/dgraph-orm", 25 | "devDependencies": { 26 | "@types/google-protobuf": "^3.7.2", 27 | "@types/node": "^12.11.1", 28 | "nodemon": "^1.19.4", 29 | "ts-node": "^8.4.1", 30 | "typescript": "^3.6.4" 31 | }, 32 | "dependencies": { 33 | "dgraph-js": "^2.0.2", 34 | "grpc": "^1.24.0", 35 | "protobufjs": "^6.8.8" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ashok Vishwakarma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/schema.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Schema 3 | * 4 | * dgraph-orm Schema class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | import { SchemaFields } from './types'; 9 | /** 10 | * Schema 11 | * 12 | * Schema class 13 | */ 14 | declare class Schema { 15 | /** 16 | * name 17 | * 18 | * @type string 19 | */ 20 | name: string; 21 | /** 22 | * schema 23 | * 24 | * @type Array 25 | */ 26 | schema: Array; 27 | /** 28 | * original 29 | * 30 | * @type SchemaFields 31 | */ 32 | original: SchemaFields; 33 | /** 34 | * 35 | * @param name {string} 36 | * @param schema {SchemaFields} 37 | */ 38 | constructor(name: string, original: SchemaFields); 39 | /** 40 | * _build 41 | * @param name {string} 42 | * @param params {string | FieldProps} 43 | * 44 | * @returns string 45 | */ 46 | private _build; 47 | /** 48 | * _generate 49 | * @param name {string} 50 | * @param original {SchemaFields} 51 | * 52 | * @returns Array 53 | */ 54 | private _generate; 55 | } 56 | export default Schema; 57 | -------------------------------------------------------------------------------- /lib/helpers/types.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * types 4 | * 5 | * dgraph-orm field types 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | Object.defineProperty(exports, "__esModule", { value: true }); 10 | /** 11 | * INT 12 | * 13 | * dgraph int type 14 | */ 15 | var INT = 'int'; 16 | /** 17 | * FLOAT 18 | * 19 | * dgraph float type 20 | */ 21 | var FLOAT = 'float'; 22 | /** 23 | * STRING 24 | * 25 | * dgraph string type 26 | */ 27 | var STRING = 'string'; 28 | /** 29 | * BOOL 30 | * 31 | * dgraph bool type 32 | */ 33 | var BOOL = 'bool'; 34 | /** 35 | * DATETIME 36 | * 37 | * dgraph datetime type 38 | */ 39 | var DATETIME = 'datetime'; 40 | /** 41 | * GEO 42 | * 43 | * dgraph geo type 44 | */ 45 | var GEO = 'geo'; 46 | /** 47 | * PASSWORD 48 | * 49 | * dgraph password type 50 | */ 51 | var PASSWORD = 'password'; 52 | /** 53 | * UID 54 | * 55 | * dgraph uid type 56 | */ 57 | var UID = 'uid'; 58 | var Types = { 59 | INT: INT, 60 | FLOAT: FLOAT, 61 | STRING: STRING, 62 | BOOL: BOOL, 63 | DATETIME: DATETIME, 64 | GEO: GEO, 65 | PASSWORD: PASSWORD, 66 | UID: UID 67 | }; 68 | exports.default = Types; 69 | //# sourceMappingURL=types.js.map -------------------------------------------------------------------------------- /src/helpers/types.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * types 3 | * 4 | * dgraph-orm field types 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | import { TypesType } from "../types"; 10 | 11 | /** 12 | * INT 13 | * 14 | * dgraph int type 15 | */ 16 | const INT: string = 'int'; 17 | 18 | /** 19 | * FLOAT 20 | * 21 | * dgraph float type 22 | */ 23 | const FLOAT: string = 'float'; 24 | 25 | /** 26 | * STRING 27 | * 28 | * dgraph string type 29 | */ 30 | const STRING: string = 'string'; 31 | 32 | /** 33 | * BOOL 34 | * 35 | * dgraph bool type 36 | */ 37 | const BOOL: string = 'bool'; 38 | 39 | /** 40 | * DATETIME 41 | * 42 | * dgraph datetime type 43 | */ 44 | const DATETIME: string = 'datetime'; 45 | 46 | /** 47 | * GEO 48 | * 49 | * dgraph geo type 50 | */ 51 | const GEO: string = 'geo'; 52 | 53 | /** 54 | * PASSWORD 55 | * 56 | * dgraph password type 57 | */ 58 | const PASSWORD: string = 'password'; 59 | 60 | /** 61 | * UID 62 | * 63 | * dgraph uid type 64 | */ 65 | const UID: string = 'uid'; 66 | 67 | const Types: TypesType = { 68 | INT, 69 | FLOAT, 70 | STRING, 71 | BOOL, 72 | DATETIME, 73 | GEO, 74 | PASSWORD, 75 | UID 76 | } 77 | 78 | export default Types; -------------------------------------------------------------------------------- /lib/connection.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Connection 3 | * 4 | * dgraph-orm Connection class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | /** 9 | * dgraph 10 | * 11 | * https://www.npmjs.com/package/dgraph-js 12 | */ 13 | import * as dgraph from 'dgraph-js'; 14 | import { ConnectionConfig } from './types'; 15 | /** 16 | * Connection 17 | * 18 | * Connection class 19 | */ 20 | export default class Connection { 21 | /** 22 | * config 23 | * 24 | * @type ConnectionConfig 25 | */ 26 | config: ConnectionConfig; 27 | /** 28 | * clientStub 29 | * 30 | * @type dgraph.DgraphClientStub 31 | */ 32 | clientStub: dgraph.DgraphClientStub; 33 | /** 34 | * client 35 | * 36 | * @type dgraph.DgraphClient 37 | */ 38 | client: dgraph.DgraphClient; 39 | /** 40 | * dgraph 41 | * 42 | * @type any 43 | */ 44 | dgraph: any; 45 | /** 46 | * constructor 47 | * @param config {ConnectionConfig} 48 | * @param logger {Function} 49 | */ 50 | constructor(config?: ConnectionConfig, logger?: Function); 51 | /** 52 | * close 53 | * 54 | * @retuns void 55 | */ 56 | close(): void; 57 | } 58 | -------------------------------------------------------------------------------- /lib/schema.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * Schema 4 | * 5 | * dgraph-orm Schema class 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | Object.defineProperty(exports, "__esModule", { value: true }); 10 | /** 11 | * helper utilities 12 | */ 13 | var utility_1 = require("./helpers/utility"); 14 | /** 15 | * Schema 16 | * 17 | * Schema class 18 | */ 19 | var Schema = /** @class */ (function () { 20 | /** 21 | * 22 | * @param name {string} 23 | * @param schema {SchemaFields} 24 | */ 25 | function Schema(name, original) { 26 | this.name = name; 27 | this.original = original; 28 | this.schema = this._generate(name, original); 29 | } 30 | /** 31 | * _build 32 | * @param name {string} 33 | * @param params {string | FieldProps} 34 | * 35 | * @returns string 36 | */ 37 | Schema.prototype._build = function (name, params) { 38 | utility_1.checkOptions(name, params); 39 | return utility_1.prepareSchema(name, params); 40 | }; 41 | /** 42 | * _generate 43 | * @param name {string} 44 | * @param original {SchemaFields} 45 | * 46 | * @returns Array 47 | */ 48 | Schema.prototype._generate = function (name, original) { 49 | var _this = this; 50 | var _schema = []; 51 | Object.keys(original).forEach(function (key) { 52 | _schema.push(name + '.' + _this._build(key, original[key])); 53 | }); 54 | return _schema; 55 | }; 56 | return Schema; 57 | }()); 58 | exports.default = Schema; 59 | //# sourceMappingURL=schema.js.map -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # dgraph-orm 2 | Simplified schema creation, queries and mutations for Dgraph. 3 | 4 | ## Install 5 | 6 | ```javascript 7 | npm install dgraph-orm 8 | ``` 9 | 10 | ## Define schema 11 | 12 | ```javascript 13 | import {Schema, model} from 'dgraph-orm'; 14 | 15 | const UserSchema = new Schema('user', { 16 | name: { 17 | type: dgraph.Types.STRING, 18 | index: true, 19 | token: { 20 | term: true 21 | } 22 | }, 23 | email: { 24 | type: dgraph.Types.STRING, 25 | index: true, 26 | unique: true, 27 | token: { 28 | exact: true 29 | } 30 | }, 31 | password: dgraph.Types.PASSWORD, 32 | bio: dgraph.Types.STRING, 33 | friend: { 34 | type: dgraph.Types.UID, 35 | model: 'user', // related model name 36 | count: true, 37 | reverse: true 38 | } 39 | }); 40 | 41 | /** 42 | * Set and create model out of the schema 43 | */ 44 | const User = model(UserSchema); 45 | ``` 46 | 47 | ## Use model 48 | 49 | ```javascript 50 | /** 51 | * Creates a new user with passed fields 52 | * 53 | * Returns the created user along with the generated uid 54 | */ 55 | const user = await User.create({ 56 | name: 'Ashok Vishwakarma', 57 | email: 'akvlko@gmail.com', 58 | bio: 'My bio ...' 59 | }); 60 | 61 | console.log(user); 62 | // { 63 | // uid: '0x1', 64 | // name: 'Ashok Vishwakarma', 65 | // email: 'akvlko@gmail.com', 66 | // bio: 'My bio ...' 67 | // } 68 | ``` 69 | ## Author 70 | 71 | ![my_pic](https://avatars1.githubusercontent.com/u/389185?s=100&v=1) 72 | 73 | [](https://avatars1.githubusercontent.com/u/389185?s=460&v=4) 74 | ### Ashok Vishwakarma 75 | 76 | [LinkedIn](https://www.linkedin.com/in/ashokvishwakarmaa/) • [Twitter](https://twitter.com/_avishwakarma) • [Medium](https://medium.com/@avishwakarma) -------------------------------------------------------------------------------- /src/schema.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Schema 3 | * 4 | * dgraph-orm Schema class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | /** 10 | * helper utilities 11 | */ 12 | import { prepareSchema, checkOptions } from './helpers/utility'; 13 | 14 | import { SchemaFields, FieldProps } from './types'; 15 | 16 | /** 17 | * Schema 18 | * 19 | * Schema class 20 | */ 21 | class Schema { 22 | 23 | /** 24 | * name 25 | * 26 | * @type string 27 | */ 28 | name: string; 29 | 30 | /** 31 | * schema 32 | * 33 | * @type Array 34 | */ 35 | schema: Array; 36 | 37 | /** 38 | * original 39 | * 40 | * @type SchemaFields 41 | */ 42 | original: SchemaFields; 43 | 44 | /** 45 | * 46 | * @param name {string} 47 | * @param schema {SchemaFields} 48 | */ 49 | constructor(name: string, original: SchemaFields) { 50 | this.name = name; 51 | this.original = original; 52 | 53 | this.schema = this._generate(name, original); 54 | } 55 | 56 | /** 57 | * _build 58 | * @param name {string} 59 | * @param params {string | FieldProps} 60 | * 61 | * @returns string 62 | */ 63 | private _build(name: string, params: string | FieldProps): string { 64 | checkOptions(name, params); 65 | return prepareSchema(name, params); 66 | } 67 | 68 | /** 69 | * _generate 70 | * @param name {string} 71 | * @param original {SchemaFields} 72 | * 73 | * @returns Array 74 | */ 75 | private _generate(name: string, original: SchemaFields): Array { 76 | const _schema: Array = []; 77 | Object.keys(original).forEach((key: string) => { 78 | _schema.push(name + '.' + this._build(key, original[key])); 79 | }); 80 | 81 | return _schema; 82 | } 83 | } 84 | 85 | export default Schema; -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | import { ChannelCredentials } from 'grpc'; 2 | 3 | interface Base { 4 | include?: Include 5 | filter?: Filter, 6 | attributes?: Array 7 | order?: Array 8 | first?: number 9 | offset?: number 10 | after?: string 11 | } 12 | 13 | export interface ConnectionConfig { 14 | host?: string 15 | port?: number 16 | debug?: boolean 17 | credentails?: ChannelCredentials 18 | } 19 | 20 | export interface Token { 21 | term?: boolean 22 | trigram?: boolean 23 | fulltext?: boolean 24 | exact?: boolean 25 | hash?: boolean 26 | } 27 | 28 | export interface FieldProps { 29 | type: string 30 | index?: boolean 31 | token?: string | Token 32 | unique?: boolean 33 | list?: boolean 34 | count?: Boolean 35 | lang?: boolean 36 | model?: string 37 | reverse?: boolean, 38 | replace?: boolean 39 | } 40 | 41 | export interface QueryParams { 42 | query: string 43 | variables: { 44 | [key: string]: any 45 | } 46 | } 47 | 48 | export interface SchemaFields { 49 | [key: string]: string | FieldProps 50 | } 51 | 52 | export interface TypesType { 53 | INT: string 54 | FLOAT: string 55 | STRING: string 56 | BOOL: string 57 | DATETIME: string 58 | GEO: string 59 | PASSWORD: string 60 | UID: string 61 | } 62 | 63 | export interface MethodsType { 64 | eq: string 65 | uid: string 66 | allofterms: string 67 | anyofterms: string 68 | regexp: string 69 | anyoftext: string 70 | alloftext: string 71 | has: string 72 | near: string 73 | contains: string 74 | } 75 | 76 | export interface RelationParam { 77 | field: string | Array 78 | attributes?: { 79 | [key: string]: Array 80 | } 81 | } 82 | 83 | export interface Filter { 84 | [key: string]: any 85 | $or?: any 86 | $and?: any 87 | } 88 | 89 | export interface Include extends Base { 90 | [key: string]: any 91 | as?: string 92 | } 93 | 94 | export interface Params extends Base {} -------------------------------------------------------------------------------- /docs/_sidebar.md: -------------------------------------------------------------------------------- 1 | * [Quick Start](/) 2 | * [Install](?id=install) 3 | * [Define schema](?id=define-schema) 4 | * [Use model](?id=use-model) 5 | * [Schema](schema) 6 | * [Types](schema?id=types) 7 | * [Scaler](schema?id=scaler-type) 8 | * [UID](schema?id=uid-type) 9 | * [Defining Schema](schema?id=defining-schema) 10 | * [Schema Fields](schema?id=schema-fields) 11 | * [INT](schema?id=int) 12 | * [STRING](schema?id=string) 13 | * [FLOAT](schema?id=float) 14 | * [BOOL](schema?id=bool) 15 | * [DATETIME](schema?id=datetime) 16 | * [GEO](schema?id=geo) 17 | * [PASSWORD](schema?id=password) 18 | * [UID](schema?id=uid) 19 | * [Model](model) 20 | * [Defining Model](model?id=defining-model) 21 | * [Properties](model?id=properties) 22 | * [schema](model?id=schema) 23 | * [connection](model?id=connection) 24 | * [Methods](model?id=methods) 25 | * [query](model?id=query) 26 | * [queryWithVars](model?id=queryWithVars) 27 | * [create](model?id=create) 28 | * [update](model?id=update) 29 | * [delete](model?id=delete) 30 | * [checkPassword](model?id=checkPassword) 31 | * [relation](model?id=relation) 32 | * [has](model?id=has) 33 | * [eq](model?id=eq) 34 | * [uid](model?id=uid) 35 | * [allofterms](model?id=allofterms) 36 | * [anyofterms](model?id=anyofterms) 37 | * [alloftext](model?id=alloftext) 38 | * [anyoftext](model?id=anyoftext) 39 | * [regexp](model?id=regexp) 40 | * [near](model?id=near) 41 | * [contains](model?id=contains) 42 | * [Params](model?id=params) 43 | * [attributes](model?id=attributes) 44 | * [include](model?id=include) 45 | * [filter](model?id=filter) 46 | * [or](model?id=or) 47 | * [and](model?id=and) 48 | * [equals](model?id=equals) 49 | * [not equals](model?id=not-equals) 50 | * [less-than](model?id=le) 51 | * [greater-than](model?id=ge) 52 | * [Order](model?id=order) 53 | * [Pagination](model?id=pagination) 54 | * [Types](types) 55 | -------------------------------------------------------------------------------- /lib/types.d.ts: -------------------------------------------------------------------------------- 1 | import { ChannelCredentials } from 'grpc'; 2 | interface Base { 3 | include?: Include; 4 | filter?: Filter; 5 | attributes?: Array; 6 | order?: Array; 7 | first?: number; 8 | offset?: number; 9 | after?: string; 10 | } 11 | export interface ConnectionConfig { 12 | host?: string; 13 | port?: number; 14 | debug?: boolean; 15 | credentails?: ChannelCredentials; 16 | } 17 | export interface Token { 18 | term?: boolean; 19 | trigram?: boolean; 20 | fulltext?: boolean; 21 | exact?: boolean; 22 | hash?: boolean; 23 | } 24 | export interface FieldProps { 25 | type: string; 26 | index?: boolean; 27 | token?: string | Token; 28 | unique?: boolean; 29 | list?: boolean; 30 | count?: Boolean; 31 | lang?: boolean; 32 | model?: string; 33 | reverse?: boolean; 34 | replace?: boolean; 35 | } 36 | export interface QueryParams { 37 | query: string; 38 | variables: { 39 | [key: string]: any; 40 | }; 41 | } 42 | export interface SchemaFields { 43 | [key: string]: string | FieldProps; 44 | } 45 | export interface TypesType { 46 | INT: string; 47 | FLOAT: string; 48 | STRING: string; 49 | BOOL: string; 50 | DATETIME: string; 51 | GEO: string; 52 | PASSWORD: string; 53 | UID: string; 54 | } 55 | export interface MethodsType { 56 | eq: string; 57 | uid: string; 58 | allofterms: string; 59 | anyofterms: string; 60 | regexp: string; 61 | anyoftext: string; 62 | alloftext: string; 63 | has: string; 64 | near: string; 65 | contains: string; 66 | } 67 | export interface RelationParam { 68 | field: string | Array; 69 | attributes?: { 70 | [key: string]: Array; 71 | }; 72 | } 73 | export interface Filter { 74 | [key: string]: any; 75 | $or?: any; 76 | $and?: any; 77 | } 78 | export interface Include extends Base { 79 | [key: string]: any; 80 | as?: string; 81 | } 82 | export interface Params extends Base { 83 | } 84 | export {}; 85 | -------------------------------------------------------------------------------- /src/connection.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Connection 3 | * 4 | * dgraph-orm Connection class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | /** 10 | * dgraph 11 | * 12 | * https://www.npmjs.com/package/dgraph-js 13 | */ 14 | import * as dgraph from 'dgraph-js'; 15 | 16 | /** 17 | * grpc 18 | * 19 | * https://www.npmjs.com/package/grpc 20 | */ 21 | import * as grpc from 'grpc'; 22 | 23 | import { ConnectionConfig } from './types'; 24 | 25 | /** 26 | * _config 27 | * 28 | * @type ConnectionConfig 29 | */ 30 | const _config: ConnectionConfig = { 31 | host: '127.0.0.1', 32 | port: 9080, 33 | debug: false, 34 | credentails: grpc.credentials.createInsecure() 35 | } 36 | 37 | /** 38 | * Connection 39 | * 40 | * Connection class 41 | */ 42 | export default class Connection { 43 | 44 | /** 45 | * config 46 | * 47 | * @type ConnectionConfig 48 | */ 49 | config: ConnectionConfig; 50 | 51 | /** 52 | * clientStub 53 | * 54 | * @type dgraph.DgraphClientStub 55 | */ 56 | clientStub: dgraph.DgraphClientStub; 57 | 58 | /** 59 | * client 60 | * 61 | * @type dgraph.DgraphClient 62 | */ 63 | client: dgraph.DgraphClient; 64 | 65 | /** 66 | * dgraph 67 | * 68 | * @type any 69 | */ 70 | dgraph: any = dgraph; 71 | 72 | /** 73 | * constructor 74 | * @param config {ConnectionConfig} 75 | * @param logger {Function} 76 | */ 77 | constructor(config: ConnectionConfig = {}, logger: Function = console.log) { 78 | 79 | this.config = { 80 | ..._config, 81 | ...config 82 | } 83 | 84 | try { 85 | this.clientStub = new dgraph.DgraphClientStub( 86 | `${this.config.host}:${this.config.port}`, 87 | this.config.credentails 88 | ); 89 | 90 | this.client = new dgraph.DgraphClient(this.clientStub); 91 | 92 | if(this.config.debug) { 93 | this.client.setDebugMode(true); 94 | } 95 | } catch (error) { 96 | logger(error); 97 | } 98 | } 99 | 100 | /** 101 | * close 102 | * 103 | * @retuns void 104 | */ 105 | close(): void { 106 | this.clientStub.close(); 107 | } 108 | } -------------------------------------------------------------------------------- /lib/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBH;;;;GAIG;AACH,oDAA8B;AAE9B;;;;GAIG;AACH,0DAAoC;AAEpC;;;;GAIG;AACH,4DAAsC;AAEtC;;;;GAIG;AACH,kDAA4B;AAI5B;;;;GAIG;AACH;IA2CE;;OAEG;IACH;QA5CA;;;;;WAKG;QACK,YAAO,GAAa,cAAO,CAAC,CAAC;QAUrC;;;;;WAKG;QACH,WAAM,GAAQ,EAAE,CAAC;QAEjB;;;;;WAKG;QACH,UAAK,GAAc,eAAK,CAAC;QAEzB;;;;;;WAMG;QACH,WAAM,GAAsD,gBAAM,CAAC;QAMjE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;IAC9C,CAAC;IAED;;;;;;OAMG;IACH,8BAAU,GAAV;QACE,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,2BAAO,GAAP,UAAQ,QAAkB;QACxB,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC;IAC1B,CAAC;IAED;;;;;;OAMG;IACK,wBAAI,GAAZ,UAAa,OAAe;QAC1B,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACxB,CAAC;IAGD;;;;;;OAMG;IACK,8BAAU,GAAlB,UAAmB,MAAc;QAC/B,IAAG,MAAM,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,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M,MAAmB;QACvB,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM;aAC1B,MAAM,EAAE;aACR,aAAa,CACZ,MAAM,CAAC,KAAK,EACZ,MAAM,CAAC,SAAS,CACjB,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACH,0BAAM,GAAN,UAAO,QAAgB;QACrB,IAAM,EAAE,GAAa,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;QAC3D,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;QACxB,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,MAAM,EAAE;aACnC,MAAM,CAAC,EAAE,CAAC,CAAC;IAChB,CAAC;IACH,gBAAC;AAAD,CAAC,AA/KD,IA+KC;AAED,kBAAe,IAAI,SAAS,EAAE,CAAC"} -------------------------------------------------------------------------------- /lib/connection.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * Connection 4 | * 5 | * dgraph-orm Connection class 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | var __assign = (this && this.__assign) || function () { 10 | __assign = Object.assign || function(t) { 11 | for (var s, i = 1, n = arguments.length; i < n; i++) { 12 | s = arguments[i]; 13 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) 14 | t[p] = s[p]; 15 | } 16 | return t; 17 | }; 18 | return __assign.apply(this, arguments); 19 | }; 20 | var __importStar = (this && this.__importStar) || function (mod) { 21 | if (mod && mod.__esModule) return mod; 22 | var result = {}; 23 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; 24 | result["default"] = mod; 25 | return result; 26 | }; 27 | Object.defineProperty(exports, "__esModule", { value: true }); 28 | /** 29 | * dgraph 30 | * 31 | * https://www.npmjs.com/package/dgraph-js 32 | */ 33 | var dgraph = __importStar(require("dgraph-js")); 34 | /** 35 | * grpc 36 | * 37 | * https://www.npmjs.com/package/grpc 38 | */ 39 | var grpc = __importStar(require("grpc")); 40 | /** 41 | * _config 42 | * 43 | * @type ConnectionConfig 44 | */ 45 | var _config = { 46 | host: '127.0.0.1', 47 | port: 9080, 48 | debug: false, 49 | credentails: grpc.credentials.createInsecure() 50 | }; 51 | /** 52 | * Connection 53 | * 54 | * Connection class 55 | */ 56 | var Connection = /** @class */ (function () { 57 | /** 58 | * constructor 59 | * @param config {ConnectionConfig} 60 | * @param logger {Function} 61 | */ 62 | function Connection(config, logger) { 63 | if (config === void 0) { config = {}; } 64 | if (logger === void 0) { logger = console.log; } 65 | /** 66 | * dgraph 67 | * 68 | * @type any 69 | */ 70 | this.dgraph = dgraph; 71 | this.config = __assign(__assign({}, _config), config); 72 | try { 73 | this.clientStub = new dgraph.DgraphClientStub(this.config.host + ":" + this.config.port, this.config.credentails); 74 | this.client = new dgraph.DgraphClient(this.clientStub); 75 | if (this.config.debug) { 76 | this.client.setDebugMode(true); 77 | } 78 | } 79 | catch (error) { 80 | logger(error); 81 | } 82 | } 83 | /** 84 | * close 85 | * 86 | * @retuns void 87 | */ 88 | Connection.prototype.close = function () { 89 | this.clientStub.close(); 90 | }; 91 | return Connection; 92 | }()); 93 | exports.default = Connection; 94 | //# sourceMappingURL=connection.js.map -------------------------------------------------------------------------------- /docs/types.md: -------------------------------------------------------------------------------- 1 | # Types 2 | 3 | For TypeScript uses 4 | 5 | ## ConnectionConfig 6 | 7 | The `ConnectionConfig` type 8 | 9 | ```javascript 10 | interface ConnectionConfig { 11 | host?: string 12 | port?: number 13 | debug?: boolean 14 | credentails?: ChannelCredentials // from grpc 15 | } 16 | ``` 17 | 18 | ## Token 19 | 20 | The `Token` type 21 | 22 | ```javascript 23 | interface Token { 24 | term?: boolean 25 | trigram?: boolean 26 | fulltext?: boolean 27 | exact?: boolean 28 | hash?: boolean 29 | } 30 | ``` 31 | 32 | ## FieldProps 33 | 34 | The `FieldProps` type 35 | 36 | ```javascript 37 | interface FieldProps { 38 | type: string 39 | index?: boolean 40 | token?: string | Token 41 | unique?: boolean 42 | list?: boolean 43 | count?: Boolean 44 | lang?: boolean 45 | model?: string 46 | reverse?: boolean 47 | } 48 | ``` 49 | 50 | ## QueryParams 51 | 52 | The `QueryParams` type 53 | 54 | ```javascript 55 | interface QueryParams { 56 | query: string 57 | variables: { 58 | [key: string]: any 59 | } 60 | } 61 | ``` 62 | 63 | ## SchemaFields 64 | 65 | The `SchemaFields` type 66 | 67 | ```javascript 68 | interface SchemaFields { 69 | [key: string]: string | FieldProps 70 | } 71 | ``` 72 | 73 | ## TypesType 74 | 75 | The `TypesType` type 76 | 77 | ```javascript 78 | interface TypesType { 79 | INT: string 80 | FLOAT: string 81 | STRING: string 82 | BOOL: string 83 | DATETIME: string 84 | GEO: string 85 | PASSWORD: string 86 | UID: string 87 | } 88 | ``` 89 | 90 | ## MethodsType 91 | 92 | The `MethodsType` type 93 | 94 | ```javascript 95 | interface MethodsType { 96 | eq: string 97 | uid: string 98 | allofterms: string 99 | anyofterms: string 100 | regexp: string 101 | anyoftext: string 102 | alloftext: string 103 | has: string 104 | near: string 105 | contains: string 106 | } 107 | ``` 108 | 109 | ## Filter 110 | 111 | The `Filter` type 112 | 113 | ```javascript 114 | interface Filter { 115 | [key: string]: any 116 | $or?: any 117 | $and?: any 118 | } 119 | ``` 120 | 121 | ## Include 122 | 123 | The `Include` type 124 | 125 | ```javascript 126 | interface Include { 127 | [key: string]: any 128 | as?: string 129 | include?: Include 130 | filter?: Filter, 131 | attributes?: Array 132 | order?: Array 133 | first?: number 134 | offset?: number 135 | after?: string 136 | } 137 | ``` 138 | 139 | ## Params 140 | 141 | The `Params` type 142 | 143 | ```javascript 144 | interface Params { 145 | include?: Include 146 | filter?: Filter, 147 | attributes?: Array 148 | order?: Array 149 | first?: number 150 | offset?: number 151 | after?: string 152 | } 153 | ``` -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dgraph-orm 2 | Simplified schema creation, queries and mutations for Dgraph. 3 | 4 | ## Installation 5 | 6 | ``` 7 | npm install dgraph-orm 8 | ``` 9 | 10 | Depending on the version of Dgraph that you are connecting to, you will have to 11 | use a different version. 12 | 13 | | Dgraph version | dgraph-orm version | 14 | | :------------: | :----------------: | 15 | | 1.0.X | _1.X.Y_ | 16 | | 1.1.X | _2.X.Y_ | 17 | 18 | Note: Only API breakage from _v1.X.Y_ to _v2.X.Y_ is in dependency -`dgraph-js`. 19 | Function `DgraphClient.newTxn().mutate()` returns a `messages.Assigned` 20 | type in _v1.X_ but a `messages.Response` type in _v2.X_. 21 | 22 | ## Full Documentation 23 | 24 | https://avishwakarma.github.io/dgraph-orm 25 | 26 | ## Your first schema and model 27 | 28 | ```javascript 29 | import dgraph from 'dgraph-orm'; 30 | 31 | const UserSchema = new dgraph.Schema('user', { 32 | name: { 33 | type: dgraph.Types.STRING, 34 | index: true, 35 | token: { 36 | term: true 37 | } 38 | }, 39 | email: { 40 | type: dgraph.Types.STRING, 41 | index: true, 42 | unique: true, 43 | token: { 44 | exact: true 45 | } 46 | }, 47 | password: dgraph.Types.PASSWORD, 48 | bio: dgraph.Types.STRING, 49 | friend: { 50 | type: dgraph.Types.UID, 51 | model: 'user', // related model name 52 | count: true, 53 | reverse: true 54 | } 55 | }); 56 | 57 | /** 58 | * Set and create model out of the schema 59 | */ 60 | const User = dgraph.model(UserSchema); 61 | 62 | /** 63 | * Creates a new user with passed fields 64 | * 65 | * Returns the created user along with the generated uid 66 | */ 67 | const user = await User.create({ 68 | name: 'Ashok Vishwakarma', 69 | email: 'akvlko@gmail.com', 70 | bio: 'My bio ...' 71 | }); 72 | 73 | console.log(user); 74 | // { 75 | // uid: '0x1', 76 | // name: 'Ashok Vishwakarma', 77 | // email: 'akvlko@gmail.com', 78 | // bio: 'My bio ...' 79 | // } 80 | ``` 81 | 82 | For the full documentation please visit the below link 83 | 84 | https://ashokvishwakarma.github.io/dgraph-orm 85 | 86 | 87 | ## Futute releases 88 | 89 | * Other geo queries within, intersects 90 | * Group by 91 | * Aggregation 92 | 93 | 94 | ## Contribution 95 | 96 | Issues and pull requests are welcome for 97 | 98 | * Unit test cases 99 | * Feature and query method implementation 100 | * Bug fixes 101 | 102 | ## Author 103 | ![my_pic](https://avatars1.githubusercontent.com/u/389185?s=100&v=1) 104 | 105 | [](https://avatars1.githubusercontent.com/u/389185?s=460&v=4) 106 | ### Ashok Vishwakarma 107 | 108 | [LinkedIn](https://www.linkedin.com/in/ashokvishwakarmaa/) • [Twitter](https://twitter.com/_avishwakarma) • [Medium](https://medium.com/@avishwakarma) 109 | -------------------------------------------------------------------------------- /lib/query.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Query 3 | * 4 | * dgraph-orm Query class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | import { Params } from './types'; 9 | /** 10 | * Query 11 | * 12 | * Class Query 13 | */ 14 | declare class Query { 15 | /** 16 | * name 17 | * 18 | * @type string 19 | */ 20 | private name; 21 | /** 22 | * params 23 | * 24 | * @type Params 25 | */ 26 | private params; 27 | /** 28 | * type 29 | * 30 | * @type string 31 | */ 32 | private type; 33 | /** 34 | * field 35 | * 36 | * @type string 37 | */ 38 | private field; 39 | /** 40 | * value 41 | * 42 | * @type any 43 | */ 44 | private value; 45 | /** 46 | * logger 47 | * 48 | * @type Function 49 | */ 50 | private logger; 51 | /** 52 | * where 53 | * 54 | * @type any 55 | */ 56 | private where; 57 | /** 58 | * query 59 | * 60 | * @type string 61 | */ 62 | query: string; 63 | /** 64 | * constructor 65 | * @param type {string} 66 | * @param field {string} 67 | * @param value {any} 68 | * @param params {Params} 69 | * @param name {string} 70 | * @param logger {Function} 71 | */ 72 | constructor(type: string, field: string, value: any, params: Params, name: string, logger: Function); 73 | /** 74 | * _where 75 | * @param type {string} 76 | * @param field {string} 77 | * @param value {any} 78 | * @param name {string} 79 | * 80 | * @returns string 81 | */ 82 | private _where; 83 | /** 84 | * _filter 85 | * @param key {string} 86 | * @param value {any} 87 | * @param name {string} 88 | * 89 | * @returns string 90 | */ 91 | private _filter; 92 | /** 93 | * _parse_filter 94 | * @param filter {any} 95 | * @param name {string} 96 | * 97 | * @returns string 98 | */ 99 | private _parse_filter; 100 | /** 101 | * _attributes 102 | * @param attributes {Array} 103 | * @param name {string} 104 | * 105 | * @return string 106 | */ 107 | private _attributes; 108 | /** 109 | * _include 110 | * @param include {Include} 111 | * 112 | * @returns string 113 | */ 114 | private _include; 115 | /** 116 | * _extra 117 | * @param params {Params} 118 | * 119 | * @return string 120 | */ 121 | private _extras; 122 | /** 123 | * _parse_order 124 | * @param order {Array} 125 | * 126 | * @returns string 127 | */ 128 | private _parse_order; 129 | /** 130 | * _build 131 | * @param params {any} 132 | * 133 | * @returns string 134 | */ 135 | private _build; 136 | } 137 | export default Query; 138 | -------------------------------------------------------------------------------- /lib/index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * dgraph-orm 3 | * 4 | * Simplified schema creation, queries and mutations for Dgraph. 5 | * 6 | * @author Ashok Vishwakarma 7 | * 8 | * @uses npm install dgraph-orm 9 | * @docs https://ashokvishwakarma.github.io/dgraph-orm 10 | * 11 | */ 12 | /** 13 | * Schema 14 | * 15 | * dgraph-orm Schema class 16 | */ 17 | import Schema from './schema'; 18 | /** 19 | * Connection 20 | * 21 | * dgraph-orm Connection class 22 | */ 23 | import Connection from './connection'; 24 | /** 25 | * Model 26 | * 27 | * dgraph-orm Model class 28 | */ 29 | import Model from './model'; 30 | import { TypesType, SchemaFields, ConnectionConfig, QueryParams } from './types'; 31 | /** 32 | * DgraphORM 33 | * 34 | * DgraphORM class 35 | */ 36 | declare class DgraphORM { 37 | /** 38 | * _logger 39 | * 40 | * @type Function 41 | * Methods for logging 42 | */ 43 | private _logger; 44 | /** 45 | * connection 46 | * 47 | * @type Connection 48 | * connection object 49 | */ 50 | private connection; 51 | /** 52 | * _models 53 | * 54 | * @type any 55 | * created models 56 | */ 57 | models: any; 58 | /** 59 | * Types 60 | * 61 | * @type TypesType 62 | * Field types for dagraph-orm 63 | */ 64 | Types: TypesType; 65 | /** 66 | * Schema 67 | * 68 | * @type {new(name: string, schema: SchemaFields): Schema} 69 | * 70 | * Schema class 71 | */ 72 | Schema: { 73 | new (name: string, schema: SchemaFields): Schema; 74 | }; 75 | /** 76 | * contructor 77 | */ 78 | constructor(); 79 | /** 80 | * disconnect 81 | * 82 | * @returns void 83 | * 84 | * disconnect the dgraph connection 85 | */ 86 | disconnect(): void; 87 | /** 88 | * logging 89 | * @param callback {Function} 90 | * 91 | * @returns void 92 | */ 93 | logging(callback: Function): void; 94 | /** 95 | * _log 96 | * 97 | * @param message {string} 98 | * 99 | * @returns void 100 | */ 101 | private _log; 102 | /** 103 | * _set_model 104 | * 105 | * @param schema {Schema} 106 | * 107 | * @returns void 108 | */ 109 | private _set_model; 110 | /** 111 | * _generate_schema 112 | * 113 | * @param schema {Array} 114 | * 115 | * @returns void 116 | */ 117 | _generate_schema(schema: Array): Promise; 118 | /** 119 | * _create_connection 120 | * 121 | * @param config {ConnectionConfig} 122 | * 123 | * @returns Connection 124 | */ 125 | private _create_connection; 126 | /** 127 | * model 128 | * 129 | * @param schema {Schema} 130 | * 131 | * @returns Model 132 | */ 133 | model(schema: Schema): Model; 134 | /** 135 | * connect 136 | * 137 | * @param config {ConnectionConfig} 138 | * 139 | * @returns void 140 | */ 141 | connect(config: ConnectionConfig): Connection; 142 | /** 143 | * query 144 | * 145 | * @param params {QueryParams} 146 | * 147 | * @returns Promise 148 | */ 149 | query(params: QueryParams): Promise; 150 | /** 151 | * mutate 152 | * 153 | * @param mutation {string} 154 | * 155 | * @returns Promise 156 | */ 157 | mutate(mutation: string): Promise; 158 | } 159 | declare const _default: DgraphORM; 160 | export default _default; 161 | -------------------------------------------------------------------------------- /lib/helpers/utility.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"utility.js","sourceRoot":"","sources":["../../src/helpers/utility.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;AAEH,kDAA4B;AAC5B,sDAAgC;AAChC,wDAAkC;AAGlC;;;;;;GAMG;AACU,QA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AAE,CAAC;KACX;IAED,KAAe,UAAG,EAAH,WAAG,EAAH,iBAAG,EAAH,IAAG,EAAE;QAAhB,IAAI,GAAG,YAAA;QACT,IAAG,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,WAAW,EAAE;YAC7D,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;SACtB;KACF;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAA;AAED;;;;;;;GAOG;AACU,QAAA,KAAK,GAAa,UAAC,IAAS,EAAE,IAAmB;IAC5D,IAAG,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,WAAW,EAAE;QAC5D,OAAO,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;KACtB;IAED,IAAM,KAAK,GAAQ,EAAE,CAAC;IACtB,IAAI,CAAC,OAAO,CAAC,UAAC,IAAY;QACxB,IAAG,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,WAAW,EAAE;YACpC,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;SAC1B;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC,CAAA"} -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * dgraph-orm 3 | * 4 | * Simplified schema creation, queries and mutations for Dgraph. 5 | * 6 | * @author Ashok Vishwakarma 7 | * 8 | * @uses npm install dgraph-orm 9 | * @docs https://ashokvishwakarma.github.io/dgraph-orm 10 | * 11 | */ 12 | 13 | /** 14 | * Operation 15 | * 16 | * from dgraph-js to perform database operation 17 | * 18 | * https://www.npmjs.com/package/dgraph-js 19 | */ 20 | import { Operation } from 'dgraph-js'; 21 | 22 | /** 23 | * Mutation 24 | * 25 | * from dgraph-orm for mutation 26 | */ 27 | import { Mutation } from 'dgraph-js/generated/api_pb'; 28 | 29 | /** 30 | * Schema 31 | * 32 | * dgraph-orm Schema class 33 | */ 34 | import Schema from './schema'; 35 | 36 | /** 37 | * Types 38 | * 39 | * dgraph-orm feilds Types 40 | */ 41 | import Types from './helpers/types'; 42 | 43 | /** 44 | * Connection 45 | * 46 | * dgraph-orm Connection class 47 | */ 48 | import Connection from './connection'; 49 | 50 | /** 51 | * Model 52 | * 53 | * dgraph-orm Model class 54 | */ 55 | import Model from './model'; 56 | 57 | import { TypesType, SchemaFields, ConnectionConfig, QueryParams } from './types'; 58 | 59 | /** 60 | * DgraphORM 61 | * 62 | * DgraphORM class 63 | */ 64 | class DgraphORM { 65 | 66 | /** 67 | * _logger 68 | * 69 | * @type Function 70 | * Methods for logging 71 | */ 72 | private _logger: Function = () => {}; 73 | 74 | /** 75 | * connection 76 | * 77 | * @type Connection 78 | * connection object 79 | */ 80 | private connection: Connection; 81 | 82 | /** 83 | * _models 84 | * 85 | * @type any 86 | * created models 87 | */ 88 | models: any = {}; 89 | 90 | /** 91 | * Types 92 | * 93 | * @type TypesType 94 | * Field types for dagraph-orm 95 | */ 96 | Types: TypesType = Types; 97 | 98 | /** 99 | * Schema 100 | * 101 | * @type {new(name: string, schema: SchemaFields): Schema} 102 | * 103 | * Schema class 104 | */ 105 | Schema: {new(name: string, schema: SchemaFields): Schema} = Schema; 106 | 107 | /** 108 | * contructor 109 | */ 110 | constructor() { 111 | this.connection = this._create_connection(); 112 | } 113 | 114 | /** 115 | * disconnect 116 | * 117 | * @returns void 118 | * 119 | * disconnect the dgraph connection 120 | */ 121 | disconnect(): void { 122 | this.connection.close(); 123 | } 124 | 125 | /** 126 | * logging 127 | * @param callback {Function} 128 | * 129 | * @returns void 130 | */ 131 | logging(callback: Function): void { 132 | this._logger = callback; 133 | } 134 | 135 | /** 136 | * _log 137 | * 138 | * @param message {string} 139 | * 140 | * @returns void 141 | */ 142 | private _log(message: string): void { 143 | this._logger(message); 144 | } 145 | 146 | 147 | /** 148 | * _set_model 149 | * 150 | * @param schema {Schema} 151 | * 152 | * @returns void 153 | */ 154 | private _set_model(schema: Schema): void { 155 | if(schema.name && typeof this.models[schema.name] === 'undefined') { 156 | this.models[schema.name] = schema.original; 157 | this._generate_schema(schema.schema); 158 | } 159 | } 160 | 161 | /** 162 | * _generate_schema 163 | * 164 | * @param schema {Array} 165 | * 166 | * @returns void 167 | */ 168 | async _generate_schema(schema: Array): Promise { 169 | const op: Operation = new this.connection.dgraph.Operation(); 170 | op.setSchema(schema.join("\n")); 171 | this.connection.client.alter(op).catch(error => {}); 172 | } 173 | 174 | /** 175 | * _create_connection 176 | * 177 | * @param config {ConnectionConfig} 178 | * 179 | * @returns Connection 180 | */ 181 | private _create_connection(config: ConnectionConfig = null): Connection { 182 | return new Connection(config, this._log.bind(this)); 183 | } 184 | 185 | /** 186 | * model 187 | * 188 | * @param schema {Schema} 189 | * 190 | * @returns Model 191 | */ 192 | model(schema: Schema): Model { 193 | this._set_model(schema); 194 | 195 | return new Model(schema, this.models, this.connection, this._log.bind(this)); 196 | } 197 | 198 | /** 199 | * connect 200 | * 201 | * @param config {ConnectionConfig} 202 | * 203 | * @returns void 204 | */ 205 | connect(config: ConnectionConfig): Connection { 206 | this.connection = this._create_connection(config); 207 | return this.connection; 208 | } 209 | 210 | /** 211 | * query 212 | * 213 | * @param params {QueryParams} 214 | * 215 | * @returns Promise 216 | */ 217 | query(params: QueryParams): Promise { 218 | return this.connection.client 219 | .newTxn() 220 | .queryWithVars( 221 | params.query, 222 | params.variables 223 | ); 224 | } 225 | 226 | /** 227 | * mutate 228 | * 229 | * @param mutation {string} 230 | * 231 | * @returns Promise 232 | */ 233 | mutate(mutation: string): Promise { 234 | const mu: Mutation = new this.connection.dgraph.Mutation(); 235 | mu.setSetJson(mutation); 236 | return this.connection.client.newTxn() 237 | .mutate(mu); 238 | } 239 | } 240 | 241 | export default new DgraphORM(); -------------------------------------------------------------------------------- /lib/model.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Model 3 | * 4 | * dgraph-orm Model class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | /** 9 | * Schema 10 | * 11 | * dgraph-orm Schema class 12 | */ 13 | import Schema from './schema'; 14 | /** 15 | * Connection 16 | * 17 | * dgraph-orm Connection class 18 | */ 19 | import Connection from './connection'; 20 | import { QueryParams, RelationParam } from './types'; 21 | /** 22 | * Model 23 | * 24 | * Class Model 25 | */ 26 | declare class Model { 27 | /** 28 | * index type support 29 | */ 30 | [index: string]: any; 31 | /** 32 | * schema 33 | * 34 | * @type Schema 35 | */ 36 | schema: Schema; 37 | /** 38 | * connection 39 | * 40 | * @type Connection 41 | */ 42 | connection: Connection; 43 | /** 44 | * _models 45 | * 46 | * @type any 47 | */ 48 | private _models; 49 | /** 50 | * _logger 51 | * 52 | * @type Function 53 | */ 54 | private _logger; 55 | /** 56 | * contructor 57 | * @param schema {Schema} 58 | * @param models {any} 59 | * @param connection {Connection} 60 | * @param logger {Function} 61 | */ 62 | constructor(schema: Schema, models: any, connection: Connection, logger: Function); 63 | relation(uid: string, params: RelationParam): Promise; 64 | /** 65 | * _check_if_password_type 66 | * 67 | * @param field {string} 68 | * 69 | * @returns boolean 70 | */ 71 | private _check_if_password_type; 72 | /** 73 | * checkPassword 74 | * @param uid {string} 75 | * @param field {string} 76 | * @param password {string} 77 | * 78 | * @returns Promise 79 | */ 80 | checkPassword(uid: string, field: string, password: string): Promise; 81 | /** 82 | * _generate_methods 83 | * 84 | * @returns void 85 | */ 86 | private _generate_methods; 87 | /** 88 | * _execute 89 | * @param query {string} 90 | * 91 | * @returns Promise 92 | */ 93 | private _execute; 94 | /** 95 | * _method 96 | * @param type {string} 97 | * @param field {any} 98 | * @param value {any} 99 | * @param params {any} 100 | * 101 | * @returns Promise 102 | */ 103 | private _method; 104 | /** 105 | * query 106 | * @param query {string} 107 | * 108 | * @returns Promise 109 | */ 110 | query(query: string): Promise; 111 | /** 112 | * queryWithVars 113 | * @param params {QueryParams} 114 | * 115 | * @returns Promise 116 | */ 117 | queryWithVars(params: QueryParams): Promise; 118 | /** 119 | * _is_relation 120 | * @param _key {string} 121 | * 122 | * @returns boolean 123 | */ 124 | private _is_relation; 125 | /** 126 | * _parse_mutation 127 | * @param mutation {any} 128 | * @param name {string} 129 | * 130 | * @returns {[index: string]: any} 131 | */ 132 | private _parse_mutation; 133 | /** 134 | * _create 135 | * @param mutation {any} 136 | * 137 | * @returns Promise 138 | */ 139 | private _create; 140 | /** 141 | * create 142 | * @param data {any} 143 | * 144 | * @returns Promise 145 | */ 146 | create(data: any): Promise; 147 | /** 148 | * _update 149 | * @param mutation {any} 150 | * @param uid {any} 151 | * 152 | * @returns Promise 153 | */ 154 | private _update; 155 | /** 156 | * update 157 | * @param data {any} 158 | * @param uid {any} 159 | * 160 | * @returns Promise 161 | */ 162 | update(data: any, uid: any): Promise; 163 | /** 164 | * _delete 165 | * @param mutation {any} 166 | * 167 | * @returns Promise 168 | */ 169 | private _delete; 170 | /** 171 | * delete 172 | * @param params {any} 173 | * @param uid {any} 174 | * 175 | * @returns Promise 176 | */ 177 | delete(params: any, uid?: any): Promise; 178 | /** 179 | * _get_unique_fields 180 | * 181 | * @returns Array 182 | */ 183 | private _get_unique_fields; 184 | /** 185 | * _check_unique_values 186 | * @param mutation {any} 187 | * @param _txn {any} 188 | * 189 | * @returns Promise 190 | */ 191 | private _check_unique_values; 192 | /** 193 | * _lang_fields 194 | * @param original {any} 195 | * 196 | * @returns Array 197 | */ 198 | private _lang_fields; 199 | /** 200 | * _check_attributes 201 | * @param original {any} 202 | * @param attributes {any} 203 | * @param isUpdate {boolean} 204 | * @param isRelation {boolean} 205 | * 206 | * @returs void 207 | */ 208 | private _check_attributes; 209 | /** 210 | * _all_attributes 211 | * @param original {any} 212 | * 213 | * @return Array 214 | */ 215 | private _all_attributes; 216 | /** 217 | * _validate 218 | * @param original {any} 219 | * @param params {any} 220 | * 221 | * @returns Params 222 | */ 223 | private _validate; 224 | } 225 | export default Model; 226 | -------------------------------------------------------------------------------- /docs/schema.md: -------------------------------------------------------------------------------- 1 | The complete `dgraph-orm` guide on Schema 2 | 3 | ## Types 4 | `dgraph-orm` suports all the types from Dgraph scaler and UID 5 | 6 | ### Scaler Type 7 | For all triples with a predicate of scalar types the object is a literal. 8 | 9 | |dgraph-orm|Dgraph Schema Type|Uses| 10 | |:---|:---|:---| 11 | |`dgraph.Types.INT`|`int`|to stores integer values in 64 bit format| 12 | |`dgraph.Types.STRING`|`string`|to stores string values| 13 | |`dgraph.Types.FLOAT`|`float`|to store floating points| 14 | |`dgraph.Types.BOOL`|`bool`| to store boolean values| 15 | |`dgraph.Types.DATETIME`|`dateTime`|to store date and time in RFC3339 format| 16 | |`dgraph.Types.GEO`|`geo`|to store geo cordinates in go-gem format| 17 | |`dgraph.Types.PASSWORD`|`password`|to store password in encrypted format| 18 | 19 | *Dgraph supports date and time formats for dateTime scalar type only if they are RFC 3339 compatible which is different from ISO 8601(as defined in the RDF spec). You should convert your values to RFC 3339 format before sending them to Dgraph.* 20 | 21 | ### UID Type 22 | The `uid` type denotes a node-node edge; internally each node is represented as a `uint64` id. 23 | 24 | |dgraph-orm|Dgraph Schema Type|Uses| 25 | |:---|:---|:---| 26 | |`dgraph.Types.UID`|`uid`|to stores integer values in 64 bit format| 27 | 28 | ## Defining Schema 29 | `dgraph-orm` schema definition is very similar to mongoose ORM 30 | 31 | ```javascript 32 | import { Schema } from 'dgraph-orm'; 33 | 34 | /** 35 | * Schema 36 | * 37 | * @param name {string} 38 | * @param fields {SchemaFields} 39 | * @returns Schema 40 | */ 41 | const UserSchema = new Schema('user', { 42 | name: { 43 | type: dgraph.Types.STRING, 44 | index: true, 45 | token: { 46 | term: true 47 | } 48 | }, 49 | email: { 50 | type: dgraph.Types.STRING, 51 | index: true, 52 | unique: true, 53 | token: { 54 | exact: true 55 | } 56 | }, 57 | password: dgraph.Types.PASSWORD, 58 | bio: dgraph.Types.STRING, 59 | friend: { 60 | type: dgraph.Types.UID, 61 | model: 'user', // related model name 62 | count: true, 63 | reverse: true 64 | } 65 | }); 66 | ``` 67 | 68 | ## Schema Fields 69 | 70 | `dgraph-orm` supports all the indexes and toknizers available in Dgraph. 71 | 72 | *`upsert` requires `index` property* 73 | 74 | *`count` requires `list` property* 75 | 76 | ### INT 77 | 78 | Supports `list`, `count`, `index` and `upsert` properties. 79 | 80 | ```javascript 81 | import { Schema, Types } from 'dgraph-orm'; 82 | 83 | const SampleSchema = new Schema('sample', { 84 | phone: { 85 | type: Types.INT, 86 | list: true, 87 | count: true, 88 | index: true, 89 | upsert: true 90 | } 91 | }) 92 | ``` 93 | 94 | ### String 95 | Supports `list`, `count`, `lang`, `index`, `upsert`, `token`, `unique` properies and `exact`, `hash`, `term`, `fulltext`, `trigram` tokenizers. 96 | 97 | *`exact` and `hash` tokenizers should not be used along side `term` tokenizers.* 98 | 99 | *`lang` can be used to define values with language* 100 | 101 | ```javascript 102 | import {Schema, Types} from 'dgraph-orm'; 103 | 104 | const SampleSchema = new Schema('sample', { 105 | email: { 106 | type: Types.STRING, 107 | unique: true, 108 | count: true, 109 | index: true, 110 | upsert: true, 111 | token: { 112 | exact: true, 113 | fulltext: true, 114 | trigram: true 115 | } 116 | } 117 | }); 118 | ``` 119 | 120 | ### FLOAT 121 | 122 | Supports `list`, `count`, `index` and `upsert` properties. 123 | 124 | ```javascript 125 | import { Schema, Types } from 'dgraph-orm'; 126 | 127 | const SampleSchema = new Schema('sample', { 128 | price: { 129 | type: Types.FLOAT, 130 | list: true, 131 | count: true, 132 | index: true, 133 | upsert: true 134 | } 135 | }) 136 | ``` 137 | 138 | ### BOOL 139 | 140 | Supports `list`, `count`, `index` and `upsert` properties. 141 | 142 | ```javascript 143 | import { Schema, Types } from 'dgraph-orm'; 144 | 145 | const SampleSchema = new Schema('sample', { 146 | isActive: { 147 | type: Types.BOOL, 148 | list: true, 149 | count: true, 150 | index: true, 151 | upsert: true 152 | } 153 | }) 154 | ``` 155 | 156 | ### DATETIME 157 | 158 | Supports `list`, `count`, `index`, `upsert` and `token` properties with anyone tokenizer from `year`, `month`, `day`, `hour`. 159 | 160 | ```javascript 161 | import { Schema, Types } from 'dgraph-orm'; 162 | 163 | const SampleSchema = new Schema('sample', { 164 | createdAt: { 165 | type: Types.DATETIME, 166 | list: true, 167 | count: true, 168 | index: true, 169 | upsert: true, 170 | token: 'day' 171 | } 172 | }) 173 | ``` 174 | 175 | ### GEO 176 | 177 | Supports `list`, `count`, `index` and `upsert` properties. 178 | 179 | ```javascript 180 | import { Schema, Types } from 'dgraph-orm'; 181 | 182 | const SampleSchema = new Schema('sample', { 183 | geo: { 184 | type: Types.BOOL, 185 | list: true, 186 | count: true, 187 | index: true, 188 | upsert: true 189 | } 190 | }) 191 | ``` 192 | 193 | ### PASSWORD 194 | 195 | Does not supports any properties. 196 | 197 | ```javascript 198 | import { Schema, Types } from 'dgraph-orm'; 199 | 200 | const SampleSchema = new Schema('sample', { 201 | password: Types.PASSWORD 202 | }) 203 | ``` 204 | 205 | ### UID 206 | Supports `count` and `reverse` properties 207 | 208 | ```javascript 209 | 210 | import {Schema, Types} from 'dgraph-orm'; 211 | 212 | const SampleSchema = new Schema('sample', { 213 | friend: { 214 | type: Types.UID, 215 | count: true, 216 | reverse: true, 217 | model: 'user' // related model name 218 | } 219 | }) 220 | ``` 221 | -------------------------------------------------------------------------------- /src/helpers/utility.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * utility 3 | * 4 | * dgraph-orm utilities 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | import types from './types'; 10 | import typemap from './typemap'; 11 | import tokenmap from './tokenmap'; 12 | import { FieldProps } from '../types'; 13 | 14 | /** 15 | * checkOptions 16 | * @param name {string} 17 | * @param options {string | FieldProps} 18 | * 19 | * @returns void 20 | */ 21 | export const checkOptions = (name: string, options: string | FieldProps): void => { 22 | Object.keys(options).forEach((key: string) => { 23 | if(key === 'type' || typeof options === 'string') return; 24 | 25 | if(typemap[options.type].indexOf(key) === -1) { 26 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', ' + key + ' is not allowed.'); 27 | } 28 | }); 29 | 30 | if(typeof options === 'string') { 31 | return; 32 | } 33 | 34 | if(options.type === types.UID && !options.model) { 35 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', model is required.'); 36 | } 37 | 38 | if(options.count && (options.type !== types.UID && !options.list)) { 39 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', count requires list.'); 40 | } 41 | 42 | if(!options.index && options.token) { 43 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', token requires index.'); 44 | } 45 | 46 | if(!options.token && options.index && (options.type === types.DATETIME || options.type === types.STRING)) { 47 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', index requires token.'); 48 | } 49 | 50 | if(options.type === types.DATETIME && options.index && typeof options.token !== 'string') { 51 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', token must be a string.'); 52 | } 53 | 54 | if(options.type === types.DATETIME && options.index && typeof options.token === 'string' && tokenmap[types.DATETIME].indexOf(options.token) === -1) { 55 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', token must be any one of ' + tokenmap[types.DATETIME].join(', ') + '.'); 56 | } 57 | 58 | if(options.token && typeof options.token !== 'string' && options.type !== types.DATETIME) { 59 | if(options.token.exact && options.token.hash) { 60 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', exact and hash index types shouldn\'t be used together.'); 61 | } 62 | 63 | if(options.token.exact && options.token.term) { 64 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', exact or hash index types shouldn\'t be used alongside the term index type.'); 65 | } 66 | 67 | if(options.token.hash && options.token.term) { 68 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', exact or hash index types shouldn\'t be used alongside the term index type.'); 69 | } 70 | 71 | Object.keys(options.token).forEach((key: string) => { 72 | if(tokenmap[options.type].indexOf(key) === -1) { 73 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', for token only ' + tokenmap[options.type].join(', ') + ' are allowd.'); 74 | } 75 | }); 76 | } 77 | } 78 | 79 | /** 80 | * prepareSchema 81 | * @param name {string} 82 | * @param options {string | FieldProps} 83 | * 84 | * @returns string 85 | */ 86 | export const prepareSchema = (name: string, options: string | FieldProps): string => { 87 | 88 | if(typeof options === 'string') { 89 | return name + ': ' + options + ' .'; 90 | } 91 | 92 | let schema: string = options.type + ' '; 93 | 94 | if(options.list) { 95 | schema = '[' + schema + '] '; 96 | } 97 | 98 | if(options.index) { 99 | if(options.type !== types.STRING && options.type !== types.DATETIME) { 100 | schema += ' @index(' + options.type + ') '; 101 | }else if(options.type === types.DATETIME) { 102 | schema += ' @index(' + options.token + ') '; 103 | }else { 104 | schema += ' @index(' + Object.keys(options.token).join(', ') + ') '; 105 | } 106 | } 107 | 108 | Object.keys(options).forEach((key: string) => { 109 | if(key === 'type' || key === 'list' || key === 'index' || key === 'token' || key === 'model' || key === 'unique') { 110 | return; 111 | } 112 | 113 | schema += '@' + key + ' '; 114 | }); 115 | 116 | return name + ': ' + schema + '.'; 117 | } 118 | 119 | /** 120 | * pluck 121 | * @param arr Array 122 | * @param key string 123 | * 124 | * @returns Array 125 | */ 126 | export const pluck = (arr: Array, key: string): Array => { 127 | const _data: Array = []; 128 | 129 | if(!Array.isArray(arr)) { 130 | return []; 131 | } 132 | 133 | for(let obj of arr) { 134 | if(typeof obj === 'object' && typeof obj[key] !== 'undefined') { 135 | _data.push(obj[key]); 136 | } 137 | } 138 | 139 | return _data; 140 | } 141 | 142 | /** 143 | * merge 144 | * 145 | * @param data {any} 146 | * @param keys {Array} 147 | * 148 | * @returns any 149 | */ 150 | export const merge: Function = (data: any, keys: Array): any => { 151 | if(keys.length === 1 && typeof data[keys[0]] !== 'undefined') { 152 | return data[keys[0]]; 153 | } 154 | 155 | const _data: any = {}; 156 | keys.forEach((_key: string) => { 157 | if(typeof data[_key] !== 'undefined') { 158 | _data[_key] = data[_key]; 159 | } 160 | }); 161 | 162 | return _data; 163 | } -------------------------------------------------------------------------------- /lib/helpers/utility.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * utility 4 | * 5 | * dgraph-orm utilities 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | var __importDefault = (this && this.__importDefault) || function (mod) { 10 | return (mod && mod.__esModule) ? mod : { "default": mod }; 11 | }; 12 | Object.defineProperty(exports, "__esModule", { value: true }); 13 | var types_1 = __importDefault(require("./types")); 14 | var typemap_1 = __importDefault(require("./typemap")); 15 | var tokenmap_1 = __importDefault(require("./tokenmap")); 16 | /** 17 | * checkOptions 18 | * @param name {string} 19 | * @param options {string | FieldProps} 20 | * 21 | * @returns void 22 | */ 23 | exports.checkOptions = function (name, options) { 24 | Object.keys(options).forEach(function (key) { 25 | if (key === 'type' || typeof options === 'string') 26 | return; 27 | if (typemap_1.default[options.type].indexOf(key) === -1) { 28 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', ' + key + ' is not allowed.'); 29 | } 30 | }); 31 | if (typeof options === 'string') { 32 | return; 33 | } 34 | if (options.type === types_1.default.UID && !options.model) { 35 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', model is required.'); 36 | } 37 | if (options.count && (options.type !== types_1.default.UID && !options.list)) { 38 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', count requires list.'); 39 | } 40 | if (!options.index && options.token) { 41 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', token requires index.'); 42 | } 43 | if (!options.token && options.index && (options.type === types_1.default.DATETIME || options.type === types_1.default.STRING)) { 44 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', index requires token.'); 45 | } 46 | if (options.type === types_1.default.DATETIME && options.index && typeof options.token !== 'string') { 47 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', token must be a string.'); 48 | } 49 | if (options.type === types_1.default.DATETIME && options.index && typeof options.token === 'string' && tokenmap_1.default[types_1.default.DATETIME].indexOf(options.token) === -1) { 50 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', token must be any one of ' + tokenmap_1.default[types_1.default.DATETIME].join(', ') + '.'); 51 | } 52 | if (options.token && typeof options.token !== 'string' && options.type !== types_1.default.DATETIME) { 53 | if (options.token.exact && options.token.hash) { 54 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', exact and hash index types shouldn\'t be used together.'); 55 | } 56 | if (options.token.exact && options.token.term) { 57 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', exact or hash index types shouldn\'t be used alongside the term index type.'); 58 | } 59 | if (options.token.hash && options.token.term) { 60 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', exact or hash index types shouldn\'t be used alongside the term index type.'); 61 | } 62 | Object.keys(options.token).forEach(function (key) { 63 | if (tokenmap_1.default[options.type].indexOf(key) === -1) { 64 | throw new Error('[Type Error]: In ' + name + ' of type ' + options.type.toUpperCase() + ', for token only ' + tokenmap_1.default[options.type].join(', ') + ' are allowd.'); 65 | } 66 | }); 67 | } 68 | }; 69 | /** 70 | * prepareSchema 71 | * @param name {string} 72 | * @param options {string | FieldProps} 73 | * 74 | * @returns string 75 | */ 76 | exports.prepareSchema = function (name, options) { 77 | if (typeof options === 'string') { 78 | return name + ': ' + options + ' .'; 79 | } 80 | var schema = options.type + ' '; 81 | if (options.list) { 82 | schema = '[' + schema + '] '; 83 | } 84 | if (options.index) { 85 | if (options.type !== types_1.default.STRING && options.type !== types_1.default.DATETIME) { 86 | schema += ' @index(' + options.type + ') '; 87 | } 88 | else if (options.type === types_1.default.DATETIME) { 89 | schema += ' @index(' + options.token + ') '; 90 | } 91 | else { 92 | schema += ' @index(' + Object.keys(options.token).join(', ') + ') '; 93 | } 94 | } 95 | Object.keys(options).forEach(function (key) { 96 | if (key === 'type' || key === 'list' || key === 'index' || key === 'token' || key === 'model' || key === 'unique') { 97 | return; 98 | } 99 | schema += '@' + key + ' '; 100 | }); 101 | return name + ': ' + schema + '.'; 102 | }; 103 | /** 104 | * pluck 105 | * @param arr Array 106 | * @param key string 107 | * 108 | * @returns Array 109 | */ 110 | exports.pluck = function (arr, key) { 111 | var _data = []; 112 | if (!Array.isArray(arr)) { 113 | return []; 114 | } 115 | for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) { 116 | var obj = arr_1[_i]; 117 | if (typeof obj === 'object' && typeof obj[key] !== 'undefined') { 118 | _data.push(obj[key]); 119 | } 120 | } 121 | return _data; 122 | }; 123 | /** 124 | * merge 125 | * 126 | * @param data {any} 127 | * @param keys {Array} 128 | * 129 | * @returns any 130 | */ 131 | exports.merge = function (data, keys) { 132 | if (keys.length === 1 && typeof data[keys[0]] !== 'undefined') { 133 | return data[keys[0]]; 134 | } 135 | var _data = {}; 136 | keys.forEach(function (_key) { 137 | if (typeof data[_key] !== 'undefined') { 138 | _data[_key] = data[_key]; 139 | } 140 | }); 141 | return _data; 142 | }; 143 | //# sourceMappingURL=utility.js.map -------------------------------------------------------------------------------- /lib/query.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";AACA;;;;;;GAMG;;;;;AAEH;;;;GAIG;AACH,8DAAwC;AAIxC;;;;GAIG;AACH,IAAM,WAAW,GAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAEnD;;;;GAIG;AACH;IA0DE;;;;;;;;OAQG;IACH,eAAY,IAAY,EAAE,KAAa,EAAE,KAAU,EAAE,MAAc,EAAE,IAAY,EAAE,MAAgB;QACjG,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,C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,oBAC/H,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC,IAAI,CAAC,kBAC9C,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,qBAEjC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAEnB,OAAO,KAAK,CAAC;IACf,CAAC;IACH,YAAC;AAAD,CAAC,AA5WD,IA4WC;AAED,kBAAe,KAAK,CAAC"} -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * dgraph-orm 4 | * 5 | * Simplified schema creation, queries and mutations for Dgraph. 6 | * 7 | * @author Ashok Vishwakarma 8 | * 9 | * @uses npm install dgraph-orm 10 | * @docs https://ashokvishwakarma.github.io/dgraph-orm 11 | * 12 | */ 13 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 14 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 15 | return new (P || (P = Promise))(function (resolve, reject) { 16 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } 17 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } 18 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } 19 | step((generator = generator.apply(thisArg, _arguments || [])).next()); 20 | }); 21 | }; 22 | var __generator = (this && this.__generator) || function (thisArg, body) { 23 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; 24 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; 25 | function verb(n) { return function (v) { return step([n, v]); }; } 26 | function step(op) { 27 | if (f) throw new TypeError("Generator is already executing."); 28 | while (_) try { 29 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; 30 | if (y = 0, t) op = [op[0] & 2, t.value]; 31 | switch (op[0]) { 32 | case 0: case 1: t = op; break; 33 | case 4: _.label++; return { value: op[1], done: false }; 34 | case 5: _.label++; y = op[1]; op = [0]; continue; 35 | case 7: op = _.ops.pop(); _.trys.pop(); continue; 36 | default: 37 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } 38 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } 39 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } 40 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } 41 | if (t[2]) _.ops.pop(); 42 | _.trys.pop(); continue; 43 | } 44 | op = body.call(thisArg, _); 45 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } 46 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; 47 | } 48 | }; 49 | var __importDefault = (this && this.__importDefault) || function (mod) { 50 | return (mod && mod.__esModule) ? mod : { "default": mod }; 51 | }; 52 | Object.defineProperty(exports, "__esModule", { value: true }); 53 | /** 54 | * Schema 55 | * 56 | * dgraph-orm Schema class 57 | */ 58 | var schema_1 = __importDefault(require("./schema")); 59 | /** 60 | * Types 61 | * 62 | * dgraph-orm feilds Types 63 | */ 64 | var types_1 = __importDefault(require("./helpers/types")); 65 | /** 66 | * Connection 67 | * 68 | * dgraph-orm Connection class 69 | */ 70 | var connection_1 = __importDefault(require("./connection")); 71 | /** 72 | * Model 73 | * 74 | * dgraph-orm Model class 75 | */ 76 | var model_1 = __importDefault(require("./model")); 77 | /** 78 | * DgraphORM 79 | * 80 | * DgraphORM class 81 | */ 82 | var DgraphORM = /** @class */ (function () { 83 | /** 84 | * contructor 85 | */ 86 | function DgraphORM() { 87 | /** 88 | * _logger 89 | * 90 | * @type Function 91 | * Methods for logging 92 | */ 93 | this._logger = function () { }; 94 | /** 95 | * _models 96 | * 97 | * @type any 98 | * created models 99 | */ 100 | this.models = {}; 101 | /** 102 | * Types 103 | * 104 | * @type TypesType 105 | * Field types for dagraph-orm 106 | */ 107 | this.Types = types_1.default; 108 | /** 109 | * Schema 110 | * 111 | * @type {new(name: string, schema: SchemaFields): Schema} 112 | * 113 | * Schema class 114 | */ 115 | this.Schema = schema_1.default; 116 | this.connection = this._create_connection(); 117 | } 118 | /** 119 | * disconnect 120 | * 121 | * @returns void 122 | * 123 | * disconnect the dgraph connection 124 | */ 125 | DgraphORM.prototype.disconnect = function () { 126 | this.connection.close(); 127 | }; 128 | /** 129 | * logging 130 | * @param callback {Function} 131 | * 132 | * @returns void 133 | */ 134 | DgraphORM.prototype.logging = function (callback) { 135 | this._logger = callback; 136 | }; 137 | /** 138 | * _log 139 | * 140 | * @param message {string} 141 | * 142 | * @returns void 143 | */ 144 | DgraphORM.prototype._log = function (message) { 145 | this._logger(message); 146 | }; 147 | /** 148 | * _set_model 149 | * 150 | * @param schema {Schema} 151 | * 152 | * @returns void 153 | */ 154 | DgraphORM.prototype._set_model = function (schema) { 155 | if (schema.name && typeof this.models[schema.name] === 'undefined') { 156 | this.models[schema.name] = schema.original; 157 | this._generate_schema(schema.schema); 158 | } 159 | }; 160 | /** 161 | * _generate_schema 162 | * 163 | * @param schema {Array} 164 | * 165 | * @returns void 166 | */ 167 | DgraphORM.prototype._generate_schema = function (schema) { 168 | return __awaiter(this, void 0, void 0, function () { 169 | var op; 170 | return __generator(this, function (_a) { 171 | op = new this.connection.dgraph.Operation(); 172 | op.setSchema(schema.join("\n")); 173 | this.connection.client.alter(op).catch(function (error) { }); 174 | return [2 /*return*/]; 175 | }); 176 | }); 177 | }; 178 | /** 179 | * _create_connection 180 | * 181 | * @param config {ConnectionConfig} 182 | * 183 | * @returns Connection 184 | */ 185 | DgraphORM.prototype._create_connection = function (config) { 186 | if (config === void 0) { config = null; } 187 | return new connection_1.default(config, this._log.bind(this)); 188 | }; 189 | /** 190 | * model 191 | * 192 | * @param schema {Schema} 193 | * 194 | * @returns Model 195 | */ 196 | DgraphORM.prototype.model = function (schema) { 197 | this._set_model(schema); 198 | return new model_1.default(schema, this.models, this.connection, this._log.bind(this)); 199 | }; 200 | /** 201 | * connect 202 | * 203 | * @param config {ConnectionConfig} 204 | * 205 | * @returns void 206 | */ 207 | DgraphORM.prototype.connect = function (config) { 208 | this.connection = this._create_connection(config); 209 | return this.connection; 210 | }; 211 | /** 212 | * query 213 | * 214 | * @param params {QueryParams} 215 | * 216 | * @returns Promise 217 | */ 218 | DgraphORM.prototype.query = function (params) { 219 | return this.connection.client 220 | .newTxn() 221 | .queryWithVars(params.query, params.variables); 222 | }; 223 | /** 224 | * mutate 225 | * 226 | * @param mutation {string} 227 | * 228 | * @returns Promise 229 | */ 230 | DgraphORM.prototype.mutate = function (mutation) { 231 | var mu = new this.connection.dgraph.Mutation(); 232 | mu.setSetJson(mutation); 233 | return this.connection.client.newTxn() 234 | .mutate(mu); 235 | }; 236 | return DgraphORM; 237 | }()); 238 | exports.default = new DgraphORM(); 239 | //# sourceMappingURL=index.js.map -------------------------------------------------------------------------------- /example/index.ts: -------------------------------------------------------------------------------- 1 | import grpc from 'grpc'; 2 | 3 | import dgraph from '../src'; 4 | 5 | /** 6 | * dgraph.connect 7 | * 8 | * Connect to dgraph instance 9 | * 10 | * @param {objact} { 11 | * host: 'localhost', // dgraph database host default is localhost 12 | * port: 9080, // dgrpah port default is 9080 13 | * credentails: // gprc credential default grpc.credentials.createInsecure() 14 | * } 15 | */ 16 | dgraph.connect({ 17 | /** 18 | * host 19 | * 20 | * Dgraph host 21 | * 22 | * @default 'localhost' 23 | */ 24 | host: 'localhost', 25 | 26 | /** 27 | * port 28 | * 29 | * Dgraph port 30 | * 31 | * @default 9080 32 | */ 33 | port: 9080, 34 | 35 | /** 36 | * credentails 37 | * 38 | * Dgraph database grpc credentials 39 | * 40 | * @default grpc.credentials.createInsecure() 41 | * 42 | */ 43 | credentails: grpc.credentials.createInsecure(), 44 | 45 | /** 46 | * debug 47 | * 48 | * set debug mode 49 | * 50 | * @default false 51 | */ 52 | debug: false 53 | }); 54 | 55 | /** 56 | * logging 57 | * 58 | * Set your own logger Function 59 | */ 60 | dgraph.logging(console.log); 61 | 62 | /** 63 | * dgraph.Schema 64 | * 65 | * Create a Dgraph Schema 66 | * 67 | * @params {string} name // name of the schema 68 | * @params {object} schema // schema options 69 | * 70 | * in the below code the schame will be generated a nd prefixed with name 71 | * 72 | * user.name string @index(term) 73 | */ 74 | const UserSchema = new dgraph.Schema('user', { 75 | /** 76 | * Generated Dgraph Schema 77 | * 78 | * user.name string @index(term, trigram) . 79 | */ 80 | name: { 81 | type: dgraph.Types.STRING, 82 | index: true, 83 | lang: true, 84 | token: { 85 | term: true, 86 | trigram: true 87 | } 88 | }, 89 | 90 | /** 91 | * Generated Dgraph schema 92 | * 93 | * user.password password . 94 | */ 95 | password: dgraph.Types.PASSWORD, 96 | 97 | /** 98 | * Generated Dgraph Schema 99 | * 100 | * user.email string @index(excat) . 101 | */ 102 | email: { 103 | type: dgraph.Types.STRING, 104 | index: true, 105 | unique: true, 106 | token: { 107 | exact: true 108 | } 109 | }, 110 | /** 111 | * Generated Dgraph Schema 112 | * 113 | * user.bio string . 114 | */ 115 | bio: dgraph.Types.STRING, 116 | 117 | /** 118 | * GeneratedDgraph schema 119 | * user.age int @index(int) 120 | */ 121 | age: { 122 | type: dgraph.Types.INT, 123 | index: true 124 | }, 125 | 126 | /** 127 | * Generated Dgraph Schema 128 | * 129 | * user.friend uid @count @reverse 130 | */ 131 | friend: { 132 | type: dgraph.Types.UID, 133 | model: 'user', 134 | count: true 135 | }, 136 | avatar: { 137 | type: dgraph.Types.UID, 138 | model: 'media', 139 | replace: true 140 | } 141 | }); 142 | 143 | /** 144 | * dgraph.model 145 | * 146 | * Generate the Dgraph model 147 | */ 148 | const User = dgraph.model(UserSchema); 149 | 150 | const PostSchema = new dgraph.Schema('post', { 151 | title: dgraph.Types.STRING, 152 | content: dgraph.Types.STRING, 153 | author: { 154 | type: dgraph.Types.UID, 155 | model: 'user', 156 | replace: true 157 | }, 158 | banner: { 159 | type: dgraph.Types.UID, 160 | model: 'media', 161 | replace: true 162 | } 163 | }); 164 | 165 | const Post = dgraph.model(PostSchema); 166 | 167 | const MediaSchema = new dgraph.Schema('media', { 168 | type: dgraph.Types.STRING, 169 | src: dgraph.Types.STRING 170 | }); 171 | 172 | const Media = dgraph.model(MediaSchema); 173 | 174 | /** 175 | * @example 176 | * 177 | * User.has(field_name: String, options); 178 | * 179 | * User.eq(field_name: String, value: String, options); 180 | * 181 | * User.uid(uid_value: String | String[], options); 182 | * 183 | * User.allofterms(field: String, value: String, options); 184 | * 185 | * User.anyofterms(field: String, value: String, options); 186 | * 187 | * User.anyoftext(field: String, value: String, options); 188 | * 189 | * User.alloftext(field: String, value: String, options); 190 | * 191 | * User.regexp(field: String, regex: String, options); 192 | * 193 | * User.near(field: String, value = { 194 | * latitude: float, 195 | * longitude: float 196 | * distance: number 197 | * }, options); 198 | * 199 | * User.contains(field: String, value = { 200 | * latitude: float, 201 | * longitude: float 202 | * }, options); 203 | */ 204 | 205 | (async () => { 206 | // const user = await User.create({ 207 | // name: 'Someone', 208 | // email: 'someone@gmail.com', 209 | // bio: 'Performance and load tester', 210 | // password: 'p@ssw0rd' 211 | // }); 212 | 213 | // await User.delete(['0x2']); 214 | 215 | // await User.update({ 216 | // 'name@fr': 'Parinita Sharma', 217 | // }, '0x1'); 218 | 219 | // const pari = await User.uid('0x1', { 220 | // first: 1 221 | // }); 222 | // console.log(pari); 223 | 224 | // await User.update({ 225 | // name: 'Parinita Sharma', 226 | // bio: 'Co Founder and COO, Impulsive Web Pvt. Ltd.' 227 | // }, '0x8'); 228 | 229 | // await User.delete('0x8'); 230 | 231 | // console.log(user); 232 | 233 | // const data = await User.has('name'); 234 | 235 | // console.log(data); 236 | 237 | // const _check = await User.checkPassword('0x1', 'password', 'p@ssw0rd'); 238 | 239 | // console.log(_check); 240 | 241 | // await User.update({ 242 | // friend: ['0x1'] 243 | // }, '0x13881'); 244 | 245 | // await User.delete({ 246 | // friend: ['0x271c', '0x2711'] 247 | // }, '0x1'); 248 | 249 | // const users = await User.has('name', { 250 | // include: { 251 | // friend: { 252 | // as: 'friends' 253 | // } 254 | // } 255 | // }); 256 | 257 | // await User.delete({ 258 | // name: null 259 | // }, ['0x271c', '0x2711']); 260 | 261 | // await User.update({ 262 | // age: 32 263 | // }, '0x271c'); 264 | 265 | // const users = await User.has('email'); 266 | 267 | // const posts = await Post.has('title'); 268 | 269 | // console.log(users); 270 | // console.log(posts); 271 | 272 | // await Media.create({ 273 | // type: 'image', 274 | // src: 'https://miro.medium.com/max/3150/1*RJuEp_08DylEspADBgsHoQ.jpeg' 275 | // }); 276 | 277 | // const _isDeleted = await Media.has('src'); 278 | 279 | // console.log(_isDeleted); 280 | 281 | // const user = await User.relation('0x2711', { 282 | // field: 'friend', 283 | // attributes: { 284 | // friend: ['uid', 'name'] 285 | // } 286 | // }); 287 | 288 | // console.log(user); 289 | 290 | // await Post.delete({ 291 | // author: '0x1' 292 | // }, '0xc351'); 293 | 294 | // await Post.update({ 295 | // author: '0x1' 296 | // }, '0x9c42'); 297 | 298 | // const post = await Post.create({ 299 | // title: 'A new sample post', 300 | // content: '

Sample content

', 301 | // author: '0x1' 302 | // }) 303 | 304 | // console.log(post); 305 | 306 | // await User.delete({ 307 | // name: null 308 | // }, '0xea63'); 309 | 310 | // const user = await User.has('friend', { 311 | // include: { 312 | // friend: { 313 | // as: 'friends', 314 | // include: { 315 | // friend: { 316 | // as: 'friends', 317 | // include: { 318 | // friend: { 319 | // as: 'friends', 320 | // } 321 | // } 322 | // } 323 | // } 324 | // } 325 | // } 326 | // }); 327 | 328 | // console.log(user); 329 | 330 | // await Media.create({ 331 | // src: 'myname.jpg', 332 | // type :'image' 333 | // }); 334 | 335 | // await User.update({ 336 | // avatar: '0x7533' 337 | // }, '0x1'); 338 | 339 | // const posts = await Post.has('') 340 | 341 | // await Post.update({ 342 | // banner: '0x7533' 343 | // }, '0xc353'); 344 | 345 | const posts = await Post.uid('0x9c41', { 346 | include: { 347 | author: { 348 | as: 'author', 349 | include: { 350 | friend: { 351 | as: 'friends' 352 | }, 353 | avatar: { 354 | as: 'avatar' 355 | } 356 | }, 357 | }, 358 | banner: { 359 | as: 'banner' 360 | } 361 | } 362 | }); 363 | 364 | console.log(posts); 365 | 366 | })(); -------------------------------------------------------------------------------- /src/query.ts: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Query 4 | * 5 | * dgraph-orm Query class 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | 10 | /** 11 | * methods 12 | * 13 | * dgraph-orm model methods 14 | */ 15 | import methods from './helpers/methods'; 16 | 17 | import { Params, Include } from './types'; 18 | 19 | /** 20 | * _conditions 21 | * 22 | * @type Array 23 | */ 24 | const _conditions: Array = ['$or', '$and']; 25 | 26 | /** 27 | * Query 28 | * 29 | * Class Query 30 | */ 31 | class Query { 32 | 33 | /** 34 | * name 35 | * 36 | * @type string 37 | */ 38 | private name: string; 39 | 40 | /** 41 | * params 42 | * 43 | * @type Params 44 | */ 45 | private params: Params; 46 | 47 | /** 48 | * type 49 | * 50 | * @type string 51 | */ 52 | private type: string; 53 | 54 | /** 55 | * field 56 | * 57 | * @type string 58 | */ 59 | private field: string; 60 | 61 | /** 62 | * value 63 | * 64 | * @type any 65 | */ 66 | private value: any; 67 | 68 | /** 69 | * logger 70 | * 71 | * @type Function 72 | */ 73 | private logger: Function; 74 | 75 | /** 76 | * where 77 | * 78 | * @type any 79 | */ 80 | private where: any; 81 | 82 | /** 83 | * query 84 | * 85 | * @type string 86 | */ 87 | query: string; 88 | 89 | /** 90 | * constructor 91 | * @param type {string} 92 | * @param field {string} 93 | * @param value {any} 94 | * @param params {Params} 95 | * @param name {string} 96 | * @param logger {Function} 97 | */ 98 | constructor(type: string, field: string, value: any, params: Params, name: string, logger: Function) { 99 | this.name = name; 100 | this.params = params; 101 | this.type = type; 102 | this.field = field; 103 | this.value = value; 104 | this.logger = logger; 105 | this.where = this._where(this.type, this.field, this.value, this.name); 106 | this.query = this._build(this.params) 107 | } 108 | 109 | /** 110 | * _where 111 | * @param type {string} 112 | * @param field {string} 113 | * @param value {any} 114 | * @param name {string} 115 | * 116 | * @returns string 117 | */ 118 | private _where(type: string, field: string, value: any, name: string): string { 119 | let _where = ''; 120 | 121 | switch (type) { 122 | case methods.eq: 123 | case methods.allofterms: 124 | case methods.alloftext: 125 | case methods.anyofterms: 126 | case methods.anyoftext: 127 | _where = `(func: ${type}(${name}.${field}, ${'"' + value + '"'}){{ORDER}}{{LIMIT}})`; 128 | break; 129 | 130 | case methods.regexp: 131 | _where = `(func: ${type}(${name}.${field}, ${value}){{ORDER}}{{LIMIT}})`; 132 | break; 133 | 134 | case methods.uid: 135 | if(Array.isArray(field)) { 136 | field = field.join(', '); 137 | } 138 | _where = `(func: ${methods.uid}(${field}){{ORDER}}{{LIMIT}})`; 139 | break; 140 | 141 | case methods.has: 142 | _where = `(func: ${methods.has}(${name}.${field}){{ORDER}}{{LIMIT}})`; 143 | break; 144 | 145 | case methods.near: 146 | _where = `(func: ${methods.near}(${name}.${field}, [${value.longitude}, ${value.latitude}], ${value.distance}){{ORDER}}{{LIMIT}})`; 147 | break; 148 | 149 | case methods.contains: 150 | _where = `(func: ${methods.contains}(${name}.${field}, [${value.longitude}, ${value.latitude}]){{ORDER}}{{LIMIT}})`; 151 | break; 152 | 153 | } 154 | 155 | return _where; 156 | } 157 | 158 | /** 159 | * _filter 160 | * @param key {string} 161 | * @param value {any} 162 | * @param name {string} 163 | * 164 | * @returns string 165 | */ 166 | private _filter(key: string, value: any, name: string): string { 167 | if(key.toLowerCase() === '$has') { 168 | return `${key.replace('$', '')}(${name}.${value})`; 169 | } 170 | 171 | if(typeof value === 'string') { 172 | return `eq(${name}.${key}, "${value}")`; 173 | } 174 | 175 | if(typeof value === 'object' && !Array.isArray(value)) { 176 | const _key = Object.keys(value)[0]; 177 | 178 | if(_key) { 179 | let _value = value[_key]; 180 | 181 | if(Array.isArray(_value)) { 182 | const _sub: Array = []; 183 | _value.forEach((_val: any) => { 184 | _sub.push(`uid_in(${name}.${key}, ${_val})`); 185 | }); 186 | 187 | return _sub.join(' OR '); 188 | } 189 | 190 | if(typeof _value === 'string' && _key !== '$regexp') { 191 | _value = '"' + _value + '"'; 192 | } 193 | 194 | if(_key === '$ne') { 195 | return `NOT eq(${name}.${key}, ${_value})`; 196 | } 197 | 198 | return `${_key.replace('$', '')}(${name}.${key}, ${_value})`; 199 | } 200 | } 201 | } 202 | 203 | /** 204 | * _parse_filter 205 | * @param filter {any} 206 | * @param name {string} 207 | * 208 | * @returns string 209 | */ 210 | private _parse_filter(filter: any, name: string): string { 211 | 212 | const _filters: Array = [] 213 | 214 | if(typeof filter !== 'undefined') { 215 | Object.keys(filter).forEach(_key => { 216 | if(_conditions.indexOf(_key.toLowerCase()) === -1) { 217 | _filters.push(this._filter(_key, filter[_key], name)); 218 | } else { 219 | const _sub: Array = []; 220 | Object.keys(filter[_key]).forEach(_k => { 221 | if(Array.isArray(filter[_key][_k])) { 222 | filter[_key][_k].forEach((_val: any) => { 223 | _sub.push(this._filter(_k, _val, name)); 224 | }) 225 | }else { 226 | _sub.push(this._filter(_k, filter[_key][_k], name)); 227 | } 228 | }); 229 | 230 | if(_sub.length > 0) { 231 | _filters.push(`(${_sub.join(` ${_key.replace('$', '').toUpperCase()} `)})`); 232 | } 233 | } 234 | }); 235 | } 236 | 237 | if(_filters.length > 0) { 238 | return ` @filter(${_filters.join(' AND ')})`; 239 | } 240 | 241 | return ''; 242 | } 243 | 244 | /** 245 | * _attributes 246 | * @param attributes {Array} 247 | * @param name {string} 248 | * 249 | * @return string 250 | */ 251 | private _attributes(attributes: Array, name: string): string { 252 | const _attrs: Array = []; 253 | for(let attr of attributes) { 254 | if(attr === 'uid') { 255 | _attrs.push('uid'); 256 | }else { 257 | _attrs.push(`${attr}: ${name}.${attr}`); 258 | } 259 | } 260 | 261 | return _attrs.join('\n'); 262 | } 263 | 264 | /** 265 | * _include 266 | * @param include {Include} 267 | * 268 | * @returns string 269 | */ 270 | private _include(include: Include, name: string = this.name): string { 271 | let _inc: string = ''; 272 | 273 | if(!include) { 274 | return _inc; 275 | } 276 | 277 | for(let relation of Object.keys(include)) { 278 | 279 | if(include[relation].count) { 280 | _inc += `${include[relation].as ? include[relation].as : relation}: count(${name}.${relation})`; 281 | continue; 282 | } 283 | 284 | _inc += `${include[relation].as ? include[relation].as : relation}: ${name}.${relation}`; 285 | 286 | const _limit: string = this._extras(include[relation]); 287 | const _order: string = this._parse_order(include[relation].order); 288 | 289 | if(include[relation].filter) { 290 | _inc += `${this._parse_filter(include[relation].filter, include[relation].model)}` 291 | } 292 | 293 | if(_limit) { 294 | _inc += ` (${_limit}) `; 295 | } 296 | 297 | if(_order) { 298 | _inc += ` (${_order})`; 299 | } 300 | 301 | _inc += `{ 302 | ${this._attributes(include[relation].attributes, include[relation].model)} 303 | ${this._include(include[relation].include, include[relation].model)} 304 | }` 305 | } 306 | 307 | return _inc; 308 | } 309 | 310 | /** 311 | * _extra 312 | * @param params {Params} 313 | * 314 | * @return string 315 | */ 316 | private _extras(params: Params): string { 317 | let _extra = []; 318 | 319 | if(params.first && typeof params.first === 'number') { 320 | _extra.push(`first: ${params.first}`); 321 | } 322 | 323 | if(params.offset && typeof params.offset === 'number') { 324 | _extra.push(`offset: ${params.offset}`); 325 | } 326 | 327 | if(params.after) { 328 | _extra.push(`after: ${params.after}`); 329 | } 330 | 331 | if(_extra.length > 0) { 332 | return `${_extra.join(', ')}`; 333 | } 334 | 335 | return ''; 336 | } 337 | 338 | /** 339 | * _parse_order 340 | * @param order {Array} 341 | * 342 | * @returns string 343 | */ 344 | private _parse_order(order: Array): string { 345 | const _order: Array = []; 346 | 347 | if(order && order.length > 0) { 348 | if(Array.isArray(order[0])) { 349 | order.forEach((_o: any) => { 350 | if(typeof _o[1] !== 'undefined') { 351 | _order.push(`order${_o[1].toLowerCase()}: ${this.name}.${_o[0]}`); 352 | } 353 | }); 354 | }else { 355 | _order.push(`order${order[1].toLowerCase()}: ${this.name}.${order[0]}`); 356 | } 357 | } 358 | 359 | if(_order.length > 0) { 360 | return `${_order.join(', ')}`; 361 | } 362 | 363 | return ''; 364 | } 365 | 366 | /** 367 | * _build 368 | * @param params {any} 369 | * 370 | * @returns string 371 | */ 372 | private _build(params: any): string { 373 | let _order: string = this._parse_order(params.order); 374 | let _limit: string = this._extras(params); 375 | 376 | if(_order) { 377 | _order = `, ${_order}`; 378 | } 379 | 380 | if(_limit) { 381 | _limit = `, ${_limit}`; 382 | } 383 | 384 | const query: string = `{ 385 | ${this.name} ${this.where.replace('{{ORDER}}', _order).replace('{{LIMIT}}', _limit)} ${this._parse_filter(params.filter, this.name)} { 386 | ${this._attributes(params.attributes, this.name)} 387 | ${this._include(params.include)} 388 | } 389 | }`; 390 | 391 | this.logger(query); 392 | 393 | return query; 394 | } 395 | } 396 | 397 | export default Query; -------------------------------------------------------------------------------- /lib/query.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /** 3 | * Query 4 | * 5 | * dgraph-orm Query class 6 | * 7 | * @author Ashok Vishwakarma 8 | */ 9 | var __importDefault = (this && this.__importDefault) || function (mod) { 10 | return (mod && mod.__esModule) ? mod : { "default": mod }; 11 | }; 12 | Object.defineProperty(exports, "__esModule", { value: true }); 13 | /** 14 | * methods 15 | * 16 | * dgraph-orm model methods 17 | */ 18 | var methods_1 = __importDefault(require("./helpers/methods")); 19 | /** 20 | * _conditions 21 | * 22 | * @type Array 23 | */ 24 | var _conditions = ['$or', '$and']; 25 | /** 26 | * Query 27 | * 28 | * Class Query 29 | */ 30 | var Query = /** @class */ (function () { 31 | /** 32 | * constructor 33 | * @param type {string} 34 | * @param field {string} 35 | * @param value {any} 36 | * @param params {Params} 37 | * @param name {string} 38 | * @param logger {Function} 39 | */ 40 | function Query(type, field, value, params, name, logger) { 41 | this.name = name; 42 | this.params = params; 43 | this.type = type; 44 | this.field = field; 45 | this.value = value; 46 | this.logger = logger; 47 | this.where = this._where(this.type, this.field, this.value, this.name); 48 | this.query = this._build(this.params); 49 | } 50 | /** 51 | * _where 52 | * @param type {string} 53 | * @param field {string} 54 | * @param value {any} 55 | * @param name {string} 56 | * 57 | * @returns string 58 | */ 59 | Query.prototype._where = function (type, field, value, name) { 60 | var _where = ''; 61 | switch (type) { 62 | case methods_1.default.eq: 63 | case methods_1.default.allofterms: 64 | case methods_1.default.alloftext: 65 | case methods_1.default.anyofterms: 66 | case methods_1.default.anyoftext: 67 | _where = "(func: " + type + "(" + name + "." + field + ", " + ('"' + value + '"') + "){{ORDER}}{{LIMIT}})"; 68 | break; 69 | case methods_1.default.regexp: 70 | _where = "(func: " + type + "(" + name + "." + field + ", " + value + "){{ORDER}}{{LIMIT}})"; 71 | break; 72 | case methods_1.default.uid: 73 | if (Array.isArray(field)) { 74 | field = field.join(', '); 75 | } 76 | _where = "(func: " + methods_1.default.uid + "(" + field + "){{ORDER}}{{LIMIT}})"; 77 | break; 78 | case methods_1.default.has: 79 | _where = "(func: " + methods_1.default.has + "(" + name + "." + field + "){{ORDER}}{{LIMIT}})"; 80 | break; 81 | case methods_1.default.near: 82 | _where = "(func: " + methods_1.default.near + "(" + name + "." + field + ", [" + value.longitude + ", " + value.latitude + "], " + value.distance + "){{ORDER}}{{LIMIT}})"; 83 | break; 84 | case methods_1.default.contains: 85 | _where = "(func: " + methods_1.default.contains + "(" + name + "." + field + ", [" + value.longitude + ", " + value.latitude + "]){{ORDER}}{{LIMIT}})"; 86 | break; 87 | } 88 | return _where; 89 | }; 90 | /** 91 | * _filter 92 | * @param key {string} 93 | * @param value {any} 94 | * @param name {string} 95 | * 96 | * @returns string 97 | */ 98 | Query.prototype._filter = function (key, value, name) { 99 | if (key.toLowerCase() === '$has') { 100 | return key.replace('$', '') + "(" + name + "." + value + ")"; 101 | } 102 | if (typeof value === 'string') { 103 | return "eq(" + name + "." + key + ", \"" + value + "\")"; 104 | } 105 | if (typeof value === 'object' && !Array.isArray(value)) { 106 | var _key = Object.keys(value)[0]; 107 | if (_key) { 108 | var _value = value[_key]; 109 | if (Array.isArray(_value)) { 110 | var _sub_1 = []; 111 | _value.forEach(function (_val) { 112 | _sub_1.push("uid_in(" + name + "." + key + ", " + _val + ")"); 113 | }); 114 | return _sub_1.join(' OR '); 115 | } 116 | if (typeof _value === 'string' && _key !== '$regexp') { 117 | _value = '"' + _value + '"'; 118 | } 119 | if (_key === '$ne') { 120 | return "NOT eq(" + name + "." + key + ", " + _value + ")"; 121 | } 122 | return _key.replace('$', '') + "(" + name + "." + key + ", " + _value + ")"; 123 | } 124 | } 125 | }; 126 | /** 127 | * _parse_filter 128 | * @param filter {any} 129 | * @param name {string} 130 | * 131 | * @returns string 132 | */ 133 | Query.prototype._parse_filter = function (filter, name) { 134 | var _this = this; 135 | var _filters = []; 136 | if (typeof filter !== 'undefined') { 137 | Object.keys(filter).forEach(function (_key) { 138 | if (_conditions.indexOf(_key.toLowerCase()) === -1) { 139 | _filters.push(_this._filter(_key, filter[_key], name)); 140 | } 141 | else { 142 | var _sub_2 = []; 143 | Object.keys(filter[_key]).forEach(function (_k) { 144 | if (Array.isArray(filter[_key][_k])) { 145 | filter[_key][_k].forEach(function (_val) { 146 | _sub_2.push(_this._filter(_k, _val, name)); 147 | }); 148 | } 149 | else { 150 | _sub_2.push(_this._filter(_k, filter[_key][_k], name)); 151 | } 152 | }); 153 | if (_sub_2.length > 0) { 154 | _filters.push("(" + _sub_2.join(" " + _key.replace('$', '').toUpperCase() + " ") + ")"); 155 | } 156 | } 157 | }); 158 | } 159 | if (_filters.length > 0) { 160 | return " @filter(" + _filters.join(' AND ') + ")"; 161 | } 162 | return ''; 163 | }; 164 | /** 165 | * _attributes 166 | * @param attributes {Array} 167 | * @param name {string} 168 | * 169 | * @return string 170 | */ 171 | Query.prototype._attributes = function (attributes, name) { 172 | var _attrs = []; 173 | for (var _i = 0, attributes_1 = attributes; _i < attributes_1.length; _i++) { 174 | var attr = attributes_1[_i]; 175 | if (attr === 'uid') { 176 | _attrs.push('uid'); 177 | } 178 | else { 179 | _attrs.push(attr + ": " + name + "." + attr); 180 | } 181 | } 182 | return _attrs.join('\n'); 183 | }; 184 | /** 185 | * _include 186 | * @param include {Include} 187 | * 188 | * @returns string 189 | */ 190 | Query.prototype._include = function (include, name) { 191 | if (name === void 0) { name = this.name; } 192 | var _inc = ''; 193 | if (!include) { 194 | return _inc; 195 | } 196 | for (var _i = 0, _a = Object.keys(include); _i < _a.length; _i++) { 197 | var relation = _a[_i]; 198 | if (include[relation].count) { 199 | _inc += (include[relation].as ? include[relation].as : relation) + ": count(" + name + "." + relation + ")"; 200 | continue; 201 | } 202 | _inc += (include[relation].as ? include[relation].as : relation) + ": " + name + "." + relation; 203 | var _limit = this._extras(include[relation]); 204 | var _order = this._parse_order(include[relation].order); 205 | if (include[relation].filter) { 206 | _inc += "" + this._parse_filter(include[relation].filter, include[relation].model); 207 | } 208 | if (_limit) { 209 | _inc += " (" + _limit + ") "; 210 | } 211 | if (_order) { 212 | _inc += " (" + _order + ")"; 213 | } 214 | _inc += "{\n " + this._attributes(include[relation].attributes, include[relation].model) + "\n " + this._include(include[relation].include, include[relation].model) + "\n }"; 215 | } 216 | return _inc; 217 | }; 218 | /** 219 | * _extra 220 | * @param params {Params} 221 | * 222 | * @return string 223 | */ 224 | Query.prototype._extras = function (params) { 225 | var _extra = []; 226 | if (params.first && typeof params.first === 'number') { 227 | _extra.push("first: " + params.first); 228 | } 229 | if (params.offset && typeof params.offset === 'number') { 230 | _extra.push("offset: " + params.offset); 231 | } 232 | if (params.after) { 233 | _extra.push("after: " + params.after); 234 | } 235 | if (_extra.length > 0) { 236 | return "" + _extra.join(', '); 237 | } 238 | return ''; 239 | }; 240 | /** 241 | * _parse_order 242 | * @param order {Array} 243 | * 244 | * @returns string 245 | */ 246 | Query.prototype._parse_order = function (order) { 247 | var _this = this; 248 | var _order = []; 249 | if (order && order.length > 0) { 250 | if (Array.isArray(order[0])) { 251 | order.forEach(function (_o) { 252 | if (typeof _o[1] !== 'undefined') { 253 | _order.push("order" + _o[1].toLowerCase() + ": " + _this.name + "." + _o[0]); 254 | } 255 | }); 256 | } 257 | else { 258 | _order.push("order" + order[1].toLowerCase() + ": " + this.name + "." + order[0]); 259 | } 260 | } 261 | if (_order.length > 0) { 262 | return "" + _order.join(', '); 263 | } 264 | return ''; 265 | }; 266 | /** 267 | * _build 268 | * @param params {any} 269 | * 270 | * @returns string 271 | */ 272 | Query.prototype._build = function (params) { 273 | var _order = this._parse_order(params.order); 274 | var _limit = this._extras(params); 275 | if (_order) { 276 | _order = ", " + _order; 277 | } 278 | if (_limit) { 279 | _limit = ", " + _limit; 280 | } 281 | var query = "{\n " + this.name + " " + this.where.replace('{{ORDER}}', _order).replace('{{LIMIT}}', _limit) + " " + this._parse_filter(params.filter, this.name) + " {\n " + this._attributes(params.attributes, this.name) + "\n " + this._include(params.include) + "\n }\n }"; 282 | this.logger(query); 283 | return query; 284 | }; 285 | return Query; 286 | }()); 287 | exports.default = Query; 288 | //# sourceMappingURL=query.js.map -------------------------------------------------------------------------------- /docs/model.md: -------------------------------------------------------------------------------- 1 | `dgraph-orm` model 2 | 3 | ## Defining model 4 | 5 | In `dgraph-orm` defining a model is very easy 6 | 7 | ```javascript 8 | /** 9 | * model 10 | * 11 | * @param schema {Schema} 12 | * @returns Model 13 | */ 14 | const Model = model(SampleSchema); 15 | ``` 16 | 17 | ## Properties 18 | 19 | The available properties in `dgraph-orm` model 20 | 21 | ### schema 22 | 23 | Every `dgraph-orm` model contains its schema in it 24 | 25 | ```javascript 26 | Model.schema // returns passed schema object 27 | ``` 28 | 29 | ### connection 30 | 31 | Every `dgraph-orm` model contains the connection object in it 32 | 33 | ```javascript 34 | Model.connection // returns the connection object 35 | ``` 36 | 37 | ## Methods 38 | 39 | The available methods in `dgraph-orm` model 40 | 41 | ### query 42 | 43 | Executes raw query 44 | 45 | ```javascript 46 | /** 47 | * query 48 | * 49 | * @param query {string} 50 | * @retuns Promise 51 | */ 52 | 53 | const query = ` 54 | user (func: has(user.email)) { 55 | uid 56 | name: user.name 57 | email: user.email 58 | } 59 | `; 60 | Model.query(query) 61 | ``` 62 | 63 | ### queryWithVars 64 | 65 | Executes raw query with variables 66 | 67 | ```javascript 68 | /** 69 | * queryWithVars 70 | * 71 | * @param param {QueryParams} 72 | * @retuns Promise 73 | */ 74 | 75 | const query = ` 76 | query test ($email: string) { 77 | test(func: eq(user.email, $email)) { 78 | uid 79 | name: user.name 80 | email: user.email 81 | } 82 | } 83 | `; 84 | 85 | const variables = { 86 | $email: 'akvlko@gmail.com' 87 | } 88 | Model.queryWithVars({ 89 | query, 90 | variables 91 | }) 92 | ``` 93 | 94 | ### create 95 | 96 | Create data 97 | 98 | ```javascript 99 | /** 100 | * create 101 | * 102 | * @param param {any} 103 | * @retuns Promise 104 | */ 105 | Model.create({ 106 | name: 'Ashok', 107 | email: 'akvlko@gmail.com' 108 | }); 109 | 110 | /** 111 | * create with relation 112 | * 113 | * @param param {any} 114 | * @retuns Promise 115 | */ 116 | Model.create({ 117 | name: 'Ashok', 118 | email: 'akvlko@gmail.com', 119 | friend: ['0x1', '0x2'] 120 | }); 121 | ``` 122 | 123 | ### update 124 | 125 | Update data 126 | 127 | ```javascript 128 | /** 129 | * update 130 | * 131 | * @param param {any} 132 | * @param condition {string | any} 133 | * @retuns Promise 134 | */ 135 | Model.update({ 136 | name: 'Ashok Vishwakarma', 137 | }, { 138 | email: 'akvlko@gmail.com' 139 | }); 140 | 141 | // or using uid 142 | 143 | Model.update({ 144 | name: 'Ashok Vishwakarma', 145 | }, '0x1'); 146 | ``` 147 | 148 | ### delete 149 | 150 | Delete edges and values 151 | 152 | ```javascript 153 | /** 154 | * delete 155 | * 156 | * @param param {any} 157 | * @param condition {string | Array | any} 158 | * @retuns Promise 159 | */ 160 | 161 | // Delete edges and values of single model 162 | Model.delete('0x1'); 163 | 164 | // Delete edges and values of mutliple model 165 | Model.delete(['0x1', '0x2']); 166 | 167 | // Delete edges 168 | Model.delete({ 169 | freind: ['0x2'] 170 | }, '0x1'); 171 | 172 | // Delete values 173 | Model.delete({ 174 | name: null, 175 | email: null 176 | }, '0x1'); 177 | 178 | // Delete using condition 179 | Model.delete({ 180 | email: 'akvlko@gmail.com' 181 | }) 182 | ``` 183 | 184 | ### checkPassword 185 | 186 | To validate a password value on PASSWORD type field 187 | 188 | *The `PASSWORD` type field stores data in encrypted format* 189 | 190 | ```javascript 191 | /** 192 | * checkPassword 193 | * 194 | * @param uid {string} 195 | * @param field {string} 196 | * @param password {string} 197 | * @retuns Promise 198 | */ 199 | Model.checkPassword('0x1', 'password', 'p@ssw0rd'); 200 | ``` 201 | 202 | ### relation 203 | 204 | The `realtion` root method to fetch the relationship data 205 | 206 | ```javascript 207 | /** 208 | * realtion 209 | * 210 | * @param uid {string} 211 | * @param params {RelationParam} 212 | * 213 | */ 214 | 215 | // Single relation default attributes ['uid'] 216 | Model.realtion('0x1', { 217 | field: 'friend' 218 | }); 219 | 220 | // Multiple relation default attributes ['uid'] 221 | Model.realtion('0x1', { 222 | field: ['friend', 'location'] 223 | }); 224 | 225 | // With attributes 226 | Model.realtion('0x1', { 227 | field: 'friend' // string | Array 228 | attributes: { 229 | friend: ['uid', 'name'] 230 | } 231 | }); 232 | ``` 233 | 234 | ### has 235 | 236 | The `has` root method of dgraph 237 | 238 | ```javascript 239 | /** 240 | * has 241 | * 242 | * @param field {string} 243 | * @param params {Params} 244 | * @retuns Promise 245 | */ 246 | Model.has('email'); 247 | ``` 248 | 249 | ### eq 250 | 251 | The `eq` root method of dgraph 252 | 253 | ```javascript 254 | /** 255 | * eq 256 | * 257 | * @param field {string} 258 | * @param value {string} 259 | * @param params {Params} 260 | * @retuns Promise 261 | */ 262 | 263 | Model.eq('email', 'akvlko@gmail.com'); 264 | ``` 265 | 266 | ### uid 267 | 268 | The `uid` root method of dgraph 269 | 270 | ```javascript 271 | /** 272 | * uid 273 | * 274 | * @param field {string | Array} 275 | * @param params {Params} 276 | * @retuns Promise 277 | */ 278 | 279 | // for single uid 280 | Model.uid('0x1'); 281 | 282 | // for multiple uid 283 | Model.uid(['0x1', '0x2']); 284 | ``` 285 | 286 | ### allofterms 287 | 288 | The `allofterms` root method of dgraph 289 | 290 | ```javascript 291 | /** 292 | * allofterms 293 | * 294 | * @param field {string} 295 | * @param value {string} 296 | * @param params {Params} 297 | * @retuns Promise 298 | */ 299 | 300 | Model.allofterms('email', 'akvlko'); 301 | ``` 302 | 303 | ### anyofterms 304 | 305 | The `anyofterms` root method of dgraph 306 | 307 | ```javascript 308 | /** 309 | * anyofterms 310 | * 311 | * @param field {string} 312 | * @param value {string} 313 | * @param params {Params} 314 | * @retuns Promise 315 | */ 316 | 317 | Model.anyofterms('email', 'akvlko'); 318 | ``` 319 | 320 | ### alloftext 321 | 322 | The `alloftext` root method of dgraph 323 | 324 | ```javascript 325 | /** 326 | * alloftext 327 | * 328 | * @param field {string} 329 | * @param value {string} 330 | * @param params {Params} 331 | * @retuns Promise 332 | */ 333 | 334 | Model.alloftext('email', 'akvlko'); 335 | ``` 336 | 337 | ### anyoftext 338 | 339 | The `anyoftext` root method of dgraph 340 | 341 | ```javascript 342 | /** 343 | * anyoftext 344 | * 345 | * @param field {string} 346 | * @param value {string} 347 | * @param params {Params} 348 | * @retuns Promise 349 | */ 350 | 351 | Model.anyoftext('email', 'akvlko'); 352 | ``` 353 | 354 | ### regexp 355 | 356 | The `regexp` root method of dgraph 357 | 358 | ```javascript 359 | /** 360 | * regexp 361 | * 362 | * @param field {string} 363 | * @param regex {RegExp} 364 | * @param params {Params} 365 | * @retuns Promise 366 | */ 367 | 368 | Model.regexp('email', /^akvlko.*$/); 369 | ``` 370 | 371 | ### near 372 | 373 | The `near` root method of dgraph 374 | 375 | *Only with `GEO` type field* 376 | 377 | ```javascript 378 | /** 379 | * near 380 | * 381 | * @param field {string} 382 | * @param value {any} 383 | * @param params {Params} 384 | * @retuns Promise 385 | */ 386 | 387 | Model.near('location', { 388 | latitude: 77.389001, 389 | longitude: 28.557416, 390 | distance: 1000 // in meters 391 | }); 392 | ``` 393 | 394 | ### contains 395 | 396 | The `contains` root method of dgraph 397 | 398 | *Only with `GEO` type field* 399 | 400 | ```javascript 401 | /** 402 | * near 403 | * 404 | * @param field {string} 405 | * @param value {any} 406 | * @param params {Params} 407 | * @retuns Promise 408 | */ 409 | 410 | Model.contains('location', { 411 | latitude: 77.389001, 412 | longitude: 28.557416 413 | }); 414 | ``` 415 | 416 | ## Params 417 | 418 | Methods `has`, `eq`, `uid`, `allofterms`, `anyofterms`, `alloftext`, `anyoftext`, `regexp`, `near` and `contains` have a last parameter of `Param` type which can be used for follwings 419 | 420 | ### attributes 421 | 422 | To determine the attributes to fetch from database 423 | 424 | ```javascript 425 | /** 426 | * attributes 427 | * 428 | * @type Array 429 | */ 430 | Mode.has('email', { 431 | attributes: ['name', 'email', 'age'] 432 | }); 433 | ``` 434 | 435 | ### include 436 | 437 | To fetch the related models 438 | 439 | ```javascript 440 | /** 441 | * include 442 | * 443 | * @type Include 444 | */ 445 | Mode.has('email', { 446 | include: { 447 | friend: { 448 | as: 'freinds', 449 | filter: {} // filter object 450 | attributes: ['name', 'age', 'email'], 451 | order: ['age', 'ASC'] 452 | } 453 | } 454 | }); 455 | 456 | // nested include 457 | // Includes freinds of friends in the result 458 | 459 | Mode.has('email', { 460 | include: { 461 | friend: { 462 | as: 'freinds', 463 | include: { 464 | freind: { 465 | as: 'freinds' 466 | } 467 | } 468 | } 469 | } 470 | }); 471 | ``` 472 | 473 | ### filter 474 | 475 | To filter the records 476 | 477 | ```javascript 478 | /** 479 | * filter 480 | * 481 | * @type Filter 482 | */ 483 | 484 | // using multiple fields in filter 485 | // will use the AND condition 486 | Model.has('email', { 487 | filter: { 488 | name: 'Ashok Vishwakarma', 489 | email: 'akvlko@gmail.com' 490 | } 491 | }) 492 | ``` 493 | 494 | #### or 495 | 496 | OR condition between fields 497 | 498 | ```javascript 499 | Model.has('email', { 500 | filter: { 501 | $or: { 502 | name: 'Ashok Vishwakarma', 503 | age: 25 504 | } 505 | } 506 | }) 507 | ``` 508 | 509 | #### and 510 | 511 | AND conditon between fields 512 | 513 | ```javascript 514 | Model.has('email', { 515 | filter: { 516 | name: 'Ashok Vishwakarma', 517 | age: 25 518 | } 519 | }) 520 | 521 | // you use $and property as well 522 | 523 | Model.has('email', { 524 | filter: { 525 | $and: { 526 | name: 'Ashok Vishwakarma', 527 | age: 25 528 | } 529 | } 530 | }) 531 | ``` 532 | 533 | #### equals 534 | 535 | Equality check 536 | 537 | ```javascript 538 | Model.has('email', { 539 | filter: { 540 | name: 'Ashok Vishwakarma' 541 | } 542 | }) 543 | ``` 544 | 545 | #### not equals 546 | 547 | Inequality check 548 | 549 | ```javascript 550 | Model.has('email', { 551 | filter: { 552 | name: { 553 | $ne: 'Ashok Vishwakarma' 554 | } 555 | } 556 | }) 557 | ``` 558 | 559 | #### le 560 | 561 | Less than or equal to 562 | 563 | ```javascript 564 | Model.has('email', { 565 | filter: { 566 | age: { 567 | $le: 25 568 | } 569 | } 570 | }) 571 | ``` 572 | 573 | #### lt 574 | 575 | Less than 576 | 577 | ```javascript 578 | Model.has('email', { 579 | filter: { 580 | age: { 581 | $lt: 25 582 | } 583 | } 584 | }) 585 | ``` 586 | 587 | #### ge 588 | 589 | Greater than or equal to 590 | 591 | ```javascript 592 | Model.has('email', { 593 | filter: { 594 | age: { 595 | $ge: 25 596 | } 597 | } 598 | }) 599 | ``` 600 | 601 | #### gt 602 | 603 | Greater than 604 | 605 | ```javascript 606 | Model.has('email', { 607 | filter: { 608 | age: { 609 | $gt: 25 610 | } 611 | } 612 | }) 613 | ``` 614 | 615 | #### uid_in 616 | 617 | Filter by edges (relationships) 618 | 619 | ```javascript 620 | // One UID 621 | Model.has('email', { 622 | filter: { 623 | friend: { 624 | $uid_in: '0x1' 625 | } 626 | } 627 | }); 628 | 629 | // multiple UIDs 630 | Model.has('email', { 631 | filter: { 632 | friend: { 633 | $uid_in: ['0x1', '0x2'] 634 | } 635 | } 636 | }) 637 | ``` 638 | 639 | ### Order 640 | 641 | Sort the records 642 | 643 | ```javascript 644 | /** 645 | * order 646 | * 647 | * @type Array 648 | */ 649 | 650 | // Assending 651 | Model.has('email', { 652 | order: ['email', 'ASC'] 653 | }) 654 | 655 | // Desending 656 | Model.has('email', { 657 | order: ['email', 'DESC'] 658 | }) 659 | 660 | // Multiple order 661 | Model.has('email', { 662 | order: [['email', 'DESC'], ['name', 'ASC']] 663 | }) 664 | ``` 665 | 666 | ### Pagination 667 | 668 | Pagination allows returning only a portion, rather than the whole, result set. This can be useful for top-k style queries as well as to reduce the size of the result set for client side processing or to allow paged access to results. 669 | 670 | *Without order specified, the results are sorted by `uid`, which is assigned randomly. So the ordering, while deterministic, might not be what you expected.* 671 | 672 | #### first 673 | 674 | For positive `N`, first: `N` retrieves the first `N` results, by sorted or `UID` order. 675 | 676 | ```javascript 677 | /** 678 | * first 679 | * 680 | * @type number 681 | */ 682 | 683 | // first 5 records 684 | Model.has('email', { 685 | first: 5 686 | }) 687 | ``` 688 | 689 | #### offset 690 | 691 | With offset: `N` the first `N` results are not returned. Used in combination with first, first: `M`, offset: `N` skips over `N` results and returns the following `M`. 692 | 693 | ```javascript 694 | /** 695 | * offset 696 | * 697 | * @type number 698 | */ 699 | 700 | // 5 records after sckiping first 4 records 701 | Model.has('email', { 702 | first: 5, 703 | offset: 4 704 | }) 705 | ``` 706 | 707 | #### after 708 | 709 | Another way to get results after skipping over some results is to use the default UID ordering and skip directly past a node specified by UID 710 | 711 | ```javascript 712 | /** 713 | * after 714 | * 715 | * @type string 716 | */ 717 | 718 | // 5 records after uid '0x1' 719 | Model.has('email', { 720 | first: 5, 721 | after: '0x1' 722 | }) 723 | ``` 724 | -------------------------------------------------------------------------------- /lib/model.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],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AC,CAAC;SACrC;QAED,IAAI,CAAC,iBAAiB,CAAC,QAAQ,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;QAEpD,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAEjC,IAAG,MAAM,CAAC,OAAO,EAAE;YACjB,KAAoB,UAA2B,EAA3B,KAAA,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,EAA3B,cAA2B,EAA3B,IAA2B,EAAE;gBAA7C,IAAI,QAAQ,SAAA;gBACd,IAAG,OAAO,QAAQ,CAAC,QAAQ,CAAC,KAAK,WAAW,EAAE;oBAC5C,MAAM,IAAI,KAAK,CAAI,IAAI,CAAC,MAAM,CAAC,IAAI,yBAAoB,QAAU,CAAC,CAAC;iBACpE;gBAED,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,KAAK,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC;gBAE1D,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;aAClF;SACF;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IACH,YAAC;AAAD,CAAC,AAluBD,IAkuBC;AAED,kBAAe,KAAK,CAAC"} -------------------------------------------------------------------------------- /docs/style.css: -------------------------------------------------------------------------------- 1 | @import url("https://fonts.googleapis.com/css?family=Roboto+Mono|Source+Sans+Pro:300,400,600"); 2 | * { 3 | -webkit-font-smoothing: antialiased; 4 | -webkit-overflow-scrolling: touch; 5 | -webkit-tap-highlight-color: rgba(0,0,0,0); 6 | -webkit-text-size-adjust: none; 7 | -webkit-touch-callout: none; 8 | -webkit-box-sizing: border-box; 9 | box-sizing: border-box; 10 | } 11 | body:not(.ready) { 12 | overflow: hidden; 13 | } 14 | body:not(.ready) [data-cloak], 15 | body:not(.ready) .app-nav, 16 | body:not(.ready) > nav { 17 | display: none; 18 | } 19 | div#app { 20 | font-size: 30px; 21 | font-weight: lighter; 22 | margin: 40vh auto; 23 | text-align: center; 24 | } 25 | div#app:empty::before { 26 | content: 'Loading...'; 27 | } 28 | .emoji { 29 | height: 1.2rem; 30 | vertical-align: middle; 31 | } 32 | .progress { 33 | background-color: var(--theme-color, #42b983); 34 | height: 2px; 35 | left: 0px; 36 | position: fixed; 37 | right: 0px; 38 | top: 0px; 39 | -webkit-transition: width 0.2s, opacity 0.4s; 40 | transition: width 0.2s, opacity 0.4s; 41 | width: 0%; 42 | z-index: 999999; 43 | } 44 | .search a:hover { 45 | color: var(--theme-color, #42b983); 46 | } 47 | .search .search-keyword { 48 | color: var(--theme-color, #42b983); 49 | font-style: normal; 50 | font-weight: bold; 51 | } 52 | html, 53 | body { 54 | height: 100%; 55 | } 56 | body { 57 | -moz-osx-font-smoothing: grayscale; 58 | -webkit-font-smoothing: antialiased; 59 | color: #34495e; 60 | font-family: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif; 61 | font-size: 15px; 62 | letter-spacing: 0; 63 | margin: 0; 64 | overflow-x: hidden; 65 | } 66 | img { 67 | max-width: 100%; 68 | } 69 | a[disabled] { 70 | cursor: not-allowed; 71 | opacity: 0.6; 72 | } 73 | kbd { 74 | border: solid 1px #ccc; 75 | border-radius: 3px; 76 | display: inline-block; 77 | font-size: 12px !important; 78 | line-height: 12px; 79 | margin-bottom: 3px; 80 | padding: 3px 5px; 81 | vertical-align: middle; 82 | } 83 | li input[type='checkbox'] { 84 | margin: 0 0.2em 0.25em 0; 85 | vertical-align: middle; 86 | } 87 | .app-nav { 88 | margin: 25px 60px 0 0; 89 | position: absolute; 90 | right: 0; 91 | text-align: right; 92 | z-index: 10; 93 | /* navbar dropdown */ 94 | } 95 | .app-nav.no-badge { 96 | margin-right: 25px; 97 | } 98 | .app-nav p { 99 | margin: 0; 100 | } 101 | .app-nav > a { 102 | margin: 0 1rem; 103 | padding: 5px 0; 104 | } 105 | .app-nav ul, 106 | .app-nav li { 107 | display: inline-block; 108 | list-style: none; 109 | margin: 0; 110 | } 111 | .app-nav a { 112 | color: inherit; 113 | font-size: 16px; 114 | text-decoration: none; 115 | -webkit-transition: color 0.3s; 116 | transition: color 0.3s; 117 | } 118 | .app-nav a:hover { 119 | color: var(--theme-color, #42b983); 120 | } 121 | .app-nav a.active { 122 | border-bottom: 2px solid var(--theme-color, #42b983); 123 | color: var(--theme-color, #42b983); 124 | } 125 | .app-nav li { 126 | display: inline-block; 127 | margin: 0 1rem; 128 | padding: 5px 0; 129 | position: relative; 130 | } 131 | .app-nav li ul { 132 | background-color: #fff; 133 | border: 1px solid #ddd; 134 | border-bottom-color: #ccc; 135 | border-radius: 4px; 136 | -webkit-box-sizing: border-box; 137 | box-sizing: border-box; 138 | display: none; 139 | max-height: calc(100vh - 61px); 140 | overflow-y: auto; 141 | padding: 10px 0; 142 | position: absolute; 143 | right: -15px; 144 | text-align: left; 145 | top: 100%; 146 | white-space: nowrap; 147 | } 148 | .app-nav li ul li { 149 | display: block; 150 | font-size: 14px; 151 | line-height: 1rem; 152 | margin: 0; 153 | margin: 8px 14px; 154 | white-space: nowrap; 155 | } 156 | .app-nav li ul a { 157 | display: block; 158 | font-size: inherit; 159 | margin: 0; 160 | padding: 0; 161 | } 162 | .app-nav li ul a.active { 163 | border-bottom: 0; 164 | } 165 | .app-nav li:hover ul { 166 | display: block; 167 | } 168 | .github-corner { 169 | border-bottom: 0; 170 | position: fixed; 171 | right: 0; 172 | text-decoration: none; 173 | top: 0; 174 | z-index: 1; 175 | } 176 | .github-corner:hover .octo-arm { 177 | -webkit-animation: octocat-wave 560ms ease-in-out; 178 | animation: octocat-wave 560ms ease-in-out; 179 | } 180 | .github-corner svg { 181 | color: #fff; 182 | fill: var(--theme-color, #42b983); 183 | height: 80px; 184 | width: 80px; 185 | } 186 | main { 187 | display: block; 188 | position: relative; 189 | width: 100vw; 190 | height: 100%; 191 | z-index: 0; 192 | } 193 | main.hidden { 194 | display: none; 195 | } 196 | .anchor { 197 | display: inline-block; 198 | text-decoration: none; 199 | -webkit-transition: all 0.3s; 200 | transition: all 0.3s; 201 | } 202 | .anchor span { 203 | color: #34495e; 204 | } 205 | .anchor:hover { 206 | text-decoration: underline; 207 | } 208 | .sidebar { 209 | border-right: 1px solid rgba(0,0,0,0.07); 210 | overflow-y: auto; 211 | padding: 40px 0 0; 212 | position: absolute; 213 | top: 0; 214 | bottom: 0; 215 | left: 0; 216 | -webkit-transition: -webkit-transform 250ms ease-out; 217 | transition: -webkit-transform 250ms ease-out; 218 | transition: transform 250ms ease-out; 219 | transition: transform 250ms ease-out, -webkit-transform 250ms ease-out; 220 | width: 300px; 221 | z-index: 20; 222 | } 223 | .sidebar > h1 { 224 | margin: 0 auto 1rem; 225 | font-size: 1.5rem; 226 | font-weight: 300; 227 | text-align: center; 228 | } 229 | .sidebar > h1 a { 230 | color: inherit; 231 | text-decoration: none; 232 | } 233 | .sidebar > h1 .app-nav { 234 | display: block; 235 | position: static; 236 | } 237 | .sidebar .sidebar-nav { 238 | line-height: 2em; 239 | padding-bottom: 40px; 240 | } 241 | .sidebar li.collapse .app-sub-sidebar { 242 | display: none; 243 | } 244 | .sidebar ul { 245 | margin: 0 0 0 15px; 246 | padding: 0; 247 | } 248 | .sidebar li > p { 249 | font-weight: 700; 250 | margin: 0; 251 | } 252 | .sidebar ul, 253 | .sidebar ul li { 254 | list-style: none; 255 | } 256 | .sidebar ul li a { 257 | border-bottom: none; 258 | display: block; 259 | } 260 | .sidebar ul li ul { 261 | padding-left: 20px; 262 | } 263 | .sidebar::-webkit-scrollbar { 264 | width: 4px; 265 | } 266 | .sidebar::-webkit-scrollbar-thumb { 267 | background: transparent; 268 | border-radius: 4px; 269 | } 270 | .sidebar:hover::-webkit-scrollbar-thumb { 271 | background: rgba(136,136,136,0.4); 272 | } 273 | .sidebar:hover::-webkit-scrollbar-track { 274 | background: rgba(136,136,136,0.1); 275 | } 276 | .sidebar-toggle { 277 | background-color: transparent; 278 | background-color: rgba(255,255,255,0.8); 279 | border: 0; 280 | outline: none; 281 | padding: 10px; 282 | position: absolute; 283 | bottom: 0; 284 | left: 0; 285 | text-align: center; 286 | -webkit-transition: opacity 0.3s; 287 | transition: opacity 0.3s; 288 | width: 284px; 289 | z-index: 30; 290 | } 291 | .sidebar-toggle .sidebar-toggle-button:hover { 292 | opacity: 0.4; 293 | } 294 | .sidebar-toggle span { 295 | background-color: var(--theme-color, #42b983); 296 | display: block; 297 | margin-bottom: 4px; 298 | width: 16px; 299 | height: 2px; 300 | } 301 | body.sticky .sidebar, 302 | body.sticky .sidebar-toggle { 303 | position: fixed; 304 | } 305 | .content { 306 | padding-top: 60px; 307 | position: absolute; 308 | top: 0; 309 | right: 0; 310 | bottom: 0; 311 | left: 300px; 312 | -webkit-transition: left 250ms ease; 313 | transition: left 250ms ease; 314 | } 315 | .markdown-section { 316 | margin: 0 auto; 317 | max-width: 800px; 318 | padding: 30px 15px 40px 15px; 319 | position: relative; 320 | } 321 | .markdown-section > * { 322 | -webkit-box-sizing: border-box; 323 | box-sizing: border-box; 324 | font-size: inherit; 325 | } 326 | .markdown-section > :first-child { 327 | margin-top: 0 !important; 328 | } 329 | .markdown-section hr { 330 | border: none; 331 | border-bottom: 1px solid #eee; 332 | margin: 2em 0; 333 | } 334 | .markdown-section iframe { 335 | border: 1px solid #eee; 336 | /* fix horizontal overflow on iOS Safari */ 337 | width: 1px; 338 | min-width: 100%; 339 | } 340 | .markdown-section table { 341 | border-collapse: collapse; 342 | border-spacing: 0; 343 | display: block; 344 | margin-bottom: 1rem; 345 | overflow: auto; 346 | width: 100%; 347 | } 348 | .markdown-section th { 349 | border: 1px solid #ddd; 350 | font-weight: bold; 351 | padding: 6px 13px; 352 | } 353 | .markdown-section td { 354 | border: 1px solid #ddd; 355 | padding: 6px 13px; 356 | } 357 | .markdown-section tr { 358 | border-top: 1px solid #ccc; 359 | } 360 | .markdown-section tr:nth-child(2n) { 361 | background-color: #f8f8f8; 362 | } 363 | .markdown-section p.tip { 364 | background-color: #f8f8f8; 365 | border-bottom-right-radius: 2px; 366 | border-left: 4px solid #f66; 367 | border-top-right-radius: 2px; 368 | margin: 2em 0; 369 | padding: 12px 24px 12px 30px; 370 | position: relative; 371 | } 372 | .markdown-section p.tip:before { 373 | background-color: #f66; 374 | border-radius: 100%; 375 | color: #fff; 376 | content: '!'; 377 | font-family: 'Dosis', 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif; 378 | font-size: 14px; 379 | font-weight: bold; 380 | left: -12px; 381 | line-height: 20px; 382 | position: absolute; 383 | height: 20px; 384 | width: 20px; 385 | text-align: center; 386 | top: 14px; 387 | } 388 | .markdown-section p.tip code { 389 | background-color: #efefef; 390 | } 391 | .markdown-section p.tip em { 392 | color: #34495e; 393 | } 394 | .markdown-section p.warn { 395 | background: rgba(66,185,131,0.1); 396 | border-radius: 2px; 397 | padding: 1rem; 398 | } 399 | .markdown-section ul.task-list > li { 400 | list-style-type: none; 401 | } 402 | body.close .sidebar { 403 | -webkit-transform: translateX(-300px); 404 | transform: translateX(-300px); 405 | } 406 | body.close .sidebar-toggle { 407 | width: auto; 408 | } 409 | body.close .content { 410 | left: 0; 411 | } 412 | @media print { 413 | .github-corner, 414 | .sidebar-toggle, 415 | .sidebar, 416 | .app-nav { 417 | display: none; 418 | } 419 | } 420 | @media screen and (max-width: 768px) { 421 | .github-corner, 422 | .sidebar-toggle, 423 | .sidebar { 424 | position: fixed; 425 | } 426 | .app-nav { 427 | margin-top: 16px; 428 | } 429 | .app-nav li ul { 430 | top: 30px; 431 | } 432 | main { 433 | height: auto; 434 | overflow-x: hidden; 435 | } 436 | .sidebar { 437 | left: -300px; 438 | -webkit-transition: -webkit-transform 250ms ease-out; 439 | transition: -webkit-transform 250ms ease-out; 440 | transition: transform 250ms ease-out; 441 | transition: transform 250ms ease-out, -webkit-transform 250ms ease-out; 442 | } 443 | .content { 444 | left: 0; 445 | max-width: 100vw; 446 | position: static; 447 | padding-top: 20px; 448 | -webkit-transition: -webkit-transform 250ms ease; 449 | transition: -webkit-transform 250ms ease; 450 | transition: transform 250ms ease; 451 | transition: transform 250ms ease, -webkit-transform 250ms ease; 452 | } 453 | .app-nav, 454 | .github-corner { 455 | -webkit-transition: -webkit-transform 250ms ease-out; 456 | transition: -webkit-transform 250ms ease-out; 457 | transition: transform 250ms ease-out; 458 | transition: transform 250ms ease-out, -webkit-transform 250ms ease-out; 459 | } 460 | .sidebar-toggle { 461 | background-color: transparent; 462 | width: auto; 463 | padding: 30px 30px 10px 10px; 464 | } 465 | body.close .sidebar { 466 | -webkit-transform: translateX(300px); 467 | transform: translateX(300px); 468 | } 469 | body.close .sidebar-toggle { 470 | background-color: rgba(255,255,255,0.8); 471 | -webkit-transition: 1s background-color; 472 | transition: 1s background-color; 473 | width: 284px; 474 | padding: 10px; 475 | } 476 | body.close .content { 477 | -webkit-transform: translateX(300px); 478 | transform: translateX(300px); 479 | } 480 | body.close .app-nav, 481 | body.close .github-corner { 482 | display: none; 483 | } 484 | .github-corner:hover .octo-arm { 485 | -webkit-animation: none; 486 | animation: none; 487 | } 488 | .github-corner .octo-arm { 489 | -webkit-animation: octocat-wave 560ms ease-in-out; 490 | animation: octocat-wave 560ms ease-in-out; 491 | } 492 | } 493 | @-webkit-keyframes octocat-wave { 494 | 0%, 100% { 495 | -webkit-transform: rotate(0); 496 | transform: rotate(0); 497 | } 498 | 20%, 60% { 499 | -webkit-transform: rotate(-25deg); 500 | transform: rotate(-25deg); 501 | } 502 | 40%, 80% { 503 | -webkit-transform: rotate(10deg); 504 | transform: rotate(10deg); 505 | } 506 | } 507 | @keyframes octocat-wave { 508 | 0%, 100% { 509 | -webkit-transform: rotate(0); 510 | transform: rotate(0); 511 | } 512 | 20%, 60% { 513 | -webkit-transform: rotate(-25deg); 514 | transform: rotate(-25deg); 515 | } 516 | 40%, 80% { 517 | -webkit-transform: rotate(10deg); 518 | transform: rotate(10deg); 519 | } 520 | } 521 | section.cover { 522 | -webkit-box-align: center; 523 | -ms-flex-align: center; 524 | align-items: center; 525 | background-position: center center; 526 | background-repeat: no-repeat; 527 | background-size: cover; 528 | height: 100vh; 529 | display: none; 530 | } 531 | section.cover.show { 532 | display: -webkit-box; 533 | display: -ms-flexbox; 534 | display: flex; 535 | } 536 | section.cover.has-mask .mask { 537 | background-color: #fff; 538 | opacity: 0.8; 539 | position: absolute; 540 | top: 0; 541 | height: 100%; 542 | width: 100%; 543 | } 544 | section.cover .cover-main { 545 | -webkit-box-flex: 1; 546 | -ms-flex: 1; 547 | flex: 1; 548 | margin: -20px 16px 0; 549 | text-align: center; 550 | z-index: 1; 551 | } 552 | section.cover a { 553 | color: inherit; 554 | text-decoration: none; 555 | } 556 | section.cover a:hover { 557 | text-decoration: none; 558 | } 559 | section.cover p { 560 | line-height: 1.5rem; 561 | margin: 1em 0; 562 | } 563 | section.cover h1 { 564 | color: inherit; 565 | font-size: 2.5rem; 566 | font-weight: 300; 567 | margin: 0.625rem 0 2.5rem; 568 | position: relative; 569 | text-align: center; 570 | } 571 | section.cover h1 a { 572 | display: block; 573 | } 574 | section.cover h1 small { 575 | bottom: -0.4375rem; 576 | font-size: 1rem; 577 | position: absolute; 578 | } 579 | section.cover blockquote { 580 | font-size: 1.5rem; 581 | text-align: center; 582 | } 583 | section.cover ul { 584 | line-height: 1.8; 585 | list-style-type: none; 586 | margin: 1em auto; 587 | max-width: 500px; 588 | padding: 0; 589 | } 590 | section.cover .cover-main > p:last-child a { 591 | border-color: var(--theme-color, #42b983); 592 | border-radius: 2rem; 593 | border-style: solid; 594 | border-width: 1px; 595 | -webkit-box-sizing: border-box; 596 | box-sizing: border-box; 597 | color: var(--theme-color, #42b983); 598 | display: inline-block; 599 | font-size: 1.05rem; 600 | letter-spacing: 0.1rem; 601 | margin: 0.5rem 1rem; 602 | padding: 0.75em 2rem; 603 | text-decoration: none; 604 | -webkit-transition: all 0.15s ease; 605 | transition: all 0.15s ease; 606 | } 607 | section.cover .cover-main > p:last-child a:last-child { 608 | background-color: var(--theme-color, #42b983); 609 | color: #fff; 610 | } 611 | section.cover .cover-main > p:last-child a:last-child:hover { 612 | color: inherit; 613 | opacity: 0.8; 614 | } 615 | section.cover .cover-main > p:last-child a:hover { 616 | color: inherit; 617 | } 618 | section.cover blockquote > p > a { 619 | border-bottom: 2px solid var(--theme-color, #42b983); 620 | -webkit-transition: color 0.3s; 621 | transition: color 0.3s; 622 | } 623 | section.cover blockquote > p > a:hover { 624 | color: var(--theme-color, #42b983); 625 | } 626 | body { 627 | background-color: #fff; 628 | } 629 | /* sidebar */ 630 | .sidebar { 631 | background-color: #fff; 632 | color: #364149; 633 | } 634 | .sidebar li { 635 | margin: 6px 0 6px 0; 636 | } 637 | .sidebar ul li a { 638 | color: #505d6b; 639 | font-size: 14px; 640 | font-weight: normal; 641 | overflow: hidden; 642 | text-decoration: none; 643 | text-overflow: ellipsis; 644 | white-space: nowrap; 645 | } 646 | .sidebar ul li a:hover { 647 | text-decoration: underline; 648 | } 649 | .sidebar ul li ul { 650 | padding: 0; 651 | } 652 | .sidebar ul li.active > a { 653 | border-right: 2px solid; 654 | color: var(--theme-color, #42b983); 655 | font-weight: 600; 656 | } 657 | .app-sub-sidebar li::before { 658 | content: '-'; 659 | padding-right: 4px; 660 | float: left; 661 | } 662 | /* markdown content found on pages */ 663 | .markdown-section h1, 664 | .markdown-section h2, 665 | .markdown-section h3, 666 | .markdown-section h4, 667 | .markdown-section strong { 668 | color: #2c3e50; 669 | font-weight: 600; 670 | } 671 | .markdown-section a { 672 | color: var(--theme-color, #42b983); 673 | font-weight: 600; 674 | } 675 | .markdown-section h1 { 676 | font-size: 2rem; 677 | margin: 0 0 1rem; 678 | } 679 | .markdown-section h2 { 680 | font-size: 1.75rem; 681 | margin: 45px 0 0.8rem; 682 | } 683 | .markdown-section h3 { 684 | font-size: 1.5rem; 685 | margin: 40px 0 0.6rem; 686 | } 687 | .markdown-section h4 { 688 | font-size: 1.25rem; 689 | } 690 | .markdown-section h5 { 691 | font-size: 1rem; 692 | } 693 | .markdown-section h6 { 694 | color: #777; 695 | font-size: 1rem; 696 | } 697 | .markdown-section figure, 698 | .markdown-section p { 699 | margin: 1.2em 0; 700 | } 701 | .markdown-section p, 702 | .markdown-section ul, 703 | .markdown-section ol { 704 | line-height: 1.6rem; 705 | word-spacing: 0.05rem; 706 | } 707 | .markdown-section ul, 708 | .markdown-section ol { 709 | padding-left: 1.5rem; 710 | } 711 | .markdown-section blockquote { 712 | border-left: 4px solid var(--theme-color, #42b983); 713 | color: #858585; 714 | margin: 2em 0; 715 | padding-left: 20px; 716 | } 717 | .markdown-section blockquote p { 718 | font-weight: 600; 719 | margin-left: 0; 720 | } 721 | .markdown-section iframe { 722 | margin: 1em 0; 723 | } 724 | .markdown-section em { 725 | color: #7f8c8d; 726 | } 727 | .markdown-section code { 728 | background-color: #f8f8f8; 729 | border-radius: 2px; 730 | color: #e96900; 731 | font-family: 'Roboto Mono', Monaco, courier, monospace; 732 | font-size: 0.8rem; 733 | margin: 0 2px; 734 | padding: 3px 5px; 735 | white-space: pre-wrap; 736 | } 737 | .markdown-section pre { 738 | -moz-osx-font-smoothing: initial; 739 | -webkit-font-smoothing: initial; 740 | background-color: #f8f8f8; 741 | font-family: 'Roboto Mono', Monaco, courier, monospace; 742 | line-height: 1.5rem; 743 | margin: 1.2em 0; 744 | overflow: auto; 745 | padding: 0 1.4rem; 746 | position: relative; 747 | word-wrap: normal; 748 | } 749 | /* code highlight */ 750 | .token.comment, 751 | .token.prolog, 752 | .token.doctype, 753 | .token.cdata { 754 | color: #8e908c; 755 | } 756 | .token.namespace { 757 | opacity: 0.7; 758 | } 759 | .token.boolean, 760 | .token.number { 761 | color: #c76b29; 762 | } 763 | .token.punctuation { 764 | color: #525252; 765 | } 766 | .token.property { 767 | color: #c08b30; 768 | } 769 | .token.tag { 770 | color: #2973b7; 771 | } 772 | .token.string { 773 | color: var(--theme-color, #42b983); 774 | } 775 | .token.selector { 776 | color: #6679cc; 777 | } 778 | .token.attr-name { 779 | color: #2973b7; 780 | } 781 | .token.entity, 782 | .token.url, 783 | .language-css .token.string, 784 | .style .token.string { 785 | color: #22a2c9; 786 | } 787 | .token.attr-value, 788 | .token.control, 789 | .token.directive, 790 | .token.unit { 791 | color: var(--theme-color, #42b983); 792 | } 793 | .token.keyword, 794 | .token.function { 795 | color: #e96900; 796 | } 797 | .token.statement, 798 | .token.regex, 799 | .token.atrule { 800 | color: #22a2c9; 801 | } 802 | .token.placeholder, 803 | .token.variable { 804 | color: #3d8fd1; 805 | } 806 | .token.deleted { 807 | text-decoration: line-through; 808 | } 809 | .token.inserted { 810 | border-bottom: 1px dotted #202746; 811 | text-decoration: none; 812 | } 813 | .token.italic { 814 | font-style: italic; 815 | } 816 | .token.important, 817 | .token.bold { 818 | font-weight: bold; 819 | } 820 | .token.important { 821 | color: #c94922; 822 | } 823 | .token.entity { 824 | cursor: help; 825 | } 826 | .markdown-section pre > code { 827 | -moz-osx-font-smoothing: initial; 828 | -webkit-font-smoothing: initial; 829 | background-color: #f8f8f8; 830 | border-radius: 2px; 831 | color: #525252; 832 | display: block; 833 | font-family: 'Roboto Mono', Monaco, courier, monospace; 834 | font-size: 0.8rem; 835 | line-height: inherit; 836 | margin: 0 2px; 837 | max-width: inherit; 838 | overflow: inherit; 839 | padding: 2.2em 5px; 840 | white-space: inherit; 841 | } 842 | .markdown-section code::after, 843 | .markdown-section code::before { 844 | letter-spacing: 0.05rem; 845 | } 846 | code .token { 847 | -moz-osx-font-smoothing: initial; 848 | -webkit-font-smoothing: initial; 849 | min-height: 1.5rem; 850 | } 851 | pre::after { 852 | color: #ccc; 853 | content: attr(data-lang); 854 | font-size: 0.6rem; 855 | font-weight: 600; 856 | height: 15px; 857 | line-height: 15px; 858 | padding: 5px 10px 0; 859 | position: absolute; 860 | right: 0; 861 | text-align: right; 862 | top: 0; 863 | } -------------------------------------------------------------------------------- /src/model.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Model 3 | * 4 | * dgraph-orm Model class 5 | * 6 | * @author Ashok Vishwakarma 7 | */ 8 | 9 | /** 10 | * Query 11 | * 12 | * dgraph-orm Query class 13 | */ 14 | import Query from './query'; 15 | 16 | /** 17 | * methods 18 | * 19 | * dgraph-orm model methods 20 | */ 21 | import methods from './helpers/methods'; 22 | 23 | /** 24 | * pluck 25 | * 26 | * pluck utility method 27 | */ 28 | import { pluck, merge } from './helpers/utility'; 29 | 30 | /** 31 | * Schema 32 | * 33 | * dgraph-orm Schema class 34 | */ 35 | import Schema from './schema'; 36 | 37 | /** 38 | * Connection 39 | * 40 | * dgraph-orm Connection class 41 | */ 42 | import Connection from './connection'; 43 | 44 | import { QueryParams, FieldProps, Params, RelationParam } from './types'; 45 | 46 | /** 47 | * Mutation 48 | * 49 | * Type Mutation from dgraph-js 50 | */ 51 | import { Mutation } from 'dgraph-js/generated/api_pb'; 52 | 53 | /** 54 | * Txn 55 | * 56 | * Type Txn from dgraph-js 57 | */ 58 | import { Txn } from 'dgraph-js'; 59 | import Types from './helpers/types'; 60 | 61 | /** 62 | * Model 63 | * 64 | * Class Model 65 | */ 66 | class Model { 67 | /** 68 | * index type support 69 | */ 70 | [index: string]: any; 71 | 72 | /** 73 | * schema 74 | * 75 | * @type Schema 76 | */ 77 | schema: Schema; 78 | 79 | /** 80 | * connection 81 | * 82 | * @type Connection 83 | */ 84 | connection: Connection; 85 | 86 | /** 87 | * _models 88 | * 89 | * @type any 90 | */ 91 | private _models: any; 92 | 93 | /** 94 | * _logger 95 | * 96 | * @type Function 97 | */ 98 | private _logger: Function; 99 | 100 | /** 101 | * contructor 102 | * @param schema {Schema} 103 | * @param models {any} 104 | * @param connection {Connection} 105 | * @param logger {Function} 106 | */ 107 | constructor(schema: Schema, models: any, connection: Connection, logger: Function) { 108 | this.schema = schema; 109 | this._models = models; 110 | this.connection = connection; 111 | this._logger = logger; 112 | 113 | this._generate_methods(); 114 | } 115 | 116 | async relation(uid: string, params: RelationParam): Promise { 117 | 118 | if(!params.field || (Array.isArray(params.field) && params.field.length === 0)) { 119 | return null; 120 | } 121 | 122 | if(typeof params.field === 'string') { 123 | params.field = [params.field] 124 | } 125 | 126 | this._check_attributes(this.schema.original, params.field, true, true); 127 | 128 | const _include: any = {}; 129 | 130 | params.field.map((_field: string) => { 131 | _include[_field] = { 132 | as: _field 133 | } 134 | }) 135 | 136 | const _user = await this._method('uid', uid, { 137 | include: _include 138 | }); 139 | 140 | let _data: any = null; 141 | 142 | if(params.field.length === 1 && _user[0][params.field[0]] && _user[0][params.field[0]].length > 0) { 143 | const _attributes = params.attributes && params.attributes[params.field[0]] ? params.attributes[params.field[0]] : ['uid']; 144 | _data = _user[0][params.field[0]].map((_relation: any) => { 145 | return merge(_relation, _attributes); 146 | }); 147 | 148 | } else { 149 | _data = {}; 150 | params.field.forEach((_field: string) => { 151 | const _attributes = params.attributes && params.attributes[_field] ? params.attributes[_field] : ['uid']; 152 | if(!_user[0][_field]) { 153 | _data[_field] = null; 154 | } else { 155 | _data[_field] = _user[0][_field].map((_relation: any) => { 156 | return merge(_relation, _attributes); 157 | }); 158 | } 159 | }); 160 | } 161 | 162 | return new Promise((resolve: Function) => { 163 | return resolve(_data); 164 | }); 165 | } 166 | 167 | /** 168 | * _check_if_password_type 169 | * 170 | * @param field {string} 171 | * 172 | * @returns boolean 173 | */ 174 | private _check_if_password_type(field: string): boolean { 175 | const _field = this.schema.original[field]; 176 | 177 | if(typeof _field === 'undefined') { 178 | return false; 179 | } 180 | 181 | if(typeof _field === 'string' && _field === 'password') { 182 | return true; 183 | } 184 | 185 | if(typeof _field === 'object' && _field.type === 'password') { 186 | return true; 187 | } 188 | 189 | return false; 190 | } 191 | 192 | /** 193 | * checkPassword 194 | * @param uid {string} 195 | * @param field {string} 196 | * @param password {string} 197 | * 198 | * @returns Promise 199 | */ 200 | async checkPassword(uid: string, field: string, password: string): Promise { 201 | return new Promise(async (resolve: Function, reject: Function) => { 202 | try { 203 | 204 | if(!this._check_if_password_type(field)) { 205 | throw new Error(`Field ${field} is not of type PASSWORD.`) 206 | } 207 | 208 | const check: any = await this._execute(`{ 209 | ${this.schema.name} (func: uid(${uid})) { 210 | isValid: checkpwd(${this.schema.name}.${field}, "${password}") 211 | } 212 | }`); 213 | 214 | if(check.length === 0) { 215 | return resolve(false); 216 | } 217 | 218 | return resolve(check[0].isValid); 219 | 220 | } catch (error) { 221 | return reject(error); 222 | } 223 | }); 224 | } 225 | 226 | /** 227 | * _generate_methods 228 | * 229 | * @returns void 230 | */ 231 | private _generate_methods(): void { 232 | Object.keys(methods).forEach(_method => { 233 | Model.prototype[_method] = function(field: string, value: any = null, params: any = null): Promise { 234 | return this._method(_method, field, value, params); 235 | } 236 | }); 237 | } 238 | 239 | /** 240 | * _execute 241 | * @param query {string} 242 | * 243 | * @returns Promise 244 | */ 245 | private _execute(query: string): Promise { 246 | return new Promise(async (resolve: Function, reject: Function) => { 247 | const _txn: Txn = this.connection.client.newTxn(); 248 | 249 | try { 250 | const res = await _txn.query(query); 251 | return resolve(res.getJson()[this.schema.name]); 252 | } catch (error) { 253 | await _txn.discard(); 254 | return reject(error); 255 | } finally { 256 | await _txn.discard(); 257 | } 258 | }) 259 | } 260 | 261 | /** 262 | * _method 263 | * @param type {string} 264 | * @param field {any} 265 | * @param value {any} 266 | * @param params {any} 267 | * 268 | * @returns Promise 269 | */ 270 | private async _method(type: string, field: any, value: any = null, params: any = null): Promise { 271 | if(type === methods.uid || type === methods.has) { 272 | params = value; 273 | value = field; 274 | } 275 | 276 | const _params: any = this._validate(this.schema.original, params); 277 | 278 | const query: Query = new Query(type, field, value, _params, this.schema.name, this._logger); 279 | 280 | return this._execute(query.query); 281 | } 282 | 283 | /** 284 | * query 285 | * @param query {string} 286 | * 287 | * @returns Promise 288 | */ 289 | async query(query: string): Promise { 290 | return new Promise(async (resolve: Function, reject: Function) => { 291 | const _txn: Txn = this.connection.client.newTxn(); 292 | 293 | try { 294 | const data = await _txn.query(query); 295 | // await _txn.commit(); 296 | return resolve(data.getJson()); 297 | } catch (error) { 298 | await _txn.discard(); 299 | 300 | return reject(error); 301 | } finally { 302 | await _txn.discard(); 303 | } 304 | }); 305 | } 306 | 307 | /** 308 | * queryWithVars 309 | * @param params {QueryParams} 310 | * 311 | * @returns Promise 312 | */ 313 | async queryWithVars(params: QueryParams): Promise { 314 | return new Promise(async (resolve: Function, reject: Function) => { 315 | const _txn: Txn = this.connection.client.newTxn(); 316 | 317 | try { 318 | const data = await _txn.queryWithVars(params.query, params.variables); 319 | //await _txn.commit(); 320 | 321 | return resolve(data.getJson()); 322 | } catch (error) { 323 | await _txn.discard(); 324 | return reject(error); 325 | } finally { 326 | await _txn.discard(); 327 | } 328 | }); 329 | } 330 | 331 | /** 332 | * _is_relation 333 | * @param _key {string} 334 | * 335 | * @returns boolean 336 | */ 337 | private _is_relation(_key: string): boolean { 338 | const _field = this.schema.original[_key]; 339 | 340 | if(typeof _field !== 'undefined' && typeof _field !== 'string' && _field.type === 'uid') { 341 | return true; 342 | } 343 | 344 | return false; 345 | } 346 | 347 | /** 348 | * _parse_mutation 349 | * @param mutation {any} 350 | * @param name {string} 351 | * 352 | * @returns {[index: string]: any} 353 | */ 354 | private _parse_mutation(mutation: any, name: string): {[index: string]: any} { 355 | let _mutation: {[index: string]: any} = {}; 356 | 357 | Object.keys(mutation).forEach(_key => { 358 | if(this._is_relation(_key)) { 359 | if(Array.isArray(mutation[_key])) { 360 | const _m: any = []; 361 | mutation[_key].forEach((_uid: any ) => { 362 | _m.push({ 363 | uid: _uid 364 | }) 365 | }); 366 | _mutation[`${name}.${_key}`] = _m; 367 | }else { 368 | _mutation[`${name}.${_key}`] = { 369 | uid: mutation[_key] 370 | }; 371 | } 372 | }else { 373 | _mutation[`${name}.${_key}`] = mutation[_key]; 374 | } 375 | }); 376 | 377 | return _mutation; 378 | } 379 | 380 | /** 381 | * _create 382 | * @param mutation {any} 383 | * 384 | * @returns Promise 385 | */ 386 | private _create(mutation: any): Promise { 387 | return new Promise(async (resolve: Function, reject: Function) => { 388 | const _txn: Txn = this.connection.client.newTxn(); 389 | 390 | try { 391 | const mu: Mutation = new this.connection.dgraph.Mutation(); 392 | mu.setSetJson(mutation); 393 | 394 | const _unique_check = await this._check_unique_values(mutation, _txn); 395 | 396 | if(_unique_check) { 397 | await _txn.discard(); 398 | return reject(new Error(`[Unique Constraint]: ${_unique_check}`)); 399 | } 400 | 401 | mu.setCommitNow(true); 402 | 403 | const _mutation: any = await _txn.mutate(mu); 404 | 405 | const _uid: any = _mutation.wrappers_[1].get('blank-0'); 406 | const data: any = await this._method('uid', _uid); 407 | 408 | return resolve(data[0]); 409 | } catch (error) { 410 | await _txn.discard(); 411 | return reject(error); 412 | } finally { 413 | await _txn.discard(); 414 | } 415 | }); 416 | } 417 | 418 | /** 419 | * create 420 | * @param data {any} 421 | * 422 | * @returns Promise 423 | */ 424 | async create(data: any): Promise { 425 | this._check_attributes(this.schema.original, data, true); 426 | const mutation = this._parse_mutation(data, this.schema.name); 427 | return this._create(mutation); 428 | } 429 | 430 | /** 431 | * _update 432 | * @param mutation {any} 433 | * @param uid {any} 434 | * 435 | * @returns Promise 436 | */ 437 | private _update(mutation: any, uid: any): Promise { 438 | return new Promise(async (resolve: Function, reject: Function) => { 439 | const _txn: Txn = this.connection.client.newTxn(); 440 | 441 | try { 442 | const mu: Mutation = new this.connection.dgraph.Mutation(); 443 | mutation.uid = uid; 444 | mu.setCommitNow(true); 445 | 446 | mu.setSetJson(mutation); 447 | 448 | await _txn.mutate(mu); 449 | return resolve(true); 450 | } catch (error) { 451 | await _txn.discard(); 452 | return reject(error); 453 | } finally { 454 | await _txn.discard(); 455 | } 456 | }); 457 | } 458 | 459 | /** 460 | * update 461 | * @param data {any} 462 | * @param uid {any} 463 | * 464 | * @returns Promise 465 | */ 466 | async update(data: any, uid: any): Promise { 467 | 468 | if(!uid) { 469 | return; 470 | } 471 | 472 | const _keys: Array = Object.keys(data); 473 | 474 | if(_keys.length === 0) { 475 | return; 476 | } 477 | 478 | this._check_attributes(this.schema.original, data, true); 479 | const mutation = this._parse_mutation(data, this.schema.name); 480 | 481 | let _delete: any = null; 482 | let _isDelete: boolean = false; 483 | 484 | Object.keys(data).forEach((_key: string) => { 485 | _delete = {}; 486 | if(this.schema.original[_key].replace) { 487 | _isDelete = true; 488 | _delete[`${this.schema.name}.${_key}`] = null; 489 | } 490 | }); 491 | 492 | if(_isDelete) { 493 | _delete.uid = uid; 494 | await this._delete(_delete); 495 | } 496 | 497 | if(typeof uid === 'string') { 498 | return this._update(mutation, uid); 499 | } 500 | 501 | if(typeof uid === 'object') { 502 | const _key: string = Object.keys(uid)[0]; 503 | const data: any = await this._method('has', _key, { 504 | filter: uid 505 | }); 506 | 507 | if(data.length > 0) { 508 | const _uids: Array = pluck(data, 'uid'); 509 | _uids.forEach(async (_uid: string) => { 510 | await this._update(mutation, _uid); 511 | }); 512 | } 513 | } 514 | } 515 | 516 | /** 517 | * _delete 518 | * @param mutation {any} 519 | * 520 | * @returns Promise 521 | */ 522 | private _delete(mutation: any): Promise { 523 | return new Promise(async (resolve: Function, reject: Function) => { 524 | const _txn = this.connection.client.newTxn(); 525 | 526 | try { 527 | const mu = new this.connection.dgraph.Mutation(); 528 | mu.setCommitNow(true); 529 | mu.setIgnoreIndexConflict(true); 530 | mu.setDeleteJson(mutation); 531 | 532 | await _txn.mutate(mu); 533 | return resolve(true); 534 | } catch (error) { 535 | await _txn.discard(); 536 | return reject(error); 537 | } finally { 538 | await _txn.discard(); 539 | } 540 | }); 541 | } 542 | 543 | /** 544 | * delete 545 | * @param params {any} 546 | * @param uid {any} 547 | * 548 | * @returns Promise 549 | */ 550 | async delete(params: any, uid: any = null): Promise { 551 | 552 | if(typeof params === 'object' && !Array.isArray(params)) { 553 | this._check_attributes(this.schema.original, params, true); 554 | } 555 | 556 | if(!uid) { 557 | if(typeof params === 'string') { 558 | return this._delete({ 559 | uid: params 560 | }); 561 | } 562 | 563 | if(Array.isArray(params)) { 564 | const _uids = []; 565 | for(let _uid of params) { 566 | _uids.push({ 567 | uid: _uid 568 | }); 569 | } 570 | 571 | return this._delete(_uids); 572 | } 573 | 574 | if(typeof params === 'object') { 575 | 576 | const _fields = Object.keys(params); 577 | 578 | const _data: any = await this._method('has', _fields[0], { 579 | attributes: ['uid'], 580 | filter: params 581 | }); 582 | 583 | if(_data.length === 0) { 584 | return; 585 | } 586 | 587 | return this.delete(pluck(_data, 'uid')); 588 | } 589 | } else { 590 | let _params: {[index: string]: any} = {}; 591 | 592 | for(let _key of Object.keys(params)) { 593 | if(this._is_relation(_key)) { 594 | if(Array.isArray(params[_key])) { 595 | const _a: {[index: string]: any} = []; 596 | params[_key].forEach((_uid: any ) => { 597 | _a.push({ 598 | uid: _uid 599 | }); 600 | }); 601 | _params[`${this.schema.name}.${_key}`] = _a; 602 | }else { 603 | if(this.schema.original[_key].replace) { 604 | _params[`${this.schema.name}.${_key}`] = null 605 | } else { 606 | _params[`${this.schema.name}.${_key}`] = { 607 | uid: params[_key] 608 | }; 609 | } 610 | } 611 | }else { 612 | _params[`${this.schema.name}.${_key}`] = null; 613 | } 614 | } 615 | 616 | // if(Array.isArray(uid)) { 617 | // const _p: any = []; 618 | // uid.forEach(_uid => { 619 | // _params.uid = _uid; 620 | // _p.push(_params); 621 | // }); 622 | 623 | // return this._delete(_p); 624 | // } 625 | 626 | // _params.uid = uid; 627 | // return this._delete(_params); 628 | 629 | } 630 | } 631 | 632 | /** 633 | * _get_unique_fields 634 | * 635 | * @returns Array 636 | */ 637 | private _get_unique_fields(): Array { 638 | const _unique: Array = []; 639 | 640 | Object.keys(this.schema.original).forEach(_key => { 641 | const _param: string | FieldProps = this.schema.original[_key]; 642 | if(typeof _param !== 'string' && _param.unique) { 643 | _unique.push(_key); 644 | } 645 | }); 646 | 647 | return _unique; 648 | } 649 | 650 | /** 651 | * _check_unique_values 652 | * @param mutation {any} 653 | * @param _txn {any} 654 | * 655 | * @returns Promise 656 | */ 657 | private async _check_unique_values(mutation: any, _txn: any): Promise { 658 | return new Promise(async (resolve: Function, reject: Function) => { 659 | const _unique = this._get_unique_fields(); 660 | 661 | if(_unique.length === 0) { 662 | return resolve(false); 663 | } 664 | 665 | for(let _key of _unique) { 666 | let _mvalue: string = mutation[`${this.schema.name}.${_key}`]; 667 | let _param: string | FieldProps = this.schema.original[_key]; 668 | if(typeof _param !== 'string' && _param.type === 'string') { 669 | _mvalue = '"' + _mvalue + '"'; 670 | } 671 | const _value = await _txn.query( 672 | `{ 673 | data (func: eq(${this.schema.name}.${_key}, ${_mvalue})) { 674 | ${_key}: ${this.schema.name}.${_key} 675 | } 676 | }` 677 | ); 678 | 679 | if(_value.getJson().data.length > 0) { 680 | return resolve(`Duplicate value for ${_key}`); 681 | } 682 | } 683 | 684 | return resolve(false); 685 | }); 686 | } 687 | 688 | /** 689 | * _lang_fields 690 | * @param original {any} 691 | * 692 | * @returns Array 693 | */ 694 | private _lang_fields(original: any): Array { 695 | const _fields: Array = []; 696 | 697 | Object.keys(original).forEach((_key: string) => { 698 | if(original[_key].type === Types.STRING && original[_key].lang) { 699 | _fields.push(_key); 700 | } 701 | }); 702 | 703 | return _fields; 704 | } 705 | 706 | /** 707 | * _check_attributes 708 | * @param original {any} 709 | * @param attributes {any} 710 | * @param isUpdate {boolean} 711 | * @param isRelation {boolean} 712 | * 713 | * @returs void 714 | */ 715 | private _check_attributes(original: any, data: any, isUpdate: boolean = false, isRelation: boolean = false): void { 716 | let attributes: Array = data; 717 | let haveData: boolean = false; 718 | 719 | if(!Array.isArray(data)) { 720 | attributes = Object.keys(data); 721 | haveData = true; 722 | } 723 | 724 | if(!attributes || attributes.length === 0) { 725 | return; 726 | } 727 | 728 | const _lang_fields: Array = this._lang_fields(original); 729 | 730 | for(let attribute of attributes) { 731 | if(attribute.indexOf('@') === -1 && typeof original[attribute] === 'undefined') { 732 | throw new Error(`${this.schema.name} has no attribute ${attribute}`); 733 | }else if(attribute.indexOf('@') !== -1 && _lang_fields.indexOf(attribute.split('@')[0]) === -1) { 734 | throw new Error(`${this.schema.name} has no lang property in ${attribute}`); 735 | }else if(typeof original[attribute] === 'object' && original[attribute].type !== 'uid' && isRelation) { 736 | throw new Error(`${attribute} is not a relation.`); 737 | } else if(typeof original[attribute] === 'object' && original[attribute].type === 'uid' && !isUpdate){ 738 | throw new Error(`${attribute} is a realtion and must be in include.`); 739 | } else if(typeof original[attribute] === 'object' && original[attribute].replace && haveData && Array.isArray(data[attribute])) { 740 | throw new Error(`The value of ${attribute} cannot be an array as it has replace set to true.`); 741 | } 742 | } 743 | } 744 | 745 | /** 746 | * _all_attributes 747 | * @param original {any} 748 | * 749 | * @return Array 750 | */ 751 | private _all_attributes(original: any): Array { 752 | const _attrs: Array = []; 753 | for(let attr of Object.keys(original)) { 754 | if(original[attr].type === 'uid' || original[attr] === 'password' || original[attr].type === 'password') { 755 | continue; 756 | } 757 | _attrs.push(attr); 758 | } 759 | 760 | return _attrs; 761 | } 762 | 763 | /** 764 | * _validate 765 | * @param original {any} 766 | * @param params {any} 767 | * 768 | * @returns Params 769 | */ 770 | private _validate(original:any , params: Params = {}): Params { 771 | 772 | if(!params) { 773 | params = {}; 774 | } 775 | 776 | if(!params.attributes || params.attributes.length === 0) { 777 | params.attributes = this._all_attributes(original); 778 | } 779 | 780 | const _index = params.attributes.indexOf('uid'); 781 | 782 | if(_index !== -1) { 783 | params.attributes.splice(_index, 1); 784 | } 785 | 786 | this._check_attributes(original, params.attributes); 787 | 788 | params.attributes.unshift('uid'); 789 | 790 | if(params.include) { 791 | for(let relation of Object.keys(params.include)) { 792 | if(typeof original[relation] === 'undefined') { 793 | throw new Error(`${this.schema.name} has no relation ${relation}`); 794 | } 795 | 796 | params.include[relation].model = original[relation].model; 797 | 798 | this._validate(this._models[original[relation].model], params.include[relation]); 799 | } 800 | } 801 | 802 | return params; 803 | } 804 | } 805 | 806 | export default Model; 807 | --------------------------------------------------------------------------------