Skip to content

Commit 27e7377

Browse files
authored
Add support for Statement.pluck() (#171)
2 parents 275d4bc + 3389f98 commit 27e7377

File tree

4 files changed

+66
-14
lines changed

4 files changed

+66
-14
lines changed

index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Database {
118118
prepare(sql) {
119119
try {
120120
const stmt = databasePrepareSync.call(this.db, sql);
121-
return new Statement(stmt);
121+
return new Statement(stmt);
122122
} catch (err) {
123123
throw convertError(err);
124124
}
@@ -282,6 +282,7 @@ class Database {
282282
class Statement {
283283
constructor(stmt) {
284284
this.stmt = stmt;
285+
this.pluckMode = false;
285286
}
286287

287288
/**
@@ -294,6 +295,16 @@ class Statement {
294295
return this;
295296
}
296297

298+
/**
299+
* Toggle pluck mode.
300+
*
301+
* @param pluckMode Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled.
302+
*/
303+
pluck(pluckMode) {
304+
this.pluckMode = pluckMode ?? true;
305+
return this;
306+
}
307+
297308
get reader() {
298309
return statementIsReader.call(this.stmt);
299310
}
@@ -307,7 +318,7 @@ class Statement {
307318
return statementRun.call(this.stmt, bindParameters[0]);
308319
} else {
309320
return statementRun.call(this.stmt, bindParameters.flat());
310-
}
321+
}
311322
} catch (err) {
312323
throw convertError(err);
313324
}
@@ -378,7 +389,11 @@ class Statement {
378389
try {
379390
const result = [];
380391
for (const row of this.iterate(...bindParameters)) {
381-
result.push(row);
392+
if (this.pluckMode) {
393+
result.push(row[Object.keys(row)[0]]);
394+
} else {
395+
result.push(row);
396+
}
382397
}
383398
return result;
384399
} catch (err) {

integration-tests/tests/async.test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ test.serial("Statement.all() [raw]", async (t) => {
149149
t.deepEqual(await stmt.raw().all(), expected);
150150
});
151151

152+
test.serial("Statement.all() [pluck]", async (t) => {
153+
const db = t.context.db;
154+
155+
const stmt = await db.prepare("SELECT * FROM users");
156+
const expected = [
157+
1,
158+
2,
159+
];
160+
t.deepEqual(await stmt.pluck().all(), expected);
161+
});
162+
152163
test.serial("Statement.all() [default safe integers]", async (t) => {
153164
const db = t.context.db;
154165
db.defaultSafeIntegers();
@@ -298,9 +309,9 @@ test.serial("errors", async (t) => {
298309
test.serial("Database.prepare() after close()", async (t) => {
299310
const db = t.context.db;
300311
await db.close();
301-
await t.throwsAsync(async () => {
312+
await t.throwsAsync(async () => {
302313
await db.prepare("SELECT 1");
303-
}, {
314+
}, {
304315
instanceOf: TypeError,
305316
message: "The database connection is not open"
306317
});
@@ -309,9 +320,9 @@ test.serial("Database.prepare() after close()", async (t) => {
309320
test.serial("Database.exec() after close()", async (t) => {
310321
const db = t.context.db;
311322
await db.close();
312-
await t.throwsAsync(async () => {
323+
await t.throwsAsync(async () => {
313324
await db.exec("SELECT 1");
314-
}, {
325+
}, {
315326
instanceOf: TypeError,
316327
message: "The database connection is not open"
317328
});
@@ -397,4 +408,4 @@ const connect = async (path_opt, options = {}) => {
397408
/// Generate a unique database filename
398409
const genDatabaseFilename = () => {
399410
return `test-${crypto.randomBytes(8).toString('hex')}.db`;
400-
};
411+
};

integration-tests/tests/sync.test.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@ test.serial("Statement.all() [raw]", async (t) => {
169169
t.deepEqual(stmt.raw().all(), expected);
170170
});
171171

172+
test.serial("Statement.all() [pluck]", async (t) => {
173+
const db = t.context.db;
174+
175+
const stmt = db.prepare("SELECT * FROM users");
176+
const expected = [
177+
1,
178+
2,
179+
];
180+
t.deepEqual(stmt.pluck().all(), expected);
181+
});
182+
172183
test.serial("Statement.all() [default safe integers]", async (t) => {
173184
const db = t.context.db;
174185
db.defaultSafeIntegers();
@@ -391,9 +402,9 @@ test.serial("errors", async (t) => {
391402
test.serial("Database.prepare() after close()", async (t) => {
392403
const db = t.context.db;
393404
db.close();
394-
t.throws(() => {
405+
t.throws(() => {
395406
db.prepare("SELECT 1");
396-
}, {
407+
}, {
397408
instanceOf: TypeError,
398409
message: "The database connection is not open"
399410
});
@@ -402,9 +413,9 @@ test.serial("Database.prepare() after close()", async (t) => {
402413
test.serial("Database.exec() after close()", async (t) => {
403414
const db = t.context.db;
404415
db.close();
405-
t.throws(() => {
416+
t.throws(() => {
406417
db.exec("SELECT 1");
407-
}, {
418+
}, {
408419
instanceOf: TypeError,
409420
message: "The database connection is not open"
410421
});
@@ -452,4 +463,4 @@ const connect = async (path_opt, options = {}) => {
452463
/// Generate a unique database filename
453464
const genDatabaseFilename = () => {
454465
return `test-${crypto.randomBytes(8).toString('hex')}.db`;
455-
};
466+
};

promise.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ class Database {
282282
class Statement {
283283
constructor(stmt) {
284284
this.stmt = stmt;
285+
this.pluckMode = false;
285286
}
286287

287288
/**
@@ -294,6 +295,16 @@ class Statement {
294295
return this;
295296
}
296297

298+
/**
299+
* Toggle pluck mode.
300+
*
301+
* @param pluckMode Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled.
302+
*/
303+
pluck(pluckMode) {
304+
this.pluckMode = pluckMode ?? true;
305+
return this;
306+
}
307+
297308
get reader() {
298309
return statementIsReader.call(this.stmt);
299310
}
@@ -380,7 +391,11 @@ class Statement {
380391
const result = [];
381392
const it = await this.iterate(...bindParameters);
382393
for (const row of it) {
383-
result.push(row);
394+
if (this.pluckMode) {
395+
result.push(row[Object.keys(row)[0]]);
396+
} else {
397+
result.push(row);
398+
}
384399
}
385400
return result;
386401
} catch (e) {

0 commit comments

Comments
 (0)