yggio-prisma 0.0.0-stage → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,150 @@
1
- # Temporary Holding Version
1
+ # yggio-prisma
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A Prisma Client extension whose query engine is the yggio plan. A plan's text
4
+ is read by the core's parser; what the database can answer exactly is pushed
5
+ into `findMany` arguments, and the residual runs natively over the rows they
6
+ return, landed once under the model's typed struct field - a decimal at its
7
+ column's scale, a UUID, an instant at its unit. The documentation page is
8
+ [Prisma](https://platob.github.io/yggio/expression/prisma/).
9
+
10
+ | | |
11
+ | --- | --- |
12
+ | Install | `npm install yggio-prisma yggio` - `yggio` is a peer at the exact version, `@prisma/client` (`>=5`) an optional peer: nothing of it is imported, the extension is a plain object |
13
+ | Prisma | `$extends` from Prisma 5; `$use` in Prisma 5 and 6; tested against 7.10.1 |
14
+ | Doors | `yggioExtension(options)` for `prisma.$extends`: `model.plan(text, params?)` on every model, `yggioWhere(text, params?)` under any operation's `where`; `yggioMiddleware(options)`, the marker filter for `$use` |
15
+ | By hand | `compilePlan(plan, model, { yggio, params?, provider?, insensitive?, field? })` -> `{ args, residual, complete, pushed, columns }`; `modelField(model, yggio, columns?)`; `landRows(model, rows, yggio, columns?)`; `runPlan(landedRows, residual, field, yggio)`; `filterRows(rows, filter, model, columns, yggio)` |
16
+ | Refusals | `PlanRefused`, an `Error` naming what it refuses |
17
+
18
+ ## The extension
19
+
20
+ ```ts
21
+ import * as yggio from 'yggio'
22
+ import { yggioExtension, yggioWhere } from 'yggio-prisma'
23
+
24
+ const db = prisma.$extends(yggioExtension({ yggio, models: [TRADE], provider: 'postgresql' }))
25
+
26
+ // findMany with what pushes, then the residual natively.
27
+ const totals = await db.trade.plan(
28
+ 'select venue, sum(quantity) as total from trade where price > :floor group by venue order by total desc',
29
+ { floor: 10 },
30
+ )
31
+
32
+ // The marker filter: compiled where the operation runs, its keys beside any Prisma filter of the call.
33
+ const buys = await db.trade.findMany({ where: { ...yggioWhere("side = 'BUY' and venue like 'X%'"), tradable: true } })
34
+ ```
35
+
36
+ | Option | Is |
37
+ | --- | --- |
38
+ | `yggio` | the `yggio` module as the caller loaded it |
39
+ | `models` | a list of DMMF models, or `Prisma.dmmf` itself (`{ datamodel: { models, enums } }`), whose `enums` give each enum column its `values`; a model is found by the delegate's `$name`, case aside |
40
+ | `provider` | the datasource provider word - `sqlite`, `postgresql`, `cockroachdb`, `mongodb`, `mysql`, `sqlserver`; text equality pushes exactly under the first four alone; another word is refused |
41
+ | `insensitive` | `true` pushes `ilike` as `mode: 'insensitive'`, only for a database that takes that mode |
42
+ | `marker` | the `where` key `yggioWhere` writes, `$yggio` by default; pass the same name as `yggioWhere`'s third argument |
43
+
44
+ Prisma 7's own `Prisma.dmmf` keeps no `isRequired`, `isList` or `nativeType`,
45
+ and no enum's members: hand each model with them, as the schema states them,
46
+ and each enum column its `values` (`Object.values(Side)`). A model without
47
+ `isRequired` lands every column nullable, and no null test and no order push;
48
+ an enum column without `values` pushes no equality and no `in`.
49
+
50
+ `plan` answers the rows as Prisma answered them where nothing remains or the
51
+ residual is a filter alone, else one object per native output row keyed by the
52
+ plan's output columns. The pushed `where` sits beside the caller's at the top
53
+ level, a key both state joined by `AND`, so a unique key stays where
54
+ `findUnique`, `update`, `delete` and `upsert` read it. A marker filter Prisma
55
+ answers whole runs on any operation; a residual filter runs over `findMany`
56
+ rows alone.
57
+
58
+ ## The middleware
59
+
60
+ ```ts
61
+ prisma.$use(yggioMiddleware({ yggio, models: Prisma.dmmf }))
62
+ const buys = await prisma.trade.findMany({ where: yggioWhere("side = 'BUY' and venue like 'X%'") })
63
+ ```
64
+
65
+ The same marker filter over `{ model, action, args }` and `next`; middleware
66
+ adds no model method, so it has no `plan`.
67
+
68
+ ## Pushdown
69
+
70
+ | Plan | Prisma | Condition |
71
+ | --- | --- | --- |
72
+ | `col = v`, `<>`, `<`, `<=`, `>`, `>=` (either side) | `{ col: { equals \| not \| lt \| lte \| gt \| gte: v } }` | a literal or parameter, not null; equality on every type but `Json`, an enum's where it states its `values`; ranges on `Int`, `BigInt`, `Float`, `Decimal`, `DateTime` alone |
73
+ | `col in (...)`, `not col in (...)` | `{ in }`, `{ notIn }` | every item a non-null literal or parameter |
74
+ | `col between a and b` | `{ gte: a, lte: b }` | a range type, each bound as its comparison pushes |
75
+ | text `col = v`, `col in (...)` | `equals`, `in`, checked again | under `mysql`, `sqlserver` or no `provider`, whose default collations fold case and accents (MySQL's pads trailing spaces); `<>`, `not` and `not in` over text stay whole. Exact under `sqlite`, `postgresql`, `cockroachdb`, `mongodb` |
76
+ | `col = v`, `col in (...)` no row can equal | `{ in: [] }`, the item dropped | an enum value that is no member, a fraction on `Int` or `BigInt`; the equality checked again, `<>` left to the residual |
77
+ | `col > 1.5`, `>=`, `<`, `<=` on `Int`, `BigInt` | `{ gte: 2 }`, `{ lte: 1 }` | the whole bound inside the fraction |
78
+ | a `DateTime` bound between two milliseconds | the millisecond outside it - floored for `>`, `>=`, raised for `<`, `<=`; `=` the range around it - checked again | a native type of more than three fractional digits; `<>` and `in` stay |
79
+ | `col is null`, `col is not null` | `{ equals: null }`, `{ not: null }` | the model states the column optional |
80
+ | `col like 'X%'`, `'%X'`, `'%X%'`, `'X'` | `startsWith`, `endsWith`, `contains`, `equals` | a `String` column that is not `@db.Uuid`, no `_`, no `escape`, `%` at the ends alone; pushed to prune and checked again natively, since a database's `LIKE` may fold case |
81
+ | `col ilike ...` | the same, `mode: 'insensitive'` | `insensitive: true`; checked again |
82
+ | `a or b` | `OR: [...]` | every branch pushes |
83
+ | `not a` | `NOT`, `notIn` | `a` pushes exactly |
84
+ | conjuncts on one column | one object | their keys differ and none states `mode`; else `AND: [...]` |
85
+ | `order by col [desc] [nulls first]` | `orderBy: [{ col: 'asc' \| 'desc' }]`, `{ col: { sort, nulls } }` on an optional column, `nulls: 'last'` unless stated | every key a bare column of a range type or `Boolean` whose nullability the model states, no alias of that name, no grouping; one key that does not push keeps the whole order |
86
+ | `limit`, `offset` | `take`, `skip` | the filter pushed whole with nothing to check again, the order pushed or absent, no `group by`, `having` or aggregate |
87
+ | `select a, b` | `select: { a: true, b: true, ...what the residual reads }` | no star; bare columns with no alias push whole, computed projections and aliases stay; a plan reading no column selects the id |
88
+ | `group by`, `having`, aggregates, `exclude` | - | the residual, behind the pushed `where` |
89
+ | `from` | - | absent or the model's name |
90
+
91
+ A text column pushes equality, `in` and `like` alone, a UUID column equality
92
+ and `in`: a database collation orders text by locale, where the native orders
93
+ by code units. Every literal or parameter a pushed filter compares is
94
+ converted by the column, in the core: a `Decimal` at its scale as text, a
95
+ `DateTime` a `Date` from a `Date`, an ISO text or an epoch count, a `Float` a
96
+ float, a `BigInt` a `bigint`, a UUID canonical.
97
+
98
+ ## The residual
99
+
100
+ The residual is the plan less what pushed, its parameters bound and every
101
+ literal or parameter its `where` compares typed by the datatype of the term it
102
+ meets (`price > 2.5` on a `decimal128` is a decimal comparison). A residual
103
+ that is a filter alone keeps the rows Prisma answered, untouched; any other
104
+ answers the native output. A plan Prisma answers whole answers its rows as
105
+ Prisma typed them - a `Decimal` a `Prisma.Decimal`.
106
+
107
+ ## Types
108
+
109
+ | Prisma type | yggio datatype | Back from a native run |
110
+ | --- | --- | --- |
111
+ | `String` | `utf8`; `uuid` under `@db.Uuid` | `string`, a UUID lower case and hyphenated |
112
+ | `Int` | `int32`; `uint32` under `@db.UnsignedInt`, `@db.UnsignedMediumInt`, `@db.Oid` | `number` |
113
+ | `BigInt` | `int64`; `uint64` under `@db.UnsignedBigInt` | `bigint` |
114
+ | `Float` | `float64` | `number` |
115
+ | `Decimal` | `decimal128(p,s)` under `@db.Decimal(p,s)` (`decimal256` past 38 digits), else `decimal128(38,18)` | `string`, the canonical text (`'12.5'`) |
116
+ | `Boolean` | `boolean` | `boolean` |
117
+ | `DateTime` | `datetime64(ms, UTC)`; `datetime64(us, UTC)` past three fractional digits | `Date`, floored to the millisecond |
118
+ | `Json` | `utf8`, the JSON text | `string`, the JSON text |
119
+ | `Bytes` | `binary` | `Buffer` |
120
+ | an enum | `utf8` | `string` |
121
+ | a list | `serie<T>` | an array |
122
+
123
+ A `Decimal` no native type states the precision and scale of lands as
124
+ `decimal128(38,18)`: a value past 18 fractional digits or 20 integral ones
125
+ fails the residual run (`decimal has more fractional digits than the field
126
+ allows`, `decimal coefficient exceeds 38 digits`).
127
+
128
+ An aggregate answers by its own datatype: `count` a `bigint`, a `sum` over
129
+ integers or decimals a decimal's text, `avg` a `number`. A column is nullable
130
+ unless the model states it required; relations are left out.
131
+
132
+ ## Refusals
133
+
134
+ - A section that is not a read: `create`, a write, a join.
135
+ - `from` naming anything but the model, a location, or a plan.
136
+ - A parameter the call does not give; a literal a column cannot hold
137
+ (`id = 'abc'`, `u = -1` on an unsigned column) - a fraction meeting an
138
+ integer column is none: it bounds the range or matches nothing.
139
+ - A Prisma type with no yggio datatype; an unknown model; a `provider` that is
140
+ no datasource provider.
141
+ - `yggioWhere` text holding anything but a `where` clause, or no text.
142
+ - A residual marker filter under any operation but `findMany`, or beside
143
+ `take`, `skip`, `cursor` or `distinct`, which would cut rows before it runs.
144
+
145
+ ## Tests
146
+
147
+ `npm test` generates the integration client (`prisma generate` into
148
+ `tests/generated/`), runs every suite against the addon beside this package -
149
+ the integration one over an embedded database file through Prisma's driver adapter - then
150
+ `tsc --noEmit`.