sqlite-ulid

repository·main·Indexed 41 days ago

https://github.com/asg017/sqlite-ulid

A SQLite extension for generating and working with Universally Unique Lexicographically Sortable Identifiers (ULIDs). Version 0.2.2-alpha.1 provides SQL functions such as ulid(), ulid_bytes(), and ulid_datetime(). It includes support for Node.js (via npm), Python (via pip), Deno, and a dedicated plugin for Datasette.

Tokens
6.5K
Snippets
37
Records
43
Agent score
49%

What's inside sqlite-ulid

  1. Install the datasette-sqlite-ulid plugin

    main

    To enable ULID support in your Datasette instance, install the datasette-sqlite-ulid plugin. This plugin loads the sqlite-ulid extension, allowing you to generate and work with ULIDs directly within your SQL queries.

    You can install it locally using the datasette install command, or specify it during the publishing process for cloud deployments.

    datasette install datasette-sqlite-ulid
  2. Install and use sqlite-ulid with Deno

    main

    Import the module directly from deno.land/x/sqlite_ulid and use the load method on your database instance.

    import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts";
    import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid@v${VERSION}/mod.ts";
    
    const db = new Database(":memory:");
    
    db.enableLoadExtension = true;
    sqlite_ulid.load(db);
    
    const [version] = db.prepare("select ulid_version()").value<[string]>()!;
    console.log(version);
  3. Install and use sqlite-ulid with Node.js

    main

    Install the sqlite-ulid npm package. When using better-sqlite3, use sqlite_ulid.getLoadablePath() to find the correct binary to load.

    npm install sqlite-ulid
    import Database from "better-sqlite3";
    import * as sqlite_ulid from "sqlite-ulid";
    
    const db = new Database(":memory:");
    db.loadExtension(sqlite_ulid.getLoadablePath());
    
    console.log(db.prepare("select ulid()").get());
  4. Install sqlite-ulid via npm

    main

    For Node.js developers, sqlite-ulid is available as an NPM package. While the main package is sqlite-ulid, there are also autogenerated platform-specific packages available (e.g., sqlite-ulid-darwin-arm64, sqlite-ulid-linux-x64, sqlite-ulid-windows-x64) to ensure compatibility with specific architectures.

    npm install sqlite-ulid
  5. Install sqlite-ulid Python packages

    main

    The sqlite-ulid project provides two distinct Python packages depending on your use case:

    1. sqlite-ulid: The core package for general Python usage.
    2. datasette-sqlite-ulid: A plugin specifically designed for Datasette, acting as a lightweight wrapper around the core sqlite-ulid package.
  6. Use the x/sqlite_ulid Deno module

    main

    The x/sqlite_ulid module provides the sqlite-ulid SQLite extension for Deno. It is designed to work with the x/sqlite3 module (the native Deno SQLite3 module).

    Important Compatibility Note: x/sqlite_ulid does not work with the WASM-based x/sqlite module because WASM does not support loading external extensions. You must use x/sqlite3.

    import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts";
    import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid@v0.2.2-alpha.1/mod.ts";
    
    const db = new Database(":memory:");
    
    db.enableLoadExtension = true;
    sqlite_ulid.load(db);
    
    const [version] = db
      .prepare("select ulid_version()")
      .value<[string]>()!;
    
    console.log(version);
  7. Run Deno scripts with x/sqlite_ulid

    main

    Because x/sqlite_ulid downloads and caches a pre-compiled SQLite extension for your specific machine, it requires network and filesystem permissions. Since the underlying x/sqlite3 module already requires --allow-ffi and --unstable, it is recommended to run your script with the --allow-all (or -A) flag and the --unstable flag.

    deno run -A --unstable <file>
  8. Install and use sqlite-ulid with Python

    main

    Install the package via pip and use the sqlite_ulid.load(db) helper to register the extension with your connection.

    pip install sqlite-ulid
    import sqlite3
    import sqlite_ulid
    
    db = sqlite3.connect(':memory:')
    db.enable_load_extension(True)
    sqlite_ulid.load(db)
    
    print(db.execute('select ulid()').fetchone())