Skip to content

Commit

Permalink
d1 all()
Browse files Browse the repository at this point in the history
  • Loading branch information
chientrm committed Jul 1, 2023
1 parent afc9197 commit b54da61
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Enable Cloudflare Workers runtime for local development.
| --------- | ------ |
| `first()` ||
| `run()` ||
| `all()` | |
| `all()` | |
| `raw()` ||
| `bind()` ||

Expand Down
1 change: 1 addition & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface ErrorResult extends Result {
enum ProxyType {
D1DatabasePreparedStatementRun,
D1DatabasePreparedStatementFirst,
D1DatabasePreparedStatementAll,
D1DatabaseExec,
}

Expand Down
3 changes: 3 additions & 0 deletions src/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Data, ProxyType } from './data';
import { D1DatabaseExecProxy } from './proxies/d1_database/exec/proxy';
import { D1DatabasePreparedStatementAllProxy } from './proxies/d1_database/prepared_statement/all/proxy';
import { D1DatabasePreparedStatementFirstProxy } from './proxies/d1_database/prepared_statement/first/proxy';
import { D1DatabasePreparedStatementRunProxy } from './proxies/d1_database/prepared_statement/run/proxy';

Expand All @@ -10,6 +11,8 @@ class ProxyFactory {
return D1DatabasePreparedStatementRunProxy.create(data);
case ProxyType.D1DatabasePreparedStatementFirst:
return D1DatabasePreparedStatementFirstProxy.create(data);
case ProxyType.D1DatabasePreparedStatementAll:
return D1DatabasePreparedStatementAllProxy.create(data);
case ProxyType.D1DatabaseExec:
return D1DatabaseExecProxy.create(data);
default:
Expand Down
40 changes: 40 additions & 0 deletions src/proxies/d1_database/prepared_statement/all/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Data, ProxyType } from '../../../../data';
import { Proxy } from '../../../proxy';

interface ProxyData extends Data {
query: string;
values?: any[];
colName?: string;
}

class D1DatabasePreparedStatementAllProxy extends Proxy<ProxyData> {
query: string;
values?: any[];
constructor(data: ProxyData) {
super(data);
const { query, values } = data;
this.query = query;
this.values = values;
}
getData(): ProxyData {
const { host, name, query, values } = this;
return {
proxyType: ProxyType.D1DatabasePreparedStatementFirst,
host,
name,
query,
values,
};
}
async execute(env: any): Promise<any> {
const d1 = env[this.name] as D1Database,
statement = d1.prepare(this.query).bind(...(this.values ?? [])),
result = await statement.all();
return result;
}
static create(data: Data) {
return new D1DatabasePreparedStatementAllProxy(data as ProxyData);
}
}

export { D1DatabasePreparedStatementAllProxy };

0 comments on commit b54da61

Please sign in to comment.