{"id":15044150,"url":"https://github.com/roll-w/light","last_synced_at":"2025-04-10T00:41:47.305Z","repository":{"id":57384548,"uuid":"495294204","full_name":"roll-w/light","owner":"roll-w","description":"A simple and lightweight relational database development module for Java.","archived":false,"fork":false,"pushed_at":"2024-04-07T12:42:52.000Z","size":698,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T00:41:15.641Z","etag":null,"topics":["java","java-8","java-annotations","java-library","jdbc","mysql","sql","sqlite","sqlite3"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/roll-w.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":"2022-05-23T07:01:51.000Z","updated_at":"2024-12-16T05:50:21.000Z","dependencies_parsed_at":"2024-03-17T05:41:23.203Z","dependency_job_id":"16852c95-5f5d-4551-bdbb-6676cf9e48c5","html_url":"https://github.com/roll-w/light","commit_stats":{"total_commits":188,"total_committers":1,"mean_commits":188.0,"dds":0.0,"last_synced_commit":"68cd76a4dd752d51be6ff9eb170ff2b005d68e9d"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roll-w%2Flight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roll-w%2Flight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roll-w%2Flight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/roll-w%2Flight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/roll-w","download_url":"https://codeload.github.com/roll-w/light/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248137999,"owners_count":21053775,"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":["java","java-8","java-annotations","java-library","jdbc","mysql","sql","sqlite","sqlite3"],"created_at":"2024-09-24T20:50:08.536Z","updated_at":"2025-04-10T00:41:47.283Z","avatar_url":"https://github.com/roll-w.png","language":"Java","readme":"# Light\n\n[![Maven Central][mcBadge]][mcLink] [![License][liBadge]][liLink]\n\nA simple and lightweight relational database development module.\n\n## Required\n\nJDK 1.8 or higher.\n\n## Getting Started\n\nTo add dependencies on Light using Maven, use the following:\n\n```xml\n\n\u003cdependencies\u003e\n    \u003c!-- Code Generator Module --\u003e\n    \u003cdependency\u003e\n        \u003cgroupId\u003espace.lingu.light\u003c/groupId\u003e\n        \u003cartifactId\u003elight-compiler\u003c/artifactId\u003e\n        \u003cversion\u003e0.4.6\u003c/version\u003e\n        \u003cscope\u003eprovided\u003c/scope\u003e\n    \u003c/dependency\u003e\n\n    \u003c!-- Runtime Core Module --\u003e\n    \u003cdependency\u003e\n        \u003cgroupId\u003espace.lingu.light\u003c/groupId\u003e\n        \u003cartifactId\u003elight-core\u003c/artifactId\u003e\n        \u003cversion\u003e0.4.6\u003c/version\u003e\n    \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\nOr using Gradle:\n\n```groovy\ndependencies {\n    compileOnly(\"space.lingu.light:light-compiler:0.4.6\")\n\n    implementation(\"space.lingu.light:light-core:0.4.6\")\n}\n```\n\nAfter adding dependencies, you can now use Light to help you build your database.\n\n## Usage Example\n\nHere is a simple example of using Light to build a database with tables.\n\n### Define a table\n\nHere defines a `User` table.\nEach instance of `User` represents a column in the data table.\n\n```java\n\n@DataTable\npublic class User {\n    @PrimaryKey\n    @DataColumn\n    private long uid;\n\n    @DataColumn(name = \"first_name\")\n    private String firstName;\n\n    @DataColumn(name = \"last_name\")\n    private String lastName;\n\n    // ... setters and getters\n}\n```\n\nLight uses the class name as the table name unless you specify the table name manually.\n\nIn this case, the table name is _User_.\n\nSimilar rules apply to column names. Light uses the field name as the column name\nunless you specify the column name manually.\n\n### Create a Data Access Object (DAO)\n\nTo access the data in the database, you need to create a DAO.\n\nHere is a simple example for the `User` table.\n\n```java\n\n@Dao\npublic interface UserDao {\n    @Insert\n    void insert(User... users);\n\n    @Update\n    void update(User... users);\n\n    @Delete\n    void delete(User user);\n\n    @Query(\"SELECT * FROM User\")\n    List\u003cUser\u003e get();\n\n    @Query(\"SELECT * FROM User WHERE uid IN ({ids})\")\n    List\u003cUser\u003e getByIds(int[] ids);\n}\n```\n\nThe `@Dao` annotation indicates that this is a DAO,\nand the DAO class needs to be an interface or abstract class.\n\nBy use the `{}` to specify the SQL parameter,\nLight will automatically replace the parameter with the corresponding value.\n\nWhat's more, you can write a simple expression in the `{}`. Such as:\n\n```java\n\n@Query(\"SELECT * FROM User WHERE first_name = {user.getFirstName()} AND last_name = {user.getLastName()}\")\nList\u003cUser\u003e getByName(User user);\n\n```\n\n\u003e Currently, Light only supports field access and parameterless method calls in the expression.\n\n### Create a database\n\nAfter completing the above work, you also need to define a database class\nto get the DAO instance.\n\nHere is an example of a database class, it needs to be an abstract class\nand extends `LightDatabase`.\n\n```java\n\n@Database(name = \"example\", version = 1, tables = {User.class})\npublic abstract class ExampleDatabase extends LightDatabase {\n    public abstract UserDao getUserDao();\n}\n\n```\n\nWhen connecting to the database, Light will try to create the database.\nBut if the database is already specified in the jdbc URL,\nthis step will be skipped.\n\nAfter connecting to the database, Light will try to create tables \nthat are not yet created. \n\n### Set up connection URL\n\nWhen connecting to the database, you need to specify the connection URL\nand also the jdbc driver class name in the `light.properties` file\n(or you can specify the path in the `@Database` annotation) in the\nclasspath.\n\nHere we use the MySQL database as an example.\n\n```properties\nlight.data.url=jdbc:mysql://localhost:3306/\nlight.data.jdbcName=com.mysql.cj.jdbc.Driver\n# Set your username and password if needed\nlight.data.username=root\nlight.data.password=123456\n```\n\nOr you can replace `data` with your database name, in this case is `example`.\n\n### Last step, get ready to go!\n\nTo get the DAO instance, you need to build the database class instance.\n\nDefine in somewhere of your code:\n\n```java\npublic static ExampleDatabase buildDatabase() {\n    return Light.databaseBuilder(ExampleDatabase.class, MySQLDialectProvider.class)\n            // This connection pool implementation is low performance and is used only as a test.\n            // Can be replaced with Hikari Connection Pool, etc.\n            .setConnectionPool(DisposableConnectionPool.class)\n            // Optional, use light-core-logger-slf4j/LightSlf4jLogger as logger\n            .setLogger(LightSlf4jLogger.createLogger(ExampleDatabase.class))\n            .build();\n}\n```\n\nNote: `DialectProvider` and `ConnectionPool` are must be specified.\n\nWe suggest that you use a singleton pattern to save the database instance.\n\n## License\n\n```text\n   Copyright (C) 2022 Lingu Light Project\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n```\n\n[liBadge]: https://img.shields.io/github/license/Roll-W/light?color=569cd6\u0026style=flat-square\n\n[liLink]: https://github.com/Roll-W/light/blob/master/LICENSE\n\n[mcBadge]: https://img.shields.io/maven-central/v/space.lingu.light/light-parent?style=flat-square\n\n[mcLink]: https://search.maven.org/search?q=g:space.lingu.light\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froll-w%2Flight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froll-w%2Flight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froll-w%2Flight/lists"}