Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 12 additions & 7 deletions src/DB/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>] Default is `Record<string, any>`
* @template {DBModel<Options & {db: DB}>} [T=DBModel<Options & {db: DB}>]
* Default is `DBModel<Options & {db: DB}>`
* @param {T} Model - A class.
* @param {Options} [options] - Options passed during Model creation.
* @returns {InstanceType<T>} - 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
Expand Down
37 changes: 37 additions & 0 deletions src/JsonModel/JM-makeSelect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
})
7 changes: 3 additions & 4 deletions src/JsonModel/JsonModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
/** @type {JMColumnDef<Item, IDCol>[]} */
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) {
Expand Down Expand Up @@ -395,9 +395,8 @@
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
Expand Down Expand Up @@ -530,7 +529,7 @@
[`SELECT COUNT(*) as t from (`, selectQ, join, whereQ, ')']
.filter(Boolean)
.join(' ')
// TODO next major make this an object

Check warning on line 532 in src/JsonModel/JsonModel.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Unexpected 'todo' comment: 'TODO next major make this an object'
return [q, qVals, cursorColAliases, totalQ, vals, invert]
}

Expand Down
5 changes: 4 additions & 1 deletion types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@
* db}`.
* @returns - The created Model instance.
*/
addModel(Model: DBModel, options?: Record<string, any>): InstanceType<DBModel>
addModel<
Options = Record<string, any>,
T extends DBModel<Options & {db: DB}> = DBModel<Options & {db: DB}>,
>(Model: T, options?: Options): InstanceType<T>
/**
* Register an object with migrations. Migrations are marked completed by the
* given name + their name in the `{sdb} migrations` table.
Expand Down Expand Up @@ -482,7 +485,7 @@
SearchAttrs = JMSearchAttrs<Columns>,
SearchOptions = JMSearchOptions<Columns>,
> {
// TODO have it infer the columns from the call to super

Check warning on line 488 in types.d.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Unexpected 'todo' comment: 'TODO have it infer the columns from the...'
constructor(options: JMOptions<Item, IDCol, Columns>)
/** The DB instance storing this model */
db: SQLite
Expand Down Expand Up @@ -880,7 +883,7 @@
]
) => void

// TODO get from models config

Check warning on line 886 in types.d.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Unexpected 'todo' comment: 'TODO get from models config'
type ESDBModelArgs = {
name: string
db: DB
Expand Down Expand Up @@ -978,7 +981,7 @@
* true}]` Pass the type of the item it stores and the config so it can
* determine the columns
*/
// TODO fix Item vs Item type incompatibility

Check warning on line 984 in types.d.ts

View workflow job for this annotation

GitHub Actions / lint (20.x)

Unexpected 'todo' comment: 'TODO fix Item vs Item type...'
interface ESModel<
RealItem extends {[x: string]: any} = {id: string},
// Allow the id column name as well for backwards compatibility
Expand Down