Skip to content

Commit

Permalink
fix(sql): avoid getting bigint as string in stats (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest authored Apr 21, 2024
1 parent 76316a2 commit 38e2134
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class MemoryDriver extends Driver<MemoryDriver.Config> {
}

async stats() {
return {}
return { tables: valueMap(this._store, (rows, name) => ({ name, count: rows.length, size: 0 })), size: 0 }
}

async get(sel: Selection.Immutable) {
Expand Down
4 changes: 2 additions & 2 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ INSERT INTO mtt VALUES(json_extract(j, concat('$[', i, ']'))); SET i=i+1; END WH
const data = await this._select('information_schema.tables', ['TABLE_NAME', 'TABLE_ROWS', 'DATA_LENGTH'], 'TABLE_SCHEMA = ?', [this.config.database])
const stats: Partial<Driver.Stats> = { size: 0 }
stats.tables = Object.fromEntries(data.map(({ TABLE_NAME: name, TABLE_ROWS: count, DATA_LENGTH: size }) => {
stats.size += size
return [name, { count, size }]
stats.size! += +size
return [name, { count: +count, size: +size }]
}))
return stats
}
Expand Down
13 changes: 7 additions & 6 deletions packages/sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,21 @@ export class SQLiteDriver extends Driver<SQLiteDriver.Config> {
}
}

_all(sql: string, params: any = []) {
_all(sql: string, params: any = [], config?: { useBigInt: boolean }) {
return this._exec(sql, params, (stmt) => {
stmt.bind(params)
const result: any[] = []
while (stmt.step()) {
// @ts-ignore
result.push(stmt.getAsObject(null, { useBigInt: true }))
result.push(stmt.getAsObject(null, config))
}
return result
})
}

_get(sql: string, params: any = []) {
return this._exec(sql, params, stmt => stmt.getAsObject(params))
_get(sql: string, params: any = [], config?: { useBigInt: boolean }) {
// @ts-ignore
return this._exec(sql, params, stmt => stmt.getAsObject(params, config))
}

_export() {
Expand Down Expand Up @@ -312,15 +313,15 @@ export class SQLiteDriver extends Driver<SQLiteDriver.Config> {
const builder = new SQLiteBuilder(this, tables)
const sql = builder.get(sel)
if (!sql) return []
const rows: any[] = this._all(sql)
const rows: any[] = this._all(sql, [], { useBigInt: true })
return rows.map(row => builder.load(row, model))
}

async eval(sel: Selection.Immutable, expr: Eval.Expr) {
const builder = new SQLiteBuilder(this, sel.tables)
const inner = builder.get(sel.table as Selection, true, true)
const output = builder.parseEval(expr, false)
const { value } = this._get(`SELECT ${output} AS value FROM ${inner}`)
const { value } = this._get(`SELECT ${output} AS value FROM ${inner}`, [], { useBigInt: true })
return builder.load(value, expr)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/tests/src/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function isSubsetOf(subset, superset, cmp, contains, ordered) {
});
}

export = (({ Assertion }) => {
export default (({ Assertion }) => {
function checkShape(expect, actual, path, ordered) {
if (actual === expect || Number.isNaN(expect) && Number.isNaN(actual)) return

Expand Down
5 changes: 4 additions & 1 deletion packages/tests/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ namespace OrmOperations {

export const stats = function Stats(database: Database<Tables>) {
it('basic support', async () => {
await expect(database.stats()).to.eventually.ok
const stats = await database.stats()
expect(stats.size).to.be.a('number')
expect(stats.tables['temp2'].count).to.be.a('number')
expect(stats.tables['temp2'].size).to.be.a('number')
})
}

Expand Down

0 comments on commit 38e2134

Please sign in to comment.