shapedef 1.0.12 → 1.0.14
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/dist/cjs/translations/postgres.d.ts +5 -2
- package/dist/cjs/translations/postgres.d.ts.map +1 -1
- package/dist/cjs/translations/postgres.js +8 -6
- package/dist/cjs/translations/postgres.js.map +1 -1
- package/dist/esm/translations/postgres.d.ts +5 -2
- package/dist/esm/translations/postgres.d.ts.map +1 -1
- package/dist/esm/translations/postgres.js +8 -6
- package/dist/esm/translations/postgres.js.map +1 -1
- package/package.json +1 -1
- package/src/translations/postgres.test.ts +54 -10
- package/src/translations/postgres.ts +12 -7
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
1
|
+
import { Shape } from "../shape";
|
|
2
|
+
export type PostgresOptions = {
|
|
3
|
+
tablePrefix?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const postgres: (shape: Shape, options?: PostgresOptions) => string;
|
|
3
6
|
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,MAAM,MAAM,eAAe,GAAG;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAA;AAqED,eAAO,MAAM,QAAQ,GAAI,OAAO,KAAK,EAAE,UAAS,eAAoB,WASnE,CAAC"}
|
|
@@ -43,8 +43,8 @@ const pgType = (shape) => {
|
|
|
43
43
|
return "JSONB";
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
|
-
const pgColumnDef = (colName, shape) => {
|
|
47
|
-
const parts = [colName
|
|
46
|
+
const pgColumnDef = (colName, shape, options) => {
|
|
47
|
+
const parts = [`"${colName}"`, pgType(shape)];
|
|
48
48
|
if (shape.anno.primary) {
|
|
49
49
|
parts.push("PRIMARY KEY");
|
|
50
50
|
}
|
|
@@ -60,16 +60,18 @@ const pgColumnDef = (colName, shape) => {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
if (shape.anno.foreign) {
|
|
63
|
-
|
|
63
|
+
const refTable = `${options.tablePrefix ?? ""}${shape.anno.foreign.shapeName}`;
|
|
64
|
+
parts.push(`REFERENCES "${refTable}"("${shape.anno.foreign.fieldName}")`);
|
|
64
65
|
}
|
|
65
66
|
return parts.join(" ");
|
|
66
67
|
};
|
|
67
|
-
const postgres = (shape) => {
|
|
68
|
+
const postgres = (shape, options = {}) => {
|
|
68
69
|
if (shape.type === "mapping" && shape.anno.name) {
|
|
70
|
+
const tableName = `${options.tablePrefix ?? ""}${shape.anno.name}`;
|
|
69
71
|
const cols = Object.entries(shape.rec)
|
|
70
|
-
.map(([k, v]) => ` ${pgColumnDef(k, v)}`)
|
|
72
|
+
.map(([k, v]) => ` ${pgColumnDef(k, v, options)}`)
|
|
71
73
|
.join(",\n");
|
|
72
|
-
return `CREATE TABLE IF NOT EXISTS "${
|
|
74
|
+
return `CREATE TABLE IF NOT EXISTS "${tableName}" (\n${cols}\n)`;
|
|
73
75
|
}
|
|
74
76
|
return pgType(shape);
|
|
75
77
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":";;;AAMA,MAAM,MAAM,GAAG,CAAC,KAAY,EAAU,EAAE;IACtC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,KAAK;YACR,OAAO,SAAS,CAAC;QACnB,KAAK,OAAO;YACV,OAAO,kBAAkB,CAAC;QAC5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AE,CAAC,KAAK,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC;aAClD,IAAI,CAAC,KAAK,CAAC,CAAC;QACf,OAAO,+BAA+B,SAAS,QAAQ,IAAI,KAAK,CAAC;IACnE,CAAC;IACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC;AATW,QAAA,QAAQ,YASnB"}
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
1
|
+
import { Shape } from "../shape";
|
|
2
|
+
export type PostgresOptions = {
|
|
3
|
+
tablePrefix?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare const postgres: (shape: Shape, options?: PostgresOptions) => string;
|
|
3
6
|
//# sourceMappingURL=postgres.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAEjC,MAAM,MAAM,eAAe,GAAG;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAA;AAqED,eAAO,MAAM,QAAQ,GAAI,OAAO,KAAK,EAAE,UAAS,eAAoB,WASnE,CAAC"}
|
|
@@ -43,8 +43,8 @@ const pgType = (shape) => {
|
|
|
43
43
|
return "JSONB";
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
|
-
const pgColumnDef = (colName, shape) => {
|
|
47
|
-
const parts = [colName
|
|
46
|
+
const pgColumnDef = (colName, shape, options) => {
|
|
47
|
+
const parts = [`"${colName}"`, pgType(shape)];
|
|
48
48
|
if (shape.anno.primary) {
|
|
49
49
|
parts.push("PRIMARY KEY");
|
|
50
50
|
}
|
|
@@ -60,16 +60,18 @@ const pgColumnDef = (colName, shape) => {
|
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
if (shape.anno.foreign) {
|
|
63
|
-
|
|
63
|
+
const refTable = `${options.tablePrefix ?? ""}${shape.anno.foreign.shapeName}`;
|
|
64
|
+
parts.push(`REFERENCES "${refTable}"("${shape.anno.foreign.fieldName}")`);
|
|
64
65
|
}
|
|
65
66
|
return parts.join(" ");
|
|
66
67
|
};
|
|
67
|
-
const postgres = (shape) => {
|
|
68
|
+
const postgres = (shape, options = {}) => {
|
|
68
69
|
if (shape.type === "mapping" && shape.anno.name) {
|
|
70
|
+
const tableName = `${options.tablePrefix ?? ""}${shape.anno.name}`;
|
|
69
71
|
const cols = Object.entries(shape.rec)
|
|
70
|
-
.map(([k, v]) => ` ${pgColumnDef(k, v)}`)
|
|
72
|
+
.map(([k, v]) => ` ${pgColumnDef(k, v, options)}`)
|
|
71
73
|
.join(",\n");
|
|
72
|
-
return `CREATE TABLE IF NOT EXISTS "${
|
|
74
|
+
return `CREATE TABLE IF NOT EXISTS "${tableName}" (\n${cols}\n)`;
|
|
73
75
|
}
|
|
74
76
|
return pgType(shape);
|
|
75
77
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../../../src/translations/postgres.ts"],"names":[],"mappings":";;;AAMA,MAAM,MAAM,GAAG,CAAC,KAAY,EAAU,EAAE;IACtC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,KAAK;YACR,OAAO,SAAS,CAAC;QACnB,KAAK,OAAO;YACV,OAAO,kBAAkB,CAAC;QAC5B,KAAK,QAAQ;YACX,OAAO,SAAS,CAAC;QACnB,KAAK,KAAK;YACR,OAAO,MAAM,CAAC;QAChB,KAAK,MAAM;YACT,OAAO,SAAS,CAAC;QACnB,KAAK,MAAM;YACT,OAAO,aAAa,CAA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AC;IACnE,CAAC;IACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC;AATW,QAAA,QAAQ,YASnB"}
|
package/package.json
CHANGED
|
@@ -64,8 +64,8 @@ describe("postgres", (it) => {
|
|
|
64
64
|
).toBe(
|
|
65
65
|
[
|
|
66
66
|
`CREATE TABLE IF NOT EXISTS "User" (`,
|
|
67
|
-
"
|
|
68
|
-
"
|
|
67
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
68
|
+
` "name" TEXT NOT NULL`,
|
|
69
69
|
")",
|
|
70
70
|
].join("\n"),
|
|
71
71
|
);
|
|
@@ -85,8 +85,8 @@ describe("postgres", (it) => {
|
|
|
85
85
|
).toBe(
|
|
86
86
|
[
|
|
87
87
|
`CREATE TABLE IF NOT EXISTS "Foobar" (`,
|
|
88
|
-
"
|
|
89
|
-
"
|
|
88
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
89
|
+
` "my_embedding" vector(300) NOT NULL`,
|
|
90
90
|
")",
|
|
91
91
|
].join("\n"),
|
|
92
92
|
);
|
|
@@ -106,8 +106,8 @@ describe("postgres", (it) => {
|
|
|
106
106
|
).toBe(
|
|
107
107
|
[
|
|
108
108
|
`CREATE TABLE IF NOT EXISTS "Profile" (`,
|
|
109
|
-
"
|
|
110
|
-
"
|
|
109
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
110
|
+
` "nickname" TEXT`,
|
|
111
111
|
")",
|
|
112
112
|
].join("\n"),
|
|
113
113
|
);
|
|
@@ -127,8 +127,8 @@ describe("postgres", (it) => {
|
|
|
127
127
|
).toBe(
|
|
128
128
|
[
|
|
129
129
|
`CREATE TABLE IF NOT EXISTS "Account" (`,
|
|
130
|
-
"
|
|
131
|
-
"
|
|
130
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
131
|
+
` "email" TEXT NOT NULL UNIQUE`,
|
|
132
132
|
")",
|
|
133
133
|
].join("\n"),
|
|
134
134
|
);
|
|
@@ -148,8 +148,8 @@ describe("postgres", (it) => {
|
|
|
148
148
|
).toBe(
|
|
149
149
|
[
|
|
150
150
|
`CREATE TABLE IF NOT EXISTS "Post" (`,
|
|
151
|
-
"
|
|
152
|
-
` user_id INTEGER NOT NULL REFERENCES "User"(id)`,
|
|
151
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
152
|
+
` "user_id" INTEGER NOT NULL REFERENCES "User"("id")`,
|
|
153
153
|
")",
|
|
154
154
|
].join("\n"),
|
|
155
155
|
);
|
|
@@ -158,4 +158,48 @@ describe("postgres", (it) => {
|
|
|
158
158
|
it("returns column type for unnamed mappings", () => {
|
|
159
159
|
expect(postgres(shapes.mapping({ x: shapes.int() }))).toBe("JSONB");
|
|
160
160
|
});
|
|
161
|
+
|
|
162
|
+
it("applies tablePrefix to table name", () => {
|
|
163
|
+
expect(
|
|
164
|
+
postgres(
|
|
165
|
+
annotate.named(
|
|
166
|
+
shapes.mapping({
|
|
167
|
+
id: annotate.primary(shapes.int()),
|
|
168
|
+
name: shapes.str(),
|
|
169
|
+
}),
|
|
170
|
+
"User",
|
|
171
|
+
),
|
|
172
|
+
{ tablePrefix: "app_" },
|
|
173
|
+
),
|
|
174
|
+
).toBe(
|
|
175
|
+
[
|
|
176
|
+
`CREATE TABLE IF NOT EXISTS "app_User" (`,
|
|
177
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
178
|
+
` "name" TEXT NOT NULL`,
|
|
179
|
+
")",
|
|
180
|
+
].join("\n"),
|
|
181
|
+
);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("applies tablePrefix to REFERENCES clauses", () => {
|
|
185
|
+
expect(
|
|
186
|
+
postgres(
|
|
187
|
+
annotate.named(
|
|
188
|
+
shapes.mapping({
|
|
189
|
+
id: annotate.primary(shapes.int()),
|
|
190
|
+
user_id: annotate.foreign(shapes.int(), "User", "id"),
|
|
191
|
+
}),
|
|
192
|
+
"Post",
|
|
193
|
+
),
|
|
194
|
+
{ tablePrefix: "app_" },
|
|
195
|
+
),
|
|
196
|
+
).toBe(
|
|
197
|
+
[
|
|
198
|
+
`CREATE TABLE IF NOT EXISTS "app_Post" (`,
|
|
199
|
+
` "id" INTEGER PRIMARY KEY,`,
|
|
200
|
+
` "user_id" INTEGER NOT NULL REFERENCES "app_User"("id")`,
|
|
201
|
+
")",
|
|
202
|
+
].join("\n"),
|
|
203
|
+
);
|
|
204
|
+
});
|
|
161
205
|
});
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Shape } from "../shape";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
export type PostgresOptions = {
|
|
4
|
+
tablePrefix?: string;
|
|
5
|
+
}
|
|
3
6
|
|
|
4
7
|
const pgType = (shape: Shape): string => {
|
|
5
8
|
switch (shape.type) {
|
|
@@ -44,8 +47,8 @@ const pgType = (shape: Shape): string => {
|
|
|
44
47
|
}
|
|
45
48
|
};
|
|
46
49
|
|
|
47
|
-
const pgColumnDef = (colName: string, shape: Shape): string => {
|
|
48
|
-
const parts: string[] = [colName
|
|
50
|
+
const pgColumnDef = (colName: string, shape: Shape, options: PostgresOptions): string => {
|
|
51
|
+
const parts: string[] = [`"${colName}"`, pgType(shape)];
|
|
49
52
|
if (shape.anno.primary) {
|
|
50
53
|
parts.push("PRIMARY KEY");
|
|
51
54
|
} else if (!shape.anno.optional) {
|
|
@@ -60,19 +63,21 @@ const pgColumnDef = (colName: string, shape: Shape): string => {
|
|
|
60
63
|
}
|
|
61
64
|
}
|
|
62
65
|
if (shape.anno.foreign) {
|
|
66
|
+
const refTable = `${options.tablePrefix ?? ""}${shape.anno.foreign.shapeName}`;
|
|
63
67
|
parts.push(
|
|
64
|
-
`REFERENCES "${
|
|
68
|
+
`REFERENCES "${refTable}"("${shape.anno.foreign.fieldName}")`,
|
|
65
69
|
);
|
|
66
70
|
}
|
|
67
71
|
return parts.join(" ");
|
|
68
72
|
};
|
|
69
73
|
|
|
70
|
-
export const postgres:
|
|
74
|
+
export const postgres = (shape: Shape, options: PostgresOptions = {}) => {
|
|
71
75
|
if (shape.type === "mapping" && shape.anno.name) {
|
|
76
|
+
const tableName = `${options.tablePrefix ?? ""}${shape.anno.name}`;
|
|
72
77
|
const cols = Object.entries(shape.rec)
|
|
73
|
-
.map(([k, v]) => ` ${pgColumnDef(k, v)}`)
|
|
78
|
+
.map(([k, v]) => ` ${pgColumnDef(k, v, options)}`)
|
|
74
79
|
.join(",\n");
|
|
75
|
-
return `CREATE TABLE IF NOT EXISTS "${
|
|
80
|
+
return `CREATE TABLE IF NOT EXISTS "${tableName}" (\n${cols}\n)`;
|
|
76
81
|
}
|
|
77
82
|
return pgType(shape);
|
|
78
83
|
};
|