@remix-run/data-table-postgres 0.1.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.
@@ -1,37 +1,38 @@
1
1
  import { getTableName, getTablePrimaryKey } from '@remix-run/data-table';
2
- export function compilePostgresStatement(statement) {
3
- if (statement.kind === 'raw') {
4
- return compileRawStatement(statement.sql);
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);
5
6
  }
6
7
  let context = { values: [] };
7
- if (statement.kind === 'select') {
8
+ if (operation.kind === 'select') {
8
9
  let selection = '*';
9
- if (statement.select !== '*') {
10
- selection = statement.select
10
+ if (operation.select !== '*') {
11
+ selection = operation.select
11
12
  .map((field) => quotePath(field.column) + ' as ' + quoteIdentifier(field.alias))
12
13
  .join(', ');
13
14
  }
14
15
  let text = 'select ' +
15
- (statement.distinct ? 'distinct ' : '') +
16
+ (operation.distinct ? 'distinct ' : '') +
16
17
  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);
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);
24
25
  return {
25
26
  text,
26
27
  values: context.values,
27
28
  };
28
29
  }
29
- if (statement.kind === 'count' || statement.kind === 'exists') {
30
+ if (operation.kind === 'count' || operation.kind === 'exists') {
30
31
  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);
32
+ compileFromClause(operation.table, operation.joins, context) +
33
+ compileWhereClause(operation.where, context) +
34
+ compileGroupByClause(operation.groupBy) +
35
+ compileHavingClause(operation.having, context);
35
36
  return {
36
37
  text: 'select count(*) as ' +
37
38
  quoteIdentifier('count') +
@@ -42,42 +43,42 @@ export function compilePostgresStatement(statement) {
42
43
  values: context.values,
43
44
  };
44
45
  }
45
- if (statement.kind === 'insert') {
46
- return compileInsertStatement(statement.table, statement.values, statement.returning, context);
46
+ if (operation.kind === 'insert') {
47
+ return compileInsertOperation(operation.table, operation.values, operation.returning, context);
47
48
  }
48
- if (statement.kind === 'insertMany') {
49
- return compileInsertManyStatement(statement.table, statement.values, statement.returning, context);
49
+ if (operation.kind === 'insertMany') {
50
+ return compileInsertManyOperation(operation.table, operation.values, operation.returning, context);
50
51
  }
51
- if (statement.kind === 'update') {
52
- let changes = Object.keys(statement.changes);
52
+ if (operation.kind === 'update') {
53
+ let changes = Object.keys(operation.changes);
53
54
  let assignments = changes
54
- .map((column) => quotePath(column) + ' = ' + pushValue(context, statement.changes[column]))
55
+ .map((column) => quotePath(column) + ' = ' + pushValue(context, operation.changes[column]))
55
56
  .join(', ');
56
57
  return {
57
58
  text: 'update ' +
58
- quotePath(getTableName(statement.table)) +
59
+ quotePath(getTableName(operation.table)) +
59
60
  ' set ' +
60
61
  assignments +
61
- compileWhereClause(statement.where, context) +
62
- compileReturningClause(statement.returning),
62
+ compileWhereClause(operation.where, context) +
63
+ compileReturningClause(operation.returning),
63
64
  values: context.values,
64
65
  };
65
66
  }
66
- if (statement.kind === 'delete') {
67
+ if (operation.kind === 'delete') {
67
68
  return {
68
69
  text: 'delete from ' +
69
- quotePath(getTableName(statement.table)) +
70
- compileWhereClause(statement.where, context) +
71
- compileReturningClause(statement.returning),
70
+ quotePath(getTableName(operation.table)) +
71
+ compileWhereClause(operation.where, context) +
72
+ compileReturningClause(operation.returning),
72
73
  values: context.values,
73
74
  };
74
75
  }
75
- if (statement.kind === 'upsert') {
76
- return compileUpsertStatement(statement, context);
76
+ if (operation.kind === 'upsert') {
77
+ return compileUpsertOperation(operation, context);
77
78
  }
78
- throw new Error('Unsupported statement kind');
79
+ throw new Error('Unsupported operation kind');
79
80
  }
80
- function compileInsertStatement(table, values, returning, context) {
81
+ function compileInsertOperation(table, values, returning, context) {
81
82
  let columns = Object.keys(values);
82
83
  if (columns.length === 0) {
83
84
  return {
@@ -102,7 +103,7 @@ function compileInsertStatement(table, values, returning, context) {
102
103
  values: context.values,
103
104
  };
104
105
  }
105
- function compileInsertManyStatement(table, rows, returning, context) {
106
+ function compileInsertManyOperation(table, rows, returning, context) {
106
107
  if (rows.length === 0) {
107
108
  return {
108
109
  text: 'select 0 where 1 = 0',
@@ -138,15 +139,15 @@ function compileInsertManyStatement(table, rows, returning, context) {
138
139
  values: context.values,
139
140
  };
140
141
  }
141
- function compileUpsertStatement(statement, context) {
142
- let insertColumns = Object.keys(statement.values);
143
- let conflictTarget = statement.conflictTarget ?? [...getTablePrimaryKey(statement.table)];
142
+ function compileUpsertOperation(operation, context) {
143
+ let insertColumns = Object.keys(operation.values);
144
+ let conflictTarget = operation.conflictTarget ?? [...getTablePrimaryKey(operation.table)];
144
145
  if (insertColumns.length === 0) {
145
146
  throw new Error('upsert requires at least one value');
146
147
  }
147
148
  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;
149
+ let insertPlaceholders = insertColumns.map((column) => pushValue(context, operation.values[column]));
150
+ let updateValues = operation.update ?? operation.values;
150
151
  let updateColumns = Object.keys(updateValues);
151
152
  let onConflictClause = '';
152
153
  if (updateColumns.length === 0) {
@@ -166,18 +167,18 @@ function compileUpsertStatement(statement, context) {
166
167
  }
167
168
  return {
168
169
  text: 'insert into ' +
169
- quotePath(getTableName(statement.table)) +
170
+ quotePath(getTableName(operation.table)) +
170
171
  ' (' +
171
172
  quotedInsertColumns.join(', ') +
172
173
  ') values (' +
173
174
  insertPlaceholders.join(', ') +
174
175
  ')' +
175
176
  onConflictClause +
176
- compileReturningClause(statement.returning),
177
+ compileReturningClause(operation.returning),
177
178
  values: context.values,
178
179
  };
179
180
  }
180
- function compileRawStatement(statement) {
181
+ function compileRawOperation(statement) {
181
182
  if (!statement.text.includes('?')) {
182
183
  return {
183
184
  text: statement.text,
@@ -344,49 +345,18 @@ function compileComparisonValue(predicate, context) {
344
345
  return pushValue(context, predicate.value);
345
346
  }
346
347
  function normalizeJoinType(type) {
347
- if (type === 'left') {
348
- return 'left';
349
- }
350
- if (type === 'right') {
351
- return 'right';
352
- }
353
- return 'inner';
348
+ return normalizeJoinTypeHelper(type);
354
349
  }
355
350
  function quoteIdentifier(value) {
356
351
  return '"' + value.replace(/"/g, '""') + '"';
357
352
  }
358
353
  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('.');
354
+ return quotePathHelper(path, quoteIdentifier);
371
355
  }
372
356
  function pushValue(context, value) {
373
357
  context.values.push(value);
374
358
  return '$' + String(context.values.length);
375
359
  }
376
360
  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;
361
+ return collectColumnsHelper(rows);
392
362
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@remix-run/data-table-postgres",
3
- "version": "0.1.0",
4
- "description": "PostgreSQL adapter for @remix-run/data-table",
3
+ "version": "0.2.0",
4
+ "description": "PostgreSQL adapter for remix/data-table",
5
5
  "author": "Michael Jackson <mjijackson@gmail.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -31,11 +31,10 @@
31
31
  "@types/pg": "^8.15.6",
32
32
  "@typescript/native-preview": "7.0.0-dev.20251125.1",
33
33
  "pg": "^8.16.3",
34
- "@remix-run/data-schema": "0.1.0",
35
- "@remix-run/data-table": "0.1.0"
34
+ "@remix-run/data-table": "0.2.0"
36
35
  },
37
36
  "dependencies": {
38
- "@remix-run/data-table": "^0.1.0"
37
+ "@remix-run/data-table": "^0.2.0"
39
38
  },
40
39
  "peerDependencies": {
41
40
  "pg": "^8.16.3"
@@ -55,8 +54,8 @@
55
54
  "scripts": {
56
55
  "build": "tsgo -p tsconfig.build.json",
57
56
  "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",
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",
60
59
  "typecheck": "tsgo --noEmit"
61
60
  }
62
61
  }
package/src/index.ts CHANGED
@@ -1,8 +1,2 @@
1
- export type {
2
- PostgresDatabaseAdapterOptions,
3
- PostgresDatabaseClient,
4
- PostgresDatabasePool,
5
- PostgresQueryResult,
6
- PostgresTransactionClient,
7
- } from './lib/adapter.ts'
1
+ export type { PostgresDatabaseAdapterOptions } from './lib/adapter.ts'
8
2
  export { createPostgresDatabaseAdapter, PostgresDatabaseAdapter } from './lib/adapter.ts'