{"id":20129324,"url":"https://github.com/aicore/libmysql","last_synced_at":"2025-04-09T16:09:28.954Z","repository":{"id":40797203,"uuid":"505412218","full_name":"aicore/libmysql","owner":"aicore","description":"Mysql library to access core.ai datbases","archived":false,"fork":false,"pushed_at":"2025-04-04T21:53:44.000Z","size":986,"stargazers_count":2,"open_issues_count":17,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-09T16:09:21.813Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aicore.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2022-06-20T11:27:11.000Z","updated_at":"2025-03-20T05:38:23.000Z","dependencies_parsed_at":"2024-03-14T15:02:14.654Z","dependency_job_id":"3a95bef5-8640-4aa1-9ad2-bcbaa99f7b55","html_url":"https://github.com/aicore/libmysql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"aicore/template-nodejs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aicore%2Flibmysql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aicore%2Flibmysql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aicore%2Flibmysql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aicore%2Flibmysql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aicore","download_url":"https://codeload.github.com/aicore/libmysql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248065283,"owners_count":21041871,"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-11-13T20:33:34.408Z","updated_at":"2025-04-09T16:09:28.934Z","avatar_url":"https://github.com/aicore.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libmysql\n\nThis library helps to model MySQL as document DB. We have simplified MySQL to have only\ntwo columns. More columns will be added only while creating an index for JSON fields using\n`createIndexForJsonField` method.\n\n1. column1: documentId, a random alphanumeric of type `VARCHAR(32).`\n2. column2: Document column to store documents in MySQL. Documents are stored as `JSON` documents.\n\n`documentId` is created  when we put a document into the database by calling `put` method\n\n\n## Code Guardian\n\n[![\u003capp\u003e build verification](https://github.com/aicore/libmysql/actions/workflows/build_verify.yml/badge.svg)](https://github.com/aicore/libmysql/actions/workflows/build_verify.yml)\n\n\u003ca href=\"https://sonarcloud.io/project/issues?id=aicore_libmysql\"\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=alert_status\" alt=\"Sonar code quality check\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=security_rating\" alt=\"Security rating\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=vulnerabilities\" alt=\"vulnerabilities\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=coverage\" alt=\"Code Coverage\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=bugs\" alt=\"Code Bugs\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=reliability_rating\" alt=\"Reliability Rating\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=sqale_rating\" alt=\"Maintainability Rating\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=ncloc\" alt=\"Lines of Code\" /\u003e\n  \u003cimg src=\"https://sonarcloud.io/api/project_badges/measure?project=aicore_libmysql\u0026metric=sqale_index\" alt=\"Technical debt\" /\u003e\n\u003c/a\u003e\n\n## Examples\n\n### How to create a table?\n\n```javascript\nimport {createTable, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\ninit(configs);\nconst tableName = 'customers';\ntry {\n    await createTable(tableName);\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n\n#### How table looks after create table?\n\n| documentID  | document   |\n|--------------------------|----------------|\n\n### How to put a document on a table?\n\n```javascript\nimport {put, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\ninit(configs);\nconst tableName = 'customers';\nconst document = {\n    'lastName': 'Alice',\n    'Age': 100,\n    'active': true,\n    'location': {\n        'city': 'Banglore',\n        'state': 'Karnataka',\n        'layout': {\n            'block': '1stblock'\n        }\n\n    }\n};\ntry {\n    const docId = await put(tableName, document);\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n#### How table looks after putting data to table?\n\n| documentID  | document |\n|--------------------------|---------|\n|d20ab50a3e4deefe508f1b26a32e2632|`{'lastName': 'Alice','Age': 100, 'active': true, 'location': {'city': 'Banglore','state': 'Karnataka','layout': {'block': '1stblock'} }}` |\n\n### How to delete a document from a database?\n\n```javascript\nimport {deleteKey, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\ninit(configs);\nconst tableName = 'customers';\nconst docId = '1234';\n\ntry {\n    await deleteKey(tableName, docId);\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n\n### How to get a document?\n\n```javascript\nimport {get, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\nconst tableName = 'customers';\nconst docId = '1234';\ntry {\n    const document = await get(tableName, docId);\n    console.log(JSON.stringify(document));\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n\n### How to scan a database to get a list of matching documents?\n\n```javascript\nimport {getFromNonIndex, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\nconst tableName = 'customers';\nconst queryObject = {\n    'lastName': 'Alice',\n    'Age': 100\n};\ntry {\n    const documents = await getFromNonIndex(tableName, queryObject);     \n    console.log(JSON.stringify(documents));\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\nNote that only a maximum of 1000 entries will be returned. Use page options to get paginated results.\nTo get paginated results past 1000 results, Eg. `getFromNonIndex(tableName, queryObject, {pageOffset: 56,pageLimit: 1000});`\n* pageOffset [number]: specify which row to start retrieving documents from. Eg: to get 10 documents from\n  the 100'th document, you should specify pageOffset = 100 and pageLimit = 10\n* pageLimit [number]: specify number of documents to retrieve. Eg: to get 10 documents from\n  the 100'th document, you should specify pageOffset = 100 and pageLimit = 10\n\n### How to delete a table?\n\n```javascript\nimport {deleteTable, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\nconst tableName = 'customers';\ntry {\n    await deleteTable(tableName);\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n\n### How to create an index for a JSON field?\n\n```javascript\nimport {createIndexForJsonField, DATA_TYPES, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\nconst tableName = 'customers';\ntry {\n    // To make index unique constraint for new column and index set isUnique to true;\n    const isUnique = false;\n    await createIndexForJsonField(tableName, 'lastName', DATA_TYPES.VARCHAR(50), isUnique);\n    await createIndexForJsonField(tableName, 'Age', DATA_TYPES.INT, isUnique);\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n#### How table looks after creating index?\n\n| documentID  | document                                                                                                                                    | ef21925fada6dfb684b5d8ec72114bb1|9d8d2d5ab12b515182a505f54db7f538|\n|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|----|---|\n|9d8d2d5ab12b515182a505f54db7f538| `{\"Age\": 100, \"active\": true, \"lastName\": \"Alice\", \"location\": {\"city\": \"Banglore\", \"state\": \"Karnataka\", \"layout\": {\"block\": \"1stblock\"}}}` |Alice| 100|\n\n### How to get data from indexed fields?\n\n```javascript\nimport {getFromIndex, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\nconst tableName = 'customers';\nconst queryObject = {\n    'lastName': 'Alice',\n    'Age': 100\n};\ntry {\n    const documents = await getFromIndex(tableName, queryObject);\n    console.log(JSON.stringify(documents));\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n\nNote that only a maximum of 1000 entries will be returned. Use page options to get paginated results.\nTo get paginated results past 1000 results, Eg. `getFromIndex(tableName, queryObject, {pageOffset: 56,pageLimit: 1000});`\n* pageOffset [number]: specify which row to start retrieving documents from. Eg: to get 10 documents from\n  the 100'th document, you should specify pageOffset = 100 and pageLimit = 10\n* pageLimit [number]: specify number of documents to retrieve. Eg: to get 10 documents from\n  the 100'th document, you should specify pageOffset = 100 and pageLimit = 10\n\n### How to update / re-write an existing document?\n\n```javascript\nimport {update, init, close} from \"@aicore/libmysql\";\nimport {getMySqlConfigs} from \"@aicore/libcommonutils\";\n\nconst configs = getMySqlConfigs();\ninit(configs);\n\nconst tableName = 'customers';\nconst docId = 1234;\nconst document = {\n    'FirstName': 'Alice',\n    'lastName': 'Bob',\n    'Age': 20,\n    'active': true\n};\n\ntry {\n    const docId = await update(tableName, docId, document);\n    // or if you want to do conditional updates, ie update the document\n    // only if the condition specified is satisfied.\n    const docId1 = await update(tableName, docId, document, \"$.Age=20\");\n} catch (e) {\n    console.error(JSON.stringify(e));\n}\nclose();\n```\n\n# Commands available\n\n## Building\n\nSince this is a pure JS template project, build command just runs test with coverage.\n\n```shell\n\u003e npm install   // do this only once.\n\u003e npm run build\n```\n\n## Linting\n\nTo lint the files in the project, run the following command:\n\n```shell\n\u003e npm run lint\n```\n\nTo Automatically fix lint errors:\n\n```shell\n\u003e npm run lint:fix\n```\n\n## Testing\n\nTo run all tests:\n\n```shell\n\u003e npm run test\n```\n\nAdditionally, to run unit/integration tests only, use the commands:\n\n```shell\n\u003e npm run test:unit\n\u003e npm run test:integ\n```\n\n## Coverage Reports\n\nTo run all tests with coverage:\n\n```shell\n\u003e npm run cover\n \n```\n\nAfter running coverage, detailed reports can be found in the coverage folder listed in the output of coverage command.\nOpen the file in browser to view detailed reports.\n\nTo run unit/integration tests only with coverage\n\n```shell\n\u003e npm run cover:unit\n\u003e npm run cover:integ\n```\n\nSample coverage report:\n![image](https://user-images.githubusercontent.com/5336369/148687351-6d6c12a2-a232-433d-ab62-2cf5d39c96bd.png)\n\n### Unit and Integration coverage configs\n\nUnit and integration test coverage settings can be updated by configs `.nycrc.unit.json` and `.nycrc.integration.json`.\n\nSee https://github.com/istanbuljs/nyc for config options.\n\n# Publishing packages to NPM\n\n## Preparing for release\n\nPlease run `npm run release` on the `main` branch and push the changes to main. The release command will bump the npm\nversion.\n\n!NB: NPM publish will faill if there is another release with the same version.\n\n## Publishing\n\nTo publish a package to npm, push contents to `npm` branch in\nthis repository.\n\n## Publishing `@aicore/package*`\n\nIf you are looking to publish to package owned by core.ai, you will need access to the GitHub Organization\nsecret `NPM_TOKEN`.\n\nFor repos managed by [aicore](https://github.com/aicore) org in GitHub, Please contact your Admin to get access to\ncore.ai's NPM tokens.\n\n## Publishing to your own npm account\n\nAlternatively, if you want to publish the package to your own npm account, please follow these docs:\n\n1. Create an automation access token by following this [link](https://docs.npmjs.com/creating-and-viewing-access-tokens)\n   .\n2. Add NPM_TOKEN to your repository secret by following\n   this [link](https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow)\n\nTo edit the publishing workflow, please see file: `.github/workflows/npm-publish.yml`\n\n# Dependency updates\n\nWe use Rennovate for dependency updates: https://blog.logrocket.com/renovate-dependency-updates-on-steroids/\n\n* By default, dep updates happen on sunday every week.\n* The status of dependency updates can be viewed here if you have this repo permissions in\n  github: https://app.renovatebot.com/dashboard#github/aicore/template-nodejs\n* To edit rennovate options, edit the rennovate.json file in root,\n  see https://docs.renovatebot.com/configuration-options/\n  Refer\n\n# Code Guardian\n\nSeveral automated workflows that check code integrity are integrated into this template.\nThese include:\n\n1. GitHub actions that runs build/test/coverage flows when a contributor raises a pull request\n2. [Sonar cloud](https://sonarcloud.io/) integration using `.sonarcloud.properties`\n    1. In sonar cloud, enable Automatic analysis from `Administration\n       Analysis Method` for the first\n       time ![image](https://user-images.githubusercontent.com/5336369/148695840-65585d04-5e59-450b-8794-54ca3c62b9fe.png)\n\n## IDE setup\n\nSonarLint is currently available as a free plugin for jetbrains, eclipse, vscode and visual studio IDEs.\nUse sonarLint plugin for webstorm or any of the available\nIDEs from this link before raising a pull request: https://www.sonarlint.org/ .\n\nSonarLint static code analysis checker is not yet available as a Brackets\nextension.\n\n## Local integration testing\n### Steps\n#### [Ubuntu install docker instructions](https://docs.docker.com/engine/install/ubuntu/)\n#### [Or Other OS Install docker](https://docs.docker.com/engine/install/)\n#### Install and setup Mysql Docker image\n\n```console\n# install docker\nsudo docker pull mysql\nsudo docker images\nsudo docker run -d --name mysql-server -p 3306:3306 -e \"MYSQL_ROOT_PASSWORD=1234\" mysql\n\n# install mysql client\nsudo apt-get install mysql-client\n# connect to mysql running in docker\n# type password as 1234\nmysql -h 127.0.0.1 -u root -p\n\n# create a database\nCREATE DATABASE testdb;\n\n# now Goto file `setupIntegTest.js`  and uncomment the config part. Tests can now be run\n\n# list running dockers\nsudo docker container ls\n# stop docker\nsudo docker container stop  \u003ccontainer id obtained from previous step\u003e\n```\n#### Uncomment config in setupIntegTest.js\n#### Remove docker image\n```console\n# get list of all avalible containers\nsudo  docker container ls -a\n\n# Remove container first\nsudo docker container rm   \u003ccontainer id from previous step\u003e\n\n# Remover docker image\nsudo  docker image rm mysql\n```\n\n#### How to start docker container\n```console\n# get mysql container id\nsudo  docker container ls -a\n# start Mysql\nsudo docker start \u003ccontianer Id\u003e\n\n# Connect to MySql\nmysql -h 127.0.0.1 -u root -p \n```\n## Internals\n\n### Testing framework: Mocha , assertion style: chai\n\nSee https://mochajs.org/#getting-started on how to write tests\nUse chai for BDD style assertions (expect, should etc..). See move here: https://www.chaijs.com/guide/styles/#expect\n\n### Mocks and spies: sinon\n\nif you want to mock/spy on fn() for unit tests, use sinon. refer docs: https://sinonjs.org/\n\n### Note on coverage suite used here:\n\nwe use c8 for coverage https://github.com/bcoe/c8. Its reporting is based on nyc, so detailed docs can be found\nhere: https://github.com/istanbuljs/nyc ; We didn't use nyc as it do not yet have ES module support\nsee: https://github.com/digitalbazaar/bedrock-test/issues/16 . c8 is drop replacement for nyc coverage reporting tool\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faicore%2Flibmysql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faicore%2Flibmysql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faicore%2Flibmysql/lists"}