Lovefield - Relational Database for Web Apps

2.1.12 · abandoned · verified Wed Apr 22

Lovefield is a relational database written entirely in JavaScript, designed for web applications. It provides a SQL-like query API, leveraging IndexedDB for persistent storage in the browser environment. Developed by Google, the package reached its last published version, 2.1.12, in February 2017. While the GitHub repository saw minor updates until January 2023, active feature development and official maintenance appear to have ceased significantly earlier, around 2015-2017, as indicated by project videos and publishing dates. Lovefield offers cross-browser compatibility (supporting older versions of Chrome, Firefox, IE, and Safari) but explicitly does not support Node.js due to its reliance on IndexedDB. Its key differentiators were bringing a relational model and SQL-like syntax to client-side data management before more modern alternatives emerged, offering atomic transactions and a query optimizer.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a database schema, connect to the database, insert data, and query data using Lovefield's SQL-like API in a browser environment, including a simple update operation.

<!-- In an HTML file -->
<!doctype html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Lovefield Quick Start</title>
  <script src="https://unpkg.com/lovefield@2.1.12/dist/lovefield.min.js"></script>
</head>
<body>
  <script>
    // Define the database schema
    const schemaBuilder = lf.schema.create('todoDb', 1);
    schemaBuilder.createTable('Item')
        .addColumn('id', lf.Type.INTEGER)
        .addColumn('description', lf.Type.STRING)
        .addColumn('deadline', lf.Type.DATE_TIME)
        .addColumn('done', lf.Type.BOOLEAN)
        .addPrimaryKey(['id'])
        .addIndex('idxDeadline', ['deadline'], false, lf.Order.DESC);

    let todoDb;
    let itemTable;

    // Connect to the database and perform operations
    schemaBuilder.connect().then(function(db) {
      todoDb = db;
      itemTable = db.getSchema().table('Item');

      // Create a new row
      const row = itemTable.createRow({
        'id': 1,
        'description': 'Buy groceries',
        'deadline': new Date(2026, 4, 25),
        'done': false
      });

      // Insert the row into the database
      return db.insertOrReplace().into(itemTable).values([row]).exec();
    }).then(function() {
      // Query data
      return todoDb.select().from(itemTable).where(itemTable.done.eq(false)).exec();
    }).then(function(results) {
      // Process results
      results.forEach(function(row) {
        console.log(`Task: ${row['description']}, Due: ${row['deadline'].toLocaleDateString()}`);
      });

      // Example: Update an item
      return todoDb.update(itemTable)
                   .set(itemTable.done, true)
                   .where(itemTable.id.eq(1))
                   .exec();
    }).then(() => {
      console.log('Task ID 1 marked as done.');
    }).catch(err => {
      console.error('Lovefield operation failed:', err);
    });
  </script>
</body>
</html>

view raw JSON →