@supawatch/target-factories 0.1.3
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/LICENSE +21 -0
- package/README.md +25 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +112 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Omar Dulaimi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @supawatch/target-factories
|
|
2
|
+
|
|
3
|
+
The fixture-factory target for
|
|
4
|
+
[supawatch](https://github.com/omar-dulaimi/supawatch). Emits one typed
|
|
5
|
+
factory per table returning a full, deterministic row in driver shape,
|
|
6
|
+
with overrides merged on top.
|
|
7
|
+
|
|
8
|
+
Configure it in `supawatch.config.ts` (the CLI loads this package by name):
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
targets: [{ kind: "factories" }]
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { makeTasks } from "./src/schemas/factories/tasks.mjs";
|
|
16
|
+
|
|
17
|
+
const task = makeTasks({ title: "override just what the test cares about" });
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The repo's suite asserts every factory's default row satisfies the
|
|
21
|
+
generated Zod schema for the same table, so factories cannot drift into
|
|
22
|
+
the narrow, wrong fixtures that hide bugs. That check has already caught
|
|
23
|
+
one: an invalid placeholder UUID.
|
|
24
|
+
|
|
25
|
+
MIT.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Rendered, Snapshot, Table, Target, TargetCapabilities, TargetOptions } from "@supawatch/core";
|
|
2
|
+
export type FactoriesTargetOptions = TargetOptions;
|
|
3
|
+
export declare function exportNameFor(table: Table): string;
|
|
4
|
+
export declare class FactoriesTarget implements Target<FactoriesTargetOptions> {
|
|
5
|
+
readonly name = "factories";
|
|
6
|
+
readonly fileExtension = ".mjs";
|
|
7
|
+
readonly capabilities: TargetCapabilities;
|
|
8
|
+
renderTable(table: Table, _snapshot: Snapshot, _opts: FactoriesTargetOptions): Rendered;
|
|
9
|
+
renderTypes(table: Table, _snapshot: Snapshot, _opts: FactoriesTargetOptions): string;
|
|
10
|
+
}
|
|
11
|
+
export default FactoriesTarget;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
function defaultExpr(runtime, seedPath) {
|
|
2
|
+
switch (runtime.kind) {
|
|
3
|
+
case "number":
|
|
4
|
+
return runtime.integer ? "1" : "1.5";
|
|
5
|
+
case "string":
|
|
6
|
+
switch (runtime.format) {
|
|
7
|
+
case "uuid":
|
|
8
|
+
// A real v4 literal: zod's z.uuid() enforces RFC 4122 version
|
|
9
|
+
// and variant bits, so an all-zeros-style value is rejected.
|
|
10
|
+
// The cross-check against the zod target caught exactly that.
|
|
11
|
+
return '"9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"';
|
|
12
|
+
case "numeric":
|
|
13
|
+
return '"0.00"';
|
|
14
|
+
case "bigint":
|
|
15
|
+
return '"1"';
|
|
16
|
+
default:
|
|
17
|
+
return JSON.stringify(seedPath);
|
|
18
|
+
}
|
|
19
|
+
case "boolean":
|
|
20
|
+
return "false";
|
|
21
|
+
case "date":
|
|
22
|
+
return "new Date(0)";
|
|
23
|
+
case "bytes":
|
|
24
|
+
return "new Uint8Array()";
|
|
25
|
+
case "json":
|
|
26
|
+
case "unknown":
|
|
27
|
+
return "null";
|
|
28
|
+
case "array":
|
|
29
|
+
return "[]";
|
|
30
|
+
case "enum":
|
|
31
|
+
return JSON.stringify(runtime.labels[0] ?? "");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function baseNameFor(table) {
|
|
35
|
+
return table.name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
36
|
+
}
|
|
37
|
+
export function exportNameFor(table) {
|
|
38
|
+
const base = baseNameFor(table);
|
|
39
|
+
return "make" + base.charAt(0).toUpperCase() + base.slice(1);
|
|
40
|
+
}
|
|
41
|
+
function fieldDefault(table, col) {
|
|
42
|
+
if (col.nullable)
|
|
43
|
+
return "null";
|
|
44
|
+
return defaultExpr(col.runtime, `${table.name}.${col.name}`);
|
|
45
|
+
}
|
|
46
|
+
export class FactoriesTarget {
|
|
47
|
+
name = "factories";
|
|
48
|
+
fileExtension = ".mjs";
|
|
49
|
+
capabilities = {
|
|
50
|
+
strictObjects: false,
|
|
51
|
+
brandedTypes: false,
|
|
52
|
+
dateInstances: true,
|
|
53
|
+
};
|
|
54
|
+
renderTable(table, _snapshot, _opts) {
|
|
55
|
+
const fields = table.columns
|
|
56
|
+
.map((col) => ` ${JSON.stringify(col.name)}: ${fieldDefault(table, col)},`)
|
|
57
|
+
.join("\n");
|
|
58
|
+
const body = [
|
|
59
|
+
`export function ${exportNameFor(table)}(overrides = {}) {`,
|
|
60
|
+
" return {",
|
|
61
|
+
fields,
|
|
62
|
+
" ...overrides,",
|
|
63
|
+
" };",
|
|
64
|
+
"}",
|
|
65
|
+
].join("\n");
|
|
66
|
+
return { imports: [], body, exportName: exportNameFor(table) };
|
|
67
|
+
}
|
|
68
|
+
renderTypes(table, _snapshot, _opts) {
|
|
69
|
+
const name = exportNameFor(table);
|
|
70
|
+
const base = baseNameFor(table);
|
|
71
|
+
const rowType = `${base}FactoryRow`;
|
|
72
|
+
const tsType = (runtime) => {
|
|
73
|
+
switch (runtime.kind) {
|
|
74
|
+
case "number":
|
|
75
|
+
return "number";
|
|
76
|
+
case "string":
|
|
77
|
+
return "string";
|
|
78
|
+
case "boolean":
|
|
79
|
+
return "boolean";
|
|
80
|
+
case "date":
|
|
81
|
+
return "Date";
|
|
82
|
+
case "bytes":
|
|
83
|
+
return "Uint8Array";
|
|
84
|
+
case "json":
|
|
85
|
+
case "unknown":
|
|
86
|
+
return "unknown";
|
|
87
|
+
case "array": {
|
|
88
|
+
const el = tsType(runtime.element);
|
|
89
|
+
return el.includes("|") ? `(${el})[]` : `${el}[]`;
|
|
90
|
+
}
|
|
91
|
+
case "enum":
|
|
92
|
+
return runtime.labels.map((l) => JSON.stringify(l)).join(" | ");
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
const fields = table.columns
|
|
96
|
+
.map((col) => {
|
|
97
|
+
const base = tsType(col.runtime);
|
|
98
|
+
const t = col.nullable && base !== "unknown" ? `${base} | null` : base;
|
|
99
|
+
return ` ${JSON.stringify(col.name)}: ${t};`;
|
|
100
|
+
})
|
|
101
|
+
.join("\n");
|
|
102
|
+
return [
|
|
103
|
+
"// Generated by supawatch. Do not edit.",
|
|
104
|
+
`export type ${rowType} = {`,
|
|
105
|
+
fields,
|
|
106
|
+
"};",
|
|
107
|
+
`export declare function ${name}(overrides?: Partial<${rowType}>): ${rowType};`,
|
|
108
|
+
"",
|
|
109
|
+
].join("\n");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export default FactoriesTarget;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@supawatch/target-factories",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Typed fixture factories generated from live Postgres by supawatch, guaranteed to satisfy the generated Zod schemas.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"supabase",
|
|
7
|
+
"postgres",
|
|
8
|
+
"postgresql",
|
|
9
|
+
"codegen",
|
|
10
|
+
"schema",
|
|
11
|
+
"typescript",
|
|
12
|
+
"fixtures",
|
|
13
|
+
"factories",
|
|
14
|
+
"testing"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"author": "Omar Dulaimi",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/omar-dulaimi/supawatch.git",
|
|
21
|
+
"directory": "packages/target-factories"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@supawatch/core": "0.2.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/node": "^22.20.1",
|
|
40
|
+
"typescript": "^5.9.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.json"
|
|
44
|
+
}
|
|
45
|
+
}
|