Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 2 additions & 23 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,31 +280,10 @@ void DBHostObject::create_jsi_functions(jsi::Runtime &rt) {
function_map["delete"] = HFN(this) {
invalidated = true;

std::string path = std::string(base_path);

if (count == 1) {
if (!args[1].isString()) {
throw std::runtime_error(
"[op-sqlite][open] database location must be a string");
}

std::string location = args[1].asString(rt).utf8(rt);

if (!location.empty()) {
if (location == ":memory:") {
path = ":memory:";
} else if (location.rfind('/', 0) == 0) {
path = location;
} else {
path = path + "/" + location;
}
}
}

#ifdef OP_SQLITE_USE_LIBSQL
opsqlite_libsql_remove(db, db_name, path);
opsqlite_libsql_remove(db, db_name, base_path);
#else
opsqlite_remove(db, db_name, path);
opsqlite_remove(db, db_name, base_path);
Comment thread
ospfranco marked this conversation as resolved.
Outdated
#endif
Comment thread
ospfranco marked this conversation as resolved.
Comment thread
ospfranco marked this conversation as resolved.

return {};
Expand Down
27 changes: 27 additions & 0 deletions docs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ const syncDb = openSync({
syncDb.sync();
```

## Close

Closes the current database connection. This is mainly useful before tearing down the runtime, replacing the file on disk, or deleting the database.

```tsx
const db = open({
name: 'myDb.sqlite',
});

db.close();
```

On web, use `await db.closeAsync()` instead.

## Delete

Deletes the database file represented by the current connection.

```tsx
const db = open({
name: 'myDb.sqlite',
});

db.close();
db.delete();
```

## Execute

Base async query operation. All execute calls run on a (**single**) separate and dedicated thread, so the JS thread is not blocked. It’s recommended to ALWAYS use transactions since even read calls can corrupt a sqlite database.
Expand Down
5 changes: 2 additions & 3 deletions node/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,9 @@ export class NodeDatabase implements DB {
}
}

delete(location?: string): void {
delete(): void {
this.close();
const dbLocation = location || './';
const dbPath = path.join(dbLocation, path.basename(this.dbPath));
const dbPath = this.dbPath;

if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
Expand Down
Loading
Loading