Skip to content

Commit 1968914

Browse files
authored
Merge pull request #99 from tursodatabase/pragma
Implement Database.pragma()
2 parents 5d4dd1b + 73193c5 commit 1968914

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ class Database {
136136
}
137137

138138
pragma(source, options) {
139-
throw new Error("not implemented");
139+
if (options == null) options = {};
140+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
141+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
142+
const simple = options['simple'];
143+
const stmt = this.prepare(`PRAGMA ${source}`, this, true);
144+
return simple ? stmt.pluck().get() : stmt.all();
140145
}
141146

142147
backup(filename, options) {

integration-tests/tests/async.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ test.serial("Database.transaction()", async (t) => {
236236
t.is(stmt.get(5).name, "Junior");
237237
});
238238

239+
test.serial("Database.pragma()", async (t) => {
240+
const db = t.context.db;
241+
await db.pragma("cache_size = 2000");
242+
t.deepEqual(await db.pragma("cache_size"), [{ "cache_size": 2000 }]);
243+
});
244+
239245
test.serial("errors", async (t) => {
240246
const db = t.context.db;
241247

integration-tests/tests/sync.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ test.serial("values", async (t) => {
251251
t.deepEqual(stmt.get(9007199254740991n), [9007199254740991]);
252252
});
253253

254+
test.serial("Database.pragma()", async (t) => {
255+
const db = t.context.db;
256+
db.pragma("cache_size = 2000");
257+
t.deepEqual(db.pragma("cache_size"), [{ "cache_size": 2000 }]);
258+
});
259+
254260
test.serial("errors", async (t) => {
255261
const db = t.context.db;
256262

promise.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ class Database {
139139
}
140140

141141
pragma(source, options) {
142-
throw new Error("not implemented");
142+
if (options == null) options = {};
143+
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
144+
if (typeof options !== 'object') throw new TypeError('Expected second argument to be an options object');
145+
const simple = options['simple'];
146+
return this.prepare(`PRAGMA ${source}`, this, true).then(async (stmt) => {
147+
return simple ? await stmt.pluck().get() : await stmt.all();
148+
});
143149
}
144150

145151
backup(filename, options) {

0 commit comments

Comments
 (0)