| 102 | } |
| 103 | |
| 104 | static _create(modelName, currentKeyspace, modelOptions) { |
| 105 | if (!currentKeyspace && (!modelOptions || !modelOptions.keyspace)) { |
| 106 | throw new Error( |
| 107 | 'You should specify the keyspace of the model in the MappingOptions when the Client is not using a keyspace'); |
| 108 | } |
| 109 | |
| 110 | if (!modelOptions) { |
| 111 | return ModelMappingInfo.createDefault(modelName, currentKeyspace); |
| 112 | } |
| 113 | |
| 114 | let tables; |
| 115 | |
| 116 | if (modelOptions.tables && modelOptions.tables.length > 0) { |
| 117 | tables = modelOptions.tables.map(item => { |
| 118 | const table = { name: null, isView: false }; |
| 119 | if (typeof item === 'string') { |
| 120 | table.name = item; |
| 121 | } else if (item) { |
| 122 | table.name = item.name; |
| 123 | table.isView = !!item.isView; |
| 124 | } |
| 125 | |
| 126 | if (!table.name) { |
| 127 | throw new Error(`Table name not specified for model '${modelName}'`); |
| 128 | } |
| 129 | |
| 130 | return table; |
| 131 | }); |
| 132 | } else { |
| 133 | tables = [ { name: modelName, isView: false }]; |
| 134 | } |
| 135 | |
| 136 | if (modelOptions.mappings && !(modelOptions.mappings instanceof TableMappings)) { |
| 137 | throw new Error('mappings should be an instance of TableMappings'); |
| 138 | } |
| 139 | |
| 140 | const columns = new Map(); |
| 141 | if (modelOptions.columns !== null && typeof modelOptions.columns === 'object') { |
| 142 | Object.keys(modelOptions.columns).forEach(columnName => { |
| 143 | columns.set(columnName, ModelColumnInfo.parse(columnName, modelOptions.columns[columnName])); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | return new ModelMappingInfo( |
| 148 | modelOptions.keyspace || currentKeyspace, |
| 149 | tables, |
| 150 | modelOptions.mappings || new DefaultTableMappings(), |
| 151 | columns |
| 152 | ); |
| 153 | } |
| 154 | |
| 155 | static createDefault(modelName, currentKeyspace) { |
| 156 | return new ModelMappingInfo( |