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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"uuid": ">=8 <10"
},
"devDependencies": {
"@types/lodash": "^4.17.23",
"@types/node": "^22.10.7",
"@typescript-eslint/eslint-plugin": "^7.16.0",
"@typescript-eslint/parser": "^7.16.0",
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/DB/DB.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {sortBy} from 'lodash'
import debug from 'debug'
import SQLite, {sql} from './SQLite'
import SQLiteImpl, {sql} from './SQLite'
import {DEV, deprecated} from '../lib/warning'

const dbg = debug('strato-db/DB')
Expand Down Expand Up @@ -45,7 +45,7 @@ const _markMigration = async (db, runKey, up) => {
*
* @implements {DB}
*/
class DBImpl extends SQLite {
class DBImpl extends SQLiteImpl {
/** @param {DBOptions} options */
constructor({migrations = [], onBeforeMigrations, ...options} = {}) {
const onDidOpen = options.readOnly
Expand All @@ -71,7 +71,7 @@ class DBImpl extends 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.
* @param {Object} [options] - Options passed during Model creation.
* @returns {Object} - The created Model instance.
*/
addModel(Model, options) {
Expand All @@ -92,7 +92,7 @@ class DBImpl extends SQLite {
*
* - The name under which to register these migrations.
*
* @param {Record<string, function | Record<string, function>>} migrations
* @param {DBMigrations} migrations
*
* - The migrations object.
*
Expand Down
3 changes: 3 additions & 0 deletions src/DB/SQLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
vacuumInterval,
vacuumPageCount,
}
/** @type {Promise} */
this.dbP = new Promise(resolve => {
/** @type {((v: any) => void) | null} */
this._resolveDbP = resolve
})
this._sema = new Sema(1)
Expand Down Expand Up @@ -172,7 +174,7 @@
file,
readOnly,
name: this.name,
// @ts-ignore

Check warning on line 177 in src/DB/SQLite.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
_sqlite,
_store: store,
})
Expand Down Expand Up @@ -265,6 +267,7 @@
open() {
const {_resolveDbP} = this
if (_resolveDbP) {
/** @type {Promise<this> | null} */
this._openingDbP = this._openDB().finally(() => {
this._openingDbP = null
})
Expand Down Expand Up @@ -350,7 +353,7 @@

// Template strings
if (!isStmt && Array.isArray(args[0])) {
// @ts-ignore

Check warning on line 356 in src/DB/SQLite.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
args = sql(...args)
if (!args[1].length) args.pop()
}
Expand Down Expand Up @@ -389,7 +392,7 @@
return
}
const error = new Error(`${name}: sqlite3: ${err.message}`)
// @ts-ignore

Check warning on line 395 in src/DB/SQLite.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
error.code = err.code
reject(error)
} else
Expand Down
23 changes: 13 additions & 10 deletions src/DB/Statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class StatementImpl {
this.name = `${db.name}${this._name}`
}

/** @type {true} */
get isStatement() {
return true
}
Expand Down Expand Up @@ -65,17 +66,19 @@ class StatementImpl {
if (!_stmt) return Promise.resolve()
return this._wrap(
() =>
new Promise((resolve, reject) => {
delete this._stmt
_stmt.finalize(err => {
if (err) {
if (!this._stmt) this._stmt = _stmt
return reject(err)
}
dbg(`${this.name} finalized`)
resolve()
/** @type {Promise<void>} */ (
new Promise((resolve, reject) => {
delete this._stmt
_stmt.finalize(err => {
if (err) {
if (!this._stmt) this._stmt = _stmt
return reject(err)
}
dbg(`${this.name} finalized`)
resolve()
})
})
})
)
)
}

Expand Down
2 changes: 2 additions & 0 deletions src/EventSourcingDB/EventSourcingDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@
* works in React.
*
* @augments EventEmitter
* @implements EventSourcingDB
*/
// eslint-disable-next-line unicorn/prefer-event-target
class EventSourcingDB extends EventEmitter {
MAX_RETRY = 38 // this is an hour

/** @param {ESDBOptions} options */
constructor({
queue,
models,
Expand Down Expand Up @@ -272,7 +274,7 @@
const reducers = {}
const migrationOptions = {queue: this.queue}

const dispatch = async (type, data, ts) => {

Check warning on line 277 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Move async arrow function 'dispatch' to the outer scope
if (this._processing) {
const store = this._alsDispatch.getStore()
dbg({store})
Expand Down Expand Up @@ -412,9 +414,9 @@
if (wantVersion > this._minVersion) this._minVersion = wantVersion
} else if (!this._isPolling) {
this._isPolling = true
// @ts-ignore

Check warning on line 417 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (module.hot) {
// @ts-ignore

Check warning on line 419 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
module.hot.dispose(() => {
this.stopPolling()
})
Expand Down Expand Up @@ -461,7 +463,7 @@
* @param {number} [ts] The timestamp of the event.
* @returns {Promise<Event>} The processed event.
*/
dispatch = makeDispatcher('dispatch', async (type, data, ts) => {

Check warning on line 466 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Move async arrow function to the outer scope
const event = await this.queue.add(type, data, ts)
return this.handledVersion(event.v)
})
Expand Down Expand Up @@ -543,7 +545,7 @@
}
}
if (o && process.env.NODE_ENV === 'test') {
// @ts-ignore

Check warning on line 548 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (!this.__BE_QUIET)
// eslint-disable-next-line no-console
console.error(
Expand Down Expand Up @@ -633,7 +635,7 @@
return _resultQueue.set(result)
})
.catch(error => {
// @ts-ignore

Check warning on line 638 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (!this.__BE_QUIET)
// eslint-disable-next-line no-console
console.error(
Expand All @@ -652,7 +654,7 @@

if (resultEvent.error) {
errorCount++
// @ts-ignore

Check warning on line 657 in src/EventSourcingDB/EventSourcingDB.js

View workflow job for this annotation

GitHub Actions / lint (20.x)

Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free
if (!this.__BE_QUIET) {
let path, error
// find the deepest error
Expand Down
2 changes: 2 additions & 0 deletions src/JsonModel/makeMigrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ export const makeMigrations = ({
)
}
// Wrap the migration functions to provide their arguments
/** @type {DBMigrations} */
const wrappedMigrations = {}
/** @type {(fn: Function) => DBMigration} */
const wrap = fn =>
fn &&
(writeableDb => {
Expand Down
28 changes: 17 additions & 11 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ type SQLiteOptions = {
* - 'finally': transaction finished
* - 'call': call to SQLite completed, includes data and duration
*/
interface SQLite extends EventEmitter {
new (options?: SQLiteOptions)
// eslint-disable-next-line unicorn/prefer-event-target
declare class SQLite extends EventEmitter {
constructor(options?: SQLiteOptions)

/** Holding space for models */
store: object
Expand All @@ -86,7 +87,7 @@ interface SQLite extends EventEmitter {
* is converted to `db.all('select * from "foo" where t = ? and json = ?', [bar,
* JSON.stringify(obj)])`
*/
sql(): {quoteId: (id: SQLiteParam) => string} & SqlTag
sql: {quoteId: (id: SQLiteParam) => string} & SqlTag
/** `true` if an sqlite connection was set up. Mostly useful for tests. */
isOpen: boolean
/**
Expand Down Expand Up @@ -195,8 +196,8 @@ interface SQLite extends EventEmitter {
*/
withTransaction(fn: () => Promise<void>): Promise<void>
}

type DBMigration = {up: DBCallback} | DBCallback
type DBUpMigration = {up: DBCallback}
type DBMigration = DBUpMigration | DBCallback
/** Migrations are marked completed by their name in the `{sdb} migrations` table */
type DBMigrations = Record<string, DBMigration>
interface DBModel<Options extends {db: DB} = {db: DB}> {
Expand All @@ -205,7 +206,7 @@ interface DBModel<Options extends {db: DB} = {db: DB}> {
type DBOptions = {
/** Open the DB read-only */
readOnly?: boolean
migrations?: DBMigrations
migrations?: (DBUpMigration & {runKey: string})[]
/** Called before migrations run. Not called for read-only */
onBeforeMigrations?: (...params: any[]) => any
/**
Expand Down Expand Up @@ -396,7 +397,7 @@ type JMColums = JMColumns
type JMOptions<
T extends {[x: string]: any},
IDCol extends string = 'id',
Columns extends JMColums<IDCol> = {[id in IDCol]?: {type: 'TEXT'}},
Columns extends JMColumns<T, IDCol> = {[id in IDCol]?: {type: 'TEXT'}},
> = {
/** A DB instance, normally passed by DB */
db: DB
Expand Down Expand Up @@ -474,7 +475,7 @@ declare class JsonModel<
? RealItem
: RealItem & {[id in IDCol]: IDValue},
Config = ConfigOrID extends string ? object : ConfigOrID,
Columns extends JMColums<IDCol> = Config extends {columns: object}
Columns extends JMColumns<Item, IDCol> = Config extends {columns: object}
? Config['columns']
: // If we didn't get a config, assume all keys are columns
Item,
Expand Down Expand Up @@ -763,7 +764,7 @@ interface EventQueue<
Item extends {[x: string]: any} = RealItem extends {[id in IDCol]?: unknown}
? RealItem
: RealItem & {[id in IDCol]: IDValue},
Columns extends JMColums<IDCol> = Config extends {columns: object}
Columns extends JMColumns<Item, IDCol> = Config extends {columns: object}
? Config['columns']
: // If we didn't get a config, assume all keys are columns
{[colName in keyof Item]: object},
Expand Down Expand Up @@ -902,8 +903,13 @@ interface ESDBModel {
}

type ESDBOptions = DBOptions & {
/**
* @deprecated 'db' is no longer an option, pass the db options instead, e.g.
* file, verbose, readOnly
*/
db?: never
models: {[name: string]: ESDBModel}
queue?: InstanceType<EventQueue>
queue?: EventQueue
queueFile?: string
withViews?: boolean
onWillOpen?: DBCallback
Expand Down Expand Up @@ -986,7 +992,7 @@ interface ESModel<
? RealItem
: RealItem & {[id in IDCol]: IDValue},
Config = ConfigOrID extends string ? object : ConfigOrID,
Columns extends JMColums<IDCol> = Config extends {columns: object}
Columns extends JMColumns<Item, IDCol> = Config extends {columns: object}
? Config['columns']
: // If we didn't get a config, assume all keys are columns
Item,
Expand Down
Loading