@remix-run/data-table-postgres 0.0.0 → 0.1.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.
@@ -0,0 +1,392 @@
1
+ import { getTableName, getTablePrimaryKey } from '@remix-run/data-table';
2
+ export function compilePostgresStatement(statement) {
3
+ if (statement.kind === 'raw') {
4
+ return compileRawStatement(statement.sql);
5
+ }
6
+ let context = { values: [] };
7
+ if (statement.kind === 'select') {
8
+ let selection = '*';
9
+ if (statement.select !== '*') {
10
+ selection = statement.select
11
+ .map((field) => quotePath(field.column) + ' as ' + quoteIdentifier(field.alias))
12
+ .join(', ');
13
+ }
14
+ let text = 'select ' +
15
+ (statement.distinct ? 'distinct ' : '') +
16
+ selection +
17
+ compileFromClause(statement.table, statement.joins, context) +
18
+ compileWhereClause(statement.where, context) +
19
+ compileGroupByClause(statement.groupBy) +
20
+ compileHavingClause(statement.having, context) +
21
+ compileOrderByClause(statement.orderBy) +
22
+ compileLimitClause(statement.limit) +
23
+ compileOffsetClause(statement.offset);
24
+ return {
25
+ text,
26
+ values: context.values,
27
+ };
28
+ }
29
+ if (statement.kind === 'count' || statement.kind === 'exists') {
30
+ let inner = 'select 1' +
31
+ compileFromClause(statement.table, statement.joins, context) +
32
+ compileWhereClause(statement.where, context) +
33
+ compileGroupByClause(statement.groupBy) +
34
+ compileHavingClause(statement.having, context);
35
+ return {
36
+ text: 'select count(*) as ' +
37
+ quoteIdentifier('count') +
38
+ ' from (' +
39
+ inner +
40
+ ') as ' +
41
+ quoteIdentifier('__dt_count'),
42
+ values: context.values,
43
+ };
44
+ }
45
+ if (statement.kind === 'insert') {
46
+ return compileInsertStatement(statement.table, statement.values, statement.returning, context);
47
+ }
48
+ if (statement.kind === 'insertMany') {
49
+ return compileInsertManyStatement(statement.table, statement.values, statement.returning, context);
50
+ }
51
+ if (statement.kind === 'update') {
52
+ let changes = Object.keys(statement.changes);
53
+ let assignments = changes
54
+ .map((column) => quotePath(column) + ' = ' + pushValue(context, statement.changes[column]))
55
+ .join(', ');
56
+ return {
57
+ text: 'update ' +
58
+ quotePath(getTableName(statement.table)) +
59
+ ' set ' +
60
+ assignments +
61
+ compileWhereClause(statement.where, context) +
62
+ compileReturningClause(statement.returning),
63
+ values: context.values,
64
+ };
65
+ }
66
+ if (statement.kind === 'delete') {
67
+ return {
68
+ text: 'delete from ' +
69
+ quotePath(getTableName(statement.table)) +
70
+ compileWhereClause(statement.where, context) +
71
+ compileReturningClause(statement.returning),
72
+ values: context.values,
73
+ };
74
+ }
75
+ if (statement.kind === 'upsert') {
76
+ return compileUpsertStatement(statement, context);
77
+ }
78
+ throw new Error('Unsupported statement kind');
79
+ }
80
+ function compileInsertStatement(table, values, returning, context) {
81
+ let columns = Object.keys(values);
82
+ if (columns.length === 0) {
83
+ return {
84
+ text: 'insert into ' +
85
+ quotePath(getTableName(table)) +
86
+ ' default values' +
87
+ compileReturningClause(returning),
88
+ values: context.values,
89
+ };
90
+ }
91
+ let quotedColumns = columns.map((column) => quotePath(column));
92
+ let placeholders = columns.map((column) => pushValue(context, values[column]));
93
+ return {
94
+ text: 'insert into ' +
95
+ quotePath(getTableName(table)) +
96
+ ' (' +
97
+ quotedColumns.join(', ') +
98
+ ') values (' +
99
+ placeholders.join(', ') +
100
+ ')' +
101
+ compileReturningClause(returning),
102
+ values: context.values,
103
+ };
104
+ }
105
+ function compileInsertManyStatement(table, rows, returning, context) {
106
+ if (rows.length === 0) {
107
+ return {
108
+ text: 'select 0 where 1 = 0',
109
+ values: context.values,
110
+ };
111
+ }
112
+ let columns = collectColumns(rows);
113
+ if (columns.length === 0) {
114
+ return {
115
+ text: 'insert into ' +
116
+ quotePath(getTableName(table)) +
117
+ ' default values' +
118
+ compileReturningClause(returning),
119
+ values: context.values,
120
+ };
121
+ }
122
+ let quotedColumns = columns.map((column) => quotePath(column));
123
+ let valueSets = rows.map((row) => {
124
+ let placeholders = columns.map((column) => {
125
+ let value = Object.prototype.hasOwnProperty.call(row, column) ? row[column] : null;
126
+ return pushValue(context, value);
127
+ });
128
+ return '(' + placeholders.join(', ') + ')';
129
+ });
130
+ return {
131
+ text: 'insert into ' +
132
+ quotePath(getTableName(table)) +
133
+ ' (' +
134
+ quotedColumns.join(', ') +
135
+ ') values ' +
136
+ valueSets.join(', ') +
137
+ compileReturningClause(returning),
138
+ values: context.values,
139
+ };
140
+ }
141
+ function compileUpsertStatement(statement, context) {
142
+ let insertColumns = Object.keys(statement.values);
143
+ let conflictTarget = statement.conflictTarget ?? [...getTablePrimaryKey(statement.table)];
144
+ if (insertColumns.length === 0) {
145
+ throw new Error('upsert requires at least one value');
146
+ }
147
+ let quotedInsertColumns = insertColumns.map((column) => quotePath(column));
148
+ let insertPlaceholders = insertColumns.map((column) => pushValue(context, statement.values[column]));
149
+ let updateValues = statement.update ?? statement.values;
150
+ let updateColumns = Object.keys(updateValues);
151
+ let onConflictClause = '';
152
+ if (updateColumns.length === 0) {
153
+ onConflictClause =
154
+ ' on conflict (' +
155
+ conflictTarget.map((column) => quotePath(column)).join(', ') +
156
+ ') do nothing';
157
+ }
158
+ else {
159
+ onConflictClause =
160
+ ' on conflict (' +
161
+ conflictTarget.map((column) => quotePath(column)).join(', ') +
162
+ ') do update set ' +
163
+ updateColumns
164
+ .map((column) => quotePath(column) + ' = ' + pushValue(context, updateValues[column]))
165
+ .join(', ');
166
+ }
167
+ return {
168
+ text: 'insert into ' +
169
+ quotePath(getTableName(statement.table)) +
170
+ ' (' +
171
+ quotedInsertColumns.join(', ') +
172
+ ') values (' +
173
+ insertPlaceholders.join(', ') +
174
+ ')' +
175
+ onConflictClause +
176
+ compileReturningClause(statement.returning),
177
+ values: context.values,
178
+ };
179
+ }
180
+ function compileRawStatement(statement) {
181
+ if (!statement.text.includes('?')) {
182
+ return {
183
+ text: statement.text,
184
+ values: [...statement.values],
185
+ };
186
+ }
187
+ let index = 1;
188
+ let text = statement.text.replace(/\?/g, function replaceParameter() {
189
+ let placeholder = '$' + String(index);
190
+ index += 1;
191
+ return placeholder;
192
+ });
193
+ return {
194
+ text,
195
+ values: [...statement.values],
196
+ };
197
+ }
198
+ function compileFromClause(table, joins, context) {
199
+ let output = ' from ' + quotePath(getTableName(table));
200
+ for (let join of joins) {
201
+ output +=
202
+ ' ' +
203
+ normalizeJoinType(join.type) +
204
+ ' join ' +
205
+ quotePath(getTableName(join.table)) +
206
+ ' on ' +
207
+ compilePredicate(join.on, context);
208
+ }
209
+ return output;
210
+ }
211
+ function compileWhereClause(predicates, context) {
212
+ if (predicates.length === 0) {
213
+ return '';
214
+ }
215
+ let where = predicates
216
+ .map((predicate) => '(' + compilePredicate(predicate, context) + ')')
217
+ .join(' and ');
218
+ return ' where ' + where;
219
+ }
220
+ function compileGroupByClause(columns) {
221
+ if (columns.length === 0) {
222
+ return '';
223
+ }
224
+ return ' group by ' + columns.map((column) => quotePath(column)).join(', ');
225
+ }
226
+ function compileHavingClause(predicates, context) {
227
+ if (predicates.length === 0) {
228
+ return '';
229
+ }
230
+ let having = predicates
231
+ .map((predicate) => '(' + compilePredicate(predicate, context) + ')')
232
+ .join(' and ');
233
+ return ' having ' + having;
234
+ }
235
+ function compileOrderByClause(orderBy) {
236
+ if (orderBy.length === 0) {
237
+ return '';
238
+ }
239
+ return (' order by ' +
240
+ orderBy
241
+ .map((clause) => quotePath(clause.column) + ' ' + clause.direction.toUpperCase())
242
+ .join(', '));
243
+ }
244
+ function compileLimitClause(limit) {
245
+ if (limit === undefined) {
246
+ return '';
247
+ }
248
+ return ' limit ' + String(limit);
249
+ }
250
+ function compileOffsetClause(offset) {
251
+ if (offset === undefined) {
252
+ return '';
253
+ }
254
+ return ' offset ' + String(offset);
255
+ }
256
+ function compileReturningClause(returning) {
257
+ if (!returning) {
258
+ return '';
259
+ }
260
+ if (returning === '*') {
261
+ return ' returning *';
262
+ }
263
+ return ' returning ' + returning.map((column) => quotePath(column)).join(', ');
264
+ }
265
+ function compilePredicate(predicate, context) {
266
+ if (predicate.type === 'comparison') {
267
+ let column = quotePath(predicate.column);
268
+ if (predicate.operator === 'eq') {
269
+ if (predicate.valueType === 'value' &&
270
+ (predicate.value === null || predicate.value === undefined)) {
271
+ return column + ' is null';
272
+ }
273
+ let comparisonValue = compileComparisonValue(predicate, context);
274
+ return column + ' = ' + comparisonValue;
275
+ }
276
+ if (predicate.operator === 'ne') {
277
+ if (predicate.valueType === 'value' &&
278
+ (predicate.value === null || predicate.value === undefined)) {
279
+ return column + ' is not null';
280
+ }
281
+ let comparisonValue = compileComparisonValue(predicate, context);
282
+ return column + ' <> ' + comparisonValue;
283
+ }
284
+ if (predicate.operator === 'gt') {
285
+ let comparisonValue = compileComparisonValue(predicate, context);
286
+ return column + ' > ' + comparisonValue;
287
+ }
288
+ if (predicate.operator === 'gte') {
289
+ let comparisonValue = compileComparisonValue(predicate, context);
290
+ return column + ' >= ' + comparisonValue;
291
+ }
292
+ if (predicate.operator === 'lt') {
293
+ let comparisonValue = compileComparisonValue(predicate, context);
294
+ return column + ' < ' + comparisonValue;
295
+ }
296
+ if (predicate.operator === 'lte') {
297
+ let comparisonValue = compileComparisonValue(predicate, context);
298
+ return column + ' <= ' + comparisonValue;
299
+ }
300
+ if (predicate.operator === 'in' || predicate.operator === 'notIn') {
301
+ let values = Array.isArray(predicate.value) ? predicate.value : [];
302
+ if (values.length === 0) {
303
+ return predicate.operator === 'in' ? '1 = 0' : '1 = 1';
304
+ }
305
+ let placeholders = values.map((value) => pushValue(context, value));
306
+ let keyword = predicate.operator === 'in' ? 'in' : 'not in';
307
+ return column + ' ' + keyword + ' (' + placeholders.join(', ') + ')';
308
+ }
309
+ if (predicate.operator === 'like') {
310
+ let comparisonValue = compileComparisonValue(predicate, context);
311
+ return column + ' like ' + comparisonValue;
312
+ }
313
+ if (predicate.operator === 'ilike') {
314
+ let comparisonValue = compileComparisonValue(predicate, context);
315
+ return column + ' ilike ' + comparisonValue;
316
+ }
317
+ }
318
+ if (predicate.type === 'between') {
319
+ return (quotePath(predicate.column) +
320
+ ' between ' +
321
+ pushValue(context, predicate.lower) +
322
+ ' and ' +
323
+ pushValue(context, predicate.upper));
324
+ }
325
+ if (predicate.type === 'null') {
326
+ return (quotePath(predicate.column) + (predicate.operator === 'isNull' ? ' is null' : ' is not null'));
327
+ }
328
+ if (predicate.type === 'logical') {
329
+ if (predicate.predicates.length === 0) {
330
+ return predicate.operator === 'and' ? '1 = 1' : '1 = 0';
331
+ }
332
+ let childOperator = predicate.operator === 'and' ? ' and ' : ' or ';
333
+ let childPredicates = predicate.predicates
334
+ .map((child) => '(' + compilePredicate(child, context) + ')')
335
+ .join(childOperator);
336
+ return childPredicates;
337
+ }
338
+ throw new Error('Unsupported predicate');
339
+ }
340
+ function compileComparisonValue(predicate, context) {
341
+ if (predicate.valueType === 'column') {
342
+ return quotePath(predicate.value);
343
+ }
344
+ return pushValue(context, predicate.value);
345
+ }
346
+ function normalizeJoinType(type) {
347
+ if (type === 'left') {
348
+ return 'left';
349
+ }
350
+ if (type === 'right') {
351
+ return 'right';
352
+ }
353
+ return 'inner';
354
+ }
355
+ function quoteIdentifier(value) {
356
+ return '"' + value.replace(/"/g, '""') + '"';
357
+ }
358
+ function quotePath(path) {
359
+ if (path === '*') {
360
+ return '*';
361
+ }
362
+ let segments = path.split('.');
363
+ return segments
364
+ .map((segment) => {
365
+ if (segment === '*') {
366
+ return '*';
367
+ }
368
+ return quoteIdentifier(segment);
369
+ })
370
+ .join('.');
371
+ }
372
+ function pushValue(context, value) {
373
+ context.values.push(value);
374
+ return '$' + String(context.values.length);
375
+ }
376
+ function collectColumns(rows) {
377
+ let columns = [];
378
+ let seen = new Set();
379
+ for (let row of rows) {
380
+ for (let key in row) {
381
+ if (!Object.prototype.hasOwnProperty.call(row, key)) {
382
+ continue;
383
+ }
384
+ if (seen.has(key)) {
385
+ continue;
386
+ }
387
+ seen.add(key);
388
+ columns.push(key);
389
+ }
390
+ }
391
+ return columns;
392
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@remix-run/data-table-postgres",
3
- "version": "0.0.0",
4
- "private": false,
5
- "description": "Placeholder package for future Remix data-table-postgres APIs",
3
+ "version": "0.1.0",
4
+ "description": "PostgreSQL adapter for @remix-run/data-table",
5
+ "author": "Michael Jackson <mjijackson@gmail.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -10,11 +10,53 @@
10
10
  "directory": "packages/data-table-postgres"
11
11
  },
12
12
  "homepage": "https://github.com/remix-run/remix/tree/main/packages/data-table-postgres#readme",
13
+ "files": [
14
+ "LICENSE",
15
+ "README.md",
16
+ "dist",
17
+ "src",
18
+ "!src/**/*.test.ts"
19
+ ],
20
+ "type": "module",
21
+ "sideEffects": false,
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^24.6.0",
31
+ "@types/pg": "^8.15.6",
32
+ "@typescript/native-preview": "7.0.0-dev.20251125.1",
33
+ "pg": "^8.16.3",
34
+ "@remix-run/data-schema": "0.1.0",
35
+ "@remix-run/data-table": "0.1.0"
36
+ },
37
+ "dependencies": {
38
+ "@remix-run/data-table": "^0.1.0"
39
+ },
40
+ "peerDependencies": {
41
+ "pg": "^8.16.3"
42
+ },
43
+ "peerDependenciesMeta": {
44
+ "pg": {
45
+ "optional": true
46
+ }
47
+ },
13
48
  "keywords": [
14
49
  "remix",
15
- "placeholder"
50
+ "orm",
51
+ "postgres",
52
+ "database",
53
+ "sql"
16
54
  ],
17
- "publishConfig": {
18
- "access": "public"
55
+ "scripts": {
56
+ "build": "tsgo -p tsconfig.build.json",
57
+ "clean": "git clean -fdX",
58
+ "test": "node --disable-warning=ExperimentalWarning --test",
59
+ "test:coverage": "node --disable-warning=ExperimentalWarning --experimental-test-coverage --test-coverage-include='src/**/*.ts' --test-coverage-lines=90 --test-coverage-branches=90 --test-coverage-functions=90 --test ./src/lib/adapter.test.ts ./src/lib/sql-compiler.test.ts",
60
+ "typecheck": "tsgo --noEmit"
19
61
  }
20
- }
62
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export type {
2
+ PostgresDatabaseAdapterOptions,
3
+ PostgresDatabaseClient,
4
+ PostgresDatabasePool,
5
+ PostgresQueryResult,
6
+ PostgresTransactionClient,
7
+ } from './lib/adapter.ts'
8
+ export { createPostgresDatabaseAdapter, PostgresDatabaseAdapter } from './lib/adapter.ts'