orchid-orm 1.5.10 → 1.5.12
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 +16 -0
- package/dist/bin.js +408 -0
- package/dist/bin.js.map +1 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +2 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -8
- package/rollup.config.js +15 -1
- package/src/bin/bin.ts +3 -0
- package/src/bin/init.test.ts +736 -0
- package/src/bin/init.ts +458 -0
- package/src/table.test.ts +5 -5
- package/src/table.ts +7 -4
- package/src/test-utils/test-tables.ts +5 -6
package/src/bin/init.ts
ADDED
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import fs from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
import prompts from 'prompts';
|
|
5
|
+
|
|
6
|
+
export type InitConfig = {
|
|
7
|
+
testDatabase?: boolean;
|
|
8
|
+
addSchemaToZod?: boolean;
|
|
9
|
+
addTestFactory?: boolean;
|
|
10
|
+
demoTables?: boolean;
|
|
11
|
+
timestamp?: 'date' | 'number';
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
type DependencyKind = 'dependencies' | 'devDependencies';
|
|
15
|
+
|
|
16
|
+
const dirPath = path.resolve(process.cwd(), 'src', 'db');
|
|
17
|
+
|
|
18
|
+
export const astOrchidORMConfig = async () => {
|
|
19
|
+
const response = await prompts([
|
|
20
|
+
{
|
|
21
|
+
type: 'select',
|
|
22
|
+
name: 'timestamp',
|
|
23
|
+
message: 'Preferred type of returned timestamps:',
|
|
24
|
+
choices: [
|
|
25
|
+
{
|
|
26
|
+
title: 'string (as returned from db)',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
title: 'number (epoch)',
|
|
30
|
+
value: 'number',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
title: 'Date object',
|
|
34
|
+
value: 'date',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
type: 'confirm',
|
|
40
|
+
name: 'testDatabase',
|
|
41
|
+
message: 'Should I add a separate database for tests?',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
type: 'confirm',
|
|
45
|
+
name: 'addSchemaToZod',
|
|
46
|
+
message: 'Are you going to use Zod for validation?',
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
type: 'confirm',
|
|
50
|
+
name: 'addTestFactory',
|
|
51
|
+
message: 'Do you want object factories for writing tests?',
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
type: 'confirm',
|
|
55
|
+
name: 'demoTables',
|
|
56
|
+
message: 'Should I add demo tables?',
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
return response as InitConfig;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const initOrchidORM = async (config: InitConfig) => {
|
|
64
|
+
await fs.mkdir(dirPath, { recursive: true });
|
|
65
|
+
|
|
66
|
+
await setupPackageJson(config);
|
|
67
|
+
await setupTSConfig();
|
|
68
|
+
await setupEnv(config);
|
|
69
|
+
await setupGitIgnore();
|
|
70
|
+
await setupBaseTable(config);
|
|
71
|
+
await setupTables(config);
|
|
72
|
+
await setupConfig(config);
|
|
73
|
+
await setupMainDb(config);
|
|
74
|
+
await setupMigrationScript(config);
|
|
75
|
+
await createMigrations(config);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const setupPackageJson = async (config: InitConfig) => {
|
|
79
|
+
const pairs = await Promise.all([
|
|
80
|
+
getLatestPackageVersion('dotenv', 'dependencies'),
|
|
81
|
+
getLatestPackageVersion('orchid-orm', 'dependencies'),
|
|
82
|
+
getLatestPackageVersion('pqb', 'dependencies'),
|
|
83
|
+
config.addSchemaToZod &&
|
|
84
|
+
getLatestPackageVersion('orchid-orm-schema-to-zod', 'dependencies'),
|
|
85
|
+
getLatestPackageVersion('rake-db', 'devDependencies'),
|
|
86
|
+
config.addTestFactory &&
|
|
87
|
+
getLatestPackageVersion('orchid-orm-test-factory', 'devDependencies'),
|
|
88
|
+
getLatestPackageVersion('@swc/core', 'devDependencies'),
|
|
89
|
+
getLatestPackageVersion('@types/node', 'devDependencies'),
|
|
90
|
+
getLatestPackageVersion('ts-node', 'devDependencies'),
|
|
91
|
+
getLatestPackageVersion('typescript', 'devDependencies'),
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
const deps: Record<string, string> = {};
|
|
95
|
+
const devDeps: Record<string, string> = {};
|
|
96
|
+
for (const item of pairs) {
|
|
97
|
+
if (!item) continue;
|
|
98
|
+
const [key, { version, kind }] = item;
|
|
99
|
+
(kind === 'dependencies' ? deps : devDeps)[key] = version;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const packageJsonPath = path.resolve(process.cwd(), 'package.json');
|
|
103
|
+
const content = await readFileSafe(packageJsonPath);
|
|
104
|
+
const json = content ? JSON.parse(content) : {};
|
|
105
|
+
|
|
106
|
+
if (!json.scripts) json.scripts = {};
|
|
107
|
+
json.scripts.db = 'ts-node src/db/dbScripts.ts';
|
|
108
|
+
|
|
109
|
+
if (!json.dependencies) json.dependencies = {};
|
|
110
|
+
|
|
111
|
+
for (const key in deps) {
|
|
112
|
+
json.dependencies[key] = deps[key];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!json.devDependencies) json.devDependencies = {};
|
|
116
|
+
for (const key in devDeps) {
|
|
117
|
+
json.devDependencies[key] = devDeps[key];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
await fs.writeFile(packageJsonPath, JSON.stringify(json, null, ' ') + '\n');
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const getLatestPackageVersion = (
|
|
124
|
+
name: string,
|
|
125
|
+
kind: DependencyKind,
|
|
126
|
+
): Promise<[string, { version: string; kind: DependencyKind }]> => {
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
https
|
|
129
|
+
.get(`https://registry.npmjs.org/${name}/latest`, (res) => {
|
|
130
|
+
let data = '';
|
|
131
|
+
res.on('data', (chunk) => (data += chunk));
|
|
132
|
+
res.on('end', () =>
|
|
133
|
+
resolve([name, { version: `^${JSON.parse(data).version}`, kind }]),
|
|
134
|
+
);
|
|
135
|
+
})
|
|
136
|
+
.on('error', reject);
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const readFileSafe = async (path: string) => {
|
|
141
|
+
try {
|
|
142
|
+
return await fs.readFile(path, 'utf-8');
|
|
143
|
+
} catch (err) {
|
|
144
|
+
if ((err as unknown as { code: string }).code === 'ENOENT') {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
throw err;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const setupTSConfig = async () => {
|
|
152
|
+
const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json');
|
|
153
|
+
const content = await readFileSafe(tsConfigPath);
|
|
154
|
+
const json = content ? JSON.parse(content) : {};
|
|
155
|
+
if (!json['ts-node']) {
|
|
156
|
+
json['ts-node'] = {};
|
|
157
|
+
}
|
|
158
|
+
if (!json['ts-node'].swc) {
|
|
159
|
+
json['ts-node'].swc = true;
|
|
160
|
+
}
|
|
161
|
+
if (!json.compilerOptions?.strict) {
|
|
162
|
+
if (!json.compilerOptions) json.compilerOptions = {};
|
|
163
|
+
json.compilerOptions.strict = true;
|
|
164
|
+
await fs.writeFile(tsConfigPath, `${JSON.stringify(json, null, ' ')}\n`);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const setupEnv = async (config: InitConfig) => {
|
|
169
|
+
const envPath = path.resolve(process.cwd(), '.env');
|
|
170
|
+
let content = ((await readFileSafe(envPath)) || '').trim();
|
|
171
|
+
let changed = false;
|
|
172
|
+
|
|
173
|
+
if (!content.match(/^DATABASE_URL=/m)) {
|
|
174
|
+
content += `\nDATABASE_URL=postgres://user:password@localhost:5432/dbname?ssl=false`;
|
|
175
|
+
changed = true;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (config.testDatabase && !content.match(/^DATABASE_TEST_URL=/m)) {
|
|
179
|
+
content += `\nDATABASE_TEST_URL=postgres://user:password@localhost:5432/dbname-test?ssl=false`;
|
|
180
|
+
changed = true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (changed) {
|
|
184
|
+
await fs.writeFile(envPath, `${content.trim()}\n`);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const setupGitIgnore = async () => {
|
|
189
|
+
const gitignorePath = path.resolve(process.cwd(), '.gitignore');
|
|
190
|
+
let content = ((await readFileSafe(gitignorePath)) || '').trim();
|
|
191
|
+
let changed = false;
|
|
192
|
+
|
|
193
|
+
if (!content.match(/^node_modules\b/m)) {
|
|
194
|
+
content += `\nnode_modules`;
|
|
195
|
+
changed = true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!content.match(/^.env\b/m)) {
|
|
199
|
+
content += `\n.env`;
|
|
200
|
+
changed = true;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (changed) {
|
|
204
|
+
await fs.writeFile(gitignorePath, `${content.trim()}\n`);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const setupBaseTable = async (config: InitConfig) => {
|
|
209
|
+
const filePath = path.join(dirPath, 'baseTable.ts');
|
|
210
|
+
|
|
211
|
+
let content = `import { createBaseTable } from 'orchid-orm';
|
|
212
|
+
|
|
213
|
+
export const BaseTable = createBaseTable({
|
|
214
|
+
columnTypes: (t) => ({
|
|
215
|
+
text: (min: 0, max: Infinity) => t.text(min, max),`;
|
|
216
|
+
|
|
217
|
+
const { timestamp } = config;
|
|
218
|
+
if (timestamp) {
|
|
219
|
+
content += `
|
|
220
|
+
timestamp: <P extends number>(precision?: P) => t.timestamp<P>(precision).${
|
|
221
|
+
timestamp === 'date' ? 'asDate' : 'asNumber'
|
|
222
|
+
}(),`;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
content += `
|
|
226
|
+
}),
|
|
227
|
+
});
|
|
228
|
+
`;
|
|
229
|
+
|
|
230
|
+
await fs.writeFile(filePath, content);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const setupTables = async (config: InitConfig) => {
|
|
234
|
+
if (!config.demoTables) return;
|
|
235
|
+
|
|
236
|
+
const tablesDir = path.join(dirPath, 'tables');
|
|
237
|
+
await fs.mkdir(tablesDir, { recursive: true });
|
|
238
|
+
|
|
239
|
+
await fs.writeFile(
|
|
240
|
+
path.join(tablesDir, 'post.table.ts'),
|
|
241
|
+
`import { BaseTable } from '../baseTable';
|
|
242
|
+
import { CommentTable } from './comment.table';
|
|
243
|
+
${
|
|
244
|
+
config.addSchemaToZod
|
|
245
|
+
? `import { tableToZod } from 'orchid-orm-schema-to-zod';\n`
|
|
246
|
+
: ''
|
|
247
|
+
}
|
|
248
|
+
export type Post = PostTable['columns']['type'];
|
|
249
|
+
class PostTable extends BaseTable {
|
|
250
|
+
table = 'post';
|
|
251
|
+
columns = this.setColumns((t) => ({
|
|
252
|
+
id: t.serial().primaryKey(),
|
|
253
|
+
title: t.text(3, 100),
|
|
254
|
+
text: t.text(20, 10000),
|
|
255
|
+
...t.timestamps(),
|
|
256
|
+
}));
|
|
257
|
+
|
|
258
|
+
relations = {
|
|
259
|
+
comments: this.hasMany(() => CommentTable, {
|
|
260
|
+
primaryKey: 'id',
|
|
261
|
+
foreignKey: 'postId',
|
|
262
|
+
}),
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
${
|
|
266
|
+
config.addSchemaToZod
|
|
267
|
+
? `\nexport const postSchema = tableToZod(PostTable);\n`
|
|
268
|
+
: ''
|
|
269
|
+
}`,
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
await fs.writeFile(
|
|
273
|
+
path.join(tablesDir, 'comment.table.ts'),
|
|
274
|
+
`import { BaseTable } from '../baseTable';
|
|
275
|
+
import { PostTable } from './post.table';
|
|
276
|
+
${
|
|
277
|
+
config.addSchemaToZod
|
|
278
|
+
? `import { tableToZod } from 'orchid-orm-schema-to-zod';\n`
|
|
279
|
+
: ''
|
|
280
|
+
}
|
|
281
|
+
export type Comment = CommentTable['columns']['type'];
|
|
282
|
+
class CommentTable extends BaseTable {
|
|
283
|
+
table = 'comment';
|
|
284
|
+
columns = this.setColumns((t) => ({
|
|
285
|
+
id: t.serial().primaryKey(),
|
|
286
|
+
postId: t.integer().foreignKey(() => PostTable, 'id').index(),
|
|
287
|
+
text: t.text(5, 1000),
|
|
288
|
+
...t.timestamps(),
|
|
289
|
+
}));
|
|
290
|
+
|
|
291
|
+
relations = {
|
|
292
|
+
post: this.belongsTo(() => PostTable, {
|
|
293
|
+
primaryKey: 'id',
|
|
294
|
+
foreignKey: 'postId',
|
|
295
|
+
}),
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
${
|
|
299
|
+
config.addSchemaToZod
|
|
300
|
+
? `\nexport const commentSchema = tableToZod(CommentTable);\n`
|
|
301
|
+
: ''
|
|
302
|
+
}`,
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const setupConfig = async (config: InitConfig) => {
|
|
307
|
+
const configPath = path.join(dirPath, 'config.ts');
|
|
308
|
+
|
|
309
|
+
let content = `import 'dotenv/config';
|
|
310
|
+
|
|
311
|
+
const database = {
|
|
312
|
+
databaseURL: process.env.DATABASE_URL,
|
|
313
|
+
};
|
|
314
|
+
if (!database.databaseURL) throw new Error('DATABASE_URL is missing in .env');`;
|
|
315
|
+
|
|
316
|
+
if (config.testDatabase) {
|
|
317
|
+
content += `
|
|
318
|
+
|
|
319
|
+
const testDatabase = {
|
|
320
|
+
databaseURL: process.env.DATABASE_TEST_URL,
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const allDatabases = [database];
|
|
324
|
+
|
|
325
|
+
if (testDatabase.databaseURL) {
|
|
326
|
+
allDatabases.push(testDatabase);
|
|
327
|
+
}`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
content += `
|
|
331
|
+
|
|
332
|
+
export const config = {`;
|
|
333
|
+
|
|
334
|
+
if (config.testDatabase) {
|
|
335
|
+
content += `
|
|
336
|
+
allDatabases,`;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (config.testDatabase) {
|
|
340
|
+
content += `
|
|
341
|
+
database: process.env.NODE_ENV === 'test' ? testDatabase : database,`;
|
|
342
|
+
} else {
|
|
343
|
+
content += `
|
|
344
|
+
database,`;
|
|
345
|
+
}
|
|
346
|
+
content += `
|
|
347
|
+
}
|
|
348
|
+
`;
|
|
349
|
+
|
|
350
|
+
await fs.writeFile(configPath, content);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
const setupMainDb = async (config: InitConfig) => {
|
|
354
|
+
let imports = '';
|
|
355
|
+
let tables = '';
|
|
356
|
+
if (config.demoTables) {
|
|
357
|
+
imports += `
|
|
358
|
+
import { Post } from './tables/post.table';
|
|
359
|
+
import { Comment } from './tables/comment.table';`;
|
|
360
|
+
tables += `
|
|
361
|
+
post: Post,
|
|
362
|
+
comment: Comment,`;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const dbPath = path.join(dirPath, 'db.ts');
|
|
366
|
+
await fs.writeFile(
|
|
367
|
+
dbPath,
|
|
368
|
+
`import { orchidORM } from 'orchid-orm';
|
|
369
|
+
import { config } from './config';${imports}
|
|
370
|
+
|
|
371
|
+
export const db = orchidORM(
|
|
372
|
+
config.database,
|
|
373
|
+
{${tables}
|
|
374
|
+
}
|
|
375
|
+
);
|
|
376
|
+
`,
|
|
377
|
+
);
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const setupMigrationScript = async (config: InitConfig) => {
|
|
381
|
+
const filePath = path.join(dirPath, 'dbScripts.ts');
|
|
382
|
+
await fs.writeFile(
|
|
383
|
+
filePath,
|
|
384
|
+
`import { rakeDb } from 'rake-db';
|
|
385
|
+
import { config } from './config';
|
|
386
|
+
import { appCodeUpdater } from 'orchid-orm';
|
|
387
|
+
|
|
388
|
+
rakeDb(${config.testDatabase ? 'config.allDatabases' : 'config.database'}, {
|
|
389
|
+
migrationsPath: 'src/db/migrations',
|
|
390
|
+
appCodeUpdater: appCodeUpdater({
|
|
391
|
+
tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
|
|
392
|
+
baseTablePath: 'src/lib/baseTable.ts',
|
|
393
|
+
baseTableName: 'BaseTable',
|
|
394
|
+
mainFilePath: 'src/db.ts',
|
|
395
|
+
}),
|
|
396
|
+
});
|
|
397
|
+
`,
|
|
398
|
+
);
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
const createMigrations = async (config: InitConfig) => {
|
|
402
|
+
const migrationsPath = path.join(dirPath, 'migrations');
|
|
403
|
+
await fs.mkdir(migrationsPath);
|
|
404
|
+
|
|
405
|
+
if (!config.demoTables) return;
|
|
406
|
+
|
|
407
|
+
const postPath = path.join(
|
|
408
|
+
migrationsPath,
|
|
409
|
+
`${makeFileTimeStamp()}_createPost.ts`,
|
|
410
|
+
);
|
|
411
|
+
await fs.writeFile(
|
|
412
|
+
postPath,
|
|
413
|
+
`import { change } from 'rake-db';
|
|
414
|
+
|
|
415
|
+
change(async (db) => {
|
|
416
|
+
await db.createTable('post', (t) => ({
|
|
417
|
+
id: t.serial().primaryKey(),
|
|
418
|
+
title: t.text(),
|
|
419
|
+
text: t.text(),
|
|
420
|
+
...t.timestamps(),
|
|
421
|
+
}));
|
|
422
|
+
});
|
|
423
|
+
`,
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
const commentPath = path.join(
|
|
427
|
+
migrationsPath,
|
|
428
|
+
`${makeFileTimeStamp()}_createComment.ts`,
|
|
429
|
+
);
|
|
430
|
+
await fs.writeFile(
|
|
431
|
+
commentPath,
|
|
432
|
+
`import { change } from 'rake-db';
|
|
433
|
+
|
|
434
|
+
change(async (db) => {
|
|
435
|
+
await db.createTable('comment', (t) => ({
|
|
436
|
+
id: t.serial().primaryKey(),
|
|
437
|
+
postId: t.integer().foreignKey('post', 'id').index(),
|
|
438
|
+
text: t.text(),
|
|
439
|
+
...t.timestamps(),
|
|
440
|
+
}));
|
|
441
|
+
});
|
|
442
|
+
`,
|
|
443
|
+
);
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
const makeFileTimeStamp = () => {
|
|
447
|
+
const now = new Date();
|
|
448
|
+
return [
|
|
449
|
+
now.getUTCFullYear(),
|
|
450
|
+
now.getUTCMonth() + 1,
|
|
451
|
+
now.getUTCDate(),
|
|
452
|
+
now.getUTCHours(),
|
|
453
|
+
now.getUTCMinutes(),
|
|
454
|
+
now.getUTCSeconds(),
|
|
455
|
+
]
|
|
456
|
+
.map((value) => (value < 10 ? `0${value}` : value))
|
|
457
|
+
.join('');
|
|
458
|
+
};
|
package/src/table.test.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { createBaseTable } from './table';
|
|
|
2
2
|
import { orchidORM } from './orm';
|
|
3
3
|
import { adapter, db } from './test-utils/test-db';
|
|
4
4
|
import { assertType, userData, useTestDatabase } from './test-utils/test-utils';
|
|
5
|
-
import { ColumnType,
|
|
5
|
+
import { ColumnType, Operators } from 'pqb';
|
|
6
6
|
import { BaseTable } from './test-utils/test-tables';
|
|
7
7
|
|
|
8
8
|
describe('table', () => {
|
|
@@ -68,12 +68,12 @@ describe('table', () => {
|
|
|
68
68
|
await db.user.create(userData);
|
|
69
69
|
|
|
70
70
|
const BaseTable = createBaseTable({
|
|
71
|
-
columnTypes: {
|
|
72
|
-
serial:
|
|
71
|
+
columnTypes: (t) => ({
|
|
72
|
+
serial: t.serial,
|
|
73
73
|
timestamp() {
|
|
74
|
-
return
|
|
74
|
+
return t.timestamp().parse((input) => new Date(input));
|
|
75
75
|
},
|
|
76
|
-
},
|
|
76
|
+
}),
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
class UserTable extends BaseTable {
|
package/src/table.ts
CHANGED
|
@@ -50,12 +50,15 @@ export type Table = {
|
|
|
50
50
|
|
|
51
51
|
export const createBaseTable = <CT extends ColumnTypesBase>(
|
|
52
52
|
options: {
|
|
53
|
-
columnTypes?: CT;
|
|
53
|
+
columnTypes?: CT | ((t: DefaultColumnTypes) => CT);
|
|
54
54
|
} = { columnTypes: columnTypes as unknown as CT },
|
|
55
55
|
) => {
|
|
56
|
-
|
|
57
|
-
options.columnTypes
|
|
58
|
-
|
|
56
|
+
const ct =
|
|
57
|
+
typeof options.columnTypes === 'function'
|
|
58
|
+
? options.columnTypes(columnTypes)
|
|
59
|
+
: options.columnTypes;
|
|
60
|
+
|
|
61
|
+
return create(ct as ColumnTypesBase extends CT ? DefaultColumnTypes : CT);
|
|
59
62
|
};
|
|
60
63
|
|
|
61
64
|
const create = <CT extends ColumnTypesBase>(columnTypes: CT) => {
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { createBaseTable } from '../table';
|
|
2
|
-
import { columnTypes } from 'pqb';
|
|
3
2
|
import { tableToZod } from 'orchid-orm-schema-to-zod';
|
|
4
3
|
|
|
5
4
|
export const BaseTable = createBaseTable({
|
|
6
|
-
columnTypes: {
|
|
7
|
-
...
|
|
8
|
-
text: (min = 0, max = Infinity) =>
|
|
5
|
+
columnTypes: (t) => ({
|
|
6
|
+
...t,
|
|
7
|
+
text: (min = 0, max = Infinity) => t.text(min, max),
|
|
9
8
|
timestamp() {
|
|
10
|
-
return
|
|
9
|
+
return t.timestamp().parse((input) => new Date(input));
|
|
11
10
|
},
|
|
12
|
-
},
|
|
11
|
+
}),
|
|
13
12
|
});
|
|
14
13
|
|
|
15
14
|
export type User = UserTable['columns']['type'];
|