From ad10e241040c2d92fafb61e45d6c1aa2d3e9be6d Mon Sep 17 00:00:00 2001 From: Damian Kacperski <7dami77@gmail.com> Date: Thu, 22 Jan 2026 10:43:57 +0100 Subject: [PATCH 1/5] JsonModel: fix options.cols mapping in parseRow --- src/JsonModel/JsonModel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JsonModel/JsonModel.js b/src/JsonModel/JsonModel.js index 730070c..8659b8a 100644 --- a/src/JsonModel/JsonModel.js +++ b/src/JsonModel/JsonModel.js @@ -157,7 +157,7 @@ class JsonModelImpl { /** @type {JMColumnDef[]} */ const mapCols = options && options.cols - ? options.cols.map(n => this.columns[n]) + ? options.cols.map(n => this.columns[n] ?? {name: n, alias: n, path: n}) : this.getCols const out = this.Item ? new this.Item() : {} for (const k of mapCols) { From de3fe372ac46dc49f1fb136da524c2728b0e9358 Mon Sep 17 00:00:00 2001 From: Damian Kacperski <7dami77@gmail.com> Date: Thu, 22 Jan 2026 10:55:15 +0100 Subject: [PATCH 2/5] JsonModel: fix sorting in makeSelect --- src/JsonModel/JsonModel.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/JsonModel/JsonModel.js b/src/JsonModel/JsonModel.js index 8659b8a..9fa8d7e 100644 --- a/src/JsonModel/JsonModel.js +++ b/src/JsonModel/JsonModel.js @@ -395,9 +395,8 @@ class JsonModelImpl { cursorArgs = [cursorVals[len]] // ID added at first for (let i = len - 1; i >= 0; i--) { const colAlias = cursorColAliases[i] - const column = Object.values(this.columns).find( - c => c.alias === colAlias - ) + const column = + Object.values(this.columns).find(c => c.alias === colAlias) ?? {} const val = cursorVals[i] // Handle columns that can contain NULL values with COALESCE From 013e565f73230b37b739db8903a5f49d309b89e3 Mon Sep 17 00:00:00 2001 From: Damian Kacperski <7dami77@gmail.com> Date: Thu, 22 Jan 2026 10:45:50 +0100 Subject: [PATCH 3/5] add prepack script --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 364b1b4..9106e46 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "scripts": { "start": "nps", "test": "npx nps test", - "prepublishOnly": "nps test.full build" + "prepublishOnly": "nps test.full", + "prepack": "nps build" }, "keywords": [ "sqlite", From 314274d15768ca32582515d76fcc0c42c1ea5bb1 Mon Sep 17 00:00:00 2001 From: Damian Kacperski <7dami77@gmail.com> Date: Thu, 22 Jan 2026 13:35:42 +0100 Subject: [PATCH 4/5] types: addModel fix --- src/DB/DB.js | 19 ++++++++++++------- types.d.ts | 5 ++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/DB/DB.js b/src/DB/DB.js index 1b6d4d5..6dfad1f 100644 --- a/src/DB/DB.js +++ b/src/DB/DB.js @@ -70,15 +70,20 @@ class DBImpl extends SQLiteImpl { * Add a model to the DB, which will manage one or more tables in the SQLite * database. The model should use the given `db` instance at creation time. * - * @param {Object} Model - A class. - * @param {Object} [options] - Options passed during Model creation. - * @returns {Object} - The created Model instance. + * @template [Options=Record] Default is `Record` + * @template {DBModel} [T=DBModel] + * Default is `DBModel` + * @param {T} Model - A class. + * @param {Options} [options] - Options passed during Model creation. + * @returns {InstanceType} - The created Model instance. */ addModel(Model, options) { - const model = new Model({ - ...options, - db: this, - }) + const model = new Model( + /** @type {Options & {db: DB}} */ ({ + ...options, + db: this, + }) + ) if (this.store[model.name]) throw new TypeError(`Model name ${model.name} was already added`) this.store[model.name] = model diff --git a/types.d.ts b/types.d.ts index 74cd0cb..b4c40c8 100644 --- a/types.d.ts +++ b/types.d.ts @@ -236,7 +236,10 @@ declare class DB extends SQLite { * db}`. * @returns - The created Model instance. */ - addModel(Model: DBModel, options?: Record): InstanceType + addModel< + Options = Record, + T extends DBModel = DBModel, + >(Model: T, options?: Options): InstanceType /** * Register an object with migrations. Migrations are marked completed by the * given name + their name in the `{sdb} migrations` table. From d5b60cc587fe974e6b90195438e444538b376c41 Mon Sep 17 00:00:00 2001 From: Damian Kacperski <7dami77@gmail.com> Date: Thu, 22 Jan 2026 15:04:58 +0100 Subject: [PATCH 5/5] JsonModel: add makeSelect tests for join option --- src/JsonModel/JM-makeSelect.test.js | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/JsonModel/JM-makeSelect.test.js b/src/JsonModel/JM-makeSelect.test.js index 02eac1c..f7bfd4a 100644 --- a/src/JsonModel/JM-makeSelect.test.js +++ b/src/JsonModel/JM-makeSelect.test.js @@ -311,3 +311,40 @@ test('distinct', async () => { false, ]) }) + +test('makeSelect join', async () => { + const m = getModel({}) + const [q] = m.makeSelect({join: 'joinStatement'}) + expect(q).toEqual( + 'SELECT tbl."id" AS _i,tbl."json" AS _j FROM "testing" tbl joinStatement' + ) +}) + +test('makeSelect select col not defined in model', async () => { + const m = getModel({}) + const [q] = m.makeSelect({cols: ['columnName']}) + expect(q).toEqual('SELECT columnName FROM "testing" tbl') +}) + +test('makeSelect sort by col not defined in model', async () => { + const m = getModel() + const [q] = m.makeSelect({sort: {columnName: 1}}) + expect(q).toEqual( + 'SELECT tbl."id" AS _i,tbl."json" AS _j FROM "testing" tbl ORDER BY columnName' + ) +}) + +test('makeSelect sort by col not defined in model with cursor', async () => { + const m = getModel({}) + const [q, , k] = m.makeSelect({ + sort: {columnName: 1}, + cursor: '!0~7', + }) + // eslint-disable-next-line no-console + console.log(k) + expect(q).toEqual( + 'SELECT tbl."id" AS _i,tbl."json" AS _j,columnName FROM "testing" tbl ' + + 'WHERE((COALESCE(columnName, "")>=COALESCE(?, "") AND ' + + '(COALESCE(columnName, "")!=COALESCE(?, "") OR _i>?))) ORDER BY columnName,_i' + ) +})