@mastra/libsql 0.0.4 → 0.1.0-alpha.1

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.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/libsql",
3
- "version": "0.0.4",
3
+ "version": "0.1.0-alpha.1",
4
4
  "description": "Libsql provider for Mastra - includes both vector and db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,8 +20,7 @@
20
20
  },
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "@libsql/client": "^0.15.4",
24
- "@mastra/core": "^0.9.4"
23
+ "@libsql/client": "^0.15.4"
25
24
  },
26
25
  "devDependencies": {
27
26
  "@microsoft/api-extractor": "^7.52.5",
@@ -31,7 +30,11 @@
31
30
  "typescript": "^5.8.3",
32
31
  "vitest": "^3.1.2",
33
32
  "@internal/lint": "0.0.5",
34
- "@internal/storage-test-utils": "0.0.2-alpha.5"
33
+ "@internal/storage-test-utils": "0.0.2-alpha.5",
34
+ "@mastra/core": "0.10.0-alpha.1"
35
+ },
36
+ "peerDependencies": {
37
+ "@mastra/core": "^0.9.4"
35
38
  },
36
39
  "scripts": {
37
40
  "build": "tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
@@ -18,6 +18,7 @@ import type {
18
18
  WorkflowRun,
19
19
  WorkflowRuns,
20
20
  } from '@mastra/core/storage';
21
+ import { parseSqlIdentifier } from '@mastra/core/utils';
21
22
  import type { WorkflowRunState } from '@mastra/core/workflows';
22
23
 
23
24
  function safelyParseJSON(jsonString: string): any {
@@ -48,7 +49,9 @@ export class LibSQLStore extends MastraStorage {
48
49
  }
49
50
 
50
51
  private getCreateTableSQL(tableName: TABLE_NAMES, schema: Record<string, StorageColumn>): string {
52
+ const parsedTableName = parseSqlIdentifier(tableName, 'table name');
51
53
  const columns = Object.entries(schema).map(([name, col]) => {
54
+ const parsedColumnName = parseSqlIdentifier(name, 'column name');
52
55
  let type = col.type.toUpperCase();
53
56
  if (type === 'TEXT') type = 'TEXT';
54
57
  if (type === 'TIMESTAMP') type = 'TEXT'; // Store timestamps as ISO strings
@@ -57,19 +60,19 @@ export class LibSQLStore extends MastraStorage {
57
60
  const nullable = col.nullable ? '' : 'NOT NULL';
58
61
  const primaryKey = col.primaryKey ? 'PRIMARY KEY' : '';
59
62
 
60
- return `${name} ${type} ${nullable} ${primaryKey}`.trim();
63
+ return `${parsedColumnName} ${type} ${nullable} ${primaryKey}`.trim();
61
64
  });
62
65
 
63
66
  // For workflow_snapshot table, create a composite primary key
64
67
  if (tableName === TABLE_WORKFLOW_SNAPSHOT) {
65
- const stmnt = `CREATE TABLE IF NOT EXISTS ${tableName} (
68
+ const stmnt = `CREATE TABLE IF NOT EXISTS ${parsedTableName} (
66
69
  ${columns.join(',\n')},
67
70
  PRIMARY KEY (workflow_name, run_id)
68
71
  )`;
69
72
  return stmnt;
70
73
  }
71
74
 
72
- return `CREATE TABLE IF NOT EXISTS ${tableName} (${columns.join(', ')})`;
75
+ return `CREATE TABLE IF NOT EXISTS ${parsedTableName} (${columns.join(', ')})`;
73
76
  }
74
77
 
75
78
  async createTable({
@@ -90,8 +93,9 @@ export class LibSQLStore extends MastraStorage {
90
93
  }
91
94
 
92
95
  async clearTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
96
+ const parsedTableName = parseSqlIdentifier(tableName, 'table name');
93
97
  try {
94
- await this.client.execute(`DELETE FROM ${tableName}`);
98
+ await this.client.execute(`DELETE FROM ${parsedTableName}`);
95
99
  } catch (e) {
96
100
  if (e instanceof Error) {
97
101
  this.logger.error(e.message);
@@ -103,7 +107,8 @@ export class LibSQLStore extends MastraStorage {
103
107
  sql: string;
104
108
  args: InValue[];
105
109
  } {
106
- const columns = Object.keys(record);
110
+ const parsedTableName = parseSqlIdentifier(tableName, 'table name');
111
+ const columns = Object.keys(record).map(col => parseSqlIdentifier(col, 'column name'));
107
112
  const values = Object.values(record).map(v => {
108
113
  if (typeof v === `undefined`) {
109
114
  // returning an undefined value will cause libsql to throw
@@ -117,7 +122,7 @@ export class LibSQLStore extends MastraStorage {
117
122
  const placeholders = values.map(() => '?').join(', ');
118
123
 
119
124
  return {
120
- sql: `INSERT OR REPLACE INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`,
125
+ sql: `INSERT OR REPLACE INTO ${parsedTableName} (${columns.join(', ')}) VALUES (${placeholders})`,
121
126
  args: values,
122
127
  };
123
128
  }
@@ -149,13 +154,15 @@ export class LibSQLStore extends MastraStorage {
149
154
  }
150
155
 
151
156
  async load<R>({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, string> }): Promise<R | null> {
152
- const conditions = Object.entries(keys)
153
- .map(([key]) => `${key} = ?`)
154
- .join(' AND ');
157
+ const parsedTableName = parseSqlIdentifier(tableName, 'table name');
158
+
159
+ const parsedKeys = Object.keys(keys).map(key => parseSqlIdentifier(key, 'column name'));
160
+
161
+ const conditions = parsedKeys.map(key => `${key} = ?`).join(' AND ');
155
162
  const values = Object.values(keys);
156
163
 
157
164
  const result = await this.client.execute({
158
- sql: `SELECT * FROM ${tableName} WHERE ${conditions} ORDER BY createdAt DESC LIMIT 1`,
165
+ sql: `SELECT * FROM ${parsedTableName} WHERE ${conditions} ORDER BY createdAt DESC LIMIT 1`,
159
166
  args: values,
160
167
  });
161
168
 
@@ -506,6 +513,7 @@ export class LibSQLStore extends MastraStorage {
506
513
  if (toDate) {
507
514
  conditions.push('createdAt <= ?');
508
515
  }
516
+
509
517
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
510
518
 
511
519
  if (name) {