forked from vsuaste/express_graphql_model_gen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-migrations-cassandra.ejs
50 lines (40 loc) · 1.79 KB
/
create-migrations-cassandra.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
const { cassandraDriver } = require('../models/index');
const dict = require('../utils/graphql-sequelize-types');
/**
* @module - Migrations to create and to undo a table correpondant to a sequelize model.
*/
module.exports = {
/**
* up - Creates a table with the fields specified in the the createTable function.
*
* @param {object} queryInterface Used to modify the table in the database.
* @param {object} Sequelize Sequelize instance with data types included
* @return {promise} Resolved if the table was created successfully, rejected otherwise.
*/
up: async function(){
let createString = "CREATE TABLE <%-namePl-%>(<%- idAttribute -%> <%= cassandraIdAttributeType %> PRIMARY KEY";
let indexCreationStrings = [];
<% let keys = Object.keys(cassandraAttributes) -%>
<%for (let i=0; i< keys.length; i++) {-%>
<% let type_seq = cassandraAttributes[ keys[i] ] -%>
createString += ", <%= keys[i] %> <%= type_seq %>";
indexCreationStrings.push("<%= keys[i] %>");
<%}-%>
createString += ");";
await cassandraDriver.execute(createString);
let indexCreationPromises = indexCreationStrings.map(async i =>
await cassandraDriver.execute('CREATE INDEX IF NOT EXISTS ' + i + '_index ON <%-namePl-%> (' + i + ');'));
await Promise.allSettled(indexCreationPromises);
},
/**
* down - Deletes a table.
*
* @param {object} queryInterface Used to modify the table in the database.
* @param {object} Sequelize Sequelize instance with data types included
* @return {promise} Resolved if the table was deleted successfully, rejected otherwise.
*/
down: async function(){
await cassandraDriver.execute('DROP TABLE IF EXISTS <%-namePl-%>');
}
};