{"_id":"@benhall-7/cube-ts","name":"@benhall-7/cube-ts","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@benhall-7/cube-ts","version":"0.1.0","description":"A TypeScript utility for handling type-safe interactions with cube.js cubes","keywords":["cube.js","typescript"],"homepage":"https://github.com/benhall-7/cube-ts","bugs":{"url":"https://github.com/benhall-7/cube-ts/issues"},"repository":{"type":"git","url":"git+https://github.com/benhall-7/cube-ts.git"},"license":"MIT","author":{"name":"Benjamin Hall"},"type":"module","main":"dist/index.cjs","typings":"dist/index.d.cts","exports":{".":{"require":"./dist/index.cjs","types":"./dist/index.d.cts"}},"tsup":{"entry":["src/index.ts"],"splitting":false,"sourcemap":false,"clean":true,"dts":true},"scripts":{"test":"jest","build":"npx tsup","lint":"npx eslint \"src/*\"","reformat":"npx prettier --write \"src/**/*.+(js|ts|json)\"","format":"npx prettier --check \"src/**/*.+(js|ts|json)\""},"jest":{"preset":"ts-jest","testEnvironment":"node"},"devDependencies":{"@eslint/js":"^9.19.0","@types/jest":"^29.5.14","@typescript-eslint/eslint-plugin":"^8.22.0","@typescript-eslint/parser":"^8.22.0","eslint":"^9.19.0","eslint-config-prettier":"^10.0.1","globals":"^15.14.0","jest":"^29.7.0","ts-jest":"^29.2.5","tsup":"^8.3.6","typescript":"^5.7.3","typescript-eslint":"^8.22.0"},"dependencies":{"@cubejs-client/core":"^1.1"},"_id":"@benhall-7/cube-ts@0.1.0","gitHead":"5f7d326a70f6bf481063338589d7f0d19f35591f","_nodeVersion":"23.7.0","_npmVersion":"11.0.0","dist":{"integrity":"sha512-6lV1e1CQWMExKMZikiJd9FDa5D5C4dKsufSwanC4WMxqmaiiMrdrAizMkHKm5i/mVx3sMXj7Rxam+Nqauno/og==","shasum":"6de065f6a8b271c9c4b38273c8c47b8648cd52f8","tarball":"https://registry.npmjs.org/@benhall-7/cube-ts/-/cube-ts-0.1.0.tgz","fileCount":9,"unpackedSize":56261,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIACNTWGh3as3os24T5S4iYAl9QG0SPQtxxQFBZvXpxtuAiBYM5AaOxVW3oIj4Y+KM15hasus55aHu49tjR0uN9Xi0g=="}]},"_npmUser":{"name":"benhall-7","email":"benjaminjahall@gmail.com"},"directories":{},"maintainers":[{"name":"benhall-7","email":"benjaminjahall@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/cube-ts_0.1.0_1739206143921_0.08501381367224314"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-10T16:49:03.783Z","0.1.0":"2025-02-10T16:49:04.097Z","modified":"2025-02-10T16:49:04.391Z"},"maintainers":[{"name":"benhall-7","email":"benjaminjahall@gmail.com"}],"description":"A TypeScript utility for handling type-safe interactions with cube.js cubes","homepage":"https://github.com/benhall-7/cube-ts","keywords":["cube.js","typescript"],"repository":{"type":"git","url":"git+https://github.com/benhall-7/cube-ts.git"},"author":{"name":"Benjamin Hall"},"bugs":{"url":"https://github.com/benhall-7/cube-ts/issues"},"license":"MIT","readme":"# cube-ts\n\nA utility for working with Cube.js queries in TypeScript.\n\n## Features\n\n### Cube declaration\n\nYou can declare a cube and its allowed members in code:\n\n```typescript\nimport { CubeDef, m } from \"cube-ts\";\n\nconst myCube = new CubeDef({\n  name: \"MyCube\",\n  measures: {\n    myNumber: m.number,\n    myString: m.string,\n  },\n  dimensions: {\n    myTimeDimension: m.time,\n  },\n  segments: [\"segment1\"],\n});\n```\n\nThere are two main exports from `cube-ts`: a class to define cube schemas with, and a set of predefined \"members types\" that can be assigned to each member, behind the \"m\" export. For example, you can use `m.number` to provide the default behavior for dealing with numeric cube members.\n\n### Query creation\n\nYou can use the cube definition to saturate the Query object when running a load query. This grants the user strong typing support and lets them use native types in filters instead of only strings:\n\n```typescript\nconst query = {\n  measures: [cube.measure(\"myNumber\"), cube.measure(\"myString\")],\n  dimensions: [cube.dimension(\"myStringDimension\")],\n  timeDimensions: [\n    cube.timeDimension({\n      dimension: \"myTimeDimension\",\n      granularity: \"hour\",\n      // notice the use of Date objects\n      dateRange: [new Date(\"2025-01-01\"), new Date(\"2025-02-09\")],\n    }),\n  ] as const,\n  segments: [cube.segment(\"segment1\")],\n  filters: [\n    cube.binaryFilter({\n      member: \"myNumber\",\n      operator: \"gte\",\n      // notice the true numbers instead of strings\n      values: [100],\n    }),\n  ],\n};\n```\n\n### Result parsing\n\nThe Cube.js client library supports automatic type inference in ResultSet objects, but this only extends to the rawData method; not to other methods such as tablePivot. To alleviate this limitation, the CubeDef class contains a \"deserializer\" method that accepts a list of measures, dimensions, and timeDimensions and produces a function that will validate and parse the expected type:\n\n```typescript\nconst deserializer = cube.deserializer({\n  measures: [\"myNumber\", \"myString\"],\n  dimensions: [\"myStringDimension\"],\n  timeDimensions: [[\"myTimeDimension\", \"day\"]],\n});\n\nconst result = deserializer(response);\nconsole.log(\n  result.myNumber, // a number\n  result.myString, // a string\n  result.myStringDimension, // a string\n  result[\"myTimeDimension.day\"] // a Date\n);\n```\n\nThis does come at the cost of some redundancy, because it requires the user to specify the measures, dimensions, and time dimensions in both the query, and the deserializer\n\n### Customization\n\nBecause the cube member type is designed to be generic, a user can choose to define their own and use that instead. For example, you could use `dayjs` instead of `Date`; or you could create a type with custom serialization/deserialization, like so:\n\n```typescript\nimport { elideMember } from \"cube-ts\";\n\nconst myCBool = elideMember({\n  filter: \"none\",\n  deserialize(input: unknown) {\n    return input === \"1\";\n  },\n  serialize(input: boolean) {\n    return input ? \"1\" : \"0\";\n  },\n});\n```\n\nand this new type can be used for the given member of the cube.\n","readmeFilename":"README.md"}