├── README.md ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # Bigquery Schema Generator 2 | 3 | Generate schema for [Google bigquery](https://cloud.google.com/bigquery/) by inspecting data to conform with [data types](https://cloud.google.com/bigquery/data-types). Currently, the `BYTES` type is not supported. 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm i -D bigquery-schema-generator 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | var generator = require('bigquery-schema-generator') 15 | var schema = generator(data) 16 | ``` 17 | 18 | The required mode is currently not supported. Everything will result in `NULLABLE` or `REPEATED` if an array is detected. 19 | 20 | ## Example 21 | 22 | ```js 23 | var generator = require('bigquery-schema-generator') 24 | var pkg = require('./package.json') 25 | var schema = generator(pkg) 26 | ``` 27 | 28 | The schema will look like: 29 | 30 | ```json 31 | [ 32 | { 33 | "name": "name", 34 | "type": "STRING", 35 | "mode": "NULLABLE" 36 | }, 37 | { 38 | "name": "version", 39 | "type": "STRING", 40 | "mode": "NULLABLE" 41 | }, 42 | { 43 | "name": "main", 44 | "type": "STRING", 45 | "mode": "NULLABLE" 46 | }, 47 | { 48 | "name": "repository", 49 | "type": "RECORD", 50 | "mode": "NULLABLE", 51 | "fields": [ 52 | { 53 | "name": "type", 54 | "type": "STRING", 55 | "mode": "NULLABLE" 56 | }, 57 | { 58 | "name": "url", 59 | "type": "STRING", 60 | "mode": "NULLABLE" 61 | } 62 | ] 63 | }, 64 | { 65 | "name": "bugs", 66 | "type": "RECORD", 67 | "mode": "NULLABLE", 68 | "fields": [ 69 | { 70 | "name": "url", 71 | "type": "STRING", 72 | "mode": "NULLABLE" 73 | } 74 | ] 75 | }, 76 | { 77 | "name": "homepage", 78 | "type": "STRING", 79 | "mode": "NULLABLE" 80 | }, 81 | { 82 | "name": "keywords", 83 | "type": "STRING", 84 | "mode": "REPEATED" 85 | }, 86 | { 87 | "name": "author", 88 | "type": "STRING", 89 | "mode": "NULLABLE" 90 | }, 91 | { 92 | "name": "license", 93 | "type": "STRING", 94 | "mode": "NULLABLE" 95 | } 96 | ] 97 | ``` 98 | 99 | ## License 100 | 101 | MIT 102 | 103 | 104 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | module.exports = function generate(data) { 4 | return traverse([], data) 5 | } 6 | 7 | 8 | function getMode(val) { 9 | if(Array.isArray(val)) return "REPEATED" 10 | else return "NULLABLE" 11 | } 12 | 13 | 14 | function getType(val) { 15 | if(typeof val === 'boolean') return "BOOLEAN" 16 | if(!isNaN(val)){ 17 | if(Number.isInteger(parseFloat(val))) return "INTEGER" 18 | return "FLOAT" 19 | } 20 | if(val instanceof Date) return "TIMESTAMP" 21 | if(Array.isArray(val)) return getType(val[0]) 22 | if(typeof val === 'object') return "RECORD" 23 | 24 | if(typeof val === 'string') { 25 | if(val.match(/\d{4}-\d{2}-\d{2}/)) return "DATE" 26 | if(val.length > 18 && !isNaN((new Date(val)).getTime())) return "TIMESTAMP" 27 | } 28 | 29 | return "STRING" 30 | } 31 | 32 | 33 | function traverse(arr, data) { 34 | return Object.keys(data).map((key) => { 35 | let val = data[key] 36 | let meta = { 37 | name: key, 38 | type: getType(data[key]), 39 | mode: getMode(data[key]) 40 | } 41 | 42 | if(meta.type === 'RECORD') { 43 | meta.fields = traverse([], (meta.mode === 'REPEATED') ? val[0] : val) 44 | } 45 | 46 | return meta 47 | }) 48 | 49 | } 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bigquery-schema-generator", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "repository": { 6 | "type": "git", 7 | "url": "git://github.com/nw/bigquery-schema-generator.git" 8 | }, 9 | "bugs": { 10 | "url": "https://github.com/nw/bigquery-schema-generator/issues" 11 | }, 12 | "homepage": "https://github.com/nw/bigquery-schema-generator", 13 | "keywords": [ 14 | "bigquery", 15 | "schema", 16 | "google cloud", 17 | "gcp" 18 | ], 19 | "author": "Nathan White (http://nwhite.net)", 20 | "license": "MIT" 21 | } 22 | --------------------------------------------------------------------------------