Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat (client): drop the DAL and the generator #1392

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/clients_typescript_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: pnpm
- name: Build Generator
run: make build_generator
- name: Install
run: make deps
- name: Build
Expand Down
63 changes: 0 additions & 63 deletions .github/workflows/generator_tests.yml

This file was deleted.

6 changes: 1 addition & 5 deletions clients/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
"long": "^5.2.0",
"object.hasown": "^1.1.2",
"ohash": "^1.1.2",
"prisma": "4.8.1",
"prompts": "^2.4.2",
"protobufjs": "^7.1.1",
"squel": "^5.13.0",
"text-encoder-lite": "^2.0.0",
Expand Down Expand Up @@ -252,7 +252,6 @@
"vue-tsc": "^1.8.27"
},
"peerDependencies": {
"prisma": "4.8.1",
"react": ">= 16.8.0",
"react-dom": ">= 16.8.0",
"react-native": ">= 0.68.0",
Expand All @@ -264,9 +263,6 @@
"expo-sqlite": {
"optional": true
},
"prisma": {
"optional": true
},
"react": {
"optional": true
},
Expand Down
65 changes: 65 additions & 0 deletions clients/typescript/src/client/conversions/converter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Row } from '../../util'
import { TableSchema } from '../model'
import { PgType } from './types'

export interface Converter {
Expand All @@ -7,12 +9,48 @@ export interface Converter {
* @param pgType The Postgres type of the column in which to store the value.
*/
encode(v: any, pgType: PgType): any
/**
* Encodes the provided row for storing in the database.
* @param row The row to encode
* @param tableSchema The schema of the table for this row.
*/
encodeRow(
row: Record<string, unknown>,
tableSchema: Pick<TableSchema, 'fields'>
): Row
/**
* Encodes the provided rows for storing in the database.
* @param rows The rows to encode
* @param tableSchema The schema of the table for these rows.
*/
encodeRows(
rows: Array<Record<string, unknown>>,
tableSchema: Pick<TableSchema, 'fields'>
): Array<Row>
/**
* Decodes the provided value from the database.
* @param v The value to decode.
* @param pgType The Postgres type of the column from which to decode the value.
*/
decode(v: any, pgType: PgType): any
/**
* Decodes the provided row from the database.
* @param row The row to decode
* @param tableSchema The schema of the table for this row.
*/
decodeRow<T extends Record<string, any> = Record<string, any>>(
row: Record<string, unknown>,
tableSchema: Pick<TableSchema, 'fields'>
): T
/**
* Decodes the provided rows from the database.
* @param rows The rows to decode
* @param tableSchema The schema of the table for these rows.
*/
decodeRows<T extends Record<string, any> = Record<string, any>>(
rows: Array<Record<string, unknown>>,
tableSchema: Pick<TableSchema, 'fields'>
): Array<T>
}

/**
Expand All @@ -26,3 +64,30 @@ export interface Converter {
export function isDataObject(v: unknown): boolean {
return v instanceof Date || typeof v === 'bigint' || ArrayBuffer.isView(v)
}

export function mapRow<T extends Record<string, any> = Record<string, any>>(
row: Record<string, any>,
tableSchema: Pick<TableSchema, 'fields'>,
f: (v: any, pgType: PgType) => any
): T {
const mappedRow = {} as T

for (const [key, value] of Object.entries(row)) {
const pgType = tableSchema.fields[key]
const mappedValue =
pgType === undefined
? value // it's an unknown column, leave it as is
: f(value, pgType)
mappedRow[key as keyof T] = mappedValue
}

return mappedRow
}

export function mapRows<T extends Record<string, any> = Record<string, any>>(
rows: Array<Record<string, unknown>>,
tableSchema: Pick<TableSchema, 'fields'>,
f: (v: any, pgType: PgType) => any
): T[] {
return rows.map((row) => mapRow<T>(row, tableSchema, f))
}
3 changes: 2 additions & 1 deletion clients/typescript/src/client/conversions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { postgresConverter } from './postgres'
export type { Converter } from './converter'
export { sqliteConverter } from './sqlite'
export { postgresConverter } from './postgres'
export { PgBasicType } from './types'
Loading
Loading