Registry / database / cosmotype

cosmotype

JSON →
library1.0.10jsnpmunverified

Cosmotype is a type-driven database framework designed to offer a unified API for interacting with various database systems, abstracting away driver-specific complexities. It is built with TypeScript, providing comprehensive type support for schema definitions and operations, which helps in preventing common data-related errors at compile time. Currently at version 1.0.10, the project appears to be actively maintained, though no explicit release cadence is specified. A key differentiator is its ability to perform advanced database operations, akin to full SQL capabilities, even when interfacing with NoSQL databases like MongoDB or key-value stores like LevelDB, all through a consistent JavaScript/TypeScript API. This makes it highly versatile and extensible, supporting simultaneous access to different databases and even enabling low-code operations in browser environments.

npm install cosmotype
INSTALL
IMPORT
SIG · COSMOTYPE
C
cosmotype
databasejavascriptv1.0.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Database
import { Database } from 'cosmotype'
import Database from 'cosmotype'
The primary class for database interaction is a named export.
EntitySchemaDefinition
import type { EntitySchemaDefinition } from 'cosmotype'
import { EntitySchemaDefinition } from 'cosmotype'
This is a type definition for the schema used in `database.extend()`. Always import types using `import type` for clarity and better tree-shaking.
DriverOptions
import type { DriverOptions } from 'cosmotype'
const DriverOptions = require('cosmotype').DriverOptions
Type definition for database connection options. Cosmotype is primarily designed for modern ESM TypeScript environments.

Demonstrates connecting to a MySQL database, defining a user schema, and performing create and get operations with type safety.

import { Database } from 'cosmotype'; import type { EntitySchemaDefinition } from 'cosmotype'; interface User { id: number; name: string; age: number; money: number; } async function runCosmotypeExample() { const database = new Database(); // Ensure you have @cosmotype/driver-mysql installed and a MySQL server running // For demonstration, we'll use a placeholder for environment variables. try { await database.connect('mysql', { host: process.env.DB_HOST ?? 'localhost', port: parseInt(process.env.DB_PORT ?? '3306'), user: process.env.DB_USER ?? 'root', password: process.env.DB_PASSWORD ?? 'password', database: process.env.DB_NAME ?? 'cosmotype_db', }); console.log('Connected to MySQL database.'); const userSchema: EntitySchemaDefinition = { id: 'number', name: 'string', age: 'number', money: { type: 'number', initial: 100 } }; database.extend<User>('user', userSchema, { primary: 'id', autoInc: true, }); console.log('User schema extended.'); const newUser = await database.create<User>('user', { name: 'Alice', age: 30, }); console.log('Created new user:', newUser); const retrievedUser = await database.get<User>('user', { id: newUser.id }); console.log('Retrieved user:', retrievedUser); } catch (error) { console.error('Cosmotype example failed:', error); } finally { // In a real application, you'd likely disconnect or keep the connection alive. // await database.disconnect(); console.log('Example finished.'); } } runCosmotypeExample();
Debug
Known issues
gotchaCosmotype relies on separate driver packages for database connectivity. Forgetting to install the specific driver (e.g., `@cosmotype/driver-mysql` for MySQL) will result in runtime errors.
fix
Ensure `npm install @cosmotype/driver-<database-name>` is run for each database type your application connects to.
affects: >=1.0.0
breakingMajor version updates of Cosmotype or its drivers may introduce changes to the API, particularly around schema definition syntax or driver options. Always review release notes for breaking changes.
fix
Consult the official migration guides and documentation when upgrading to a new major version.
affects: >=1.0.0
gotchaWhile Cosmotype aims to provide a unified API, underlying database-specific behaviors and limitations (e.g., transaction support, advanced indexing, specific data types) may still manifest. Developers should be aware of the chosen driver's capabilities.
fix
Familiarize yourself with the native capabilities and constraints of your specific database system, even when using Cosmotype's abstraction layer.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Driver 'mysql' not found.
The required database driver package (e.g., `@cosmotype/driver-mysql`) was not installed or is not resolvable.
fix
Install the correct driver package: `npm install @cosmotype/driver-mysql` (replace 'mysql' with your desired driver).
Error: Connect failed: Access denied for user '...'@'localhost' (using password: YES)
Incorrect database connection credentials (username, password) or insufficient user permissions.
fix
Verify your database host, port, user, password, and database name. Ensure the user has necessary permissions on the database.
Error: Duplicate entry '1' for key 'user.PRIMARY'
Attempting to create a record with a primary key value that already exists in the table without `autoInc` enabled or during an `upsert` operation that isn't configured to update.
fix
Ensure `autoInc` is set correctly for primary keys if you intend for them to be generated automatically, or handle unique constraint violations explicitly. Use `upsert` if you intend to create or update.
Upgrade
Version history
1.0.10latest on npm
Audit
Dependencies
@cosmotype/driver-mysqloptionalRequired for MySQL, MariaDB database connectivity.
@cosmotype/driver-mongooptionalRequired for MongoDB database connectivity.
@cosmotype/driver-sqliteoptionalRequired for SQLite database connectivity.
@cosmotype/driver-leveloptionalRequired for LevelDB database connectivity.
@cosmotype/driver-memoryoptionalRequired for in-memory database functionality, useful for testing.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
cosmotype — npm install cosmotype · libregistry