### Install and Run Datasette with ULID Plugin Source: https://context7.com/asg017/sqlite-ulid/llms.txt Provides instructions on how to install and use the `datasette-sqlite-ulid` plugin to make ULID functions available when running Datasette. This involves installing the plugin via pip and then launching Datasette with the desired database. An alternative command shows how to manually load the extension if the plugin is not installed system-wide. ```bash # Install the plugin datasette install datasette-sqlite-ulid # Run datasette with ULID functions available datasette data.db # Or load manually datasette data.db --load-extension ./ulid0 ``` -------------------------------- ### Install datasette-sqlite-ulid Plugin Source: https://github.com/asg017/sqlite-ulid/blob/main/python/datasette_sqlite_ulid/README.md Installs the datasette-sqlite-ulid plugin using the datasette CLI. This makes ULID generation functions available in SQL queries within Datasette. ```bash datasette install datasette-sqlite-ulid ``` -------------------------------- ### Generate ULID in C with Static Linking Source: https://context7.com/asg017/sqlite-ulid/llms.txt Shows how to statically link and use the sqlite-ulid extension in a C program. This method involves including the necessary headers and registering the extension using `sqlite3_auto_extension`. The example demonstrates opening an in-memory database, preparing and executing a query to generate a ULID, and printing the result. Error handling for SQLite operations is included. ```c #include "sqlite3.h" #include "sqlite-ulid.h" #include int main(int argc, char *argv[]) { int rc = SQLITE_OK; sqlite3 *db; sqlite3_stmt *stmt; // Register extension as auto-extension rc = sqlite3_auto_extension((void (*)())sqlite3_ulid_init); if (rc != SQLITE_OK) { fprintf(stderr, "Could not load sqlite3_ulid_init\n"); return 1; } // Open database rc = sqlite3_open(":memory:", &db); if (rc != SQLITE_OK) { fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } // Generate ULID rc = sqlite3_prepare_v2(db, "SELECT ulid()", -1, &stmt, NULL); if (rc != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 1; } if (sqlite3_step(stmt) == SQLITE_ROW) { printf("ulid() = %s\n", sqlite3_column_text(stmt, 0)); // ulid() = 01gqrf8x2efy50mz28b238dh2a } sqlite3_finalize(stmt); sqlite3_close(db); return 0; } ``` -------------------------------- ### Install sqlite-ulid NPM Package Source: https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/README.md This command installs the sqlite-ulid package using npm. It is a prerequisite for using the ULID SQLite extension in Node.js projects. ```bash npm install sqlite-ulid ``` -------------------------------- ### Install and Use sqlite-ulid in Node.js Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Install the sqlite-ulid npm package and load the extension into a better-sqlite3 database using its loadable path. ```bash npm install sqlite-ulid ``` ```javascript import Database from "better-sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new Database(":memory:"); db.loadExtension(sqlite_ulid.getLoadablePath()); ``` -------------------------------- ### ulid_debug() - Get Debug Information Source: https://github.com/asg017/sqlite-ulid/blob/main/docs.md Returns a debug string containing various information about the `sqlite-ulid` installation, including the version, build date, and commit hash. ```APIDOC ## ulid_debug() ### Description Returns a debug string with version, build date, and commit hash information for `sqlite-ulid`. ### Method SQL Function ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```sql select ulid_debug(); ``` ### Response #### Success Response (200) * **debug_info** (TEXT) - A multi-line string containing debug information. #### Response Example ```json { "debug_info": "Version: v0.1.0\nSource: 247dca8f4cea1abdc30ed3e852c3e5b71374c177" } ``` ``` -------------------------------- ### Generate ULID and Get Version in Go Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates how to use the sqlite-ulid extension in Go to generate a ULID and retrieve the extension's version. It requires opening an in-memory SQLite database and executing SQL queries against the provided ULID functions. Dependencies include the Go standard library and the sqlite-ulid Go binding. ```go package main import ( "database/sql" "fmt" "log" _ "github.com/asg017/sqlite-ulid/bindings/go" _ "github.com/mattn/go-sqlite3" ) func main() { db, err := sql.Open("sqlite3", ":memory:") if err != nil { log.Fatal(err) } defer db.Close() // Generate ULID var ulid string err = db.QueryRow("SELECT ulid()") if err != nil { log.Fatal(err) } fmt.Printf("ulid: %s\n", ulid) // ulid: 01gqrf7x2efy50mz28b238dh2a // Get version var version string err = db.QueryRow("SELECT ulid_version()") if err != nil { log.Fatal(err) } fmt.Printf("version: %s\n", version) // version: v0.2.2-alpha.1 } ``` -------------------------------- ### Load sqlite-ulid Extension in Datasette Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Provides the command-line instruction for loading the `sqlite-ulid` extension when starting a Datasette instance. This allows Datasette to use the extension's functions. ```bash datasette data.db --load-extension ./ulid0 ``` -------------------------------- ### Get Loadable Path for sqlite-ulid Extension in Python Source: https://github.com/asg017/sqlite-ulid/blob/main/python/sqlite_ulid/README.md Retrieves the full file system path to the locally installed sqlite-ulid extension. This path can be used with `sqlite3.Connection.load_extension()` but the `sqlite_ulid.load()` function is recommended for convenience. ```python import sqlite_ulid path = sqlite_ulid.loadable_path() print(path) # Example output: '/.../venv/lib/python3.9/site-packages/sqlite_ulid/ulid0' ``` -------------------------------- ### Load and Use ULID Extension in SQLite CLI Source: https://context7.com/asg017/sqlite-ulid/llms.txt Illustrates how to load and utilize the sqlite-ulid extension within the SQLite command-line interface (CLI). It covers loading the extension dynamically using `.load`, verifying the installation by checking the version, generating ULIDs, and using them in table creation and insertion statements. This method is suitable for interactive use or scripting directly with SQLite. ```sql -- Load the extension (omit file extension) .load ./ulid0 -- Verify it loaded SELECT ulid_version(); -- 'v0.2.2-alpha.1' -- Generate ULIDs SELECT ulid(); -- '01gqrf9x2efy50mz28b238dh2a' -- Use with tables CREATE TABLE log_events( id TEXT PRIMARY KEY, data ANY ); INSERT INTO log_events(id, data) VALUES (ulid(), 'First event'); INSERT INTO log_events(id, data) VALUES (ulid(), 'Second event'); SELECT * FROM log_events; /* ┌────────────────────────────┬──────────────┐ │ id │ data │ ├────────────────────────────┼──────────────┤ │ 01gqrf9x2efy50mz28b238dh2a │ First event │ │ 01gqrfa02efy50mz28b238dh2a │ Second event │ └────────────────────────────┴──────────────┘ */ ``` -------------------------------- ### Get sqlite-ulid Version Source: https://github.com/asg017/sqlite-ulid/blob/main/docs.md Returns the semantic versioning (semver) string of the current version of the sqlite-ulid library. ```sql select ulid_version(); -- 'v0.1.0' ``` -------------------------------- ### ulid_version() - Get extension version Source: https://context7.com/asg017/sqlite-ulid/llms.txt Returns the semantic version string of the installed sqlite-ulid extension. ```APIDOC ## ulid_version() - Get extension version ### Description Returns the semantic version string of the installed sqlite-ulid extension. ### Method SQL Function ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```sql SELECT ulid_version(); -- 'v0.2.2-alpha.1' ``` ### Response #### Success Response (200) - **version** (TEXT) - The semantic version string of the extension. #### Response Example ```json { "version": "v0.2.2-alpha.1" } ``` ``` -------------------------------- ### Load and Use sqlite-ulid with node-sqlite3 (Node.js) Source: https://context7.com/asg017/sqlite-ulid/llms.txt Illustrates loading the sqlite-ulid extension with the node-sqlite3 library in Node.js. This example shows how to use callbacks to retrieve the extension version and generate ULIDs. ```javascript import sqlite3 from "sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new sqlite3.Database(":memory:"); // Load extension db.loadExtension(sqlite_ulid.getLoadablePath()); // Use ULID functions with callbacks db.get("SELECT ulid_version() as version", (err, row) => { if (err) throw err; console.log(row); // { version: 'v0.2.2-alpha.1' } }); db.get("SELECT ulid() as id", (err, row) => { if (err) throw err; console.log(row); // { id: '01gqrf5x2efy50mz28b238dh2a' } }); ``` -------------------------------- ### Load sqlite-ulid Extension and Generate ULID in Python Source: https://github.com/asg017/sqlite-ulid/blob/main/python/sqlite_ulid/README.md Demonstrates how to use the sqlite-ulid Python package to load the extension into a SQLite connection and then use the generated ULID functions. It shows how to get the loadable path and how to execute SQL queries that utilize the extension's functions. ```python import sqlite_ulid import sqlite3 # Get the path to the loadable extension print(sqlite_ulid.loadable_path()) # Connect to an in-memory SQLite database conn = sqlite3.connect(':memory:') # Load the sqlite-ulid extension into the connection sqlite_ulid.load(conn) # Execute a query using the extension's functions result = conn.execute('select ulid_version(), ulid()').fetchone() print(result) # Expected output: ('v0.1.0', '01gr7gwc5aq22ycea6j8kxq4s9') conn.close() ``` -------------------------------- ### Install and Use sqlite-ulid in Python Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Install the sqlite-ulid Python package using pip and load the extension into an in-memory SQLite database to generate ULIDs. ```bash pip install sqlite-ulid ``` ```python import sqlite3 import sqlite_ulid db = sqlite3.connect(':memory:') db.enable_load_extension(True) sqlite_ulid.load(db) db.execute('select ulid()').fetchone() # ('01gr7gwc5aq22ycea6j8kxq4s9',) ``` -------------------------------- ### Get Extension Version (SQL) Source: https://context7.com/asg017/sqlite-ulid/llms.txt Retrieves the semantic version string of the currently installed sqlite-ulid extension. This is useful for verifying the extension's version and ensuring compatibility. ```sql SELECT ulid_version(); -- 'v0.2.2-alpha.1' ``` -------------------------------- ### Get Loadable Path for node-sqlite3 Source: https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/README.md This JavaScript snippet illustrates how to get the path to the sqlite-ulid extension file using getLoadablePath(). This path is subsequently passed to the loadExtension() method of a node-sqlite3 database object. ```javascript import sqlite3 from "sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new sqlite3.Database(":memory:"); db.loadExtension(sqlite_ulid.getLoadablePath()); ``` -------------------------------- ### Install sqlite-ulid Python Package Source: https://github.com/asg017/sqlite-ulid/blob/main/python/sqlite_ulid/README.md Installs the sqlite-ulid package from PyPI using pip. This package provides the necessary components to use ULID functions within SQLite. ```bash pip install sqlite-ulid ``` -------------------------------- ### Get Loadable Path for better-sqlite3 Source: https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/README.md This JavaScript snippet demonstrates how to obtain the file path for the sqlite-ulid extension using the getLoadablePath() function. This path is then used to load the extension into a better-sqlite3 database instance. ```javascript import Database from "better-sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new Database(":memory:"); db.loadExtension(sqlite_ulid.getLoadablePath()); ``` -------------------------------- ### Get sqlite-ulid Debug Info Source: https://github.com/asg017/sqlite-ulid/blob/main/docs.md Returns a debug string containing various information about the sqlite-ulid library, including its version, build date, and commit hash. ```sql select ulid_debug(); 'Version: v0.1.0 Source: 247dca8f4cea1abdc30ed3e852c3e5b71374c177' ``` -------------------------------- ### ulid_version() - Get Library Version Source: https://github.com/asg017/sqlite-ulid/blob/main/docs.md Returns the semantic version (semver) string of the current `sqlite-ulid` library version. ```APIDOC ## ulid_version() ### Description Returns the semver version string of the `sqlite-ulid` library. ### Method SQL Function ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```sql select ulid_version(); ``` ### Response #### Success Response (200) * **version** (TEXT) - The semver version string (e.g., 'v0.1.0'). #### Response Example ```json { "version": "v0.1.0" } ``` ``` -------------------------------- ### Publish Datasette with datasette-sqlite-ulid Plugin Source: https://github.com/asg017/sqlite-ulid/blob/main/python/datasette_sqlite_ulid/README.md Publishes a Datasette instance to Cloud Run while automatically installing the datasette-sqlite-ulid plugin. This ensures ULID SQL functions are available in the deployed service. ```bash datasette publish cloudrun data.db --service=my-service --install=datasette-sqlite-ulid ``` -------------------------------- ### Get Debug Information (SQL) Source: https://context7.com/asg017/sqlite-ulid/llms.txt Returns debug information for the sqlite-ulid extension, including its version and the source commit hash. This can be helpful for troubleshooting and reporting issues. ```sql SELECT ulid_debug(); /* 'Version: v0.2.2-alpha.1 Source: 247dca8f4cea1abdc30ed3e852c3e5b71374c177' */ ``` -------------------------------- ### Load sqlite-ulid Extension in Node.js (better-sqlite3) Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Illustrates loading the `sqlite-ulid` extension in Node.js using the `better-sqlite3` library. The `loadExtension()` method is used, followed by executing a query to get the version. ```javascript const Database = require("better-sqlite3"); const db = new Database(":memory:"); db.loadExtension("./ulid0"); console.log(db.prepare("select ulid_version()").get()); // { 'ulid_version()': 'v0.1.0' } ``` -------------------------------- ### Deno Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates loading and using the sqlite-ulid extension within a Deno environment. ```APIDOC ## Deno ### Description This section provides an example of how to load and use the sqlite-ulid extension in a Deno application. It covers enabling load extensions and calling ULID functions. ### Method Deno TypeScript Code ### Endpoint N/A ### Parameters N/A ### Request Example ```typescript import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts"; import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid/mod.ts"; const db = new Database(":memory:"); // Load extension db.enableLoadExtension = true; sqlite_ulid.load(db); // Get version const [version] = db.prepare("SELECT ulid_version()").value<[string]>()!; console.log(version); // 'v0.2.2-alpha.1' // Generate ULIDs const [ulid] = db.prepare("SELECT ulid()").value<[string]>()!; console.log(ulid); // '01gqrf6x2efy50mz28b238dh2a' // Create table with binary ULIDs db.exec(` CREATE TABLE events( id BLOB PRIMARY KEY, type TEXT, payload TEXT ) `); db.exec("INSERT INTO events VALUES (ulid_bytes(), 'click', '{\"x\": 100}')"); db.close(); ``` ### Response #### Success Response (200) Console output showing the extension version and a generated ULID. #### Response Example ``` v0.2.2-alpha.1 01gqrf6x2efy50mz28b238dh2a ``` ``` -------------------------------- ### Generate and Query Binary ULIDs in SQLite Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Demonstrates using `ulid_bytes()` for potentially faster and more compact ULID generation. It shows how to insert binary ULIDs and then convert them to their text representation using `ulid()` for display. ```sql create table log_events( id ulid primary key, data any ); insert into log_events(id, data) values (ulid_bytes(), 1); insert into log_events(id, data) values (ulid_bytes(), 2); insert into log_events(id, data) values (ulid_bytes(), 3); select hex(id), ulid(id), data from log_events; /* ┌──────────────────────────────────┬────────────────────────────┬──────┐ │ hex(id) │ ulid(id) │ data │ ├──────────────────────────────────┼────────────────────────────┼──────┤ │ 0185F0539EBF286DA9F56BA4D9981783 │ 01gqr577nz51ptkxbbmkcsg5w3 │ 1 │ │ 0185F0539EC54F85745C1ECB64DF3A97 │ 01gqr577p59y2q8q0ysdjdyemq │ 2 │ │ 0185F0539ED48113F6F67BF3F6A4BFF7 │ 01gqr577pmg49zdxkvyfva9fzq │ 3 │ └──────────────────────────────────┴────────────────────────────┴──────┘ */ ``` -------------------------------- ### Load and Use sqlite-ulid in Deno Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates loading the sqlite-ulid extension within a Deno environment. It covers enabling extension loading, using ulid_version() and ulid(), and creating tables with binary ULIDs. ```typescript import { Database } from "https://deno.land/x/sqlite3@0.8.0/mod.ts"; import * as sqlite_ulid from "https://deno.land/x/sqlite_ulid/mod.ts"; const db = new Database(":memory:"); // Load extension db.enableLoadExtension = true; sqlite_ulid.load(db); // Get version const [version] = db.prepare("SELECT ulid_version()").value<[string]>()!; console.log(version); // 'v0.2.2-alpha.1' // Generate ULIDs const [ulid] = db.prepare("SELECT ulid()").value<[string]>()!; console.log(ulid); // '01gqrf6x2efy50mz28b238dh2a' // Create table with binary ULIDs db.exec(` CREATE TABLE events( id BLOB PRIMARY KEY, type TEXT, payload TEXT ) `); db.exec("INSERT INTO events VALUES (ulid_bytes(), 'click', '{\"x\": 100}')"); db.close(); ``` -------------------------------- ### Load sqlite-ulid extension in Deno Source: https://github.com/asg017/sqlite-ulid/blob/main/deno/README.md Demonstrates how to load the sqlite-ulid extension into an in-memory SQLite database using the x/sqlite3 and x/sqlite_ulid Deno modules. It shows how to query the ULID version after loading the extension. ```javascript 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); ``` -------------------------------- ### Load sqlite-ulid with better-sqlite3 Source: https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/README.md This JavaScript code snippet demonstrates how to load the sqlite-ulid extension into a better-sqlite3 database instance. It imports the necessary modules, creates an in-memory database, loads the extension using getLoadablePath(), and then queries the ulid_version() function. ```javascript import Database from "better-sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new Database(":memory:"); db.loadExtension(sqlite_ulid.getLoadablePath()); const version = db.prepare("select ulid_version()").pluck().get(); console.log(version); // "v0.2.0" ``` -------------------------------- ### Load sqlite-ulid Extension in SQLite CLI Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Demonstrates how to load the `sqlite-ulid` extension using the `.load` command in the SQLite command-line interface and then call the `ulid_version()` function. ```sql .load ./ulid0 select ulid_version(); -- v0.1.0 ``` -------------------------------- ### Generate and Work with ULIDs in SQLite Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Demonstrates basic usage of the sqlite-ulid extension, including generating ULIDs, ULIDs with prefixes, and converting ULIDs to datetime strings. Assumes the extension is loaded as 'ulid0'. ```sql .load ./ulid0 select ulid(); -- '01gqr4j69cc7w1xdbarkcbpq17' select ulid_bytes(); -- X'0185310899dd7662b8f1e5adf9a5e7c0' select ulid_with_prefix('invoice'); -- 'invoice_01gqr4jmhxhc92x1kqkpxb8j16' select ulid_with_datetime('2023-01-26 22:53:20.556); -- '01gqr4j69cc7w1xdbarkcbpq17' select ulid_datetime('01gqr4j69cc7w1xdbarkcbpq17') -- '2023-01-26 22:53:20.556' ``` -------------------------------- ### Load sqlite-ulid with node-sqlite3 Source: https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/README.md This JavaScript code snippet shows how to load the sqlite-ulid extension with the node-sqlite3 library. It imports the required modules, initializes a node-sqlite3 database, loads the extension, and then retrieves the ulid_version() using an asynchronous query. ```javascript import sqlite3 from "sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new sqlite3.Database(":memory:"); db.loadExtension(sqlite_ulid.getLoadablePath()); db.get("select ulid_version()", (err, row) => { console.log(row); // {ulid_version(): "v0.2.0"} }); ``` -------------------------------- ### Node.js - better-sqlite3 Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates loading and using the sqlite-ulid extension with the `better-sqlite3` library in Node.js. ```APIDOC ## Node.js - better-sqlite3 ### Description This section illustrates how to integrate and utilize the sqlite-ulid extension within a Node.js application using the `better-sqlite3` library. It covers extension loading and basic function calls. ### Method Node.js Code ### Endpoint N/A ### Parameters N/A ### Request Example ```javascript import Database from "better-sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new Database(":memory:"); // Load extension db.loadExtension(sqlite_ulid.getLoadablePath()); // Get version const version = db.prepare("SELECT ulid_version()").pluck().get(); console.log(version); // "v0.2.2-alpha.1" // Generate ULIDs const ulid = db.prepare("SELECT ulid()").pluck().get(); console.log(ulid); // '01gqrf3x2efy50mz28b238dh2a' // Create table and insert data db.exec(` CREATE TABLE products( id TEXT PRIMARY KEY, name TEXT, price REAL ) `); const insert = db.prepare( "INSERT INTO products(id, name, price) VALUES (ulid_with_prefix('prod'), ?, ?)" ); insert.run("Widget", 29.99); insert.run("Gadget", 49.99); const products = db.prepare("SELECT * FROM products ORDER BY id").all(); console.log(products); // [ // { id: 'prod_01gqrf4a2efy50mz28b238dh2a', name: 'Widget', price: 29.99 }, // { id: 'prod_01gqrf4b3fgz61na39c349ei3b', name: 'Gadget', price: 49.99 } // ] ``` ### Response #### Success Response (200) Console output showing the extension version, generated ULIDs, and table data. #### Response Example ``` v0.2.2-alpha.1 01gqrf3x2efy50mz28b238dh2a [ { id: 'prod_01gqrf4a2efy50mz28b238dh2a', name: 'Widget', price: 29.99 }, { id: 'prod_01gqrf4b3fgz61na39c349ei3b', name: 'Gadget', price: 49.99 } ] ``` ``` -------------------------------- ### Node.js - node-sqlite3 Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates loading and using the sqlite-ulid extension with the `node-sqlite3` library in Node.js, using callbacks. ```APIDOC ## Node.js - node-sqlite3 ### Description This section provides an example of using the sqlite-ulid extension with the `node-sqlite3` library in Node.js. It highlights the use of callbacks for asynchronous operations. ### Method Node.js Code ### Endpoint N/A ### Parameters N/A ### Request Example ```javascript import sqlite3 from "sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new sqlite3.Database(":memory:"); // Load extension db.loadExtension(sqlite_ulid.getLoadablePath()); // Use ULID functions with callbacks db.get("SELECT ulid_version() as version", (err, row) => { if (err) throw err; console.log(row); // { version: 'v0.2.2-alpha.1' } }); db.get("SELECT ulid() as id", (err, row) => { if (err) throw err; console.log(row); // { id: '01gqrf5x2efy50mz28b238dh2a' } }); ``` ### Response #### Success Response (200) Console output showing the extension version and a generated ULID. #### Response Example ``` { version: 'v0.2.2-alpha.1' } { id: '01gqrf5x2efy50mz28b238dh2a' } ``` ``` -------------------------------- ### Python - sqlite-ulid package Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates how to load and use the sqlite-ulid extension in Python using the `sqlite-ulid` package. ```APIDOC ## Python - sqlite-ulid package ### Description This section provides examples of using the sqlite-ulid extension within a Python environment. It covers loading the extension and utilizing its functions for ULID generation and manipulation. ### Method Python Code ### Endpoint N/A ### Parameters N/A ### Request Example ```python import sqlite3 import sqlite_ulid # Get path to extension print(sqlite_ulid.loadable_path()) # '/.../site-packages/sqlite_ulid/ulid0' # Load extension into connection conn = sqlite3.connect(':memory:') conn.enable_load_extension(True) sqlite_ulid.load(conn) conn.enable_load_extension(False) # Use ULID functions result = conn.execute('SELECT ulid_version(), ulid()').fetchone() print(result) # ('v0.2.2-alpha.1', '01gr7gwc5aq22ycea6j8kxq4s9') # Create table with ULID primary key conn.execute(''' CREATE TABLE users( id TEXT PRIMARY KEY, name TEXT, email TEXT ) ''') conn.execute( "INSERT INTO users(id, name, email) VALUES (ulid_with_prefix('user'), ?, ?)", ('Alice', 'alice@example.com') ) for row in conn.execute('SELECT * FROM users'): print(row) # ('user_01gqrf2x3efy50mz28b238dh2a', 'Alice', 'alice@example.com') ``` ### Response #### Success Response (200) Output will vary based on execution, showing version, generated ULIDs, and table contents. #### Response Example ``` /path/to/sqlite_ulid/ulid0 ('v0.2.2-alpha.1', '01gr7gwc5aq22ycea6j8kxq4s9') ('user_01gqrf2x3efy50mz28b238dh2a', 'Alice', 'alice@example.com') ``` ``` -------------------------------- ### Using ULIDs as Primary Keys Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Demonstrates how to use ULIDs as primary keys for tables, leveraging their sortable and unique properties. ```APIDOC ## Using ULIDs as Primary Keys ### Description Illustrates how to define a table with a ULID column as the primary key, ensuring unique and sortable identifiers for records. ### Example 1: Using `ulid()` ```sql create table log_events( id ulid primary key, data any ); insert into log_events(id, data) values (ulid(), 1); insert into log_events(id, data) values (ulid(), 2); insert into log_events(id, data) values (ulid(), 3); select * from log_events; /* ┌────────────────────────────┬──────┐ │ id │ data │ ├────────────────────────────┼──────┤ │ 01gqr4vr487bytsf10ktfmheg4 │ 1 │ │ 01gqr4vr4dfcfk80m2yp6j866z │ 2 │ │ 01gqr4vrjxg0yex9jr0f100v1c │ 3 │ └────────────────────────────┴──────┘ */ ``` ### Example 2: Using `ulid_bytes()` for Performance Using `ulid_bytes()` can offer performance benefits. The `ulid()` function can then be used to get a text representation if needed. ```sql create table log_events( id ulid primary key, data any ); insert into log_events(id, data) values (ulid_bytes(), 1); insert into log_events(id, data) values (ulid_bytes(), 2); insert into log_events(id, data) values (ulid_bytes(), 3); select hex(id), ulid(id), data from log_events; /* ┌──────────────────────────────────┬────────────────────────────┬──────┐ │ hex(id) │ ulid(id) │ data │ ├──────────────────────────────────┼────────────────────────────┼──────┤ │ 0185F0539EBF286DA9F56BA4D9981783 │ 01gqr577nz51ptkxbbmkcsg5w3 │ 1 │ │ 0185F0539EC54F85745C1ECB64DF3A97 │ 01gqr577p59y2q8q0ysdjdyemq │ 2 │ │ 0185F0539ED48113F6F67BF3F6A4BFF7 │ 01gqr577pmg49zdxkvyfva9fzq │ 3 │ └──────────────────────────────────┴────────────────────────────┴──────┘ */ ``` ``` -------------------------------- ### Determine Current Platform Source: https://github.com/asg017/sqlite-ulid/blob/main/npm/sqlite-ulid/README.md This bash command uses Node.js to print the current platform and architecture. This is useful for verifying compatibility with the sqlite-ulid package's supported platforms. ```bash $ node -e 'console.log([process.platform, process.arch])' [ 'darwin', 'x64' ] ``` -------------------------------- ### Deno run command with permissions Source: https://github.com/asg017/sqlite-ulid/blob/main/deno/README.md Shows the command to run a Deno script that utilizes the x/sqlite_ulid module. It specifies the necessary flags for network and filesystem access, as well as unstable features. ```bash deno run -A --unstable ``` -------------------------------- ### Load and Use sqlite-ulid in Python Source: https://context7.com/asg017/sqlite-ulid/llms.txt Demonstrates how to load the sqlite-ulid extension into a Python SQLite connection and use its functions, such as ulid_version() and ulid(), including creating tables with ULID primary keys. ```python import sqlite3 import sqlite_ulid # Get path to extension print(sqlite_ulid.loadable_path()) # '/.../site-packages/sqlite_ulid/ulid0' # Load extension into connection conn = sqlite3.connect(':memory:') conn.enable_load_extension(True) sqlite_ulid.load(conn) conn.enable_load_extension(False) # Use ULID functions result = conn.execute('SELECT ulid_version(), ulid()').fetchone() print(result) # ('v0.2.2-alpha.1', '01gr7gwc5aq22ycea6j8kxq4s9') # Create table with ULID primary key conn.execute(''' CREATE TABLE users( id TEXT PRIMARY KEY, name TEXT, email TEXT ) ''') conn.execute( "INSERT INTO users(id, name, email) VALUES (ulid_with_prefix('user'), ?, ?)", ('Alice', 'alice@example.com') ) for row in conn.execute('SELECT * FROM users'): print(row) # ('user_01gqrf2x3efy50mz28b238dh2a', 'Alice', 'alice@example.com') ``` -------------------------------- ### Load and Use sqlite-ulid with better-sqlite3 (Node.js) Source: https://context7.com/asg017/sqlite-ulid/llms.txt Shows how to load the sqlite-ulid extension using the better-sqlite3 library in Node.js. It covers retrieving the extension version, generating ULIDs, and inserting data into tables with ULID primary keys. ```javascript import Database from "better-sqlite3"; import * as sqlite_ulid from "sqlite-ulid"; const db = new Database(":memory:"); // Load extension db.loadExtension(sqlite_ulid.getLoadablePath()); // Get version const version = db.prepare("SELECT ulid_version()").pluck().get(); console.log(version); // "v0.2.2-alpha.1" // Generate ULIDs const ulid = db.prepare("SELECT ulid()").pluck().get(); console.log(ulid); // '01gqrf3x2efy50mz28b238dh2a' // Create table and insert data db.exec(` CREATE TABLE products( id TEXT PRIMARY KEY, name TEXT, price REAL ) `); const insert = db.prepare( "INSERT INTO products(id, name, price) VALUES (ulid_with_prefix('prod'), ?, ?)" ); insert.run("Widget", 29.99); insert.run("Gadget", 49.99); const products = db.prepare("SELECT * FROM products ORDER BY id").all(); console.log(products); // [ // { id: 'prod_01gqrf4a2efy50mz28b238dh2a', name: 'Widget', price: 29.99 }, // { id: 'prod_01gqrf4b3fgz61na39c349ei3b', name: 'Gadget', price: 49.99 } // ] ``` -------------------------------- ### Generate ULIDs with Prefixes in SQLite Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Demonstrates the use of `ulid_with_prefix()` to generate ULIDs that are prepended with a custom string. This is useful for differentiating between various types of IDs within the same database. ```sql select ulid_with_prefix('customer'); -- 'customer_01gqr5j1ebk31wv30wgp8ebehj' select ulid_with_prefix('product'); -- 'product_01gqr5prjgsa77dhrxf2dt1dgv' select ulid_with_prefix('order'); -- 'order_01gqr5q35n68jk0sycy1ntr083' ``` -------------------------------- ### ULID Generation Functions Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md This section covers the core functions for generating ULIDs. `ulid()` generates a standard ULID string, while `ulid_bytes()` generates a binary representation which is faster and more compact. ```APIDOC ## ULID Generation Functions ### Description Provides functions to generate ULID (Universally Unique Lexicographically Sortable Identifier) values within SQLite. ### Functions #### `ulid()` - **Description**: Generates a standard ULID string. - **Usage**: `SELECT ulid();` - **Example Output**: `'01gqr4j69cc7w1xdbarkcbpq17'` #### `ulid_bytes()` - **Description**: Generates a ULID as a binary blob (16 bytes). This is faster and more space-efficient than `ulid()`. - **Usage**: `SELECT ulid_bytes();` - **Example Output**: `X'0185310899dd7662b8f1e5adf9a5e7c0'` #### `ulid_with_prefix(prefix)` - **Description**: Generates a ULID string with a specified text prefix. Useful for differentiating ID types. - **Parameters**: - **prefix** (TEXT) - Required - The prefix string for the ULID. - **Usage**: `SELECT ulid_with_prefix('invoice');` - **Example Output**: `'invoice_01gqr4jmhxhc92x1kqkpxb8j16'` #### `ulid_with_datetime(datetime_string)` - **Description**: Generates a ULID from a given datetime string. The ULID will be sortable by time. - **Parameters**: - **datetime_string** (TEXT) - Required - The datetime string (e.g., 'YYYY-MM-DD HH:MM:SS.SSS'). - **Usage**: `SELECT ulid_with_datetime('2023-01-26 22:53:20.556');` - **Example Output**: `'01gqr4j69cc7w1xdbarkcbpq17'` ``` -------------------------------- ### Load sqlite-ulid Extension in Python Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Shows how to load the `sqlite-ulid` extension in Python using the built-in `sqlite3` module. This involves enabling extension loading and then calling `load_extension()`. It then queries `ulid_version()`. ```python import sqlite3 con = sqlite3.connect(":memory:") con.enable_load_extension(True) con.load_extension("./ulid0") print(con.execute("select ulid_version()").fetchone()) # ('v0.1.0',) ``` -------------------------------- ### Use ULID as a Primary Key in SQLite Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Shows how to define a table with a ULID primary key and insert records using the generated ULIDs. This leverages the 'ulid' type provided by the extension. ```sql create table log_events( id ulid primary key, data any ); insert into log_events(id, data) values (ulid(), 1); insert into log_events(id, data) values (ulid(), 2); insert into log_events(id, data) values (ulid(), 3); select * from log_events; /* ┌────────────────────────────┬──────┐ │ id │ data │ ├────────────────────────────┼──────┤ │ 01gqr4vr487bytsf10ktfmheg4 │ 1 │ │ 01gqr4vr4dfcfk80m2yp6j866z │ 2 │ │ 01gqr4vrjxg0yex9jr0f100v1c │ 3 │ └────────────────────────────┴──────┘ */ ``` -------------------------------- ### Use sqlite-ulid in Deno Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Import the sqlite-ulid module from deno.land/x and load the extension into a Deno SQLite3 database to retrieve ULID version information. ```typescript 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); ``` -------------------------------- ### Load sqlite-ulid Extension into SQLite Connection in Python Source: https://github.com/asg017/sqlite-ulid/blob/main/python/sqlite_ulid/README.md Loads the sqlite-ulid extension into a given `sqlite3.Connection` object. This function internally calls `Connection.load_extension()` after enabling extension loading. ```python import sqlite_ulid import sqlite3 conn = sqlite3.connect(':memory:') # Enable extension loading conn.enable_load_extension(True) # Load the sqlite-ulid extension sqlite_ulid.load(conn) # Disable extension loading for security conn.enable_load_extension(False) # Verify by querying version and generating a ULID version, ulid = conn.execute('select ulid_version(), ulid()').fetchone() print(f"ULID Version: {version}") print(f"Generated ULID: {ulid}") conn.close() ``` -------------------------------- ### Generate Prefixed ULIDs with `ulid_with_prefix()` Source: https://context7.com/asg017/sqlite-ulid/llms.txt The `ulid_with_prefix()` SQL function generates a ULID prefixed with a custom string, separated by an underscore. This is useful for creating entity-specific identifiers, similar to Stripe's ID system, allowing for easier identification and categorization of records directly within the ID itself. ```sql -- Generate prefixed ULIDs for different entity types SELECT ulid_with_prefix('customer'); -- 'customer_01gqr5j1ebk31wv30wgp8ebehj' SELECT ulid_with_prefix('invoice'); -- 'invoice_01gqre8x2efy50mz28b238dh2a' SELECT ulid_with_prefix('order'); -- 'order_01gqr5q35n68jk0sycy1ntr083' SELECT ulid_with_prefix('card'); -- 'card_01gqre9c6az7835tdk00wc0gfp' -- Use in table schemas CREATE TABLE events( id TEXT PRIMARY KEY, data JSON ); INSERT INTO events SELECT ulid_with_prefix('event') AS id, '{"type": "user_signup"}'; INSERT INTO events SELECT ulid_with_prefix('event') AS id, '{"type": "purchase"}'; SELECT * FROM events; /* ┌─────────────────────────────────────┬──────────────────────────┐ │ id │ data │ ├─────────────────────────────────────┼──────────────────────────┤ │ event_01gqre8x2efy50mz28b238dh2a │ {"type": "user_signup"} │ │ event_01gqre8xc36045w1qekg404g0m │ {"type": "purchase"} │ └─────────────────────────────────────┴──────────────────────────┘ */ ``` -------------------------------- ### Extract Datetime from ULID in SQLite Source: https://github.com/asg017/sqlite-ulid/blob/main/README.md Illustrates how to extract the timestamp component from a ULID using `ulid_datetime()` and then use it with standard SQLite date/time functions like `unixepoch()` and `strftime()`. ```sql select ulid_datetime(ulid()); -- '2023-01-26 23:07:36.508' select unixepoch(ulid_datetime(ulid())); -- 1674774499 select strftime('%Y-%m-%d', ulid_datetime(ulid())); -- '2023-01-26'' ``` -------------------------------- ### ulid_with_prefix(prefix) - Generate ULID with Prefix Source: https://github.com/asg017/sqlite-ulid/blob/main/docs.md Generates a new ULID string with the given `prefix`, separated by an underscore. Useful for distinguishing different types of ULIDs. ```APIDOC ## ulid_with_prefix(prefix) ### Description Generates a new ULID string prefixed with the provided string and an underscore. ### Method SQL Function ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **prefix** (TEXT) - Required - The prefix string for the ULID. ### Request Example ```sql select ulid_with_prefix('invoice'); select ulid_with_prefix('cus'); ``` ### Response #### Success Response (200) * **prefixed_ulid** (TEXT) - The generated ULID string with the specified prefix. #### Response Example ```json { "prefixed_ulid": "invoice_01gqre8x2efy50mz28b238dh2a" } ``` ```