github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/acceptance/testdata/node/sequelize-test.js (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 const assert = require('assert'); 12 const fs = require('fs'); 13 14 const Sequelize = require('sequelize-cockroachdb'); 15 const Op = Sequelize.Op; 16 17 const config = { 18 dialect: 'postgres', 19 host: process.env.PGHOST || 'localhost', 20 port: process.env.PGPORT || 26257, 21 logging: false, 22 }; 23 24 if (process.env.PGSSLCERT && process.env.PGSSLKEY) { 25 config.ssl = true; 26 config.dialectOptions = { 27 ssl: { 28 cert: fs.readFileSync(process.env.PGSSLCERT), 29 key: fs.readFileSync(process.env.PGSSLKEY) 30 } 31 }; 32 } 33 34 const sequelize = new Sequelize('node_test', 'root', '', config); 35 36 describe('sequelize', () => { 37 after(() => { 38 sequelize.close(); 39 }); 40 41 it('can create a model with a json field', () => { 42 var Cat = sequelize.define('cat', { 43 id: {type: Sequelize.INTEGER, primaryKey: true}, 44 data: {type: Sequelize.JSONB}, 45 }); 46 47 return Cat.sync({force: true}) 48 .then(() => { 49 return Cat.bulkCreate([ 50 {id: 1, data: {name: 'smudge'}}, 51 {id: 2, data: {name: 'sissel'}}, 52 ]); 53 }) 54 .then(() => { 55 return Cat.findAll(); 56 }) 57 .then(result => { 58 assert.deepEqual(result[0].dataValues.id, 1); 59 assert.deepEqual(result[0].dataValues.data, {name: 'smudge'}); 60 assert.deepEqual(result[1].dataValues.id, 2); 61 assert.deepEqual(result[1].dataValues.data, {name: 'sissel'}); 62 }); 63 }); 64 65 it('can create a model with an inverted index', () => { 66 var Android = sequelize.define( 67 'androids', 68 { 69 id: {type: Sequelize.INTEGER, primaryKey: true}, 70 data: {type: Sequelize.JSONB}, 71 }, 72 { 73 // Not sure of a good but not fragile way to verify that this index was 74 // actually created. 75 indexes: [ 76 { 77 fields: ['data'], 78 using: 'gin', 79 }, 80 ], 81 } 82 ); 83 84 return Android.sync({force: true}) 85 .then(() => { 86 return Android.bulkCreate([ 87 {id: 1, data: {name: '2B'}}, 88 {id: 2, data: {name: '9S'}}, 89 ]); 90 }) 91 .then(() => { 92 return Android.findAll({ 93 where: { 94 data: { 95 [Op.contains]: {name: '2B'}, 96 }, 97 }, 98 }); 99 }) 100 .then(result => { 101 assert.deepEqual(result[0].dataValues.id, 1); 102 assert.deepEqual(result[0].dataValues.data, {name: '2B'}); 103 }); 104 }); 105 });