Hướng dẫn insert into mysql2 promise - chèn vào lời hứa mysql2

Tôi đang cố gắng sử dụng Async đang chờ đợi với mysql2 và gộp lại nhưng tôi nghĩ rằng tôi làm những điều sai trái. Dưới đây là mã của tôi (như tôi đã nói, tôi không thực sự chắc chắn nếu tôi đang làm mọi việc ngay tại đây).

    const pool = mysql.createPool({
      host: 'localhost',
      user: 'root',
      database: 'test',
      password: '',
      waitForConnections: true,
      connectionLimit: 10,
      queueLimit: 0
    });
    
    const promisePool = pool.promise();   // Get a Promise wrapped instance of that pool   
    

app.post('/api/register', (req, res) => {

    async function queryDB() {
    
        try {
          const addUser = await promisePool.execute(
            "INSERT INTO users (company, email, password) VALUES (?,?,?)",
            ['Ikea', '', '123'],
          )
          const addAnother = await promisePool.execute(
            "INSERT INTO users (company, email, password) VALUES (?,?,?)",
            ['Google', '', '123']
          )
    
          console.log(addUser)
          res.status(200).json('Users saved')
          
        } catch (err) {
          console.log(err)
          res.status(409).json('User already exist')
        };
      }
    
      queryDB();
})

Ý tưởng là "adduser" nên được lưu và nếu nó không phải là duy nhất, sẽ xảy ra lỗi (vì cột email cơ sở dữ liệu SQL được đặt thành duy nhất) và sau đó addanother sẽ không bắt đầu chèn.

Những gì tôi không thể hiểu là làm thế nào để nhận được phần chèn vào phần chèn adduser? Tôi sẽ cần nó cho lần chèn thứ hai. Nếu tôi Console.Log Adduser, tôi có thể thấy một đối tượng như thế này:

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn

Vì vậy, Insertid được gửi lại cho tôi, nhưng tôi dường như không thể đạt được nó. Nếu tôi cố gắng lấy nó, nó không thể đọc tài sản của không xác định. Tôi cảm thấy rằng tôi đang làm điều này sai tất cả, vậy tôi nên làm như thế nào để có được sự chèn ép?

X Devapi bao gồm các phương pháp để làm việc với các bộ sưu tập NoQuery và các bảng quan hệ. Hướng dẫn này trình bày Trình kết nối/Node.js của X Devapi.

Một số ví dụ cho rằng một phiên đã được tạo và một đối tượng

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
8 tồn tại. Có một phần mô tả chi tiết cách kết nối với cơ sở dữ liệu và tạo phiên giao thức X. Ngoài ra, API cũng cung cấp các phương thức tiện ích để làm việc với các tài liệu đơn lẻ.

Tạo bộ sưu tập

Một bộ sưu tập là một bảng mục đích đặc biệt để lưu trữ tài liệu. Để tạo bộ sưu tập, người dùng chỉ phải cung cấp tên cho mô -đun: Schema#createdCollection:

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });

Hàm

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
9 trả về một lời hứa giải quyết thành một đối tượng thu thập thành công.

Nếu một bộ sưu tập nhất định đã tồn tại trong cơ sở dữ liệu, cuộc gọi

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
0 sẽ không thành công trừ khi thuộc tính
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
1 được xác định trong một đối tượng tùy chọn bổ sung như sau:

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });

Ví dụ, đối tượng tùy chọn này có thể được sử dụng để tạo lược đồ xác thực tài liệu phía máy chủ. Đối với điều đó, người ta có thể bao gồm một thuộc tính

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
2 phù hợp với định nghĩa lược đồ JSON hợp lệ trong một đối tượng
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
3 bên ngoài. Thuộc tính
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
4, được sử dụng để kích hoạt hiệu quả (
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
5) hoặc vô hiệu hóa (
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
6) một lược đồ, cũng nên được đưa vào.

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });

Khi cố gắng chèn một tài liệu vi phạm định nghĩa lược đồ cho bộ sưu tập, một lỗi được ném:

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });

Lược đồ được tạo nhưng không được bật khi thuộc tính

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
4 không có hoặc được đặt thành
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
6 và bất kỳ tài liệu nào sẽ được chèn vào trường hợp đó.

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.OFF };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();

Phương pháp

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
9 được sử dụng để kích hoạt lược đồ JSON trên bộ sưu tập hiện có (hoặc cập nhật nó nếu nó đã tồn tại).

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').modifyCollection('myCollection', { validation })
    });

Vô hiệu hóa lược đồ JSON trên một bộ sưu tập hiện có có thể được thực hiện bằng cách đặt thuộc tính

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
4 thành
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
6 trong đối tượng Tùy chọn
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
3.

const mysqlx = require('@mysql/xdevapi');
const validation = { level: mysqlx.Schema.ValidationLevel.OFF }

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').modifyCollection('myCollection', { validation })
    });

Việc kích hoạt lại lược đồ JSON có thể là một số bằng cách đặt thuộc tính

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
4 trở lại
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { reuseExisting: true })
    });
5.

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
0

Liệt kê tất cả các bộ sưu tập hiện có

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
1

Thả một bộ sưu tập

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
2

Xử lý tài liệu trong các bộ sưu tập hiện có

Xem xét một bộ sưu tập

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
5 chứa các tài liệu sau:

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
3

Các tài liệu này có thể được sửa đổi hoặc loại bỏ trong một số điều kiện cụ thể, như được hiển thị bên dưới.

Để sửa đổi/xóa các tài liệu cụ thể khỏi bộ sưu tập, người ta sẽ cung cấp điều kiện lọc thích hợp khi gọi

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
6 hoặc
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
7. Để biết thêm chi tiết về định dạng biểu thức, vui lòng kiểm tra hướng dẫn sử dụng.

Để sửa đổi/xóa tất cả các tài liệu khỏi bộ sưu tập, người ta nên cung cấp bất kỳ biểu thức nào đánh giá thành

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
8 (nếu không có biểu thức nào được cung cấp, việc thực hiện thao tác sẽ dẫn đến lỗi) khi gọi
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
6 hoặc
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
7.

Xóa các tài liệu phù hợp với một tiêu chí nhất định

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
4

Loại bỏ tất cả các tài liệu

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
5

Sửa đổi các tài liệu phù hợp với một tiêu chí nhất định

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
6

Sửa đổi tất cả các tài liệu

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
7

Cập nhật số lượng lớn các thuộc tính tài liệu

Ngoài ra, bên cạnh việc cập nhật rõ ràng các thuộc tính tài liệu riêng lẻ với

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
1, phương thức
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
2 cho phép cập nhật nhiều thuộc tính trong một cuộc gọi.

Sử dụng

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
2 sẽ xóa các thuộc tính chứa giá trị
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
4, thêm các thuộc tính không có trước đó và cập nhật các thuộc tính hiện có. Hành vi này cũng áp dụng cho các thuộc tính lồng nhau.

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
8

Lưu ý: Chuỗi biểu thức tiêu chí được cung cấp qua

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } }, level: mysqlx.Schema.ValidationLevel.STRICT };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    });
6 Thiết lập các quy tắc lọc, do đó, bất kỳ giá trị
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
6 nào được cung cấp như một phần của các thuộc tính được cập nhật sẽ bị bỏ qua (và sẽ không được cập nhật).

Lấy kích thước bộ sưu tập

Phương pháp

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
7 lấy kích thước bộ sưu tập tại một thời điểm nhất định.

ResultSetHeader {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 72,
  info: '',
  serverStatus: 2,
  warn
9

Con trỏ

Phương pháp

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
8 để truy xuất toàn bộ tập kết quả có nguồn gốc từ mỗi truy vấn
const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060/mySchema')
    .then(sesion => {
        return session.getDefaultSchema().getCollection('myCollection').add({ name: 1 }).execute();
    })
    .catch(err => {
        console.log(err.message); // Document is not valid according to the schema assigned to collection. The JSON document location '#/name' failed requirement 'type' at JSON Schema location '#/properties/name'.
    });
9. Phương pháp này kéo dữ liệu từ bộ nhớ và xóa nó sau đó. Tuy nhiên, có hai API xen kẽ để tiêu thụ các mục nhập kết quả bằng cách sử dụng con trỏ. Một API sử dụng con trỏ dựa trên lực kéo thông thường thông qua phương thức
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
0 bổ sung có sẵn trong trường hợp kết quả. Cái còn lại là API dựa trên lực kéo hoạt động với chức năng gọi lại được cung cấp trong phương thức
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
1, hoàn toàn vô hiệu hóa bộ đệm ở cấp độ kết nối và để lại khả năng đáp ứng đó cho ứng dụng.

Cách tiếp cận dựa trên kéo

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
0

Phương pháp dựa trên đẩy

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
1

Chỉ số bộ sưu tập

Các chỉ mục thu thập là các chỉ mục MySQL thông thường trên các cột ảo trích xuất dữ liệu từ tài liệu JSON. Để tạo một chỉ mục, cả tên chỉ mục và định nghĩa chỉ số đều được yêu cầu.

Định nghĩa chỉ số chứa các thuộc tính sau:

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
2

Một chỉ mục có thể được tạo bằng một trong các loại sau:

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
3

Chi tiết bổ sung về các chỉ số không gian có thể được tìm thấy ở đây.

Tạo một chỉ mục thông thường

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
4

Tạo một chỉ mục đa giá trị

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
5

Tạo một chỉ số không gian

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
6

Bỏ một chỉ số

const mysqlx = require('@mysql/xdevapi');

mysqlx.getSession('mysqlx://localhost:33060')
    .then(session => {
        return session.getSchema('mySchema').createCollection('myCollection');
    })
    .then(collection => {
        // Use the Collection instance.
    })
    .catch(err => {
        // Something went wrong.
    });
7

Gotchas

Các chỉ mục duy nhất hiện không được hỗ trợ bởi plugin X.

Do các hạn chế khác của plugin X, các chỉ mục

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
2 cho các trường
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
3 và các chỉ mục
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
4 cho
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
5 và/hoặc
const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
6 không thể được tạo ra.

Các định nghĩa chỉ số cho các trường tài liệu có chứa các mảng nên chỉ định rõ ràng tùy chọn đa giá trị (

const mysqlx = require('@mysql/xdevapi');
const validation = { schema: { type: 'object', properties: { name: { type: 'string' } } } };

mysqlx.getSession('mysqlx://localhost:33060')
    .then(sesion => {
        return session.getSchema('mySchema').createCollection('myCollection', { validation })
    })
    .then(collection => {
        // the following will work fine
        return collection.add({ name: 1 }).execute();
    });
7).