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