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
23 changes: 18 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import getFieldAttributes from './utils/field_attributes.ts';

import extend from './utils/extend.ts';

import makeMethodsEnumerable from './utils/make_methods_enumerable.ts';

import clone from 'tricks/object/clone.js';

import format_request from './format_request.ts';
Expand Down Expand Up @@ -439,8 +441,13 @@ export type GetRequestOptions = Omit<
*/
export type PatchRequestOptions = Omit<
RequestOptions,
'fields' | 'groupby' | 'query'
>;
'fields' | 'groupby' | 'query' | 'body'
> & {
/**
* Body containing new data
*/
body?: Record<string, any>;
};

/**
* Dare.post Request Options
Expand Down Expand Up @@ -1033,9 +1040,9 @@ export default class Dare {
* @returns Affected Rows statement
*/
async patch(
table: string | PatchRequestOptions,
filter?: Record<string, any>,
body?: Record<string, any>,
table: PatchRequestOptions['table'] | PatchRequestOptions,
filter?: PatchRequestOptions['filter'],
body?: PatchRequestOptions['body'],
options: Omit<PatchRequestOptions, 'table' | 'body' | 'filter'> = {}
): Promise<any> {
const opts: QueryOptions =
Expand Down Expand Up @@ -1440,6 +1447,12 @@ export default class Dare {
}
}

/*
* ES class methods are non-enumerable by default,
* restore the enumerable behaviour of the former prototype assignments
*/
makeMethodsEnumerable(Dare);

/**
* Engine, database engine
*/
Expand Down
4 changes: 4 additions & 0 deletions src/mysql57.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Dare, {type QueryOptions} from './index.ts';
import makeMethodsEnumerable from './utils/make_methods_enumerable.ts';
import semverCompare from 'semver-compare';

/**
Expand Down Expand Up @@ -82,6 +83,9 @@ class MySQL57Dare extends Dare {
}
}

// Restore the enumerable behaviour of the former prototype assignments
makeMethodsEnumerable(MySQL57Dare);

/**
* Default engine for MySQL 5.7
*/
Expand Down
4 changes: 4 additions & 0 deletions src/postgres16.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SQL, {Sql, join} from 'sql-template-tag';
import Dare, {type QueryOptions} from './index.ts';
import makeMethodsEnumerable from './utils/make_methods_enumerable.ts';

/**
* PostgresDare
Expand Down Expand Up @@ -104,6 +105,9 @@ class PostgresDare extends Dare {
}
}

// Restore the enumerable behaviour of the former prototype assignments
makeMethodsEnumerable(PostgresDare);

/**
* Default engine for Postgres
*/
Expand Down
4 changes: 4 additions & 0 deletions src/sqlite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import SQL, {Sql, raw} from 'sql-template-tag';
import Dare, {type QueryOptions} from './index.ts';
import makeMethodsEnumerable from './utils/make_methods_enumerable.ts';

/**
* SQLiteDare
Expand Down Expand Up @@ -126,6 +127,9 @@ class SQLiteDare extends Dare {
}
}

// Restore the enumerable behaviour of the former prototype assignments
makeMethodsEnumerable(SQLiteDare);

/**
* Default engine for SQLite
*/
Expand Down
28 changes: 28 additions & 0 deletions src/utils/make_methods_enumerable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Make the prototype methods of a class enumerable
* ES class methods are non-enumerable by default, whereas the previous
* `Class.prototype.method = function` assignments were enumerable.
* This restores the original enumerable behaviour.
* @param Class - Class whose prototype methods should be made enumerable
*/
export default function makeMethodsEnumerable(Class: {
prototype: object;
}): void {
for (const name of Object.getOwnPropertyNames(Class.prototype)) {
if (name === 'constructor') {
continue;
}

const descriptor = Object.getOwnPropertyDescriptor(
Class.prototype,
name
);

if (descriptor?.enumerable === false) {
Object.defineProperty(Class.prototype, name, {
...descriptor,
enumerable: true,
});
}
}
}
72 changes: 72 additions & 0 deletions test/specs/enumerability.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assert from 'node:assert';
import {describe, it} from 'node:test';
import Dare from '../../src/index.ts';
import MySQL57Dare from '../../src/mysql57.ts';
import PostgresDare from '../../src/postgres16.ts';
import SQLiteDare from '../../src/sqlite.ts';

/*
* ES class methods are non-enumerable by default
* Dare restores the enumerable behaviour of the former prototype assignments
* via makeMethodsEnumerable, so instance methods remain visible to `for...in`
*/
describe('enumerability', () => {
const engines = {
Dare,
MySQL57Dare,
PostgresDare,
SQLiteDare,
};

for (const [name, Klass] of Object.entries(engines)) {
describe(name, () => {
it('should define enumerable prototype methods', () => {
const methods = Object.getOwnPropertyNames(
Klass.prototype
).filter(prop => prop !== 'constructor');

assert.ok(methods.length, 'has prototype methods');

for (const method of methods) {
const descriptor = Object.getOwnPropertyDescriptor(
Klass.prototype,
method
);
assert.strictEqual(
descriptor?.enumerable,
true,
`${name}.prototype.${method} should be enumerable`
);
}
});

it('should expose methods on instances via for...in', () => {
const instance = new Klass();

const enumerated = [];
for (const prop in instance) {
if (typeof instance[prop] === 'function') {
enumerated.push(prop);
}
}

assert.ok(
enumerated.includes('get'),
'get is enumerable on the instance'
);
assert.ok(
enumerated.includes('post'),
'post is enumerable on the instance'
);
});

it('should keep the constructor non-enumerable', () => {
const descriptor = Object.getOwnPropertyDescriptor(
Klass.prototype,
'constructor'
);
assert.strictEqual(descriptor?.enumerable, false);
});
});
}
});