{"id":17856057,"url":"https://github.com/abiliojr/sqlite-js","last_synced_at":"2025-03-20T14:30:51.961Z","repository":{"id":102303087,"uuid":"46517336","full_name":"abiliojr/sqlite-js","owner":"abiliojr","description":"JavaScript for SQLite","archived":false,"fork":false,"pushed_at":"2022-09-18T20:06:30.000Z","size":12,"stargazers_count":20,"open_issues_count":2,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-28T05:33:01.430Z","etag":null,"topics":["javascript","plugin","sqlite3"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/abiliojr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-19T20:07:14.000Z","updated_at":"2022-07-24T00:23:35.000Z","dependencies_parsed_at":"2023-05-01T20:13:05.413Z","dependency_job_id":null,"html_url":"https://github.com/abiliojr/sqlite-js","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiliojr%2Fsqlite-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiliojr%2Fsqlite-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiliojr%2Fsqlite-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/abiliojr%2Fsqlite-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/abiliojr","download_url":"https://codeload.github.com/abiliojr/sqlite-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244630057,"owners_count":20484306,"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":["javascript","plugin","sqlite3"],"created_at":"2024-10-28T03:00:45.694Z","updated_at":"2025-03-20T14:30:51.956Z","avatar_url":"https://github.com/abiliojr.png","language":"C","readme":"# SQLite's Cafe: JavaScript for SQLite\n\nCreate new SQL functions! Write them using JavaScript.\n\n\n# Usage\n\nThis is an [SQLite](http://sqlite.org/) plugin. After loading it, you can use the function ```createjs``` to define your own functions, like this:\n\n```\nSELECT createjs({parameters});\n```\n\nThis query will return ```ok``` if everything went fine.\n\nSQL supports two types of functions:\n  * Scalar: returns a result for each record.\n  * Aggregate: process all records, then return a single final result.\n\n\n## Scalar\n\nWhen creating a scalar function, you must provide 2 parameters, in the following order:\n  * Name: the function you are defining.\n  * Code: well... the JavaScript code. Returns the result.\n\nFor example, to create a function that calculates the cosine of an angle:\n```\nSELECT createjs('cos', 'Math.cos(arg[0])');\n```\n\nAnd now you can calculate cosines on a query:\n```\nCREATE TABLE t(angle NUMERIC);\nINSERT INTO t(angle) VALUES (0),(1.571),(3.1416);\n\nSELECT cos(angle) FROM t; -- should return approximately {1, 0, -1}\n```\n\n\n## Aggregate\n\nWhen creating aggregate functions, you must provide 4 parameters, in the following order:\n  * Name:  the function you are defining.\n  * Init:  this code will execute before the first record is processed.\n  * Step:  code called on each record.\n  * Final: code for the last step after all records have been processed. Returns the result.\n\nFor example, to create a function that calculates the [geometric mean](https://en.wikipedia.org/wiki/Geometric_mean) of a set of numbers:\n\n```\nSELECT createjs('gmean',\n                 'prod = 1; n = 0;',\n                 'n++; prod = prod * arg[0];',\n                 'Math.pow(prod, 1/n)');\n```\n\nAnd now, used on a query:\n\n```\nCREATE TABLE data(val NUMERIC);\nINSERT INTO data(val) VALUES (2), (4), (8);\n\nSELECT gmean(val) FROM data; -- should return 4\n```\n\n\n# Parameters\n\nParameters supplied to the functions can be accessed from within JavaScript. The array: ```arg[i]``` contains the values. ```arg``` is a zero-based array (0 \u003c= i \u003c n), like all JavaScript arrays.\n\nExample:\n```\n-- this is a pattern matching example using JavaScript's internal function\nSELECT createjs('regex', 'arg[0].match(arg[1])[arg[2]]');\n\nSELECT regex('abc 24 ef', '(\\w+) (\\d+)', 1); -- Should return abc\nSELECT regex('abc 24 ef', '(\\w+) (\\d+)', 2); -- Should return 24\nSELECT regex('abc 24 ef', '\\d+', 0); -- Should return 24\n```\n\n\n# Returning values\n\nAs seen in the previous examples, the result is returned by leaving the value in the JavaScript machine stack, e.g., ```a = b + c; a;``` will return the result of ```b + c```.\n\nFunctions must always return a value. For aggregates, this is only performed on the final step. Currently, you can't return an array into SQLite.\n\n\n# Reading from files\n\nYou can use the auxiliary function named ```loadfile``` to get the content of a file. This is useful to declare long functions. The signature is:\n\n```\nloadfile(filename,[type])\n```\n\nFor example, to create a function which source code is stored in a file named ```longcode.js```, do:\n\n```\nselect createjs('longcode', select loadfile('longcode.js'));\n```\n\n```loadfile``` can take an optional type parameter. If type is 'b', the file content will be read as a blob. This can be useful for other applications, e.g., reading binary files inside the database.\n\n\n# Notes\n\n* You can redefine a function at any time by calling ```createjs``` again. This holds true even for SQLite's native functions.\n\n\n# Building\n\nThis plugin uses [Duktape](http://duktape.org/) as the JavaScript machine. You must download their code from http://duktape.org/download.html, and extract it in the plugin base directory. You must end up with a directory called duktape-DUKTAPE_VERSION (ie: duktape-3.2.0).\n\nOn FreeBSD, you must have ```sqlite3``` installed. The Linux equivalent is something like ```libsqlite3-dev``` (Ubuntu) or ```sqlite-devel``` (Fedora). The library names and their locations could be different on other Operating Systems. If that's the case, you may need to edit the Makefile.\n\nOn Windows, using Visual Studio, you can use the provided ```sqlitejs.mak```. You'll need to get [sqlite source code amalgamation](https://www.sqlite.org/download.html) from their site. The files sqlite3.h and sqlite3ext.h must be extracted inside a folder named sqlite. Afterwards, you can compile the dll like this:\n\n```\nnmake -f sqlitejs.mak\n```\n\n# Loading\n\nTo use this plugin from within the sqlite3 command line tool, you must run:\n\n```.load path-to-plugin/js``` (for security reasons, SQLite needs you to provide the library path if the plugin is not located inside ```LD_LIBRARY_PATH```. Current directory '.' is a valid path, e.g., ```.load ./js```).\n\nYou could also use this from within almost any code that uses sqlite3 as a library. Refer to SQLite website to get more information about ```sqlite3_load_extension``` and ```sqlite3_enable_load_extension``` or look for their equivalents in your language bindings' documentation.\n\n\n## License\n\nThis code is published under the Simplified BSD License.\n\nCopyright (c) 2015, Abilio Marques All rights reserved.\n\nRedistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:\n\nRedistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.\n\nRedistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\nThe views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.\n","funding_links":[],"categories":["Extensions"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabiliojr%2Fsqlite-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fabiliojr%2Fsqlite-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fabiliojr%2Fsqlite-js/lists"}