Skip to content

Commit

Permalink
feat!: replace current DB access with Data Access Library
Browse files Browse the repository at this point in the history
This PR integrates the data access library in the typescript-client.
Fixes VAX-526, VAX-587, VAX-613 and VAX-623

*Code structure:*

- The DAL source files reside in `src/client`.
The main functionality is implemented by `src/client/model/table.ts`
which represents a table from the DB and provides a Prisma-inspired API
for CRUD operations on that table.

- The entry point of the DAL is the static `ElectricClient.create`
method which takes a description of the DB (generated by our custom
Prisma generator) as input and generates a namespace that extends the
electric namespace with a property for each table.

- The `src/client/input` folder defines the input types that are
accepted by the different methods of the API of a table.

- The `src/client/execution/executor.ts` file defines an `Executor`
class wraps a DB adapter and a notifier and provides methods to execute
queries and transactions while also calling `potentiallyChanged` on the
notifier.

*Notes about the code:*

The main code is located in `src/client/model/table.ts`.
This `Table` class implements the API of the DAL.
The queries are executed using one of the supported drivers.

However, drivers have subtle differences: some are synchronous (e.g.
better-sqlite3) some are async (e.g. wa-sqlite). Within the async
drivers, some support promises, others do not support promises and
require continuation passing style using callbacks.

For this reason, the unified adapter API also requires passing callbacks
and thus writing code in continuation passing style.
**As a result, the `Table` class is also implemented in continuation
passing style.** Thus, internally, we can compose API calls but have to
pass callbacks around.

---------

BREAKING CHANGE: The way how queries are executed against the DB has changed. All database access goes through the Data Access Library (DAL) which provides a Prisma-like API. Raw SQL queries can still be executed using the `raw` and `liveRaw` methods of the DAL.
  • Loading branch information
kevin-dp authored May 8, 2023
1 parent dbfdc29 commit 3c84de9
Show file tree
Hide file tree
Showing 114 changed files with 38,536 additions and 4,577 deletions.
36 changes: 29 additions & 7 deletions clients/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
],
"debug": [
"./dist/util/debug/index.d.ts"
],
"client/model": [
"./dist/client/model/index.d.ts"
]
}
},
Expand Down Expand Up @@ -127,26 +130,43 @@
"exponential-backoff": "^3.1.0",
"fastestsmallesttextencoderdecoder": "^1.0.22",
"frame-stream": "^3.0.1",
"lodash.flow": "^3.5.0",
"lodash.mapvalues": "^4.6.0",
"lodash.throttle": "^4.1.1",
"lodash.partition": "^4.6.0",
"lodash.pick": "^4.4.0",
"lodash.groupby": "^4.6.0",
"lodash.omitby": "^4.6.0",
"object.hasown": "^1.1.2",
"loglevel": "^1.8.1",
"long": "^5.2.0",
"protobufjs": "^7.1.1",
"react-native-uuid": "^2.0.1",
"sqlite-parser": "^1.0.1",
"squel": "^5.13.0",
"uuid": "^9.0.0",
"walkjs": "^3.2.4",
"ws": "^8.8.1"
"ws": "^8.8.1",
"zod": "^3.20.2"
},
"devDependencies": {
"@prisma/client": "^4.12.0",
"@ikscodes/browser-env": "^1.0.0",
"@testing-library/react": "^13.4.0",
"@tsmodule/tsmodule": "42",
"@types/base-64": "^1.0.0",
"@types/better-sqlite3": "^7.6.3",
"@types/better-sqlite3": "7.6.3",
"@types/cordova-sqlite-storage": "^1.5.5",
"@types/lodash.flow": "^3.5.7",
"@types/lodash.mapvalues": "^4.6.7",
"@types/lodash.throttle": "^4.1.7",
"@types/lodash.partition": "^4.6.7",
"@types/lodash.pick": "^4.4.7",
"@types/lodash.groupby": "^4.6.7",
"@types/lodash.omitby": "^4.6.7",
"@types/node": "^18.8.4",
"@types/react": "^18.0.18",
"@types/react-native-sqlite-storage": "^5.0.2",
"@types/react-native-sqlite-storage": "^6.0.0",
"@types/react-native-uuid": "^2.0.0",
"@types/ws": "^8.5.3",
"@typescript-eslint/eslint-plugin": "^5.34.0",
Expand All @@ -158,14 +178,16 @@
"husky": "^8.0.3",
"lint-staged": "^13.1.0",
"prettier": "2.8.2",
"prisma": "^4.8.1",
"prisma-generator-electric": "electric-sql/prisma-generator-electric#kevindp/build-on-install",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"release-it": "^15.5.0",
"ts-proto": "^1.125.0",
"typeorm": "^0.3.9",
"typescript": "^4.9",
"web-worker": "^1.2.0",
"wa-sqlite": "git+https://github.com/rhashimoto/wa-sqlite#breaking-changes"
"wa-sqlite": "git+https://github.com/rhashimoto/wa-sqlite#breaking-changes",
"web-worker": "^1.2.0"
},
"peerDependencies": {
"@aphro/absurd-sql": "^0.0.53",
Expand All @@ -176,8 +198,8 @@
"react": ">= 16.8.0",
"react-native": ">= 0.68.0",
"react-native-sqlite-storage": ">= 6.0.0",
"wa-sqlite": "git+https://github.com/rhashimoto/wa-sqlite#breaking-changes",
"typeorm": ">=0.3.0"
"typeorm": ">=0.3.0",
"wa-sqlite": "git+https://github.com/rhashimoto/wa-sqlite#breaking-changes"
},
"peerDependenciesMeta": {
"@aphro/absurd-sql": {
Expand Down
Loading

0 comments on commit 3c84de9

Please sign in to comment.