-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DAL): client-side support for JSON type (#633)
This PR adds client-side support for the JSON type. The DAL accepts JS values representing JSON and serialises them to a string that is stored in SQLite. When reading a JSON value from the database the string is deserialised back into a JS value. A corner case arises when having an optional column of type JSON because we need to differentiate between the database NULL value and the JSON null value. We treat the regular JS null value as a database NULL (to be consistent with how null values are interpreted for other column types) and require users to pass a special JsonNull object in order to store a top-level JSON null value. Still need to add an E2E test for json values. --------- Co-authored-by: Oleksii Sholik <[email protected]>
- Loading branch information
Showing
32 changed files
with
11,771 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"electric-sql": patch | ||
"@electric-sql/prisma-generator": patch | ||
--- | ||
|
||
Adds client-side support for JSON type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './io' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { readFile, writeFile } from 'fs/promises' | ||
|
||
/* | ||
* Replaces the first occurence of `find` by `replace` in the file `file`. | ||
* If `find` is a regular expression that sets the `g` flag, then it replaces all occurences. | ||
*/ | ||
export async function findAndReplaceInFile( | ||
find: string | RegExp, | ||
replace: string, | ||
file: string | ||
) { | ||
const content = await readFile(file, 'utf8') | ||
const replacedContent = content.replace(find, replace) | ||
await writeFile(file, replacedContent) | ||
} |
27 changes: 27 additions & 0 deletions
27
clients/typescript/src/client/conversions/datatypes/json.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// not the most precise JSON type | ||
// but good enough for serialising/deserialising | ||
type JSON = string | number | boolean | Array<any> | Record<string, any> | ||
|
||
export function serialiseJSON(v: JSON): string { | ||
if (isJsonNull(v)) { | ||
// user provided the special `JsonNull` value | ||
// to indicate a JSON null value rather than a DB NULL | ||
return JSON.stringify(null) | ||
} | ||
return JSON.stringify(v) | ||
} | ||
|
||
export function deserialiseJSON(v: string): JSON { | ||
if (v === JSON.stringify(null)) return { __is_electric_json_null__: true } | ||
return JSON.parse(v) | ||
} | ||
|
||
function isJsonNull(v: JSON): boolean { | ||
return ( | ||
typeof v === 'object' && | ||
!Array.isArray(v) && | ||
v !== null && | ||
Object.hasOwn(v, '__is_electric_json_null__') && | ||
v['__is_electric_json_null__'] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.