Merge branch 'master' of github.com:supabase/supabase

This commit is contained in:
Paul Copplestone committed 2020-02-06 17:35:30 +08:00
commit 6e47eaf2de
4 files changed
+188 -161

No files matched your search

@@ -2,7 +2,7 @@ version: '3'
services:
supabase:
image: supabase/supabase-dev:0.1.3
image: supabase/supabase-dev:0.1.4
ports:
- "8000:8000"
environment:
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@supabase/supabase-js",
"version": "0.1.17",
"version": "0.1.18",
"description": "Supabase Realtime API",
"main": "./lib/index.js",
"scripts": {
@@ -49,7 +49,7 @@
"rimraf": "^2.6.2"
},
"dependencies": {
"@supabase/postgrest-js": "^0.1.1",
"@supabase/postgrest-js": "^0.4.0",
"@supabase/realtime-js": "^0.1.6",
"superagent": "^5.2.1"
}
+1 -1
View File
@@ -119,7 +119,7 @@ class Supabase {
*/
initClient() {
let rest = new PostgrestClient(this.restUrl, { apikey: this.apikey })
let rest = new PostgrestClient(this.restUrl, {queryParams: { apikey: this.apikey }})
let api = rest.from(this.tableName)
return api
+184 -157
View File
@@ -8,170 +8,197 @@
* @param {Object} records
* @param {Object} options The map of various options that can be applied to the mapper
* @param {Array} options.skipTypes The array of types that should not be converted
*
*
* @example
* convertChangeData([{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], {first_name: 'Paul', age:'33'}, {})
* //=>
* { first_name: 'Paul', age: 33 }
*/
export const convertChangeData = (columns, records, options = {}) => {
let result = {}
let skipTypes = typeof options.skipTypes !== 'undefined' ? options.skipTypes : []
Object.entries(records).map(([key, value]) => {
result[key] = convertColumn(key, columns, records, skipTypes)
})
return result
}
/**
* Converts the value of an individual column
* @param {String} columnName The column that you want to convert
* @param {{name: String, type: String}[]} columns All of the columns
* @param {Object} records The map of string values
* @param {Array} skipTypes An array of types that should not be converted
* @return {object} Useless information
*
* @example
* convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], [])
* //=>
* 33
*
* @example
* convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], ['int4'])
* //=>
* "33"
*/
export const convertColumn = (columnName, columns, records, skipTypes) => {
let column = columns.find(x => x.name == columnName)
if(skipTypes.includes(column.type)) return noop(records[columnName])
else return convertCell(column.type, records[columnName])
}
/**
* If the value of the cell is `null`, returns null.
* Otherwise converts the string value to the correct type.
* @param {String} type A postgres column type
* @param {String} stringValue The cell value
*
* @example
* convertCell('bool', 'true')
* //=>
* true
*
* @example
* convertCell('int8', '10')
* //=>
* 10
*/
export const convertCell = (type, stringValue) => {
try {
if (stringValue === null) return null
// If not null, convert to correct type.
switch (type) {
case 'abstime':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'bool':
return toBoolean(stringValue)
case 'date':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'daterange':
return toDateRange(stringValue)
case 'float4':
return toFloat(stringValue)
case 'float8':
return toFloat(stringValue)
case 'int2':
return toInt(stringValue)
case 'int4':
return toInt(stringValue)
case 'int4range':
return toIntRange(stringValue)
case 'int8':
return toInt(stringValue)
case 'int8range':
return toIntRange(stringValue)
case 'json':
return toJson(stringValue)
case 'jsonb':
return toJson(stringValue)
case 'money':
return toFloat(stringValue)
case 'numeric':
return toFloat(stringValue)
case 'oid':
return toInt(stringValue)
case 'reltime':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'time':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'timestamp':
return toTimestampString(stringValue) // Format to be consistent with PostgREST
case 'timestamptz':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'timetz':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'tsrange':
return toDateRange(stringValue)
case 'tstzrange':
return toDateRange(stringValue)
default:
// All the rest will be returned as strings
return noop(stringValue)
}
} catch (error) {
console.log(`Could not convert cell of type ${type} and value ${stringValue}`)
return stringValue
let result = {}
let skipTypes = typeof options.skipTypes !== 'undefined' ? options.skipTypes : []
Object.entries(records).map(([key, value]) => {
result[key] = convertColumn(key, columns, records, skipTypes)
})
return result
}
/**
* Converts the value of an individual column
* @param {String} columnName The column that you want to convert
* @param {{name: String, type: String}[]} columns All of the columns
* @param {Object} records The map of string values
* @param {Array} skipTypes An array of types that should not be converted
* @return {object} Useless information
*
* @example
* convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], [])
* //=>
* 33
*
* @example
* convertColumn('age', [{name: 'first_name', type: 'text'}, {name: 'age', type: 'int4'}], ['Paul', '33'], ['int4'])
* //=>
* "33"
*/
export const convertColumn = (columnName, columns, records, skipTypes) => {
let column = columns.find(x => x.name == columnName)
if(skipTypes.includes(column.type)) return noop(records[columnName])
else return convertCell(column.type, records[columnName])
}
/**
* If the value of the cell is `null`, returns null.
* Otherwise converts the string value to the correct type.
* @param {String} type A postgres column type
* @param {String} stringValue The cell value
*
* @example
* convertCell('bool', 'true')
* //=>
* true
*
* @example
* convertCell('int8', '10')
* //=>
* 10
*
* @example
* convertCell('_int4', '{1,2,3,4}')
* //=>
* [1,2,3,4]
*/
export const convertCell = (type, stringValue) => {
try {
if (stringValue === null) return null
// if data type is an array
if (type.charAt(0) === '_') {
let arrayValue = type.slice(1, type.length)
return toArray(stringValue, arrayValue)
}
}
/**
* Passes back the raw string value
*/
const noop = stringValue => {
// If not null, convert to correct type.
switch (type) {
case 'abstime':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'bool':
return toBoolean(stringValue)
case 'date':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'daterange':
return toDateRange(stringValue)
case 'float4':
return toFloat(stringValue)
case 'float8':
return toFloat(stringValue)
case 'int2':
return toInt(stringValue)
case 'int4':
return toInt(stringValue)
case 'int4range':
return toIntRange(stringValue)
case 'int8':
return toInt(stringValue)
case 'int8range':
return toIntRange(stringValue)
case 'json':
return toJson(stringValue)
case 'jsonb':
return toJson(stringValue)
case 'money':
return toFloat(stringValue)
case 'numeric':
return toFloat(stringValue)
case 'oid':
return toInt(stringValue)
case 'reltime':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'time':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'timestamp':
return toTimestampString(stringValue) // Format to be consistent with PostgREST
case 'timestamptz':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'timetz':
return noop(stringValue) // To allow users to cast it based on Timezone
case 'tsrange':
return toDateRange(stringValue)
case 'tstzrange':
return toDateRange(stringValue)
default:
// All the rest will be returned as strings
return noop(stringValue)
}
} catch (error) {
console.log(`Could not convert cell of type ${type} and value ${stringValue}`)
console.log(`This is the error: ${error}`)
return stringValue
}
const toBoolean = stringValue => {
switch (stringValue) {
case 't':
return true
case 'f':
return false
default:
return null
}
}
const toDate = stringValue => {
return new Date(stringValue)
}
const toDateRange = stringValue => {
let arr = JSON.parse(stringValue)
return [new Date(arr[0]), new Date(arr[1])]
}
const toFloat = stringValue => {
return parseFloat(stringValue)
}
const toInt = stringValue => {
return parseInt(stringValue)
}
const toIntRange = stringValue => {
let arr = JSON.parse(stringValue)
return [parseInt(arr[0]), parseInt(arr[1])]
}
const toJson = stringValue => {
return JSON.parse(stringValue)
}
}
/**
* Fixes timestamp to be ISO-8601. Swaps the space between the date and time for a 'T'
* See https://github.com/supabase/supabase/issues/18
* @returns {string}
*
* @example
* toTimestampString('2019-09-10 00:00:00')
* //=>
* '2019-09-10T00:00:00'
*/
const toTimestampString = stringValue => {
return stringValue.replace(' ', 'T')
}
/**
* Passes back the raw string value
*/
const noop = stringValue => {
return stringValue
}
const toBoolean = stringValue => {
switch (stringValue) {
case 't':
return true
case 'f':
return false
default:
return null
}
}
const toDate = stringValue => {
return new Date(stringValue)
}
const toDateRange = stringValue => {
let arr = JSON.parse(stringValue)
return [new Date(arr[0]), new Date(arr[1])]
}
const toFloat = stringValue => {
return parseFloat(stringValue)
}
const toInt = stringValue => {
return parseInt(stringValue)
}
const toIntRange = stringValue => {
let arr = JSON.parse(stringValue)
return [parseInt(arr[0]), parseInt(arr[1])]
}
const toJson = stringValue => {
return JSON.parse(stringValue)
}
const toArray = (stringValue, type) => {
// this takes off the '{' & '}'
let stringEnriched = stringValue.slice(1, stringValue.length - 1)
console.log('this is the string Enriched ', stringEnriched)
// converts the string into an array
let stringArray = stringEnriched.split(',')
let array = stringArray.map(string => {
return convertCell(type, string)
})
return array
}
/**
* Fixes timestamp to be ISO-8601. Swaps the space between the date and time for a 'T'
* See https://github.com/supabase/supabase/issues/18
* @returns {string}
*
* @example
* toTimestampString('2019-09-10 00:00:00')
* //=>
* '2019-09-10T00:00:00'
*/
const toTimestampString = stringValue => {
return stringValue.replace(' ', 'T')
}