{"id":22385231,"url":"https://github.com/acoshift/nepq","last_synced_at":"2025-07-31T04:32:58.526Z","repository":{"id":57309955,"uuid":"46652858","full_name":"acoshift/nepq","owner":"acoshift","description":"Nep Query is a query language.","archived":false,"fork":false,"pushed_at":"2016-02-28T07:10:02.000Z","size":179,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-29T03:44:29.519Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/acoshift.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-22T08:46:56.000Z","updated_at":"2019-05-22T23:57:47.000Z","dependencies_parsed_at":"2022-09-09T21:21:29.342Z","dependency_job_id":null,"html_url":"https://github.com/acoshift/nepq","commit_stats":null,"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acoshift%2Fnepq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acoshift%2Fnepq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acoshift%2Fnepq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acoshift%2Fnepq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acoshift","download_url":"https://codeload.github.com/acoshift/nepq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228216467,"owners_count":17886561,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-05T01:22:18.438Z","updated_at":"2024-12-05T01:22:19.158Z","avatar_url":"https://github.com/acoshift.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nep Query (nepq; NepQ)\n\n[![Build Status](https://travis-ci.org/acoshift/nepq.svg?branch=master)](https://travis-ci.org/acoshift/nepq)\n[![npm version](https://img.shields.io/npm/v/nepq.svg)](https://www.npmjs.com/package/nepq)\n[![npm license](https://img.shields.io/npm/l/nepq.svg)]()\n\nNep Query is a query language that was inspired by Facebook's GraphQL and MongoDB.\n\n## Syntax\n\n```\n{method} {name}({params}) {retrieve_flag}{\n  {retrieves}\n}\n```\n\nwill be parsed into object:\n\n```ts\ninterface NepQ {\n  method: string;\n  name: string;\n  params: any[];\n  retrieves: any;\n  $_: number;       // retrieve flag :- 1 (inclusion), 0 (exclusion), null (no flag)\n}\n```\n\n#### Params Syntax\n```\n{id}: {value}, {id}: {value}, ...\n# or json\n{\"{id}\": {value}, ...}\n# or json with JavaScript style (ignore `\"` for keys)\n{{id}: {value}, ...}\n# or arguments\n{value}, {value}, ...\n```\n\n#### Retrieves Syntax\n```\n{id}({params}) {\n  {retrieves}\n},\n{id}({params}) {\n  {retrieves}\n},\n...\n```\n\n#### Retrieve Flag\n`''` or `'+'`: inclusion flag\n\n`'-'`: exclusion flag\n\n`'*'`: no flag\n\n### Example\n\nBasic\n\nwith inclusion :\n```\nread stock.product(id: 10) { name, price }\n```\n```json\n{\n  \"method\": \"read\",\n  \"name\": \"stock.product\",\n  \"params\": [\n    {\n      \"id\": 10\n    }\n  ],\n  \"retrieves\": {\n    \"name\": 1,\n    \"price\": 1\n  },\n  \"$_\": 1\n}\n```\n--\n\nwith exclusion :\n```\nread stock.product(id: 10) -{ price }\n```\n```json\n{\n  \"method\": \"read\",\n  \"name\": \"stock.product\",\n  \"params\": [\n    {\n      \"id\": 10\n    }\n  ],\n  \"retrieves\": {\n    \"price\": 1\n  },\n  \"$_\": 0\n}\n```\n\n---\n\nUse json as parameter :\n```\ncreate db.user.customer({\n  \"user\": \"cust1\",\n  \"email\": \"cust1@email.com\",\n  \"tel\": \"+661234567\",\n  \"address\": {\n    \"province\": \"Bangkok\",\n    \"zip\": \"12345\",\n    \"country\": \"TH\"\n  }\n}) {}\n```\n```json\n{\n  \"method\": \"create\",\n  \"name\": \"db.user.customer\",\n  \"params\": [\n    {\n      \"user\": \"cust1\",\n      \"email\": \"cust1@email.com\",\n      \"tel\": \"+661234567\",\n      \"address\": {\n        \"province\": \"Bangkok\",\n        \"zip\": \"12345\",\n        \"country\": \"TH\"\n      }\n    }\n  ],\n  \"retrieves\": 0,\n  \"$_\": 1\n}\n```\n--\n\nor JavaScript style :\n```\ncreate db.user.customer({\n  user: \"cust1\",\n  email: \"cust1@email.com\",\n  tel: \"+661234567\",\n  address: {\n    province: \"Bangkok\",\n    zip: \"12345\",\n    country: \"TH\"\n  }\n})\n```\n```json\n{\n  \"method\": \"create\",\n  \"name\": \"db.user.customer\",\n  \"params\": [\n    {\n      \"user\": \"cust1\",\n      \"email\": \"cust1@email.com\",\n      \"tel\": \"+661234567\",\n      \"address\": {\n        \"province\": \"Bangkok\",\n        \"zip\": \"12345\",\n        \"country\": \"TH\"\n      }\n    }\n  ],\n  \"retrieves\": 1,\n  \"$_\": 1\n}\n```\n\n---\n\nNested retrieves :\n```\nread db.user.customer(email: \"cust1@email.com\") {\n  id,\n  user,\n  email,\n  address {\n    zip,\n    country\n  }\n}\n```\n```json\n{\n  \"method\": \"read\",\n  \"name\": \"db.user.customer\",\n  \"params\": [\n    {\n      \"email\": \"cust1@email.com\"\n    }\n  ],\n  \"retrieves\": {\n    \"id\": 1,\n    \"user\": 1,\n    \"email\": 1,\n    \"address\": {\n      \"zip\": 1,\n      \"country\": 1\n    }\n  },\n  \"$_\": 1\n}\n```\n---\n\nParameters :\n```\nupdate user({ id: 1234 }, { email: \"new_mail@email.com\" }) -{}\n```\n```json\n{\n  \"method\": \"update\",\n  \"name\": \"user\",\n  \"params\": [\n    {\n      \"id\": 1234\n    },\n    {\n      \"email\": \"new_mail@email.com\"\n    }\n  ],\n  \"retrieves\": 1,\n  \"$_\": 0\n}\n```\n--\n\nflatten :\n```\ncalc sum(...[10, 20, 30, 40]) *{ result(0) }\n```\n```json\n{\n  \"method\": \"calc\",\n  \"name\": \"sum\",\n  \"params\": [\n    10,\n    20,\n    30,\n    40\n  ],\n  \"retrieves\": {\n    \"result.$\": [\n      0\n    ]\n  },\n  \"$_\": null\n}\n```\n\n---\n\nInclusion retrieves with parameters :\n```\ncalc sum(10, 20, 30, 40) {\n  result(0)\n}\n```\n```json\n{\n  \"method\": \"calc\",\n  \"name\": \"sum\",\n  \"params\": [\n    10,\n    20,\n    30,\n    40\n  ],\n  \"retrieves\": {\n    \"result\": 1,\n    \"result.$\": [\n      0\n    ]\n  },\n  \"$_\": 1\n}\n```\n---\n\nRetrieves with parameters :\n```\ncalc sum(10, 20, 30, 40) *{\n  result(0)\n}\n```\n```json\n{\n  \"method\": \"calc\",\n  \"name\": \"sum\",\n  \"params\": [\n    10,\n    20,\n    30,\n    40\n  ],\n  \"retrieves\": {\n    \"result.$\": [\n      0\n    ]\n  },\n  \"$_\": null\n}\n```\n---\n\nSome can be ignored :\n\n*empty string*\n```\n\n```\n```json\n{\n  \"method\": \"\",\n  \"name\": \"\",\n  \"params\": [],\n  \"retrieves\": 1,\n  \"$_\": 1\n}\n```\n--\n\n```\nread\n```\n```json\n{\n  \"method\": \"read\",\n  \"name\": \"\",\n  \"params\": [],\n  \"retrieves\": 1,\n  \"$_\": 1\n}\n```\n--\n\n```\nread stock.product\n```\n\n```json\n{\n  \"method\": \"read\",\n  \"name\": \"stock.product\",\n  \"params\": [],\n  \"retrieves\": 1,\n  \"$_\": 1\n}\n```\n--\n\n```\nread stock.product { name }\n```\n```json\n{\n  \"method\": \"read\",\n  \"name\": \"stock.product\",\n  \"params\": [],\n  \"retrieves\": {\n    \"name\": 1\n  },\n  \"$_\": 1\n}\n```\n--\n\n```\n{\n  find(10) {\n    name,\n    price\n  }\n}\n```\n```json\n{\n  \"method\": \"\",\n  \"name\": \"\",\n  \"params\": [],\n  \"retrieves\": {\n    \"find\": {\n      \"name\": 1,\n      \"price\": 1\n    },\n    \"find.$\": [\n      10\n    ]\n  },\n  \"$_\": 1\n}\n```\n---\n\nHardcore :\n```\nq test(prefix: \"123\") {\n  id,\n  obj(2) -{\n    name(1, 2) +{\n      first\n    },\n    tel\n  },\n  result *{\n    res(0),\n    obj +{\n      ok\n    }\n  }\n}\n```\n```json\n{\n  \"method\": \"q\",\n  \"name\": \"test\",\n  \"params\": [\n    {\n      \"prefix\": \"123\"\n    }\n  ],\n  \"retrieves\": {\n    \"id\": 1,\n    \"obj\": {\n      \"name\": {\n        \"first\": 1\n      },\n      \"name.$\": [\n        1,\n        2\n      ],\n      \"name.$_\": 1,\n      \"tel\": 1\n    },\n    \"obj.$\": [\n      2\n    ],\n    \"obj.$_\": 0,\n    \"result\": {\n      \"res.$\": [\n        0\n      ],\n      \"obj\": {\n        \"ok\": 1\n      },\n      \"obj.$_\": 1\n    },\n    \"result.$_\": null\n  },\n  \"$_\": 1\n}\n```\n\n---\n\n## API\n```ts\nexport interface NepQ {\n  method: string;\n  name: string;\n  params: any[];\n  retrieves: any;\n  $_: number;\n}\n\nexport var parser: {\n  parse: (input: string) =\u003e NepQ;\n};\n\nexport function parse(input: string): NepQ;\nexport function response(nq: NepQ, obj: any, cb?: (result: any) =\u003e void): void;\nexport function bodyParser(opt?: {\n  encoding?: string;\n}): (req, res, next) =\u003e void;\n```\n\n## Example\n\n```\n$ npm init\n$ npm install express nepq lodash\n```\n\n```ts\nimport express = require('express');\nimport nepq = require('nepq');\n\nvar app = express();\nvar db: { [key: string]: any[] } = {};\nvar _id = 0;\n\napp.use(nepq.bodyParser());\n\napp.use((req, res) =\u003e {\n  let nq: nepq.NepQ = req.body;\n  if (nq == null) return res.sendStatus(400);\n\n  console.log(nq);\n\n  let get = id =\u003e db[nq.name].reduce((p, v, i) =\u003e {\n    return p === null \u0026\u0026 v._id === id ? i : p;\n  }, null);\n\n  let response = result =\u003e nepq.response(nq, result, r =\u003e res.json(r));\n\n  let i, d;\n\n  switch (nq.method) {\n    case 'create':\n      if (!db[nq.name]) db[nq.name] = [];\n      nq.params[0]._id = _id++;\n      db[nq.name].push(nq.params[0]);\n      response(nq.params[0]);\n      break;\n    case 'read':\n      if (!db[nq.name]) return response(null);\n      if (nq.params.length === 0) {\n        return response(db[nq.name]);\n      }\n      response(db[nq.name].filter(x =\u003e x._id === nq.params[0])[0] || null);\n      break;\n    case 'update':\n      if (!db[nq.name]) return response(null);\n      i = get(nq.params[0]);\n      if (i === null) return response(null);\n      nq.params[1]._id = db[nq.name][i]._id;\n      db[nq.name][i] = nq.params[1];\n      response(nq.params[1]);\n      break;\n    case 'delete':\n      if (!db[nq.name]) return response(null);\n      i = get(nq.params[0]);\n      if (i === null) return response(null);\n      d = db[nq.name][i];\n      delete db[nq.name][i];\n      response(d);\n      break;\n    case 'calc':\n      switch (nq.name) {\n        case 'sum':\n          response({\n            result: ([init]) =\u003e nq.params.reduce((p, v) =\u003e p + v, init)\n          });\n          break;\n        default:\n          res.sendStatus(501);\n      }\n      break;\n    default:\n      res.sendStatus(501);\n  }\n});\n\napp.listen(8000);\n```\n\n```\n$ curl localhost:8000\n\u003e Bad Request\n\n$ curl --header \"content-type: application/nepq\" localhost:8000\n\u003e Not Implemented\n\n$ curl --header \"content-type: application/nepq\" --data \"create contact({name: \\\"n1\\\"})\" localhost:8000\n\u003e {\"name\":\"n1\",\"_id\":0}\n\n$ curl --header \"content-type: application/nepq\" --data \"create contact(name: \\\"n2\\\") { _id }\" localhost:8000\n\u003e {\"_id\":1}\n\n$ curl --header \"content-type: application/nepq\" --data \"read contact(0) -{ _id }\" localhost:8000\n\u003e {\"name\":\"n1\"}\n\n$ curl --header \"content-type: application/nepq\" --data \"update contact(0, {name: \\\"new n1\\\"})\" localhost:8000\n\u003e {\"name\":\"new n1\",\"_id\":0}\n\n$ curl --header \"content-type: application/nepq\" --data \"read contact { name }\" localhost:8000\n\u003e [{\"name\":\"new n1\"},{\"name\":\"n2\"}]\n\n$ curl --header \"content-type: application/nepq\" --data \"calc sum(10, 20, 30, 40) { result(0) }\" localhost:8000\n\u003e {\"result\":100}\n\n$ curl --header \"content-type: application/nepq\" --data \"calc sum(10, 20, 30, 40) { result(\\\"\\\") }\" localhost:8000\n\u003e {\"result\":\"10203040\"}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facoshift%2Fnepq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facoshift%2Fnepq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facoshift%2Fnepq/lists"}