{"id":530,"date":"2024-12-25T23:45:20","date_gmt":"2024-12-25T15:45:20","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=530"},"modified":"2024-12-25T23:45:20","modified_gmt":"2024-12-25T15:45:20","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-javascript-%e7%8e%af%e5%a2%83%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e8%bf%81%e7%a7%bb","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-javascript-%e7%8e%af%e5%a2%83%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%95%b0%e6%8d%ae%e8%bf%81%e7%a7%bb\/","title":{"rendered":"\u5982\u4f55\u5728 JavaScript \u73af\u5883\u4e2d\u8fdb\u884c\u6570\u636e\u8fc1\u79fb"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">JavaScript \u672c\u8eab\u6ca1\u6709\u5185\u7f6e\u7684\u6570\u636e\u5e93\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\u914d\u5408\u4f7f\u7528\uff08\u5982 MongoDB\u3001MySQL\u3001PostgreSQL\uff09\u3002\u6570\u636e\u8fc1\u79fb\u662f\u6307\u5c06\u6570\u636e\u4ece\u4e00\u4e2a\u6570\u636e\u5e93\u7ed3\u6784\u8fc1\u79fb\u5230\u53e6\u4e00\u4e2a\u7ed3\u6784\u7684\u8fc7\u7a0b\uff0c\u901a\u5e38\u5305\u62ec\uff1a<\/p>\n<\/blockquote>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6a21\u5f0f\u53d8\u66f4\uff08Schema Migration\uff09<\/strong>\uff1a\u4fee\u6539\u6570\u636e\u5e93\u8868\u7ed3\u6784\uff08\u5982\u65b0\u589e\u5b57\u6bb5\u3001\u4fee\u6539\u5b57\u6bb5\u7c7b\u578b\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u8fc1\u79fb\uff08Data Migration\uff09<\/strong>\uff1a\u5c06\u6570\u636e\u4ece\u65e7\u7ed3\u6784\u8c03\u6574\u4e3a\u65b0\u7ed3\u6784\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0b\u4ecb\u7ecd\u5982\u4f55\u5728 JavaScript \u73af\u5883\u4e2d\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528\u8fc1\u79fb\u5de5\u5177<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u4ee3\u6570\u636e\u5e93\u9a71\u52a8\u7a0b\u5e8f\u548c\u6846\u67b6\u901a\u5e38\u5e26\u6709\u8fc1\u79fb\u5de5\u5177\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u7ba1\u7406\u6570\u636e\u5e93\u8fc1\u79fb\u811a\u672c\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Knex.js<\/strong>\uff08\u9002\u7528\u4e8e SQL \u6570\u636e\u5e93\uff09<\/li>\n\n\n\n<li><strong>Mongoose<\/strong>\uff08\u9002\u7528\u4e8e MongoDB\uff09<\/li>\n\n\n\n<li><strong>TypeORM<\/strong>\uff08\u9002\u7528\u4e8e SQL \u548c NoSQL\uff09<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Knex.js \u793a\u4f8b<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Knex \u662f\u4e00\u4e2a SQL \u67e5\u8be2\u751f\u6210\u5668\uff0c\u652f\u6301\u6570\u636e\u5e93\u8fc1\u79fb\u3002<br><strong>\u5b89\u88c5 Knex \u548c\u6570\u636e\u5e93\u9a71\u52a8<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install knex sqlite3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u521d\u59cb\u5316 Knex \u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx knex init<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx knex migrate:make add_users_table<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fc1\u79fb\u811a\u672c\u793a\u4f8b<\/strong><br><code>migrations\/xxxx_add_users_table.js<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>exports.up = function (knex) {\n  return knex.schema.createTable('users', table =&gt; {\n    table.increments('id');\n    table.string('name');\n    table.string('email');\n  });\n};\n\nexports.down = function (knex) {\n  return knex.schema.dropTable('users');\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u8fd0\u884c\u8fc1\u79fb<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npx knex migrate:latest<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Mongoose\uff08MongoDB\uff09\u8fc1\u79fb<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e MongoDB\uff0c\u53ef\u4ee5\u4f7f\u7528 Mongoose \u914d\u5408 <code>migrate-mongoose<\/code> \u6216\u624b\u52a8\u7f16\u5199\u811a\u672c\u8fdb\u884c\u8fc1\u79fb\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u624b\u52a8\u8fc1\u79fb\u793a\u4f8b<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u8981\u5c06\u7528\u6237\u7684 <code>age<\/code> \u5b57\u6bb5\u62c6\u5206\u4e3a <code>birthYear<\/code> \u548c <code>birthMonth<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const mongoose = require('mongoose');\n\nasync function migrate() {\n  const users = await User.find({});\n\n  for (const user of users) {\n    if (user.age) {\n      const birthYear = new Date().getFullYear() - user.age;\n      await User.updateOne(\n        { _id: user._id },\n        { $set: { birthYear, birthMonth: null }, $unset: { age: '' } }\n      );\n    }\n  }\n}\n\nmigrate()\n  .then(() =&gt; console.log('Migration complete'))\n  .catch(err =&gt; console.error(err));<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u4f7f\u7528 TypeORM<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">TypeORM \u652f\u6301\u81ea\u52a8\u751f\u6210\u548c\u7ba1\u7406\u8fc1\u79fb\u6587\u4ef6\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b89\u88c5 TypeORM<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install typeorm reflect-metadata<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>typeorm migration:create -n AddUsersTable<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f16\u5199\u8fc1\u79fb\u903b\u8f91<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AddUsersTable.ts<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { MigrationInterface, QueryRunner, Table } from 'typeorm';\n\nexport class AddUsersTable implements MigrationInterface {\n  public async up(queryRunner: QueryRunner): Promise&lt;void&gt; {\n    await queryRunner.createTable(\n      new Table({\n        name: 'users',\n        columns: &#91;\n          { name: 'id', type: 'int', isPrimary: true, isGenerated: true },\n          { name: 'name', type: 'varchar' },\n          { name: 'email', type: 'varchar', isUnique: true },\n        ],\n      })\n    );\n  }\n\n  public async down(queryRunner: QueryRunner): Promise&lt;void&gt; {\n    await queryRunner.dropTable('users');\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8fd0\u884c\u8fc1\u79fb<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>typeorm migration:run<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u8fc1\u79fb\u6b65\u9aa4<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u5de5\u5177\uff0c\u4ee5\u4e0b\u662f\u901a\u7528\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5907\u4efd\u6570\u636e<\/strong>\uff1a\u5728\u8fc1\u79fb\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u5907\u4efd\uff0c\u4ee5\u9632\u6b62\u4e0d\u53ef\u9006\u7684\u64cd\u4f5c\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u3002<\/li>\n\n\n\n<li><strong>\u7f16\u5199\u8fc1\u79fb\u811a\u672c<\/strong>\uff1a<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Schema \u4fee\u6539<\/strong>\uff1a\u6839\u636e\u9700\u6c42\u8c03\u6574\u8868\u7ed3\u6784\u6216\u96c6\u5408\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u8f6c\u6362<\/strong>\uff1a\u901a\u8fc7\u811a\u672c\u8c03\u6574\u6570\u636e\u4ee5\u9002\u914d\u65b0\u7ed3\u6784\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6d4b\u8bd5\u8fc1\u79fb<\/strong>\uff1a\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u9a8c\u8bc1\u8fc1\u79fb\u811a\u672c\u662f\u5426\u6709\u6548\u3002<\/li>\n\n\n\n<li><strong>\u6267\u884c\u8fc1\u79fb<\/strong>\uff1a\u5c06\u7ecf\u8fc7\u9a8c\u8bc1\u7684\u8fc1\u79fb\u811a\u672c\u5e94\u7528\u5230\u751f\u4ea7\u73af\u5883\u3002<\/li>\n\n\n\n<li><strong>\u56de\u6eda\u673a\u5236<\/strong>\uff1a\u786e\u4fdd\u5728\u8fc1\u79fb\u5931\u8d25\u65f6\u53ef\u4ee5\u5feb\u901f\u56de\u6eda\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6570\u636e\u8fc1\u79fb\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5927\u89c4\u6a21\u6570\u636e\u7684\u5206\u6279\u5904\u7406<\/strong>\uff1a\u907f\u514d\u4e00\u6b21\u6027\u8fc1\u79fb\u5927\u91cf\u6570\u636e\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  async function migrateInBatches(batchSize) {\n    let skip = 0;\n    let hasMore = true;\n\n    while (hasMore) {\n      const users = await User.find().skip(skip).limit(batchSize);\n      if (users.length &gt; 0) {\n        for (const user of users) {\n          \/\/ \u6267\u884c\u6570\u636e\u8fc1\u79fb\n        }\n        skip += batchSize;\n      } else {\n        hasMore = false;\n      }\n    }\n  }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7248\u672c\u7ba1\u7406<\/strong>\uff1a\u4e3a\u8fc1\u79fb\u811a\u672c\u7ef4\u62a4\u7248\u672c\u53f7\uff0c\u786e\u4fdd\u53ef\u8ffd\u6eaf\u6027\u3002<\/li>\n\n\n\n<li><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong>\uff1a\u8bb0\u5f55\u8fc1\u79fb\u5f00\u59cb\u3001\u7ed3\u675f\u3001\u9519\u8bef\u7b49\u91cd\u8981\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0cJavaScript \u53ef\u4ee5\u7ed3\u5408\u6570\u636e\u5e93\u5de5\u5177\u9ad8\u6548\u8fdb\u884c\u6570\u636e\u8fc1\u79fb\uff0c\u9009\u62e9\u9002\u5408\u4f60\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u662f\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u672c\u8eab\u6ca1\u6709\u5185\u7f6e\u7684\u6570\u636e\u5e93\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\u914d\u5408\u4f7f\u7528\uff08\u5982 MongoDB\u3001MySQL\u3001P [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-nosql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=530"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/530\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/530\/revisions\/531"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}