Skip to content

Commit 81fd10d

Browse files
kriszypEthan-Arrowood
authored andcommitted
Fix handling of create with one/two argumetns with loadAsInstance=false
1 parent e3d29fd commit 81fd10d

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

resources/Resource.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { transaction } from './transaction.ts';
88
import { parseQuery } from './search.ts';
99
import { AsyncLocalStorage } from 'async_hooks';
1010
import { RequestTarget } from './RequestTarget.ts';
11+
import logger from '../utility/logging/logger.js';
12+
1113
export const contextStorage = new AsyncLocalStorage<Context>();
1214

1315
const EXTENSION_TYPES = {
@@ -142,16 +144,15 @@ export class Resource implements ResourceInterface {
142144
static create(idPrefix: Id, record: any, context: Context): Promise<Id>;
143145
static create(record: any, context: Context): Promise<Id>;
144146
static create(idPrefix: any, record: any, context?: Context): Promise<Id> {
145-
if (context) {
146-
if (context.getContext) context = context.getContext();
147-
} else {
148-
// try to get the context from the async context if possible
149-
context = contextStorage.getStore() ?? {};
150-
}
151-
152147
let id: Id;
153148
if (this.loadAsInstance === false) {
154-
id = idPrefix;
149+
if (typeof idPrefix === 'object' && idPrefix && !context) {
150+
// two argument form (record, context), shift the arguments
151+
context = record;
152+
record = idPrefix;
153+
id = new RequestTarget();
154+
id.isCollection = true;
155+
} else id = idPrefix;
155156
} else {
156157
if (idPrefix == null) id = record?.[this.primaryKey] ?? this.getNewId();
157158
else if (Array.isArray(idPrefix) && typeof idPrefix[0] !== 'object')
@@ -164,6 +165,12 @@ export class Resource implements ResourceInterface {
164165
record = idPrefix;
165166
}
166167
}
168+
if (context) {
169+
if (context.getContext) context = context.getContext();
170+
} else {
171+
// try to get the context from the async context if possible
172+
context = contextStorage.getStore() ?? {};
173+
}
167174
return transaction(context, async () => {
168175
context.transaction.startedFrom ??= {
169176
resourceName: this.name,
@@ -612,7 +619,12 @@ function transactional(action, options) {
612619
query = new RequestTarget();
613620
query.id = id;
614621
if (id == null) {
615-
if (!hasContent) throw new Error('Invalid id, is this used anywhere?');
622+
if (!hasContent) {
623+
logger.warn?.(
624+
`Using an argument with a value of ${id} for ${options.method}, is deprecated`,
625+
new Error('Invalid id')
626+
);
627+
}
616628
isCollection = true;
617629
}
618630
}

0 commit comments

Comments
 (0)