UNPKG

datamodel

Version:

Relational algebra compliant in-memory tabular data store

111 lines (110 loc) 13.8 kB
--- title: "Relation" description: "Documented Methods" sections: - type: "markdown-section" content: "DataModel\n\nRelation provides the definitions of basic operators of relational algebra like *selection*, *projection*, *union*,\n*difference* etc.\n\nIt is extended by [DataModel](DataModel) to inherit the functionalities of relational algebra concept." - type: "markdown-section" content: "### <a name=getSchema></a> getSchema() → {[Array &lt;Schema&gt;](Array &lt;Schema&gt;)}\n\nRetrieves the [schema](Schema) details for every [field](Field) as an array." - type: "markdown-section" content: "<a name=Array.<Schema>></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">Array&lt;Schema&gt;:</span>Array of fields schema.\n <pre><code> [\n { name: &#39;Name&#39;, type: &#39;dimension&#39; },\n { name: &#39;Miles_per_Gallon&#39;, type: &#39;measure&#39;, numberFormat: (val) =&gt; `${val} miles / gallon` },\n { name: &#39;Cylinder&#39;, type: &#39;dimension&#39; },\n { name: &#39;Displacement&#39;, type: &#39;measure&#39;, defAggFn: &#39;max&#39; },\n { name: &#39;HorsePower&#39;, type: &#39;measure&#39;, defAggFn: &#39;max&#39; },\n { name: &#39;Weight_in_lbs&#39;, type: &#39;measure&#39;, defAggFn: &#39;avg&#39;, },\n { name: &#39;Acceleration&#39;, type: &#39;measure&#39;, defAggFn: &#39;avg&#39; },\n { name: &#39;Year&#39;, type: &#39;dimension&#39;, subtype: &#39;datetime&#39;, format: &#39;%Y&#39; },\n { name: &#39;Origin&#39; }\n ]</code></pre>" - type: "markdown-section" content: "### <a name=getName></a> getName() → {[string](string)}\n\nReturns the name of the [DataModel](DataModel) instance. If no name was specified during [DataModel](DataModel)\ninitialization, then it returns a auto-generated name." - type: "markdown-section" content: "<a name=string></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">string:</span>Name of the DataModel instance." - type: "markdown-section" content: "### <a name=join></a> join(joinWith, filterFn) → {[DataModel](DataModel)}\n\nPerforms [crossproduct](link_of_cross_product) between two [DataModel](DataModel) instances and returns a\nnew [DataModel](DataModel) instance containing the results. This operation is also called theta join.\n\nCross product takes two set and create one set where each value of one set is paired with each value of another\nset.\n\nThis method takes an optional predicate which filters the generated result rows. If the predicate returns true\nthe combined row is included in the resulatant table." - type: "markdown-section" content: "<p class=\"sub-header\">Parameters:</p>\n<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead>\n<tr>\n <td class=\"param-name\">joinWith</td>\n <td><p>DataModel</p> </td>\n <td><p>The DataModel to be joined with the current instance DataModel.</p> </td>\n </tr>\n<tr>\n <td class=\"param-name\">filterFn</td>\n <td><p>SelectionPredicate</p> </td>\n <td><p>The predicate function that will filter the result of the crossProduct.</p> </td>\n </tr></table>" - type: "code-section" content: "let originDM = dm.project(['Origin','Origin_Formal_Name']);\n let carsDM = dm.project(['Name','Miles_per_Gallon','Origin'])\n\n console.log(carsDM.join(originDM)));\n\n console.log(carsDM.join(originDM,\n obj => obj.[originDM.getName()].Origin === obj.[carsDM.getName()].Origin));" preamble: "" - type: "markdown-section" content: "<a name=DataModel></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">DataModel:</span>New DataModel instance created after joining." - type: "markdown-section" content: "### <a name=naturalJoin></a> naturalJoin(joinWith) → {[DataModel](DataModel)}\n\n[Natural join](natural_join) is a special kind of cross-product join where filtering of rows are performed\ninternally by resolving common fields are from both table and the rows with common value are included." - type: "markdown-section" content: "<p class=\"sub-header\">Parameters:</p>\n<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead>\n<tr>\n <td class=\"param-name\">joinWith</td>\n <td><p>DataModel</p> </td>\n <td><p>The DataModel with which the current instance of DataModel on which the method is called will be joined.</p> </td>\n </tr></table>" - type: "code-section" content: "let originDM = dm.project(['Origin','Origin_Formal_Name']);\n let carsDM = dm.project(['Name','Miles_per_Gallon','Origin'])\n\n console.log(carsDM.naturalJoin(originDM));" preamble: "" - type: "markdown-section" content: "<a name=DataModel></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">DataModel:</span>New DataModel instance created after joining." - type: "markdown-section" content: "### <a name=union></a> union(unionWith) → {[DataModel](DataModel)}\n\n[Union](link_to_union) operation can be termed as vertical stacking of all rows from both the DataModel\ninstances, provided that both of the [DataModel](DataModel) instances should have same column names." - type: "markdown-section" content: "<p class=\"sub-header\">Parameters:</p>\n<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead>\n<tr>\n <td class=\"param-name\">unionWith</td>\n <td><p>DataModel</p> </td>\n <td><p>DataModel instance for which union has to be applied with the instance on which the method is called</p> </td>\n </tr></table>" - type: "code-section" content: "console.log(EuropeanMakerDM.union(USAMakerDM));" preamble: "" - type: "markdown-section" content: "<a name=DataModel></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">DataModel:</span>New DataModel instance with the result of the operation" - type: "markdown-section" content: "### <a name=difference></a> difference(differenceWith) → {[DataModel](DataModel)}\n\n[Difference](link_to_difference) operation only include rows which are present in the datamodel on which\nit was called but not on the one passed as argument." - type: "markdown-section" content: "<p class=\"sub-header\">Parameters:</p>\n<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead>\n<tr>\n <td class=\"param-name\">differenceWith</td>\n <td><p>DataModel</p> </td>\n <td><p>DataModel instance for which difference has to be applied with the instance on which the method is called</p> </td>\n </tr></table>" - type: "code-section" content: "console.log(highPowerDM.difference(highExpensiveDM));" preamble: "" - type: "markdown-section" content: "<a name=DataModel></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">DataModel:</span>New DataModel instance with the result of the operation" - type: "markdown-section" content: "### <a name=select></a> select(selectFn, config) → {[DataModel](DataModel)}\n\n[Selection](link_to_selection) is a row filtering operation. It expects an predicate and an optional mode\nwhich control which all rows should be included in the resultant DataModel instance.\n\n[SelectionPredicate](SelectionPredicate) is a function which returns a boolean value. For selection opearation the selection\nfunction is called for each row of DataModel instance with the current row passed as argument.\n\nAfter executing [SelectionPredicate](SelectionPredicate) the rows are labeled as either an entry of selection set or an entry\nof rejection set.\n\n[FilteringMode](FilteringMode) operates on the selection and rejection set to determine which one would reflect in the\nresulatant datamodel." - type: "markdown-section" content: "<p class=\"sub-header\">Parameters:</p>\n<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead>\n<tr>\n <td class=\"param-name\">selectFn</td>\n <td><p>SelectionPredicate</p> </td>\n <td><p>Predicate funciton which is called for each row with the current row <code>function (row, i) { ... }</code></p> </td>\n </tr>\n<tr>\n <td class=\"param-name\">config</td>\n <td><p>Object</p> </td>\n <td><p>The configuration object to control the inclusion exclusion of a row in resultant DataModel instance<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead></p> <tr> <td class=\"param-name\">mode</td> <td><p>FilteringMode</p> </td> <td><p>The mode of the selection</p> </td> </tr></table></td>\n </tr></table>" - type: "code-section" content: "// with selection mode NORMAL:\n const normDt = dt.select(fields => fields.Origin.value === \"USA\")\n console.log(normDt));\n\n// with selection mode INVERSE:\nconst inverDt = dt.select(fields => fields.Origin.value === \"USA\", { mode: DataModel.FilteringMode.INVERSE })\nconsole.log(inverDt);\n\n// with selection mode ALL:\nconst dtArr = dt.select(fields => fields.Origin.value === \"USA\", { mode: DataModel.FilteringMode.ALL })\n// print the selected parts\nconsole.log(dtArr[0]);\n// print the inverted parts\nconsole.log(dtArr[1]);" preamble: "" - type: "markdown-section" content: "<a name=DataModel></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">DataModel:</span>Returns the new DataModel instance(s) after operation." - type: "markdown-section" content: "### <a name=isEmpty></a> isEmpty() → {[Boolean](Boolean)}\n\nRetrieves a boolean value if the current [DataModel](DataModel) instance has data." - type: "code-section" content: "const schema = [\n { name: 'CarName', type: 'dimension' },\n { name: 'HorsePower', type: 'measure' },\n { name: \"Origin\", type: 'dimension' }\n];\nconst data = [];\n\nconst dt = new DataModel(schema, data);\nconsole.log(dt.isEmpty());" preamble: "" - type: "markdown-section" content: "<a name=Boolean></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">Boolean:</span>True if the datamodel has no data, otherwise false." - type: "markdown-section" content: "### <a name=project></a> project(projField, config) → {[DataModel](DataModel)}\n\n[Projection](Projection) is filter column (field) operation. It expects list of fields' name and either include those\nor exclude those based on [FilteringMode](FilteringMode) on the resultant variable.\n\nProjection expects array of fields name based on which it creates the selection and rejection set. All the field\nwhose name is present in array goes in selection set and rest of the fields goes in rejection set.\n\n[FilteringMode](FilteringMode) operates on the selection and rejection set to determine which one would reflect in the\nresulatant datamodel." - type: "markdown-section" content: "<p class=\"sub-header\">Parameters:</p>\n<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead>\n<tr>\n <td class=\"param-name\">projField</td>\n <td><p>Array&lt;(string|Regexp)&gt;</p> </td>\n <td><p>An array of column names in string or regular expression.</p> </td>\n </tr>\n<tr>\n <td class=\"param-name\">config</td>\n <td><p>Object</p> </td>\n <td><p>An optional config to control the creation of new DataModel<table><thead><tr><td>Name</td><td>Type</td><td>Description</td></tr></thead></p> <tr> <td class=\"param-name\">mode</td> <td><p>FilteringMode</p> </td> <td><p>Mode of the projection</p> </td> </tr></table></td>\n </tr></table>" - type: "code-section" content: "const dm = new DataModel(schema, data);\n\n // with projection mode NORMAL:\n const normDt = dt.project([\"Name\", \"HorsePower\"]);\n console.log(normDt.getData());\n\n // with projection mode INVERSE:\n const inverDt = dt.project([\"Name\", \"HorsePower\"], { mode: DataModel.FilteringMode.INVERSE })\n console.log(inverDt.getData());\n\n // with selection mode ALL:\n const dtArr = dt.project([\"Name\", \"HorsePower\"], { mode: DataModel.FilteringMode.ALL })\n // print the normal parts\n console.log(dtArr[0].getData());\n // print the inverted parts\n console.log(dtArr[1].getData());" preamble: "" - type: "markdown-section" content: "<a name=DataModel></a><p class=\"sub-header\">Returns:</p>\n\n <span style=\"font-family: 'Source Code Pro';margin-left: 2%;\">DataModel:</span>Returns the new DataModel instance after operation." - type: "markdown-section" content: "### <a name=dispose></a> dispose()\n\nFrees up the resources associated with the current DataModel instance and breaks all the links instance has in\nthe DAG."