{"id":22588709,"url":"https://github.com/jiawei397/deno_mongo_schema","last_synced_at":"2026-02-07T18:31:25.229Z","repository":{"id":37717523,"uuid":"432982044","full_name":"jiawei397/deno_mongo_schema","owner":"jiawei397","description":"mongo  schema","archived":false,"fork":false,"pushed_at":"2024-10-28T08:35:47.000Z","size":187,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-26T02:24:22.652Z","etag":null,"topics":["deno","mongo","mongodb"],"latest_commit_sha":null,"homepage":"https://jsr.io/@jw397/mongo-schema","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jiawei397.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-11-29T09:36:49.000Z","updated_at":"2024-12-24T07:43:55.000Z","dependencies_parsed_at":"2024-08-23T01:16:06.916Z","dependency_job_id":"e40b81e6-7d86-4427-987d-407aef606b18","html_url":"https://github.com/jiawei397/deno_mongo_schema","commit_stats":{"total_commits":191,"total_committers":1,"mean_commits":191.0,"dds":0.0,"last_synced_commit":"ba8f2fbf4eefa3acda61e1f5c79c6e0c97ad22bc"},"previous_names":[],"tags_count":69,"template":false,"template_full_name":null,"purl":"pkg:github/jiawei397/deno_mongo_schema","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiawei397%2Fdeno_mongo_schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiawei397%2Fdeno_mongo_schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiawei397%2Fdeno_mongo_schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiawei397%2Fdeno_mongo_schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jiawei397","download_url":"https://codeload.github.com/jiawei397/deno_mongo_schema/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jiawei397%2Fdeno_mongo_schema/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29203782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T17:44:10.191Z","status":"ssl_error","status_checked_at":"2026-02-07T17:44:07.936Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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":["deno","mongo","mongodb"],"created_at":"2024-12-08T08:10:34.082Z","updated_at":"2026-02-07T18:31:25.215Z","avatar_url":"https://github.com/jiawei397.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# deno_mongo_schema\n\nExtend from the Node.js\n**[mongodb client v5.3.0](https://github.com/mongodb/node-mongodb-native)**,\nsupport Schema and extend some API.\n\n[![ci](https://github.com/jiawei397/deno_mongo_schema/actions/workflows/ci.yml/badge.svg)](https://github.com/jiawei397/deno_mongo_schema/actions/workflows/ci.yml)\n[![tag](https://img.shields.io/badge/deno-v2.0.2-green.svg)](https://github.com/denoland/deno)\n\n\u003e Breaking changes on v1.0.0.\n\u003e\n\u003e I have to switch client from [deno_mongo](https://deno.land/x/mongo) to\n\u003e Node.js. Because it is officially maintained, it looks more robust.\n\u003e\n\u003e If it is to be used in production, reconnecting after wire breakage is an\n\u003e important function.\n\n## hooks\n\n```ts\nimport {\n  BaseSchema,\n  getDB,\n  getModel,\n  MongoFactory,\n  MongoHookMethod,\n  Prop,\n  Schema,\n  UpdateExOptions,\n  type Document\n} from \"@jw397/mongo-schema\";\n\nawait MongoFactory.forRoot(\"mongodb://localhost:27017/test\");\n\n@Schema()\nclass User extends BaseSchema {\n  @Prop()\n  age!: number;\n\n  @Prop({\n    required: true,\n  })\n  name!: string;\n\n  @Prop({\n    default: Date.now,\n    expires: 60, // seconds\n  })\n  expires?: Date;\n}\n\nconst UserSchema = SchemaFactory.createForClass(User);\n\nUserSchema.pre(\n  MongoHookMethod.update,\n  function (filter: Document, doc: Document, options?: UpdateExOptions) {\n    console.log(\"----pre----\", filter, doc, options);\n    if (!doc.$set) {\n      doc.$set = {};\n    }\n    doc.$set.modifyTime = new Date();\n  },\n);\n\nUserSchema.post(MongoHookMethod.findOneAndUpdate, function (doc) {\n  console.log(\"----post----\", doc);\n  doc.name = \"haha\";\n});\n\nconst userModel = await MongoFactory.getModel(User);\n\nconst id = await userModel.insertOne({\n  \"name\": \"zhangsan\",\n  \"age\": 18,\n});\n\nUserSchema.post(MongoHookMethod.findOne, function (doc) {\n  console.log(\"----post---findOne----\", doc);\n});\n\n// console.log(id);\nconst info = await userModel.findById(id, {\n  projection: {\n    name: 1,\n  },\n});\nconsole.log(info);\n\nUserSchema.post(MongoHookMethod.findMany, function (doc) {\n  console.log(\"----post---findMany----\", doc);\n});\n\nconst arr = await userModel.findMany({});\nconsole.log(arr);\n\nUserSchema.post(MongoHookMethod.delete, function (doc) {\n  console.log(\"----post---delete----\", doc);\n});\n\nconst del = await userModel.deleteOne({\n  name: \"zhangsan\",\n});\nconsole.log(del);\n\nconst delMulti = await userModel.deleteMany({\n  name: \"zhangsan\",\n});\nconsole.log(delMulti);\n```\n\nHere are some useful APIs:\n\n- find\n  - findById\n  - findOne\n  - findMany\n  - countDocuments\n  - aggregate\n  - distinct\n- insert\n  - insertOne\n  - insertMany\n- update\n  - findByIdAndUpdate\n  - findOneAndUpdate\n  - updateMany\n  - updateOne\n- delete\n  - deleteMany\n  - deleteOne/findOneAndDelete\n  - deleteById/findByIdAndDelete\n- index\n  - syncIndexes\n  - dropIndexes\n  - listIndexes\n  - createIndexes\n- drop collection\n  - drop\n\n## Virtual\n\nOr you can use virtual like this:\n\n```ts\nimport {\n  BaseSchema,\n  MongoFactory,\n  Prop,\n  Schema,\n} from \"@jw397/mongo-schema\";\n\nawait MongoFactory.forRoot(\"mongodb://localhost:27017/test\");\n\n@Schema()\nclass User extends BaseSchema {\n  @Prop()\n  group!: string;\n\n  @Prop()\n  title!: string;\n}\n\n@Schema()\nclass Role extends BaseSchema {\n  @Prop()\n  userId!: string;\n\n  @Prop()\n  name!: string;\n}\n\nconst RoleSchema = SchemaFactory.createForClass(Role);\n\nRoleSchema.virtual(\"user\", {\n  ref: User,\n  localField: \"userId\",\n  foreignField: \"_id\",\n  justOne: true,\n  isTransformLocalFieldToObjectID: true,\n});\n\n// Role.populate(\"user\", {\n//   // _id: 0,\n//   group: 1,\n//   // title: 1,\n// });\n// Role.populate(\"user\", \"group\");\n// Role.populate(\"user\", \"-group -createTime\");\n// Role.populate(\"user\", \"title group\");\n\nconst roleModel = await MongoFactory.getModel(Role);\n\n// roleModel.insertOne({\n//   userId: id,\n//   name: \"normal\",\n// });\n\nconsole.log(\n  await roleModel.findMany({}, {\n    projection: {\n      name: 1,\n      userId: 1,\n    },\n    // skip: 1,\n    // limit: 1,\n    populates: {\n      // user: {\n      //   // _id: 0,\n      //   group: 1,\n      //   title: 1,\n      // },\n      // user: \"group\",\n      user: true,\n      // user: \"-_id -title\",\n    },\n  }),\n);\n```\n\n## Special collection name\n\nIf you donnot want to use the default collection name, you must regiter it by\nyourself.\n\n```ts\nSchemaFactory.createForClass(Role, \"mongo_test_schema_roles\");\n```\n\nThen if you still want to use virtual, you must use your registered name instead\nof Schema Class.\n\n```ts\nUserSchema.virtual(\"role\", {\n  ref: \"mongo_test_schema_roles\",\n  localField: \"roleId\",\n  foreignField: \"_id\",\n  justOne: true,\n  // isTransformLocalFieldToObjectID: true,\n  // isTransformObjectIDToLocalField: true\n});\n```\n\n## TODO\n\n- [x] Modify schema as a decorator\n- [x] Unit\n- [x] Configurable whether to convert _id\n- [x] Configurable the createTime and modifyTime\n- [x] Switch the underlying layer to the official Node.js version library\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiawei397%2Fdeno_mongo_schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjiawei397%2Fdeno_mongo_schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjiawei397%2Fdeno_mongo_schema/lists"}