supalite 0.5.5 โ†’ 0.5.6

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 CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.5.6] - 2025-12-17
4
+
5
+ ### ๐Ÿž Fixed
6
+ - `select()`์˜ PostgREST-style embed(`related_table(*)`)๊ฐ€ **์–‘๋ฐฉํ–ฅ FK**๋ฅผ ์ง€์›ํ•˜๋„๋ก ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ 1:N ๊ด€๊ณ„๋Š” ๋ฐฐ์—ด(`[]` ๊ธฐ๋ณธ๊ฐ’), N:1 ๊ด€๊ณ„๋Š” ๊ฐ์ฒด(๋˜๋Š” `null`)๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. (See [docs/changelog/2025-12-17-embed-many-to-one.md](docs/changelog/2025-12-17-embed-many-to-one.md))
7
+
3
8
  ## [0.5.5] - 2025-11-26
4
9
 
5
10
  ### โœจ Added
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SupaLite
2
2
 
3
- [![npm version](https://img.shields.io/badge/version-0.3.3-blue.svg)](https://www.npmjs.com/package/supalite)
3
+ [![npm version](https://img.shields.io/npm/v/supalite.svg)](https://www.npmjs.com/package/supalite)
4
4
 
5
5
  ๊ฐ€๋ณ๊ณ  ํšจ์œจ์ ์ธ PostgreSQL ํด๋ผ์ด์–ธํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. Supabase์™€ ๋™์ผํ•œ API๋ฅผ ์ œ๊ณตํ•˜๋ฉด์„œ๋„ ๋” ๊ฐ€๋ณ๊ณ  ๋น ๋ฅธ ๊ตฌํ˜„์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
6
6
 
@@ -213,6 +213,17 @@ const { data, error } = await client
213
213
  .from('users')
214
214
  .ilike('email', '%@example.com');
215
215
 
216
+ // ๊ด€๊ณ„ ํ…Œ์ด๋ธ” ์กฐํšŒ (PostgREST-style embed)
217
+ // 1:N ๊ด€๊ณ„๋Š” ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค (๊ธฐ๋ณธ๊ฐ’: [])
218
+ const { data: authors } = await client
219
+ .from('authors')
220
+ .select('*, books(*)');
221
+
222
+ // N:1 ๊ด€๊ณ„๋Š” ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค (๋˜๋Š” null)
223
+ const { data: books } = await client
224
+ .from('books')
225
+ .select('*, authors(*)');
226
+
216
227
  // ์ •ํ™•ํ•œ ์นด์šดํŠธ์™€ ํ•จ๊ป˜ ์กฐํšŒ
217
228
  const { data, count, error } = await client
218
229
  .from('users')
@@ -270,6 +281,7 @@ const { data, error } = await client
270
281
  - `select(columns?: string, options?: { count?: 'exact' | 'planned' | 'estimated', head?: boolean })`: ์กฐํšŒํ•  ์ปฌ๋Ÿผ ์ง€์ •
271
282
  - `options.count`: `'exact'`๋กœ ์„ค์ •ํ•˜๋ฉด `limit`์˜ ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š” ์ „์ฒด ๊ฒฐ๊ณผ์˜ ๊ฐœ์ˆ˜๋ฅผ `count` ์†์„ฑ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
272
283
  - `options.head`: `true`๋กœ ์„ค์ •ํ•˜๋ฉด ๋ฐ์ดํ„ฐ ์—†์ด `count`๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. `count` ์˜ต์…˜๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด ํšจ์œจ์ ์œผ๋กœ ์ „์ฒด ๊ฐœ์ˆ˜๋งŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
284
+ - PostgREST-style embed: `select('*, related_table(*)')` ๋˜๋Š” `select('col, related_table(col1, col2)')`
273
285
  - `insert(data: T['Tables'][K]['Insert'] | T['Tables'][K]['Insert'][])`: ๋‹จ์ผ ๋˜๋Š” ๋‹ค์ค‘ ๋ ˆ์ฝ”๋“œ ์‚ฝ์ž…
274
286
  - `update(data: T['Tables'][K]['Update'])`: ๋ ˆ์ฝ”๋“œ ์—…๋ฐ์ดํŠธ
275
287
  - `delete()`: ๋ ˆ์ฝ”๋“œ ์‚ญ์ œ
@@ -67,6 +67,7 @@ export declare class SupaLitePG<T extends {
67
67
  getForeignKey(schema: string, table: string, foreignTable: string): Promise<{
68
68
  column: string;
69
69
  foreignColumn: string;
70
+ isArray: boolean;
70
71
  } | null>;
71
72
  rpc(procedureName: string, params?: Record<string, any>): RpcBuilder;
72
73
  testConnection(): Promise<boolean>;
@@ -329,11 +329,26 @@ class SupaLitePG {
329
329
  `;
330
330
  const activeClient = this.isTransaction && this.client ? this.client : await this.pool.connect();
331
331
  try {
332
+ // 1) One-to-many: `foreignTable` has a foreign key referencing `table`
333
+ // e.g. authors <- books.author_id, so embedding books(*) on authors returns an array
332
334
  const result = await activeClient.query(query, [schema, foreignTable, table]);
333
335
  if (result.rows.length > 0) {
334
336
  const relationship = {
335
337
  column: result.rows[0].foreign_column_name,
336
338
  foreignColumn: result.rows[0].column_name,
339
+ isArray: true,
340
+ };
341
+ this.foreignKeyCache.set(cacheKey, relationship);
342
+ return relationship;
343
+ }
344
+ // 2) Many-to-one: `table` has a foreign key referencing `foreignTable`
345
+ // e.g. books.author_id -> authors.id, so embedding authors(*) on books returns an object
346
+ const reverseResult = await activeClient.query(query, [schema, table, foreignTable]);
347
+ if (reverseResult.rows.length > 0) {
348
+ const relationship = {
349
+ column: reverseResult.rows[0].column_name,
350
+ foreignColumn: reverseResult.rows[0].foreign_column_name,
351
+ isArray: false,
337
352
  };
338
353
  this.foreignKeyCache.set(cacheKey, relationship);
339
354
  return relationship;
@@ -280,12 +280,23 @@ class QueryBuilder {
280
280
  return null;
281
281
  }
282
282
  const foreignSchemaTable = `"${String(this.schema)}"."${join.foreignTable}"`;
283
+ if (fk.isArray) {
284
+ return `(
285
+ SELECT COALESCE(json_agg(j), '[]'::json)
286
+ FROM (
287
+ SELECT ${join.columns}
288
+ FROM ${foreignSchemaTable}
289
+ WHERE "${fk.foreignColumn}" = ${schemaTable}."${fk.column}"
290
+ ) as j
291
+ ) as "${join.foreignTable}"`;
292
+ }
283
293
  return `(
284
- SELECT json_agg(j)
294
+ SELECT row_to_json(j)
285
295
  FROM (
286
296
  SELECT ${join.columns}
287
297
  FROM ${foreignSchemaTable}
288
298
  WHERE "${fk.foreignColumn}" = ${schemaTable}."${fk.column}"
299
+ LIMIT 1
289
300
  ) as j
290
301
  ) as "${join.foreignTable}"`;
291
302
  }));
@@ -0,0 +1,30 @@
1
+ # PostgREST-style Embed: Many-to-One Support
2
+
3
+ - **Date**: 2025-12-17
4
+ - **Author**: Codex
5
+ - **Status**: Completed
6
+
7
+ ## Summary
8
+
9
+ Fixed PostgREST-style embed syntax in `select()` (e.g. `related_table(*)`) so it works for both relationship directions:
10
+
11
+ - **1:N** (foreign table references the base table) returns an **array** (defaults to `[]`).
12
+ - **N:1** (base table references the foreign table) returns a **single object** (or `null`).
13
+
14
+ ## Changes
15
+
16
+ 1. **Bidirectional FK resolution**
17
+ - `SupaLitePG.getForeignKey()` now checks both directions between `table` and `foreignTable` and returns whether the embed should be an array or object.
18
+
19
+ 2. **Correct JSON shape in SQL generation**
20
+ - `QueryBuilder` uses `json_agg` (with `COALESCE(..., '[]'::json)`) for 1:N embeds.
21
+ - `QueryBuilder` uses `row_to_json` (with `LIMIT 1`) for N:1 embeds.
22
+
23
+ 3. **Unit tests**
24
+ - Added tests to cover N:1 embed behavior and nested column selection.
25
+
26
+ ## Impact
27
+
28
+ - Queries like `from('menu_item_opts').select('*, menu_item_opts_schema(*)')` now embed `menu_item_opts_schema` without warnings, matching PostgREST expectations.
29
+ - Existing 1:N embed behavior remains compatible, with an improved empty-result shape (`[]` instead of `null`).
30
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "supalite",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "description": "A lightweight TypeScript PostgreSQL client with Supabase-style API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",