@typesql/postgres 0.1.4 → 0.2.0
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/CHANGELOG.md +22 -0
- package/README.md +6 -9
- package/dist/client.d.ts +2 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +17 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -5
- package/dist/index.js.map +1 -1
- package/dist/type-map.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/client.ts +38 -20
- package/src/index.ts +51 -24
- package/src/type-map.ts +26 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @typesql/postgres
|
|
2
2
|
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- `NoResultError`, `Executor` and `TransactionalSql` are no longer re-exported (from `@typesql/drizzle` also `Sql`, `Query`, `RowType`, `ParamsType`, `DefaultTypeMap` and `TypeMap`); import them from `@typesql/core`.
|
|
8
|
+
|
|
9
|
+
- `createBigIntSql(sql)` is replaced by `createSql(sql, { bigint: true })`, and `PostgresJsBigIntSql<DB, Keys>` by `PostgresJsSql<DB, Keys, PostgresJsBigIntTypeMap>`.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- The README's TypeScript Playground example uses `createSql(sql, { bigint: true })` instead of the removed `createBigIntSql`.
|
|
14
|
+
|
|
15
|
+
- Updated dependencies:
|
|
16
|
+
- @typesql/core@0.5.0
|
|
17
|
+
|
|
18
|
+
## 0.1.5
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- Updated dependencies:
|
|
23
|
+
- @typesql/core@0.4.0
|
|
24
|
+
|
|
3
25
|
## 0.1.4
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ typed from the SQL.
|
|
|
9
9
|
pnpm add @typesql/core @typesql/postgres postgres
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
[Open an example in the TypeScript Playground](https://www.typescriptlang.org/play/#code/
|
|
12
|
+
[Open an example in the TypeScript Playground](https://www.typescriptlang.org/play/#code/PTAEBUE8AcFMGUCKAZUB3AlgFwBamgPYDOWA5gE6xEB0AVkaABQACWMVAjgDbCEkVUAlNVCIArrHIYqocmIB26bHiLdqCogEMAZrEZZYADywAaUADdNXCUUFnN8gCayCaIgCgQoTZVBs4zgBGkOg4mlj4xGSUNPT4PkRUAFygGPJYABypDJqgJFLypGYE5N6ggRikaRGYuKAAQpUAkunx5ERppKAE8tTu7hgAtoTkEf6woADeoADCBFxmAOKw8pLhsI5mAHJiXFyagVwTAL6g2uQEg6AARKzsqjwAxiWw1wDcA8MlEdOPlOvwbigU7nS43O5wB68KICIjvT4jMbsKagQFcYFnC5Xa58aJUeEDdKSbSaR4TAAi9Sm7gAkGJEu0UpNaTSMI4UstVuR1o4ADzXaoAFmuAD4PjSabBBpoMFwUnMuPyDMZReKaY4MERoPtIAB9eSaQawFI7PYHI5KoxYVUsv6wHm68IclZrAx865YIZULCG6BYABeNppx3FuKITJZbOdXJ5-OqGSDNM0YlwJV1Udm8zj6WFYpZnqwR3lWY9VsTPtI4czitLxgA2gBdIMh9zHfqOWCPfa+Z7yEigRyBFJot6gLy42GMHEwmJJEBcAiPKw4KLATTQaDXQTuXv9h6gAC8oDtAO4vMpIsYg8EHx3PX7YcP3jQMoiD0YtIABvAAKLIH8zOAqSbH42BHGYFYMAAYgASgA8gAspEJAMAA6gAEj+ME-qAAAkACMT4AIJbAAmkwkGCJ+Ji0tcDzXDRwhGJ2KZ6DenhgKAXEAHoAPz9F4cGKLkvarI8no9EodSfuMVbTBUVTpCkE4xNQjSkC0ETHNRqTpFkDjOM8ChYIwABUgjHpcEyBKSADW3gMAp1R9B2XY+BMu4RIO6nDme8mVNUKROa0xx5p55SVE+J4GGi571Je3mVGY-mKVgKRYHIJzseFRnpAwR6aC+2ARaQH40t+f4AUByapuQ6YgblJnmaARHwKAiiwYhyFYAwizwQAqgACg05E1SudVstR7hMYYLEGIw7FeFxoB8QJYAPNQGUOFo4kYJJmp5GogSwIpo5YIYm3cn2pISYoB25Fo5iwIQznuIVr6HVwl3bTde3yIwmhEJA8iPBRhgWQeIrUkmRVjIYZXlUN5JEeAOGPr+QEFkcT5iBukj6GBsAWRhWE4WNaZsk+BFTRKADsNE0jNc1seK4WKAVsN+PDLKfuSlWo6AnVIY+JPYcePgagaXDYJA+iaJWEOgAADDTTOPKxczGQt4pLTx-HHOx7jvhV-6AcBgvwcLURoZhYuQaAzBQ9TZh0dwW6jl4kgXOQ6Xywwjt+JotkyLkypYI27hAA) <!-- playground:postgres --> to hover the types and edit the SQL, with nothing to install.
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
@@ -29,13 +29,11 @@ await sql.transaction(async (tx) => {
|
|
|
29
29
|
});
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
With bigint parsing enabled on the connection,
|
|
32
|
+
With bigint parsing enabled on the connection, pass `{ bigint: true }`:
|
|
33
33
|
|
|
34
34
|
```ts
|
|
35
|
-
import { createBigIntSql } from "@typesql/postgres";
|
|
36
|
-
|
|
37
35
|
const dbBig = postgres(process.env.DATABASE_URL!, { types: { bigint: postgres.BigInt } });
|
|
38
|
-
const big =
|
|
36
|
+
const big = createSql<DB>(dbBig, { bigint: true }); // PostgresJsBigIntTypeMap
|
|
39
37
|
|
|
40
38
|
const counts = await big("SELECT author_id, count(*) AS n FROM posts GROUP BY author_id").execute();
|
|
41
39
|
// { author_id: number; n: bigint }[]
|
|
@@ -45,15 +43,14 @@ const counts = await big("SELECT author_id, count(*) AS n FROM posts GROUP BY au
|
|
|
45
43
|
|
|
46
44
|
| Export | |
|
|
47
45
|
|---|---|
|
|
48
|
-
| `createSql<DB>(db, options?)` | core's `createSql<DB, PostgresJsTypeMap>()` with the queries running on `db`; `{ rowKeys: "camelCase" }` camelCases the row keys |
|
|
49
|
-
| `createBigIntSql<DB>(db, options?)` | the same with `PostgresJsBigIntTypeMap`, for `types: { bigint: postgres.BigInt }` |
|
|
46
|
+
| `createSql<DB>(db, options?)` | core's `createSql<DB, PostgresJsTypeMap>()` with the queries running on `db`; `{ rowKeys: "camelCase" }` camelCases the row keys, `{ bigint: true }` uses `PostgresJsBigIntTypeMap`, for `types: { bigint: postgres.BigInt }` |
|
|
50
47
|
| `q.execute()`, `q.executeTakeFirst()`, `q.executeTakeFirstOrThrow()`, `q.executeCount()` | `Promise<Row[]>` (a plain array, result metadata stripped), `Promise<Row \| undefined>`, `Promise<Row>` (`NoResultError` on 0 rows), `Promise<number>` (`.count`, 0 for DDL) |
|
|
51
48
|
| `sql.transaction(fn)` | `db.begin(fn)` with `tx` bound to the transaction; `tx.transaction(fn)` is `savepoint(fn)` |
|
|
52
49
|
| `executor(db)` | an `Executor` for `q.execute(executor)` and core's `createSql(executor)` (reports `.count`) |
|
|
53
50
|
| `migrate(db, m)` | runs `m.statements` (e.g. from core's `migrations()`) in one transaction |
|
|
54
|
-
| `PostgresJsSql<DB, Keys
|
|
51
|
+
| `PostgresJsSql<DB, Keys, TM>` | the type `createSql` returns (`TM` is `PostgresJsBigIntTypeMap` with `{ bigint: true }`) |
|
|
55
52
|
| `PostgresJsTypeMap`, `PostgresJsBigIntTypeMap`, `PostgresJsParamTypeMap` | the type maps (rows), and what parameters also accept |
|
|
56
|
-
| `
|
|
53
|
+
| `PostgresJsQueryable`, `PostgresJsTransactable`, `PostgresJsRows`, `MigrationSource` | parameter types |
|
|
57
54
|
|
|
58
55
|
Queries run with `db.unsafe(text, values)`, so the text reaches the server unchanged and values
|
|
59
56
|
are bind parameters. `db` is the `Sql` instance, a `TransactionSql` or a `ReservedSql`.
|
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Executor
|
|
1
|
+
import type { Executor } from "@typesql/core";
|
|
2
|
+
import type { SqlDriver } from "@typesql/core/adapter";
|
|
2
3
|
/**
|
|
3
4
|
* The shape of a resolved `sql.unsafe()` result the adapter relies on: an array with `count`
|
|
4
5
|
* (`null` for statements without a count, such as DDL).
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAEvD;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,SAAS,OAAO,EAAE,GAAG;IACjD,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IACnC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,OAAO,EAAE,GAAG,WAAW,CAAC,cAAc,CAAC,CAAC;CAC3E;AAED,qFAAqF;AACrF,MAAM,WAAW,sBAAsB,CACtC,EAAE,SAAS,mBAAmB,GAAG,mBAAmB;IAEpD,KAAK,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1D;AASD;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,mBAAmB,GAAG,QAAQ,CAK3D;AAsBD,0DAA0D;AAC1D,eAAO,MAAM,MAAM,EAAE,SAAS,CAAC,mBAAmB,CAA6B,CAAC;AAEhF,oGAAoG;AACpG,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;CACvC;AAED;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,EAAE,SAAS,mBAAmB,EAC3D,GAAG,EAAE,sBAAsB,CAAC,EAAE,CAAC,EAC/B,CAAC,EAAE,eAAe,GAChB,OAAO,CAAC,IAAI,CAAC,CAIf"}
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sources":["../src/client.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"client.js","sources":["../src/client.ts"],"names":[],"mappings":"AAiCA;;;GAGG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAwB;IAChD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;QAC1D,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;IACrE,CAAC,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,WAAW,CACzB,GAAwB,EACxB,MAAc,EACd,EAA2C;IAE3C,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,GAAG,GAE5B,CAAC;IACF,IAAI,SAAS;QAAE,OAAO,CAAC,MAAM,SAAS,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAM,CAAC;IAC3D,IAAI,KAAK;QAAE,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAM,CAAC;IACnD,MAAM,IAAI,KAAK,CACd,6GAA6G,CAC7G,CAAC;AACH,CAAC;AAED,0DAA0D;AAC1D,MAAM,CAAC,MAAM,MAAM,GAAmC,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;AAOhF;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAC5B,GAA+B,EAC/B,CAAkB;IAElB,MAAM,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;QAC5B,KAAK,MAAM,SAAS,IAAI,CAAC,CAAC,UAAU;YAAE,MAAM,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAClE,CAAC,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,30 +1,32 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { RowKeys, TransactionalSql } from "@typesql/core";
|
|
2
2
|
import { type PostgresJsQueryable } from "./client.js";
|
|
3
3
|
import type { PostgresJsBigIntTypeMap, PostgresJsTypeMap } from "./type-map.js";
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export type PostgresJsSql<DB, Keys extends RowKeys = "preserve"> = TransactionalSql<DB, PostgresJsTypeMap, Keys>;
|
|
9
|
-
/** What `createBigIntSql<DB>(sql)` returns: core's `Sql` under `PostgresJsBigIntTypeMap`, with `transaction(fn)`. */
|
|
10
|
-
export type PostgresJsBigIntSql<DB, Keys extends RowKeys = "preserve"> = TransactionalSql<DB, PostgresJsBigIntTypeMap, Keys>;
|
|
4
|
+
export { executor, type MigrationSource, migrate, type PostgresJsQueryable, type PostgresJsRows, type PostgresJsTransactable, } from "./client.js";
|
|
5
|
+
export type { PostgresJsBigIntTypeMap, PostgresJsParamTypeMap, PostgresJsTypeMap, } from "./type-map.js";
|
|
6
|
+
/** What `createSql<DB>(sql)` returns: core's `Sql` under `PostgresJsTypeMap` (or `TM`), with `transaction(fn)`. */
|
|
7
|
+
export type PostgresJsSql<DB, Keys extends RowKeys = "preserve", TM = PostgresJsTypeMap> = TransactionalSql<DB, TM, Keys>;
|
|
11
8
|
/**
|
|
12
9
|
* `createSql<DB>()` from core, pre-bound to `PostgresJsTypeMap` (default parsers: `int8` and `count(*)` are `string`).
|
|
13
10
|
* The queries' `execute*` methods run on the postgres.js `sql` instance (or a `TransactionSql`,
|
|
14
11
|
* `ReservedSql`), and `sql.transaction(fn)` runs `fn` with a `sql` bound to one transaction.
|
|
15
12
|
* `{ rowKeys: "camelCase" }` camelCases the rows' keys, in the types and at runtime.
|
|
13
|
+
* `{ bigint: true }` is for `postgres({ types: { bigint: postgres.BigInt } })` connections:
|
|
14
|
+
* `int8` and `count(*)` are `bigint` (`PostgresJsBigIntTypeMap`).
|
|
16
15
|
*/
|
|
17
16
|
export declare function createSql<DB>(sql: PostgresJsQueryable | undefined, options: {
|
|
18
17
|
readonly rowKeys: "camelCase";
|
|
18
|
+
readonly bigint: true;
|
|
19
|
+
}): PostgresJsSql<DB, "camelCase", PostgresJsBigIntTypeMap>;
|
|
20
|
+
export declare function createSql<DB>(sql: PostgresJsQueryable | undefined, options: {
|
|
21
|
+
readonly rowKeys?: "preserve";
|
|
22
|
+
readonly bigint: true;
|
|
23
|
+
}): PostgresJsSql<DB, "preserve", PostgresJsBigIntTypeMap>;
|
|
24
|
+
export declare function createSql<DB>(sql: PostgresJsQueryable | undefined, options: {
|
|
25
|
+
readonly rowKeys: "camelCase";
|
|
26
|
+
readonly bigint?: false;
|
|
19
27
|
}): PostgresJsSql<DB, "camelCase">;
|
|
20
28
|
export declare function createSql<DB>(sql?: PostgresJsQueryable, options?: {
|
|
21
29
|
readonly rowKeys?: "preserve";
|
|
30
|
+
readonly bigint?: false;
|
|
22
31
|
}): PostgresJsSql<DB>;
|
|
23
|
-
/** For `postgres({ types: { bigint: postgres.BigInt } })` connections: `int8` and `count(*)` are `bigint`. */
|
|
24
|
-
export declare function createBigIntSql<DB>(sql: PostgresJsQueryable | undefined, options: {
|
|
25
|
-
readonly rowKeys: "camelCase";
|
|
26
|
-
}): PostgresJsBigIntSql<DB, "camelCase">;
|
|
27
|
-
export declare function createBigIntSql<DB>(sql?: PostgresJsQueryable, options?: {
|
|
28
|
-
readonly rowKeys?: "preserve";
|
|
29
|
-
}): PostgresJsBigIntSql<DB>;
|
|
30
32
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAc,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAE3E,OAAO,EAAU,KAAK,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,KAAK,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAEhF,OAAO,EACN,QAAQ,EACR,KAAK,eAAe,EACpB,OAAO,EACP,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,sBAAsB,GAC3B,MAAM,aAAa,CAAC;AACrB,YAAY,EACX,uBAAuB,EACvB,sBAAsB,EACtB,iBAAiB,GACjB,MAAM,eAAe,CAAC;AAEvB,mHAAmH;AACnH,MAAM,MAAM,aAAa,CACxB,EAAE,EACF,IAAI,SAAS,OAAO,GAAG,UAAU,EACjC,EAAE,GAAG,iBAAiB,IACnB,gBAAgB,CAAC,EAAE,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;AAOnC;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,EAAE,EAC3B,GAAG,EAAE,mBAAmB,GAAG,SAAS,EACpC,OAAO,EAAE;IAAE,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAA;CAAE,GAC/D,aAAa,CAAC,EAAE,EAAE,WAAW,EAAE,uBAAuB,CAAC,CAAC;AAC3D,wBAAgB,SAAS,CAAC,EAAE,EAC3B,GAAG,EAAE,mBAAmB,GAAG,SAAS,EACpC,OAAO,EAAE;IAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAA;CAAE,GAC/D,aAAa,CAAC,EAAE,EAAE,UAAU,EAAE,uBAAuB,CAAC,CAAC;AAC1D,wBAAgB,SAAS,CAAC,EAAE,EAC3B,GAAG,EAAE,mBAAmB,GAAG,SAAS,EACpC,OAAO,EAAE;IAAE,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GACjE,aAAa,CAAC,EAAE,EAAE,WAAW,CAAC,CAAC;AAClC,wBAAgB,SAAS,CAAC,EAAE,EAC3B,GAAG,CAAC,EAAE,mBAAmB,EACzB,OAAO,CAAC,EAAE;IAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,KAAK,CAAA;CAAE,GAClE,aAAa,CAAC,EAAE,CAAC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { createTransactionalSql } from "@typesql/core";
|
|
1
|
+
import { createTransactionalSql } from "@typesql/core/adapter";
|
|
2
2
|
import { driver } from "./client.js";
|
|
3
|
-
export { NoResultError } from "@typesql/core";
|
|
4
3
|
export { executor, migrate, } from "./client.js";
|
|
5
4
|
export function createSql(sql, options) {
|
|
6
5
|
return createTransactionalSql(sql, driver, options);
|
|
7
6
|
}
|
|
8
|
-
export function createBigIntSql(sql, options) {
|
|
9
|
-
return createTransactionalSql(sql, driver, options);
|
|
10
|
-
}
|
|
11
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,MAAM,EAA4B,MAAM,aAAa,CAAC;AAG/D,OAAO,EACN,QAAQ,EAER,OAAO,GAIP,MAAM,aAAa,CAAC;AA2CrB,MAAM,UAAU,SAAS,CACxB,GAAyB,EACzB,OAA8B;IAE9B,OAAO,sBAAsB,CAC5B,GAAG,EACH,MAAM,EACN,OAAO,CACP,CAAC;AACH,CAAC"}
|
package/dist/type-map.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-map.d.ts","sources":["../src/type-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"type-map.d.ts","sources":["../src/type-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEzE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,iBAChB,SAAQ,IAAI,CACX,cAAc,EACd,OAAO,GAAG,UAAU,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,CAC7D;IACD,sDAAsD;IACtD,OAAO,EAAE,sBAAsB,CAAC;IAChC,KAAK,EAAE,MAAM,CAAC;IACd,0EAA0E;IAC1E,QAAQ,EAAE,MAAM,CAAC;IACjB,oCAAoC;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,wCAAwC;IACxC,MAAM,EAAE,MAAM,CAAC;IACf,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,IAAI,EAAE,MAAM,CAAC;CACb;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,sBAChB,SAAQ,IAAI,CAAC,mBAAmB,EAAE,MAAM,GAAG,WAAW,GAAG,aAAa,CAAC;IACvE,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;IAC7B,SAAS,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;IAClC,WAAW,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,CAAC;CACpC;AAED;;;;;GAKG;AACH,MAAM,WAAW,uBAChB,SAAQ,IAAI,CAAC,iBAAiB,EAAE,MAAM,CAAC;IACvC,IAAI,EAAE,MAAM,CAAC;CACb"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typesql/postgres",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "postgres.js adapter for TypeSQL.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@typesql/core": "0.
|
|
31
|
+
"@typesql/core": "0.5.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"postgres": ">=3"
|
package/src/client.ts
CHANGED
|
@@ -1,27 +1,34 @@
|
|
|
1
|
-
import type { Executor
|
|
1
|
+
import type { Executor } from "@typesql/core";
|
|
2
|
+
import type { SqlDriver } from "@typesql/core/adapter";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* The shape of a resolved `sql.unsafe()` result the adapter relies on: an array with `count`
|
|
5
6
|
* (`null` for statements without a count, such as DDL).
|
|
6
7
|
*/
|
|
7
|
-
export type PostgresJsRows = readonly unknown[] & {
|
|
8
|
+
export type PostgresJsRows = readonly unknown[] & {
|
|
9
|
+
readonly count: number | null;
|
|
10
|
+
};
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Anything with postgres.js' `unsafe(query, parameters)` method: the `Sql` instance,
|
|
11
14
|
* a `TransactionSql` inside `begin`, a `ReservedSql`, or a fake in tests.
|
|
12
15
|
*/
|
|
13
16
|
export interface PostgresJsQueryable {
|
|
14
|
-
|
|
17
|
+
unsafe(query: string, parameters?: unknown[]): PromiseLike<PostgresJsRows>;
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
/** Anything with postgres.js' `begin(fn)`: the `Sql` instance or a `ReservedSql`. */
|
|
18
|
-
export interface PostgresJsTransactable<
|
|
19
|
-
|
|
21
|
+
export interface PostgresJsTransactable<
|
|
22
|
+
Tx extends PostgresJsQueryable = PostgresJsQueryable,
|
|
23
|
+
> {
|
|
24
|
+
begin(fn: (tx: Tx) => Promise<unknown>): Promise<unknown>;
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
/** A `TransactionSql`'s `savepoint(fn)`. */
|
|
23
28
|
interface PostgresJsSavepointable {
|
|
24
|
-
|
|
29
|
+
savepoint(
|
|
30
|
+
fn: (tx: PostgresJsQueryable) => Promise<unknown>,
|
|
31
|
+
): Promise<unknown>;
|
|
25
32
|
}
|
|
26
33
|
|
|
27
34
|
/**
|
|
@@ -29,10 +36,10 @@ interface PostgresJsSavepointable {
|
|
|
29
36
|
* `{ rows, rowCount }` (postgres.js' `.count`, so `q.executeCount()` works).
|
|
30
37
|
*/
|
|
31
38
|
export function executor(sql: PostgresJsQueryable): Executor {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
return async (text, values) => {
|
|
40
|
+
const result = await sql.unsafe(text, Array.from(values));
|
|
41
|
+
return { rows: Array.from(result), rowCount: result.count ?? null };
|
|
42
|
+
};
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
/**
|
|
@@ -40,11 +47,19 @@ export function executor(sql: PostgresJsQueryable): Executor {
|
|
|
40
47
|
* otherwise `sql.begin(fn)`. postgres.js issues BEGIN (or SAVEPOINT), runs the callback with a
|
|
41
48
|
* transaction-bound `sql`, then COMMIT, or ROLLBACK and rethrow when the callback throws.
|
|
42
49
|
*/
|
|
43
|
-
async function transaction<T>(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
async function transaction<T>(
|
|
51
|
+
sql: PostgresJsQueryable,
|
|
52
|
+
_depth: number,
|
|
53
|
+
fn: (tx: PostgresJsQueryable) => Promise<T>,
|
|
54
|
+
): Promise<T> {
|
|
55
|
+
const { savepoint, begin } = sql as Partial<
|
|
56
|
+
PostgresJsSavepointable & PostgresJsTransactable
|
|
57
|
+
>;
|
|
58
|
+
if (savepoint) return (await savepoint.call(sql, fn)) as T;
|
|
59
|
+
if (begin) return (await begin.call(sql, fn)) as T;
|
|
60
|
+
throw new Error(
|
|
61
|
+
"typesql: sql.transaction() needs a postgres.js Sql, ReservedSql or TransactionSql (with begin or savepoint)",
|
|
62
|
+
);
|
|
48
63
|
}
|
|
49
64
|
|
|
50
65
|
/** How `createSql(sql)` runs queries and transactions. */
|
|
@@ -52,7 +67,7 @@ export const driver: SqlDriver<PostgresJsQueryable> = { executor, transaction };
|
|
|
52
67
|
|
|
53
68
|
/** Anything carrying an ordered list of DDL statements, e.g. a `migrations()` builder from core. */
|
|
54
69
|
export interface MigrationSource {
|
|
55
|
-
|
|
70
|
+
readonly statements: readonly string[];
|
|
56
71
|
}
|
|
57
72
|
|
|
58
73
|
/**
|
|
@@ -60,8 +75,11 @@ export interface MigrationSource {
|
|
|
60
75
|
* Each statement goes through `unsafe` without parameters (simple protocol), so scripts with
|
|
61
76
|
* `$$`-quoted function bodies work.
|
|
62
77
|
*/
|
|
63
|
-
export async function migrate<Tx extends PostgresJsQueryable>(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
78
|
+
export async function migrate<Tx extends PostgresJsQueryable>(
|
|
79
|
+
sql: PostgresJsTransactable<Tx>,
|
|
80
|
+
m: MigrationSource,
|
|
81
|
+
): Promise<void> {
|
|
82
|
+
await sql.begin(async (tx) => {
|
|
83
|
+
for (const statement of m.statements) await tx.unsafe(statement);
|
|
84
|
+
});
|
|
67
85
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,38 +1,65 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { RowKeys, SqlOptions, TransactionalSql } from "@typesql/core";
|
|
2
|
+
import { createTransactionalSql } from "@typesql/core/adapter";
|
|
2
3
|
import { driver, type PostgresJsQueryable } from "./client.js";
|
|
3
4
|
import type { PostgresJsBigIntTypeMap, PostgresJsTypeMap } from "./type-map.js";
|
|
4
5
|
|
|
5
|
-
export { NoResultError, type Executor, type TransactionalSql } from "@typesql/core";
|
|
6
|
-
export type { PostgresJsTypeMap, PostgresJsBigIntTypeMap, PostgresJsParamTypeMap } from "./type-map.js";
|
|
7
6
|
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
executor,
|
|
8
|
+
type MigrationSource,
|
|
9
|
+
migrate,
|
|
10
|
+
type PostgresJsQueryable,
|
|
11
|
+
type PostgresJsRows,
|
|
12
|
+
type PostgresJsTransactable,
|
|
14
13
|
} from "./client.js";
|
|
14
|
+
export type {
|
|
15
|
+
PostgresJsBigIntTypeMap,
|
|
16
|
+
PostgresJsParamTypeMap,
|
|
17
|
+
PostgresJsTypeMap,
|
|
18
|
+
} from "./type-map.js";
|
|
15
19
|
|
|
16
|
-
/** What `createSql<DB>(sql)` returns: core's `Sql` under `PostgresJsTypeMap
|
|
17
|
-
export type PostgresJsSql<
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
/** What `createSql<DB>(sql)` returns: core's `Sql` under `PostgresJsTypeMap` (or `TM`), with `transaction(fn)`. */
|
|
21
|
+
export type PostgresJsSql<
|
|
22
|
+
DB,
|
|
23
|
+
Keys extends RowKeys = "preserve",
|
|
24
|
+
TM = PostgresJsTypeMap,
|
|
25
|
+
> = TransactionalSql<DB, TM, Keys>;
|
|
26
|
+
|
|
27
|
+
/** `createSql`'s options: core's `rowKeys`, and `bigint: true` for a connection that parses `int8` as `bigint`. */
|
|
28
|
+
interface PostgresJsSqlOptions extends SqlOptions {
|
|
29
|
+
readonly bigint?: boolean;
|
|
30
|
+
}
|
|
20
31
|
|
|
21
32
|
/**
|
|
22
33
|
* `createSql<DB>()` from core, pre-bound to `PostgresJsTypeMap` (default parsers: `int8` and `count(*)` are `string`).
|
|
23
34
|
* The queries' `execute*` methods run on the postgres.js `sql` instance (or a `TransactionSql`,
|
|
24
35
|
* `ReservedSql`), and `sql.transaction(fn)` runs `fn` with a `sql` bound to one transaction.
|
|
25
36
|
* `{ rowKeys: "camelCase" }` camelCases the rows' keys, in the types and at runtime.
|
|
37
|
+
* `{ bigint: true }` is for `postgres({ types: { bigint: postgres.BigInt } })` connections:
|
|
38
|
+
* `int8` and `count(*)` are `bigint` (`PostgresJsBigIntTypeMap`).
|
|
26
39
|
*/
|
|
27
|
-
export function createSql<DB>(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export function
|
|
36
|
-
|
|
37
|
-
|
|
40
|
+
export function createSql<DB>(
|
|
41
|
+
sql: PostgresJsQueryable | undefined,
|
|
42
|
+
options: { readonly rowKeys: "camelCase"; readonly bigint: true },
|
|
43
|
+
): PostgresJsSql<DB, "camelCase", PostgresJsBigIntTypeMap>;
|
|
44
|
+
export function createSql<DB>(
|
|
45
|
+
sql: PostgresJsQueryable | undefined,
|
|
46
|
+
options: { readonly rowKeys?: "preserve"; readonly bigint: true },
|
|
47
|
+
): PostgresJsSql<DB, "preserve", PostgresJsBigIntTypeMap>;
|
|
48
|
+
export function createSql<DB>(
|
|
49
|
+
sql: PostgresJsQueryable | undefined,
|
|
50
|
+
options: { readonly rowKeys: "camelCase"; readonly bigint?: false },
|
|
51
|
+
): PostgresJsSql<DB, "camelCase">;
|
|
52
|
+
export function createSql<DB>(
|
|
53
|
+
sql?: PostgresJsQueryable,
|
|
54
|
+
options?: { readonly rowKeys?: "preserve"; readonly bigint?: false },
|
|
55
|
+
): PostgresJsSql<DB>;
|
|
56
|
+
export function createSql<DB>(
|
|
57
|
+
sql?: PostgresJsQueryable,
|
|
58
|
+
options?: PostgresJsSqlOptions,
|
|
59
|
+
): PostgresJsSql<DB, RowKeys, PostgresJsTypeMap | PostgresJsBigIntTypeMap> {
|
|
60
|
+
return createTransactionalSql<DB, PostgresJsTypeMap, PostgresJsQueryable>(
|
|
61
|
+
sql,
|
|
62
|
+
driver,
|
|
63
|
+
options,
|
|
64
|
+
);
|
|
38
65
|
}
|
package/src/type-map.ts
CHANGED
|
@@ -22,20 +22,24 @@ import type { DefaultParamTypeMap, DefaultTypeMap } from "@typesql/core";
|
|
|
22
22
|
* registers an element-wise array parser for every array type, so core's `T[]` →
|
|
23
23
|
* element-type[] mapping is exact (int8[] → string[], timestamptz[] → Date[], ...).
|
|
24
24
|
*/
|
|
25
|
-
export interface PostgresJsTypeMap
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
25
|
+
export interface PostgresJsTypeMap
|
|
26
|
+
extends Omit<
|
|
27
|
+
DefaultTypeMap,
|
|
28
|
+
"bytea" | "interval" | "point" | "circle" | "record" | "void"
|
|
29
|
+
> {
|
|
30
|
+
/** What parameters accept on top of the row types. */
|
|
31
|
+
$params: PostgresJsParamTypeMap;
|
|
32
|
+
bytea: Buffer;
|
|
33
|
+
/** No default parser: the Postgres text form, e.g. `"1 day 02:00:00"`. */
|
|
34
|
+
interval: string;
|
|
35
|
+
/** No default parser: `"(1,2)"`. */
|
|
36
|
+
point: string;
|
|
37
|
+
/** No default parser: `"<(1,2),3>"`. */
|
|
38
|
+
circle: string;
|
|
39
|
+
/** Anonymous `ROW(...)` results: raw text such as `"(1,foo)"`. */
|
|
40
|
+
record: string;
|
|
41
|
+
/** `void`-returning functions (`pg_sleep`) yield an empty string. */
|
|
42
|
+
void: string;
|
|
39
43
|
}
|
|
40
44
|
|
|
41
45
|
/**
|
|
@@ -52,10 +56,11 @@ export interface PostgresJsTypeMap extends Omit<DefaultTypeMap, "bytea" | "inter
|
|
|
52
56
|
*
|
|
53
57
|
* `bool` takes only booleans: its serializer sends `'f'` for anything but `true`.
|
|
54
58
|
*/
|
|
55
|
-
export interface PostgresJsParamTypeMap
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
+
export interface PostgresJsParamTypeMap
|
|
60
|
+
extends Omit<DefaultParamTypeMap, "date" | "timestamp" | "timestamptz"> {
|
|
61
|
+
date: Date | string | number;
|
|
62
|
+
timestamp: Date | string | number;
|
|
63
|
+
timestamptz: Date | string | number;
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
/**
|
|
@@ -64,6 +69,7 @@ export interface PostgresJsParamTypeMap extends Omit<DefaultParamTypeMap, "date"
|
|
|
64
69
|
* and `int8` columns become `bigint`; `int8[]` becomes `bigint[]`. Everything else is as in
|
|
65
70
|
* `PostgresJsTypeMap`.
|
|
66
71
|
*/
|
|
67
|
-
export interface PostgresJsBigIntTypeMap
|
|
68
|
-
|
|
72
|
+
export interface PostgresJsBigIntTypeMap
|
|
73
|
+
extends Omit<PostgresJsTypeMap, "int8"> {
|
|
74
|
+
int8: bigint;
|
|
69
75
|
}
|