@capawesome/capacitor-libsql 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Package.swift +30 -0
- package/README.md +301 -0
- package/android/build.gradle +66 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/Libsql.kt +188 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/LibsqlPlugin.java +217 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/BeginTransactionOptions.java +28 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/CommitTransactionOptions.java +46 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/ConnectOptions.java +37 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/ExecuteBatchOptions.java +103 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/ExecuteOptions.java +82 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/QueryOptions.java +82 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/RollbackTransactionOptions.java +46 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/options/SyncOptions.java +28 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/results/BeginTransactionResult.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/results/ConnectResult.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/classes/results/QueryResult.java +47 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/EmptyCallback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/NonEmptyCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/libsql/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +684 -0
- package/dist/esm/definitions.d.ts +287 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +12 -0
- package/dist/esm/web.js +36 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +50 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +53 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Classes/Options/BeginTransactionOptions.swift +14 -0
- package/ios/Plugin/Classes/Options/CommitTransactionOptions.swift +19 -0
- package/ios/Plugin/Classes/Options/ConnectOptions.swift +24 -0
- package/ios/Plugin/Classes/Options/ExecuteBatchOptions.swift +41 -0
- package/ios/Plugin/Classes/Options/ExecuteOptions.swift +23 -0
- package/ios/Plugin/Classes/Options/QueryOptions.swift +23 -0
- package/ios/Plugin/Classes/Options/RollbackTransactionOptions.swift +19 -0
- package/ios/Plugin/Classes/Options/SyncOptions.swift +14 -0
- package/ios/Plugin/Classes/Results/BeginTransactionResult.swift +16 -0
- package/ios/Plugin/Classes/Results/ConnectResult.swift +16 -0
- package/ios/Plugin/Classes/Results/QueryResult.swift +36 -0
- package/ios/Plugin/Enums/LibsqlError.swift +61 -0
- package/ios/Plugin/Libsql.swift +219 -0
- package/ios/Plugin/LibsqlPlugin.swift +166 -0
- package/ios/Plugin/Protocols/Result.swift +6 -0
- package/package.json +91 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class RollbackTransactionOptions: NSObject {
|
|
5
|
+
let connectionId: String
|
|
6
|
+
let transactionId: String
|
|
7
|
+
|
|
8
|
+
init(_ call: CAPPluginCall) throws {
|
|
9
|
+
guard let connectionId = call.getString("connectionId") else {
|
|
10
|
+
throw LibsqlError.connectionIdMissing
|
|
11
|
+
}
|
|
12
|
+
guard let transactionId = call.getString("transactionId") else {
|
|
13
|
+
throw LibsqlError.transactionIdMissing
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
self.connectionId = connectionId
|
|
17
|
+
self.transactionId = transactionId
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class SyncOptions: NSObject {
|
|
5
|
+
let connectionId: String
|
|
6
|
+
|
|
7
|
+
init(_ call: CAPPluginCall) throws {
|
|
8
|
+
guard let connectionId = call.getString("connectionId") else {
|
|
9
|
+
throw LibsqlError.connectionIdMissing
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
self.connectionId = connectionId
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class BeginTransactionResult: NSObject, Result {
|
|
5
|
+
let transactionId: String
|
|
6
|
+
|
|
7
|
+
init(transactionId: String) {
|
|
8
|
+
self.transactionId = transactionId
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["transactionId"] = transactionId
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc public class ConnectResult: NSObject, Result {
|
|
5
|
+
let connectionId: String
|
|
6
|
+
|
|
7
|
+
init(connectionId: String) {
|
|
8
|
+
self.connectionId = connectionId
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@objc public func toJSObject() -> AnyObject {
|
|
12
|
+
var result = JSObject()
|
|
13
|
+
result["connectionId"] = connectionId
|
|
14
|
+
return result as AnyObject
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
import Libsql
|
|
4
|
+
|
|
5
|
+
@objc public class QueryResult: NSObject, Result {
|
|
6
|
+
let rows: [[Any]]
|
|
7
|
+
|
|
8
|
+
init(rows: Rows, columns: [String]) {
|
|
9
|
+
self.rows = QueryResult.convertRowsToAnyArray(rows, columns)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@objc public func toJSObject() -> AnyObject {
|
|
13
|
+
var result = JSObject()
|
|
14
|
+
result["rows"] = rows
|
|
15
|
+
return result as AnyObject
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
private static func convertRowsToAnyArray(_ rows: Rows, _ columns: [String]) -> [[Any]] {
|
|
19
|
+
var rowsResult: [[Any]] = []
|
|
20
|
+
while let row = rows.next() {
|
|
21
|
+
var rowResult: [Any] = []
|
|
22
|
+
var index = Int32(0)
|
|
23
|
+
for _ in columns {
|
|
24
|
+
do {
|
|
25
|
+
let value = try row.get(index)
|
|
26
|
+
rowResult.append(value)
|
|
27
|
+
index += 1
|
|
28
|
+
} catch {
|
|
29
|
+
break // Exit the loop when an error occurs (e.g., no more columns)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
rowsResult.append(rowResult)
|
|
33
|
+
}
|
|
34
|
+
return rowsResult
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
|
|
3
|
+
enum LibsqlError: Error {
|
|
4
|
+
case connectionIdMissing
|
|
5
|
+
case statementMissing
|
|
6
|
+
case tableNameNotRecognized
|
|
7
|
+
case transactionIdMissing
|
|
8
|
+
case connectionNotFound
|
|
9
|
+
case databaseNotFound
|
|
10
|
+
case transactionNotFound
|
|
11
|
+
case invalidStatement
|
|
12
|
+
case invalidValues
|
|
13
|
+
|
|
14
|
+
var code: String? {
|
|
15
|
+
switch self {
|
|
16
|
+
case .connectionIdMissing:
|
|
17
|
+
return nil
|
|
18
|
+
case .statementMissing:
|
|
19
|
+
return nil
|
|
20
|
+
case .tableNameNotRecognized:
|
|
21
|
+
return nil
|
|
22
|
+
case .transactionIdMissing:
|
|
23
|
+
return nil
|
|
24
|
+
case .connectionNotFound:
|
|
25
|
+
return nil
|
|
26
|
+
case .databaseNotFound:
|
|
27
|
+
return nil
|
|
28
|
+
case .transactionNotFound:
|
|
29
|
+
return nil
|
|
30
|
+
case .invalidStatement:
|
|
31
|
+
return nil
|
|
32
|
+
case .invalidValues:
|
|
33
|
+
return nil
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
extension LibsqlError: LocalizedError {
|
|
39
|
+
public var errorDescription: String? {
|
|
40
|
+
switch self {
|
|
41
|
+
case .connectionIdMissing:
|
|
42
|
+
return NSLocalizedString("connectionId must be provided.", comment: "connectionIdMissing")
|
|
43
|
+
case .statementMissing:
|
|
44
|
+
return NSLocalizedString("statement must be provided.", comment: "statementMissing")
|
|
45
|
+
case .tableNameNotRecognized:
|
|
46
|
+
return NSLocalizedString("Table name not recognized.", comment: "tableNameNotRecognized")
|
|
47
|
+
case .transactionIdMissing:
|
|
48
|
+
return NSLocalizedString("transactionId must be provided.", comment: "transactionIdMissing")
|
|
49
|
+
case .connectionNotFound:
|
|
50
|
+
return NSLocalizedString("Connection not found.", comment: "connectionNotFound")
|
|
51
|
+
case .databaseNotFound:
|
|
52
|
+
return NSLocalizedString("Database not found.", comment: "databaseNotFound")
|
|
53
|
+
case .transactionNotFound:
|
|
54
|
+
return NSLocalizedString("Transaction not found.", comment: "transactionNotFound")
|
|
55
|
+
case .invalidStatement:
|
|
56
|
+
return NSLocalizedString("All statements must be strings.", comment: "invalidStatement")
|
|
57
|
+
case .invalidValues:
|
|
58
|
+
return NSLocalizedString("All values must be arrays.", comment: "invalidValues")
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Libsql
|
|
3
|
+
|
|
4
|
+
@objc public class Libsql: NSObject {
|
|
5
|
+
|
|
6
|
+
private var databases: [String: Database] = [:]
|
|
7
|
+
private var connections: [String: Connection] = [:]
|
|
8
|
+
private var transactions: [String: String] = [:]
|
|
9
|
+
|
|
10
|
+
@objc public func connect(_ options: ConnectOptions, completion: @escaping (_ result: ConnectResult?, _ error: Error?) -> Void) throws {
|
|
11
|
+
do {
|
|
12
|
+
let connectionId = UUID().uuidString
|
|
13
|
+
|
|
14
|
+
let database: Database
|
|
15
|
+
|
|
16
|
+
if let url = options.url, let path = options.getPath(), let authToken = options.authToken {
|
|
17
|
+
// Embedded replica mode
|
|
18
|
+
database = try Database(
|
|
19
|
+
path: path,
|
|
20
|
+
url: url,
|
|
21
|
+
authToken: authToken
|
|
22
|
+
)
|
|
23
|
+
} else if let url = options.url, let authToken = options.authToken {
|
|
24
|
+
// Remote-only mode
|
|
25
|
+
database = try Database(
|
|
26
|
+
url: url,
|
|
27
|
+
authToken: authToken
|
|
28
|
+
)
|
|
29
|
+
} else if let path = options.getPath() {
|
|
30
|
+
// Local-only mode
|
|
31
|
+
database = try Database(path)
|
|
32
|
+
} else {
|
|
33
|
+
// In-memory database
|
|
34
|
+
database = try Database(":memory:")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
let connection = try database.connect()
|
|
38
|
+
|
|
39
|
+
self.databases[connectionId] = database
|
|
40
|
+
self.connections[connectionId] = connection
|
|
41
|
+
|
|
42
|
+
let result = ConnectResult(connectionId: connectionId)
|
|
43
|
+
completion(result, nil)
|
|
44
|
+
} catch {
|
|
45
|
+
completion(nil, error)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
@objc public func execute(_ options: ExecuteOptions, completion: @escaping (_ error: Error?) -> Void) throws {
|
|
50
|
+
do {
|
|
51
|
+
guard let connection = self.connections[options.connectionId] else {
|
|
52
|
+
completion(LibsqlError.connectionNotFound)
|
|
53
|
+
return
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if let values = options.values, !values.isEmpty {
|
|
57
|
+
try connection.execute(options.statement, values as! [ValueRepresentable])
|
|
58
|
+
} else {
|
|
59
|
+
try connection.execute(options.statement)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
completion(nil)
|
|
63
|
+
} catch {
|
|
64
|
+
completion(error)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@objc public func executeBatch(_ options: ExecuteBatchOptions, completion: @escaping (_ error: Error?) -> Void) throws {
|
|
69
|
+
do {
|
|
70
|
+
guard let connection = self.connections[options.connectionId] else {
|
|
71
|
+
completion(LibsqlError.connectionNotFound)
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (index, statement) in options.statement.enumerated() {
|
|
76
|
+
let values = options.values?[safe: index]
|
|
77
|
+
|
|
78
|
+
if let values = values, !values.isEmpty {
|
|
79
|
+
try connection.execute(statement, values as! [ValueRepresentable])
|
|
80
|
+
} else {
|
|
81
|
+
try connection.execute(statement)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
completion(nil)
|
|
86
|
+
} catch {
|
|
87
|
+
completion(error)
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@objc public func query(_ options: QueryOptions, completion: @escaping (_ result: QueryResult?, _ error: Error?) -> Void) throws {
|
|
92
|
+
do {
|
|
93
|
+
guard let connection = self.connections[options.connectionId] else {
|
|
94
|
+
completion(nil, LibsqlError.connectionNotFound)
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let rows: Rows
|
|
99
|
+
let tableName = extractTableName(from: options.statement)
|
|
100
|
+
|
|
101
|
+
guard let tableName = tableName else {
|
|
102
|
+
throw LibsqlError.tableNameNotRecognized
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let columns = try connection.query("PRAGMA table_info(\(tableName))")
|
|
106
|
+
var columnNames: [String] = []
|
|
107
|
+
|
|
108
|
+
while let column = columns.next() {
|
|
109
|
+
let nameValue = try column.get(1)
|
|
110
|
+
if case .text(let columnName) = nameValue {
|
|
111
|
+
columnNames.append(columnName)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if let values = options.values, !values.isEmpty {
|
|
116
|
+
rows = try connection.query(options.statement, values as! [ValueRepresentable])
|
|
117
|
+
} else {
|
|
118
|
+
rows = try connection.query(options.statement)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let result = QueryResult(rows: rows, columns: columnNames)
|
|
122
|
+
completion(result, nil)
|
|
123
|
+
} catch {
|
|
124
|
+
completion(nil, error)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
@objc public func beginTransaction(_ options: BeginTransactionOptions, completion: @escaping (_ result: BeginTransactionResult?, _ error: Error?) -> Void) throws {
|
|
129
|
+
do {
|
|
130
|
+
guard let connection = self.connections[options.connectionId] else {
|
|
131
|
+
completion(nil, LibsqlError.connectionNotFound)
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
_ = try connection.execute("BEGIN TRANSACTION")
|
|
136
|
+
|
|
137
|
+
let transactionId = UUID().uuidString
|
|
138
|
+
self.transactions[transactionId] = options.connectionId
|
|
139
|
+
|
|
140
|
+
let result = BeginTransactionResult(transactionId: transactionId)
|
|
141
|
+
completion(result, nil)
|
|
142
|
+
} catch {
|
|
143
|
+
completion(nil, error)
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@objc public func commitTransaction(_ options: CommitTransactionOptions, completion: @escaping (_ error: Error?) -> Void) throws {
|
|
148
|
+
do {
|
|
149
|
+
guard let connection = self.connections[options.connectionId] else {
|
|
150
|
+
completion(LibsqlError.connectionNotFound)
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
guard self.transactions[options.transactionId] != nil else {
|
|
155
|
+
completion(LibsqlError.transactionNotFound)
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_ = try connection.execute("COMMIT")
|
|
160
|
+
self.transactions.removeValue(forKey: options.transactionId)
|
|
161
|
+
completion(nil)
|
|
162
|
+
} catch {
|
|
163
|
+
completion(error)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
@objc public func rollbackTransaction(_ options: RollbackTransactionOptions, completion: @escaping (_ error: Error?) -> Void) throws {
|
|
168
|
+
do {
|
|
169
|
+
guard let connection = self.connections[options.connectionId] else {
|
|
170
|
+
completion(LibsqlError.connectionNotFound)
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
guard self.transactions[options.transactionId] != nil else {
|
|
175
|
+
completion(LibsqlError.transactionNotFound)
|
|
176
|
+
return
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
_ = try connection.execute("ROLLBACK")
|
|
180
|
+
self.transactions.removeValue(forKey: options.transactionId)
|
|
181
|
+
completion(nil)
|
|
182
|
+
} catch {
|
|
183
|
+
completion(error)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
@objc public func sync(_ options: SyncOptions, completion: @escaping (_ error: Error?) -> Void) throws {
|
|
188
|
+
do {
|
|
189
|
+
guard let database = self.databases[options.connectionId] else {
|
|
190
|
+
completion(LibsqlError.databaseNotFound)
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
try database.sync()
|
|
195
|
+
completion(nil)
|
|
196
|
+
} catch {
|
|
197
|
+
completion(error)
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private func extractTableName(from query: String) -> String? {
|
|
202
|
+
let pattern = #"(?i)SELECT\s+\*\s+FROM\s+([a-zA-Z0-9_]+)"#
|
|
203
|
+
|
|
204
|
+
if let regex = try? NSRegularExpression(pattern: pattern) {
|
|
205
|
+
let range = NSRange(query.startIndex..., in: query)
|
|
206
|
+
if let match = regex.firstMatch(in: query, range: range),
|
|
207
|
+
let tableRange = Range(match.range(at: 1), in: query) {
|
|
208
|
+
return String(query[tableRange])
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return nil
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
extension Array {
|
|
216
|
+
subscript(safe index: Index) -> Element? {
|
|
217
|
+
return indices.contains(index) ? self[index] : nil
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import Capacitor
|
|
3
|
+
|
|
4
|
+
@objc(LibsqlPlugin)
|
|
5
|
+
public class LibsqlPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
6
|
+
public let identifier = "LibsqlPlugin"
|
|
7
|
+
public let jsName = "Libsql"
|
|
8
|
+
public let pluginMethods: [CAPPluginMethod] = [
|
|
9
|
+
CAPPluginMethod(name: "connect", returnType: CAPPluginReturnPromise),
|
|
10
|
+
CAPPluginMethod(name: "execute", returnType: CAPPluginReturnPromise),
|
|
11
|
+
CAPPluginMethod(name: "executeBatch", returnType: CAPPluginReturnPromise),
|
|
12
|
+
CAPPluginMethod(name: "query", returnType: CAPPluginReturnPromise),
|
|
13
|
+
CAPPluginMethod(name: "beginTransaction", returnType: CAPPluginReturnPromise),
|
|
14
|
+
CAPPluginMethod(name: "commitTransaction", returnType: CAPPluginReturnPromise),
|
|
15
|
+
CAPPluginMethod(name: "rollbackTransaction", returnType: CAPPluginReturnPromise),
|
|
16
|
+
CAPPluginMethod(name: "sync", returnType: CAPPluginReturnPromise)
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
public static let tag = "Libsql"
|
|
20
|
+
private let implementation = Libsql()
|
|
21
|
+
|
|
22
|
+
@objc func connect(_ call: CAPPluginCall) {
|
|
23
|
+
do {
|
|
24
|
+
let options = try ConnectOptions(call)
|
|
25
|
+
|
|
26
|
+
try implementation.connect(options, completion: { result, error in
|
|
27
|
+
if let error = error {
|
|
28
|
+
self.rejectCall(call, error)
|
|
29
|
+
} else {
|
|
30
|
+
self.resolveCall(call, result)
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
} catch {
|
|
34
|
+
self.rejectCall(call, error)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@objc func execute(_ call: CAPPluginCall) {
|
|
39
|
+
do {
|
|
40
|
+
let options = try ExecuteOptions(call)
|
|
41
|
+
|
|
42
|
+
try implementation.execute(options, completion: { error in
|
|
43
|
+
if let error = error {
|
|
44
|
+
self.rejectCall(call, error)
|
|
45
|
+
} else {
|
|
46
|
+
self.resolveCall(call)
|
|
47
|
+
}
|
|
48
|
+
})
|
|
49
|
+
} catch {
|
|
50
|
+
self.rejectCall(call, error)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
@objc func executeBatch(_ call: CAPPluginCall) {
|
|
55
|
+
do {
|
|
56
|
+
let options = try ExecuteBatchOptions(call)
|
|
57
|
+
|
|
58
|
+
try implementation.executeBatch(options, completion: { error in
|
|
59
|
+
if let error = error {
|
|
60
|
+
self.rejectCall(call, error)
|
|
61
|
+
} else {
|
|
62
|
+
self.resolveCall(call)
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
} catch {
|
|
66
|
+
self.rejectCall(call, error)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
@objc func query(_ call: CAPPluginCall) {
|
|
71
|
+
do {
|
|
72
|
+
let options = try QueryOptions(call)
|
|
73
|
+
|
|
74
|
+
try implementation.query(options, completion: { result, error in
|
|
75
|
+
if let error = error {
|
|
76
|
+
self.rejectCall(call, error)
|
|
77
|
+
} else {
|
|
78
|
+
self.resolveCall(call, result)
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
} catch {
|
|
82
|
+
self.rejectCall(call, error)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@objc func beginTransaction(_ call: CAPPluginCall) {
|
|
87
|
+
do {
|
|
88
|
+
let options = try BeginTransactionOptions(call)
|
|
89
|
+
|
|
90
|
+
try implementation.beginTransaction(options, completion: { result, error in
|
|
91
|
+
if let error = error {
|
|
92
|
+
self.rejectCall(call, error)
|
|
93
|
+
} else {
|
|
94
|
+
self.resolveCall(call, result)
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
} catch {
|
|
98
|
+
self.rejectCall(call, error)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@objc func commitTransaction(_ call: CAPPluginCall) {
|
|
103
|
+
do {
|
|
104
|
+
let options = try CommitTransactionOptions(call)
|
|
105
|
+
|
|
106
|
+
try implementation.commitTransaction(options, completion: { error in
|
|
107
|
+
if let error = error {
|
|
108
|
+
self.rejectCall(call, error)
|
|
109
|
+
} else {
|
|
110
|
+
self.resolveCall(call)
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
} catch {
|
|
114
|
+
self.rejectCall(call, error)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
@objc func rollbackTransaction(_ call: CAPPluginCall) {
|
|
119
|
+
do {
|
|
120
|
+
let options = try RollbackTransactionOptions(call)
|
|
121
|
+
|
|
122
|
+
try implementation.rollbackTransaction(options, completion: { error in
|
|
123
|
+
if let error = error {
|
|
124
|
+
self.rejectCall(call, error)
|
|
125
|
+
} else {
|
|
126
|
+
self.resolveCall(call)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
} catch {
|
|
130
|
+
self.rejectCall(call, error)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
@objc func sync(_ call: CAPPluginCall) {
|
|
135
|
+
do {
|
|
136
|
+
let options = try SyncOptions(call)
|
|
137
|
+
|
|
138
|
+
try implementation.sync(options, completion: { error in
|
|
139
|
+
if let error = error {
|
|
140
|
+
self.rejectCall(call, error)
|
|
141
|
+
} else {
|
|
142
|
+
self.resolveCall(call)
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
} catch {
|
|
146
|
+
self.rejectCall(call, error)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private func rejectCall(_ call: CAPPluginCall, _ error: Error) {
|
|
151
|
+
CAPLog.print("[", LibsqlPlugin.tag, "] ", error)
|
|
152
|
+
call.reject(error.localizedDescription)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private func resolveCall(_ call: CAPPluginCall) {
|
|
156
|
+
call.resolve()
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private func resolveCall(_ call: CAPPluginCall, _ result: Result?) {
|
|
160
|
+
if let result = result?.toJSObject() as? JSObject {
|
|
161
|
+
call.resolve(result)
|
|
162
|
+
} else {
|
|
163
|
+
call.resolve()
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capawesome/capacitor-libsql",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Capacitor plugin for libSQL databases.",
|
|
5
|
+
"main": "dist/plugin.cjs.js",
|
|
6
|
+
"module": "dist/esm/index.js",
|
|
7
|
+
"types": "dist/esm/index.d.ts",
|
|
8
|
+
"unpkg": "dist/plugin.js",
|
|
9
|
+
"files": [
|
|
10
|
+
"android/src/main/",
|
|
11
|
+
"android/build.gradle",
|
|
12
|
+
"dist/",
|
|
13
|
+
"ios/Plugin/",
|
|
14
|
+
"Package.swift"
|
|
15
|
+
],
|
|
16
|
+
"author": "Robin Genz <mail@robingenz.dev>",
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/capawesome-team/capacitor-plugins.git"
|
|
21
|
+
},
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/capawesome-team/capacitor-plugins/issues"
|
|
24
|
+
},
|
|
25
|
+
"funding": [
|
|
26
|
+
{
|
|
27
|
+
"type": "github",
|
|
28
|
+
"url": "https://github.com/sponsors/capawesome-team/"
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"type": "opencollective",
|
|
32
|
+
"url": "https://opencollective.com/capawesome"
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"keywords": [
|
|
36
|
+
"capacitor",
|
|
37
|
+
"plugin",
|
|
38
|
+
"native"
|
|
39
|
+
],
|
|
40
|
+
"scripts": {
|
|
41
|
+
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
|
|
42
|
+
"verify:ios": "xcodebuild -scheme CapawesomeCapacitorLibsql -destination generic/platform=iOS",
|
|
43
|
+
"verify:android": "cd android && ./gradlew clean build test && cd ..",
|
|
44
|
+
"verify:web": "npm run build",
|
|
45
|
+
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
|
|
46
|
+
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
|
|
47
|
+
"eslint": "eslint . --ext ts",
|
|
48
|
+
"prettier": "prettier \"**/*.{css,html,ts,js,java}\"",
|
|
49
|
+
"swiftlint": "node-swiftlint",
|
|
50
|
+
"docgen": "docgen --api LibsqlPlugin --output-readme README.md --output-json dist/docs.json",
|
|
51
|
+
"build": "npm run clean && npm run docgen && tsc && rollup -c rollup.config.mjs",
|
|
52
|
+
"clean": "rimraf ./dist",
|
|
53
|
+
"watch": "tsc --watch",
|
|
54
|
+
"ios:spm:install": "cd ios && swift package resolve && cd ..",
|
|
55
|
+
"prepublishOnly": "npm run build"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@capacitor/android": "7.0.0",
|
|
59
|
+
"@capacitor/cli": "7.0.0",
|
|
60
|
+
"@capacitor/core": "7.0.0",
|
|
61
|
+
"@capacitor/docgen": "0.3.0",
|
|
62
|
+
"@capacitor/ios": "7.0.0",
|
|
63
|
+
"@ionic/eslint-config": "0.4.0",
|
|
64
|
+
"@ionic/swiftlint-config": "2.0.0",
|
|
65
|
+
"eslint": "8.57.0",
|
|
66
|
+
"prettier": "3.4.2",
|
|
67
|
+
"prettier-plugin-java": "2.6.7",
|
|
68
|
+
"rimraf": "6.0.1",
|
|
69
|
+
"rollup": "4.30.1",
|
|
70
|
+
"swiftlint": "2.0.0",
|
|
71
|
+
"typescript": "4.1.5"
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"@capacitor/core": ">=7.0.0"
|
|
75
|
+
},
|
|
76
|
+
"swiftlint": "@ionic/swiftlint-config",
|
|
77
|
+
"eslintConfig": {
|
|
78
|
+
"extends": "@ionic/eslint-config/recommended"
|
|
79
|
+
},
|
|
80
|
+
"capacitor": {
|
|
81
|
+
"ios": {
|
|
82
|
+
"src": "ios"
|
|
83
|
+
},
|
|
84
|
+
"android": {
|
|
85
|
+
"src": "android"
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
"publishConfig": {
|
|
89
|
+
"access": "public"
|
|
90
|
+
}
|
|
91
|
+
}
|