{"id":26818792,"url":"https://github.com/rising3/smart-entity-js","last_synced_at":"2026-02-19T08:36:18.854Z","repository":{"id":283542221,"uuid":"952061867","full_name":"rising3/smart-entity-js","owner":"rising3","description":"A smart entity that implements generic methods such as clone(), toJSON(), and fromJSON().","archived":false,"fork":false,"pushed_at":"2025-04-11T10:34:22.000Z","size":214,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-13T20:16:32.925Z","etag":null,"topics":["clone","deserializes","entity","jsonschema","serializes"],"latest_commit_sha":null,"homepage":"https://rising3.github.io/smart-entity-js/","language":"TypeScript","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/rising3.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,"zenodo":null}},"created_at":"2025-03-20T17:09:25.000Z","updated_at":"2025-04-11T10:34:26.000Z","dependencies_parsed_at":"2025-03-20T20:13:24.069Z","dependency_job_id":"64573a7b-f557-4d57-8f6d-8d23396eb68f","html_url":"https://github.com/rising3/smart-entity-js","commit_stats":null,"previous_names":["rising3/smart-entity-js"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/rising3/smart-entity-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rising3%2Fsmart-entity-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rising3%2Fsmart-entity-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rising3%2Fsmart-entity-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rising3%2Fsmart-entity-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rising3","download_url":"https://codeload.github.com/rising3/smart-entity-js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rising3%2Fsmart-entity-js/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29608585,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T06:47:36.664Z","status":"ssl_error","status_checked_at":"2026-02-19T06:45:47.551Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["clone","deserializes","entity","jsonschema","serializes"],"created_at":"2025-03-30T05:14:54.340Z","updated_at":"2026-02-19T08:36:18.839Z","avatar_url":"https://github.com/rising3.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# smart-entity-js\n[![Build](https://github.com/rising3/smart-entity-js/actions/workflows/build.yml/badge.svg)](https://github.com/rising3/smart-entity-js/actions/workflows/build.yml)\n[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE)\n\nA smart entity that implements generic methods such as clone(), toJSON(), and fromJSON().\n\n## Requirements\n\n- Node.js 18 or higher\n\n## How to install\n\nTo install:\n```\nnpm i @rising3/smart-entity-js\n```\n\n## Getting started\n\n### How to use library\n\nCreate an entity by inheriting the SmartEntity class.\nAs an example, create a Person class and an Address class.\n\n#### Person Class\n``` typescript\nclass Person extends SmartEntity\u003cPerson\u003e {\n    protected _maskableFields = [\"name\"];\n    protected _requiredFields = [\"name\"];\n    protected _schemaHints = {\n        id: { type: \"string\" },\n        name: { type: \"string\" },\n        age: { type: \"number\", nullable: true },\n        isActive: { type: \"boolean\", nullable: false },\n        createAt: { type: \"number\" },\n        hobbies: { type: \"array\", nullable: true },\n        address: { type: \"object\", schema: Address.getJsonSchema(), nullable: true },\n    };\n\n    constructor(\n        public id: string = crypto.randomUUID(),\n        public name: string = \"\",\n        public age?: number,\n        public isActive: boolean = true,\n        public createAt: number = Date.now(),\n        public hobbies: string[] = [],\n        public address?: Address\n    ) {\n        super();\n    }\n\n    static example(): Person {\n        return new Person(\n            crypto.randomUUID(),\n            \"Alice\",\n            Math.floor(Math.random() * 100) + 1,\n            true,\n            Date.now(),\n            [\"reading\", \"video game\"],\n            Address.example()\n        );\n    }\n}\n```\n#### Address Class\n\n``` typescript\nclass Address extends SmartEntity\u003cAddress\u003e {\n    protected _maskableFields = [\"postalCode\", \"address\"];\n    protected _requiredFields = [\"postalCode\", \"address\"];\n    protected _schemaHints = {\n        postalCode: { type: \"string\" },\n        address: { type: \"string\" },\n    };\n\n    constructor(\n        public postalCode: string = \"\",\n        public address: string = \"\"\n    ) {\n        super();\n    }\n\n    static example(): Address {\n        return new Address(\n            \"123-4567\",\n            \"tokyo\"\n        );\n    }\n}\n```\n\n### Generic methods\n\n#### static getJsonSchema()\n\nGet the JSON schema for the entity.\nThe JSON schema is created based on the hints defined in the entity.\n\n``` typescript\nconst schema = Person.getJsonSchema();\n```\n\nresult:\n``` json\n{\n  \"type\": \"object\",\n  \"properties\": {\n    \"id\": {\n      \"type\": \"string\",\n      \"nullable\": false\n    },\n    \"name\": {\n      \"type\": \"string\",\n      \"nullable\": false\n    },\n    \"age\": {\n      \"type\": \"number\",\n      \"nullable\": true\n    },\n    \"isActive\": {\n      \"type\": \"boolean\",\n      \"nullable\": false\n    },\n    \"createAt\": {\n      \"type\": \"number\",\n      \"nullable\": false\n    },\n    \"hobbies\": {\n      \"type\": \"array\",\n      \"nullable\": true\n    },\n    \"address\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"postalCode\": {\n          \"type\": \"string\",\n          \"nullable\": false\n        },\n        \"address\": {\n          \"type\": \"string\",\n          \"nullable\": false\n        }\n      },\n      \"required\": [\n        \"postalCode\",\n        \"address\"\n      ],\n      \"additionalProperties\": false,\n      \"nullable\": true\n    }\n  },\n  \"required\": [\n    \"name\"\n  ],\n  \"additionalProperties\": false\n}\n```\n\n#### static fromJSON()\n\nCreate an instance of the Person class from JSON. \nValidate the JSON using a JSON Schema.\n\n``` typescript\nconst json = JSON.stringify({\n  id: \"b46021c4-2cf2-4167-a31c-50c9d297e6b8\",\n  name: \"Alice\",\n  age: 1,\n  isActive: false,\n  createAt: 1742483906966,\n  hobbies: [\"reading\", \"video game\"],\n  address: { postalCode: \"123-4567\", address: \"tokyo\" },\n});\n\nconst person = Person.from(json);\n```\n\n#### static example()\n\nCreate an instance of the example Person class.\n\n``` typescript\nconst person = Person.example();\n```\n\n#### toJSON()\n\nGet JSON from an instance of the Person class.\n\n##### compact JSON\n\n``` typescript\nperson.toJSON();\n```\n\nresult:\n``` json\n{\"id\":\"e110860c-c201-4977-8f5f-97c7eb0e31ba\",\"name\":\"Alice\",\"age\":5,\"isActive\":true,\"createAt\":1742492794876,\"hobbies\":[\"reading\",\"video game\"],\"address\":{\"postalCode\":\"123-4567\",\"address\":\"tokyo\"}}\n```\n\n##### pretty JSON\n\n``` typescript\nperson.toJSON(true);\n```\n\nresult:\n``` json\n{\n  \"id\": \"e110860c-c201-4977-8f5f-97c7eb0e31ba\",\n  \"name\": \"Alice\",\n  \"age\": 5,\n  \"isActive\": true,\n  \"createAt\": 1742492794876,\n  \"hobbies\": [\n    \"reading\",\n    \"video game\"\n  ],\n  \"address\": {\n    \"postalCode\": \"123-4567\",\n    \"address\": \"tokyo\"\n  }\n}\n```\n##### masked JSON\n\n``` typescript\nperson.toJSON(true, true);\n```\n\nresult:\n``` json\n{\n  \"id\": \"e110860c-c201-4977-8f5f-97c7eb0e31ba\",\n  \"name\": \"********\",\n  \"age\": 5,\n  \"isActive\": true,\n  \"createAt\": 1742492794876,\n  \"hobbies\": [\n    \"reading\",\n    \"video game\"\n  ],\n  \"address\": {\n    \"postalCode\": \"********\",\n    \"address\": \"*****\"\n  }\n}\n```\n\n#### validate()\n\nValidate an instance of the Person class using a JSON Schema.\n\n``` typescript\nconst person = new Person();\nperson.validate();  // Throw Error\n```\n\n#### clone()\n\nCreate a clone by deep copying an instance of the Person class.\n\n``` typescript\nconst clone = person.clone();\n```\n## How to build from source\n\n### prerequisites\n\nnode.js, npm, git need to be installed.\n\n```sh\ngit clone https://github.com/rising3/smart-entity-js.git\ncd smart-entity-js\nnpm i\nnpm run test\nnpm run build\n```\n\nTo run the example:\n```\nnpm start\n```\n\n## License\n\n[Apache 2.0](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frising3%2Fsmart-entity-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frising3%2Fsmart-entity-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frising3%2Fsmart-entity-js/lists"}