feat: JSONB

This commit is contained in:
cha0s 2021-01-23 15:24:20 -06:00
parent a439c65413
commit 66dc1b58af
3 changed files with 19 additions and 13 deletions

View File

@ -1,6 +1,7 @@
import {gather} from '@latus/core';
export {default as Resource} from './resource';
export * as JSONB from './jsonb';
export {default as JsonResource} from './json-resource';
export default {

View File

@ -1,5 +1,6 @@
import merge from 'deepmerge';
import {parse} from './jsonb';
import Resource from './resource';
export default class JsonResource extends Resource {
@ -48,19 +49,7 @@ export default class JsonResource extends Resource {
}
static async read(uri) {
const buffer = await super.read(uri);
let string;
if ('client' === process.env.SIDE) {
const decoder = new TextDecoder();
string = decoder.decode(buffer);
}
else {
// eslint-disable-next-line global-require
const {StringDecoder} = require('string_decoder');
const decoder = new StringDecoder();
string = decoder.end(buffer);
}
return this.extendJson(JSON.parse(string));
return this.extendJson(parse(await super.read(uri)));
}
}

View File

@ -0,0 +1,16 @@
export const bufferify = (value) => Buffer.from(JSON.stringify(value));
export const parse = (buffer) => {
let string;
if ('client' === process.env.SIDE) {
const decoder = new TextDecoder();
string = decoder.decode(buffer);
}
else {
// eslint-disable-next-line global-require
const {StringDecoder} = require('string_decoder');
const decoder = new StringDecoder();
string = decoder.end(buffer);
}
return JSON.parse(string);
};