NextUp Comedy Database Models
The `nc-db-new` package provides the foundational database models for the NextUp Comedy website and its associated services. Designed as an internal library, it centralizes the schema definitions, relationships, and often the data access logic for core entities such as Users, Shows, Venues, and Bookings specific to the comedy platform. Currently at version 1.6.7, this package ensures consistent data structures across various backend applications and microservices within the NextUp Comedy ecosystem. Its primary function is to abstract database interactions, enabling developers to work with TypeScript classes or interfaces that map directly to database tables. While its release cadence is tied to internal development cycles, it aims for stability in its minor and patch versions, with breaking changes typically confined to major version bumps. Key differentiators are its highly specialized domain model and its deep integration into the NextUp Comedy tech stack, providing a single source of truth for database schema.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'create')
cause The `db` object or specific model (e.g., `User`) was not properly initialized or imported before use, or the underlying ORM instance is not connected.fixEnsure `db` and models are correctly imported and initialized. Verify that the database connection is established and the ORM instance is available where models are being used. -
Property 'someField' does not exist on type 'User'.
cause Attempting to access a field that is not defined in the `User` model's TypeScript interface or class, or a field that was not explicitly selected in a query.fixUpdate your model definitions in `nc-db-new` if `someField` is new. If it's an existing field, ensure your query explicitly selects it, or that the return type of your query includes it. -
Error: SQLITE_CONSTRAINT: NOT NULL constraint failed: users.email
cause Attempted to insert or update a database record without providing a value for a column that is marked as `NOT NULL` in the database schema.fixReview your model's create/update data to ensure all required fields (especially `NOT NULL` columns) are provided with valid values.
Warnings
- gotcha Schema drift can occur if database migrations are not run consistently with model changes. Always ensure your database schema matches the model definitions in the `nc-db-new` package.
- breaking Major version updates (e.g., from v1 to v2) are likely to introduce breaking changes in model definitions, field types, or relationships. Always consult the changelog before upgrading.
- gotcha The models typically rely on an underlying ORM or database connection instance (e.g., `db`). Incorrect initialization or configuration of this instance will lead to runtime errors when performing database operations.
- gotcha Performance issues like N+1 queries can arise from inefficient data fetching patterns, especially when dealing with related entities. Eager loading or careful query construction is often required.
Install
-
npm install nc-db-new -
yarn add nc-db-new -
pnpm add nc-db-new
Imports
- User
const { User } = require('nc-db-new');import { User } from 'nc-db-new'; - Show
import { Show } from 'nc-db-new'; - UserAttributes
import { UserAttributes } from 'nc-db-new';import type { UserAttributes } from 'nc-db-new';
Quickstart
import { User, Show, db } from 'nc-db-new';
async function main() {
// Assume 'db' is an initialized ORM instance or connection pool
// For demonstration, we'll mock 'db.sync' and 'User.create'
console.log('Synchronizing database models...');
// await db.sync(); // In a real app, this might create/update tables
console.log('Database models synchronized.');
try {
const newUser = await User.create({
username: 'comedyfan',
email: `fan_${Date.now()}@example.com`,
passwordHash: 'hashedpassword123',
is_admin: false
});
console.log('Created new user:', newUser.toJSON());
const allUsers = await User.findAll();
console.log('Total users:', allUsers.length);
const firstShow = await Show.findOne({
where: { title: 'The Laughter Gala' }
});
if (firstShow) {
console.log('Found show:', firstShow.toJSON());
} else {
console.log('No show found with title "The Laughter Gala", creating one...');
const createdShow = await Show.create({
title: 'The Laughter Gala',
date: new Date('2026-07-20T19:00:00Z'),
venueId: 'venue-abc-123' // Assume a venue ID exists or is created elsewhere
});
console.log('Created new show:', createdShow.toJSON());
}
} catch (error) {
console.error('Database operation failed:', error);
}
}
// Simulate an initialized 'db' object and model methods for the quickstart
const mockDb = {
sync: async () => Promise.resolve(),
};
const mockUser = {
create: async (data: any) => ({ ...data, id: Math.random().toString(36).substring(7), toJSON: () => ({ ...data, id: 'mock-user-id' }) }),
findAll: async () => ([{ username: 'existing', email: 'existing@example.com', toJSON: () => ({ username: 'existing', email: 'existing@example.com' }) }]),
toJSON: () => ({}) // For compatibility
};
const mockShow = {
findOne: async (options: any) => options.where.title === 'The Laughter Gala' ? null : ({ title: 'Existing Comedy Night', date: new Date(), toJSON: () => ({ title: 'Existing Comedy Night' }) }),
create: async (data: any) => ({ ...data, id: Math.random().toString(36).substring(7), toJSON: () => ({ ...data, id: 'mock-show-id' }) })
};
// Overwrite for quickstart execution
// @ts-ignore
(globalThis as any).db = mockDb;
// @ts-ignore
(globalThis as any).User = mockUser;
// @ts-ignore
(globalThis as any).Show = mockShow;
main();