Skip to content

Commit

Permalink
Fix tabs in README
Browse files Browse the repository at this point in the history
  • Loading branch information
dankochetov committed Jan 27, 2023
1 parent 457b6ed commit 48c5197
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
1 change: 0 additions & 1 deletion .markdownlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ first-line-h1: false
line-length: false
MD010:
spaces_per_tab: 2
code_blocks: false
62 changes: 31 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,79 +55,79 @@ import { sql } from 'drizzle-orm/sql';
import { Pool } from 'pg';

export const users = pgTable('users', {
id: serial('id').primaryKey(),
fullName: text('full_name').notNull(),
phone: varchar('phone', { length: 20 }).notNull(),
role: text<'user' | 'admin'>('role').default('user').notNull(),
cityId: integer('city_id').references(() => cities.id),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
id: serial('id').primaryKey(),
fullName: text('full_name').notNull(),
phone: varchar('phone', { length: 20 }).notNull(),
role: text<'user' | 'admin'>('role').default('user').notNull(),
cityId: integer('city_id').references(() => cities.id),
createdAt: timestamp('created_at').defaultNow().notNull(),
updatedAt: timestamp('updated_at').defaultNow().notNull(),
});

export type User = InferModel<typeof users>;
export type NewUser = InferModel<typeof users, 'insert'>;

export const cities = pgTable('cities', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
id: serial('id').primaryKey(),
name: text('name').notNull(),
});

export type City = InferModel<typeof cities>;
export type NewCity = InferModel<typeof cities, 'insert'>;

const pool = new Pool({
connectionString: 'postgres://user:password@host:port/db',
connectionString: 'postgres://user:password@host:port/db',
});

const db = drizzle(pool);

// Insert
const newUser: NewUser = {
fullName: 'John Doe',
phone: '+123456789',
fullName: 'John Doe',
phone: '+123456789',
};
const insertedUsers /* : User */ = await db.insert(users).values(newUser).returning();
const insertedUser = insertedUsers[0]!;

const newCity: NewCity = {
name: 'New York',
name: 'New York',
};
const insertedCities /* : City */ = await db.insert(cities).values(newCity).returning();
const insertedCity = insertedCities[0]!;

// Update
const updateResult /* : { updated: Date }[] */ = await db.update(users)
.set({ cityId: insertedCity.id, updatedAt: new Date() })
.where(eq(users.id, insertedUser.id))
.returning({ updated: users.updatedAt });
.set({ cityId: insertedCity.id, updatedAt: new Date() })
.where(eq(users.id, insertedUser.id))
.returning({ updated: users.updatedAt });

// Select
const allUsers /* : User[] */ = await db.select(users);

// Select custom fields
const upperCaseNames /* : { id: number; name: string }[] */ = await db.select(users)
.fields({
id: users.id,
name: sql`upper(${users.fullName})`.as<string>(),
});
.fields({
id: users.id,
name: sql`upper(${users.fullName})`.as<string>(),
});

// Joins
// You wouldn't BELIEVE how SMART the result type is! 😱
const allUsersWithCities = await db.select(users)
.fields({
user: {
id: users.id,
name: users.fullName,
},
cityId: cities.id,
cityName: cities.name,
})
.leftJoin(cities, eq(users.cityId, cities.id));
.fields({
user: {
id: users.id,
name: users.fullName,
},
cityId: cities.id,
cityName: cities.name,
})
.leftJoin(cities, eq(users.cityId, cities.id));

// Delete
const deletedNames /* : { name: string }[] */ = await db.delete(users)
.where(eq(users.id, insertedUser.id))
.returning({ name: users.fullName });
.where(eq(users.id, insertedUser.id))
.returning({ name: users.fullName });
```

See [PostgreSQL docs](./drizzle-orm/src/pg-core/README.md) for full API reference.

0 comments on commit 48c5197

Please sign in to comment.