drizzle-orm 0.10.47 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -55
- package/builders/aggregators/abstractAggregator.js +1 -1
- package/builders/aggregators/selectAggregator.d.ts +2 -0
- package/builders/aggregators/selectAggregator.js +24 -3
- package/builders/highLvlBuilders/abstractRequestBuilder.d.ts +2 -3
- package/builders/highLvlBuilders/joins/joinBuilderResponse.d.ts +8 -0
- package/builders/highLvlBuilders/joins/joinBuilderResponse.js +16 -0
- package/builders/highLvlBuilders/joins/proxies/joinProxies.d.ts +16 -0
- package/builders/highLvlBuilders/joins/proxies/joinProxies.js +47 -0
- package/builders/highLvlBuilders/joins/selectJoinBuilder.d.ts +43 -0
- package/builders/highLvlBuilders/joins/selectJoinBuilder.js +109 -0
- package/builders/highLvlBuilders/selectRequestBuilder.d.ts +28 -12
- package/builders/highLvlBuilders/selectRequestBuilder.js +45 -6
- package/builders/index.d.ts +3 -0
- package/builders/index.js +15 -1
- package/builders/lowLvlBuilders/selects/selectFrom.d.ts +3 -0
- package/builders/lowLvlBuilders/selects/selectFrom.js +1 -0
- package/builders/lowLvlBuilders/selects/selectJoined.d.ts +3 -0
- package/builders/lowLvlBuilders/selects/selectJoined.js +4 -0
- package/builders/requestBuilders/where/and.d.ts +1 -4
- package/builders/requestBuilders/where/and.js +2 -2
- package/builders/requestBuilders/where/const.d.ts +1 -4
- package/builders/requestBuilders/where/const.js +1 -1
- package/builders/requestBuilders/where/constArray.d.ts +1 -4
- package/builders/requestBuilders/where/constArray.js +1 -1
- package/builders/requestBuilders/where/eqWhere.d.ts +1 -1
- package/builders/requestBuilders/where/eqWhere.js +3 -3
- package/builders/requestBuilders/where/greater.d.ts +1 -4
- package/builders/requestBuilders/where/greater.js +3 -3
- package/builders/requestBuilders/where/greaterEq.d.ts +1 -4
- package/builders/requestBuilders/where/greaterEq.js +3 -3
- package/builders/requestBuilders/where/in.d.ts +1 -4
- package/builders/requestBuilders/where/in.js +3 -3
- package/builders/requestBuilders/where/isNotNull.d.ts +1 -4
- package/builders/requestBuilders/where/isNotNull.js +2 -2
- package/builders/requestBuilders/where/isNull.d.ts +1 -4
- package/builders/requestBuilders/where/isNull.js +2 -2
- package/builders/requestBuilders/where/less.d.ts +1 -4
- package/builders/requestBuilders/where/less.js +3 -3
- package/builders/requestBuilders/where/lessEq.d.ts +1 -4
- package/builders/requestBuilders/where/lessEq.js +3 -3
- package/builders/requestBuilders/where/like.d.ts +1 -4
- package/builders/requestBuilders/where/like.js +3 -3
- package/builders/requestBuilders/where/notEqWhere.d.ts +1 -4
- package/builders/requestBuilders/where/notEqWhere.js +3 -3
- package/builders/requestBuilders/where/or.d.ts +1 -4
- package/builders/requestBuilders/where/or.js +2 -2
- package/builders/requestBuilders/where/rawWhere.d.ts +1 -4
- package/builders/requestBuilders/where/rawWhere.js +1 -1
- package/builders/requestBuilders/where/static.d.ts +1 -0
- package/builders/requestBuilders/where/static.js +3 -1
- package/builders/requestBuilders/where/var.d.ts +1 -4
- package/builders/requestBuilders/where/var.js +2 -2
- package/builders/requestBuilders/where/where.d.ts +1 -4
- package/docs/cases/simple_join.js +5 -5
- package/docs/tables/userGroupsTable.d.ts +1 -0
- package/docs/tables/userGroupsTable.js +1 -0
- package/docs/tables/usersTable.js +1 -1
- package/mappers/responseMapper.d.ts +3 -4
- package/mappers/responseMapper.js +2 -1
- package/package.json +2 -2
- package/tables/abstractTable.d.ts +3 -4
- package/tables/abstractTable.js +1 -0
- package/tables/inferTypes.d.ts +6 -2
- package/test.d.ts +1 -0
- package/test.js +165 -132
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { QueryResult } from 'pg';
|
|
2
|
+
import { AbstractTable } from '..';
|
|
2
3
|
import { AbstractColumn } from '../columns/column';
|
|
3
4
|
import ColumnType from '../columns/types/columnType';
|
|
4
|
-
import { ExtractModel } from '../tables/inferTypes';
|
|
5
|
+
import { ExtractModel, PartialFor } from '../tables/inferTypes';
|
|
5
6
|
export default class QueryResponseMapper {
|
|
6
7
|
static map: <ITable>(mappedServiceToDb: { [name in keyof ExtractModel<ITable>]: AbstractColumn<ColumnType<{}>, true, false, any>; }, queryResult: QueryResult<any>, joinId?: number | undefined) => ExtractModel<ITable>[];
|
|
7
|
-
static partialMap: <T
|
|
8
|
-
[name: string]: AbstractColumn<ColumnType<{}>, true, false, any>;
|
|
9
|
-
}, queryResult: QueryResult<any>, joinId?: number | undefined) => ExtractModel<T>[];
|
|
8
|
+
static partialMap: <T extends AbstractTable<T>>(partial: PartialFor<T>, queryResult: QueryResult<any>, joinId?: number | undefined) => ExtractModel<T>[];
|
|
10
9
|
}
|
|
@@ -11,7 +11,8 @@ QueryResponseMapper.map = (mappedServiceToDb, queryResult, joinId) => {
|
|
|
11
11
|
const mappedRow = {};
|
|
12
12
|
Object.keys(mappedServiceToDb).forEach((key) => {
|
|
13
13
|
const column = mappedServiceToDb[key];
|
|
14
|
-
const alias = `${column.
|
|
14
|
+
const alias = `${column.getParentName()}_${column.getColumnName()}`;
|
|
15
|
+
// const alias = `${column.getAlias()}${joinId ? `_${joinId}` : ''}`;
|
|
15
16
|
const value = column.getColumnType().selectStrategy(row[alias]);
|
|
16
17
|
mappedRow[key] = value === null ? undefined : value;
|
|
17
18
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "drizzle-orm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -40,5 +40,5 @@
|
|
|
40
40
|
"lint": "eslint ./src --ext .ts",
|
|
41
41
|
"run": "ts-node src/tables/newAbstractTable.ts"
|
|
42
42
|
},
|
|
43
|
-
"readme": "# DrizzleORM\n\n**DrizzleORM** is an ORM framework for \n[TypeScript](https://www.typescriptlang.org/).\nIt offers you several levels of Database communication:\n* Typesafe Table View approach \n* Typesafe Query Builder\n* Simple SQL query execution\n\nDrizzle ORM is highly influenced by [Exposed](https://github.com/JetBrains/Exposed) and Jetbrains development methodology\n\n## Supported Databases\n\n* PostgreSQL\n\n## Links\n\nIn Progress\n\n## Installing\n\n```bash\nnpm install drizzle-orm drizzle-kit\n```\n#### **In Progress**\n```bash\nyarn add drizzle-orm drizzle-kit\nbower install drizzle-orm drizzle-kit\n```\n\n## Connecting to database\n\n```tsx\nimport { DbConnector } from \"drizzle-orm\";\n\n// connect via postgresql connection url\nconst db = await new DbConnector()\n\t.connectionString(\"postgres://user:password@host:port/db\")\n\t.connect();\n\n// or by params\nconst db = await new DbConnector()\n\t.params({\n\t\thost: '0.0.0.0',\n\t\tport: 5432,\n\t\tuser: 'user',\n\t\tpassword: 'password',\n\t\tdb: 'optional_db_name'\n\t}).connect();\n```\n## Project structure\n- tables folder\n- migrations folder\n\n## Create tables\n### Users Table\n---\n```typescript\n\nexport const rolesEnum = createEnum({ alias: 'test-enum', values: ['user', 'guest', 'admin'] });\n\nexport default class UsersTable extends AbstractTable<UsersTable> {\n public id = this.serial('id').primaryKey();\n public fullName = this.text('full_name');\n\n public phone = this.varchar('phone', { size: 256 });\n public media = this.jsonb<string[]>('media');\n public decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();\n public bigIntField = this.bigint('test1', 'max_bytes_53');\n public role = this.type(rolesEnum, 'name_in_table').notNull();\n\n public createdAt = this.timestamp('created_at').notNull();\n\n public createdAtWithTimezone = this.timestamptz('created_at_time_zone');\n\n public updatedAt = this.timestamp('updated_at').defaultValue(Defaults.CURRENT_TIMESTAMP);\n public isArchived = this.bool('is_archived').defaultValue(false);\n\n public phoneFullNameIndex = this.index([this.phone, this.fullName]);\n public phoneIndex = this.uniqueIndex(this.phone);\n\n public tableName(): string {\n return 'users';\n }\n}\n```\n### Cities Table\n---\n```typescript\ninterface CityMeta {\n population: number,\n connection: string,\n}\n\nexport default class CitiesTable extends AbstractTable<CitiesTable> {\n public id = this.serial('id').primaryKey();\n\n public foundationDate = this.timestamp('name').notNull();\n public location = this.varchar('page', { size: 256 });\n\n public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'CASCADE' });\n\n public metadata = this.jsonb<CityMeta>('metadata');\n\n public tableName(): string {\n return 'cities';\n }\n}\n```\n### User Groups Table\n---\n```typescript\nexport default class UserGroupsTable extends AbstractTable<UserGroupsTable> {\n public id = this.serial('id').primaryKey();\n\n public name = this.varchar('name');\n public description = this.varchar('description');\n\n public tableName(): string {\n return 'user_groups';\n }\n}\n```\n### User to User Groups Table\n---\n#### Many to many connection between Users and User Groups\n```typescript\nexport default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {\n public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, { onDelete: 'CASCADE' });\n public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'CASCADE' });\n\n public manyToManyIndex = this.index([this.groupId, this.userId]);\n\n public tableName(): string {\n return 'users_to_user_groups';\n }\n}\n```\n\n## CRUD\n### **SELECT**\n---\n```typescript\nconst db = await new DbConnector()\n .connectionString('postgresql://postgres@127.0.0.1/drizzle')\n .connect();\n\nconst usersTable = new UsersTable(db);\n\n// select all\nconst allSelect = await usersTable.select().all();\n\n// select first\nconst firstSelect = await usersTable.select().findOne();\n```\n#### **Sorting and Filtering**\n---\n##### Select all records from `Users` where phone is `\"hello\"`\n```typescript\nconst eqSelect = await usersTable.select().where(\n eq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where **both** phone is `\"hello\"` **and** phone is `\"hello\"`\n```typescript\nconst andSelect = await usersTable.select().where(\n and([\n eq(usersTable.phone, 'hello'),\n eq(usersTable.phone, 'hello')\n ]),\n).all();\n```\n##### Select all records from `Users` where **either** phone is `\"hello\"` **or** phone is `\"hello\"`\n```typescript\nconst orSelect = await usersTable.select().where(\n or([eq(usersTable.phone, 'hello')]),\n).all();\n```\n##### Select all records from `Users` using **LIMIT** and **OFFSET**\n```typescript\nconst limitOffsetSelect = await usersTable.select().limit(10).offset(10).all();\n```\n##### Select all records from `Users` where `phone` contains `\"hello\"`\n```typescript\nconst likeSelect = await usersTable.select().where(\n like(usersTable.phone, '%hello%')\n).all();\n```\n##### Select all records from `Users` where `phone` equals to some of values from array\n```typescript\nconst inArraySelect = usersTable.select().where(\n inArray(usersTable.phone, ['hello'])\n).all();\n```\n##### Select all records from `Users` where `phone` greater(**>**) than `\"hello\"`\n```typescript\nconst greaterSelect = usersTable.select().where(\n greater(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` less(**<**) than `\"hello\"`\n```typescript\nconst lessSelect = usersTable.select().where(\n less(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` greater or equals(**>=**) than `\"hello\"`\n```typescript\nconst greaterEqSelect = usersTable.select().where(\n greaterEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` less or equals(**<=**) \n```typescript\nconst lessEqSelect = usersTable.select().where(\n lessEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` is **NULL**\n```typescript\nconst isNullSelect = usersTable.select().where(\n isNull(usersTable.phone)\n).all();\n```\n##### Select all records from `Users` where `phone` not equals to `\"hello\"`\n```typescript\nconst notEqSelect = usersTable.select().where(\n notEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` ordered by `phone` in ascending order\n```typescript\nconst ordered = await usersTable.select().orderBy((table) => table.phone, Order.ASC).all();\n```\n#### **Partial Selecting**\n ```typescript\n const partialSelect = await usersTable.select({\n mappedId: usersTable.id,\n mappedPhone: usersTable.phone,\n }).all();\n\n // Usage\n const { mappedId, mappedPhone } = partialSelect;\n ```\n\n\n### **Update**\n---\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"`\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .execute();\n```\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .all();\n```\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .findOne();\n```\n\n### **Delete**\n##### Delete `user` where phone is `\"hello\"`\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .execute();\n```\n##### Delete `user` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .all();\n```\n##### Delete `user` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .findOne();\n```\n\n### **Insert**\n##### Insert `user` with required fields\n```typescript\nawait usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).execute();\n```\n##### Insert `user` with required fields and get all rows as array\n```typescript\nconst user = await usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).all();\n```\n##### Insert `user` with required fields and get inserted entity\n```typescript\nconst user = await usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).findOne();\n```\n##### Insert many `users` with required fields and get all inserted entities\n```typescript\nconst users = await usersTable.insertMany([{\n test: 1,\n createdAt: new Date(),\n }, {\n test: 2,\n createdAt: new Date(),\n }]).all();\n```\n##### Insert many `users` with required fields and get all inserted entities. If such user already exists - update `phone` field\n```typescript\nawait usersTable.insertMany([{\n test: 1,\n createdAt: new Date(),\n }, {\n test: 2,\n createdAt: new Date(),\n }])\n .onConflict(\n (table) => table.phoneIndex,\n { phone: 'confilctUpdate' },\n ).all();\n```\n\n## Joins\n### Join One-To-Many Tables\n##### Join Cities with Users and map to city object with full user\n```typescript\nconst usersTable = new UsersTable(db);\nconst citiesTable = new CitiesTable(db);\n\n const userWithCities = await citiesTable.select()\n .where(eq(citiesTable.id, 1))\n .leftJoin(UsersTable,\n (city) => city.userId,\n (users) => users.id)\n .execute();\n\nconst citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));\n```\n\n### Join Many-To-Many Tables\n##### Join User Groups with Users, using many-to-many table and map response to get user object with groups array\n```typescript\n const usersWithUserGroups = await usersToUserGroupsTable.select()\n .where(eq(userGroupsTable.id, 1))\n .leftJoin(UsersTable,\n (userToGroup) => userToGroup.userId,\n (users) => users.id)\n .leftJoin(UsersToUserGroupsTable, UserGroupsTable,\n (userToGroup) => userToGroup.groupId,\n (users) => users.id)\n .execute();\n\n const userGroupWithUsers = usersWithUserGroups.group({\n one: (_, dbUser, dbUserGroup) => dbUser!,\n many: (_, dbUser, dbUserGroup) => dbUserGroup!,\n });\n\n const userWithGroups: ExtractModel<UsersTable> & { groups: ExtractModel<UserGroupsTable>[] } = {\n ...userGroupWithUsers.one,\n groups: userGroupWithUsers.many,\n };\n```\n##### Join User Groups with Users, using many-to-many table and map response to get user group object with users array\n```typescript\n const usersWithUserGroups = await usersToUserGroupsTable.select()\n .where(eq(userGroupsTable.id, 1))\n .leftJoin(UsersTable,\n (userToGroup) => userToGroup.userId,\n (users) => users.id)\n .leftJoin(UsersToUserGroupsTable, UserGroupsTable,\n (userToGroup) => userToGroup.groupId,\n (users) => users.id)\n .execute();\n\n const userGroupWithUsers = usersWithUserGroups.group({\n one: (_, dbUser, dbUserGroup) => dbUserGroup!,\n many: (_, dbUser, dbUserGroup) => dbUser!,\n });\n\n const userWithGroups: ExtractModel<UserGroupsTable> & { users: ExtractModel<UsersTable>[] } = {\n ...userGroupWithUsers.one,\n users: userGroupWithUsers.many,\n };\n```\n### Join using partial field select\n##### Join Cities with Users getting only needed fields form request\n```typescript\nawait citiesTable.select({\n id: citiesTable.id,\n userId: citiesTable.userId,\n })\n .where(eq(citiesTable.id, 1))\n .leftJoin(UsersTable,\n (city) => city.userId,\n (users) => users.id,\n {\n id: usersTable.id,\n })\n .execute();\n\nconst citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));\n```\n\n\n## Migrations\n#### To run migrations generated by drizzle-kit you could use `Migrator` class\n##### Provide drizzle-kit config path\n```typescript\nawait drizzle.migrator(db).migrate('src/drizzle.config.yaml');\n```\n##### Another possibility is to provide object with path to folder with migrations\n```typescript\nawait drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });\n```\n\n\n## Raw query usage\n#### If you have some complex queries to execute and drizzle-orm can't handle them yet, then you could use `rawQuery` execution\n\n\n##### Execute custom raw query\n```typescript\nconst res: QueryResult<any> = await db.session().execute('SELECT * FROM users WHERE user.id = $1', [1]);\n```"
|
|
43
|
+
"readme": "# DrizzleORM\n\n**DrizzleORM** is an ORM framework for \n[TypeScript](https://www.typescriptlang.org/).\nIt offers you several levels of Database communication:\n* Typesafe Table View approach \n* Typesafe Query Builder\n* Simple SQL query execution\n\nDrizzle ORM is highly influenced by [Exposed](https://github.com/JetBrains/Exposed) and Jetbrains development methodology\n\n## Supported Databases\n\n* PostgreSQL\n\n## Links\n\nIn Progress\n\n## Installing\n\n```bash\nnpm install drizzle-orm drizzle-kit\n```\n#### **In Progress**\n```bash\nyarn add drizzle-orm drizzle-kit\nbower install drizzle-orm drizzle-kit\n```\n\n## Connecting to database\n\n```tsx\nimport { DbConnector } from \"drizzle-orm\";\n\n// connect via postgresql connection url\nconst db = await new DbConnector()\n\t.connectionString(\"postgres://user:password@host:port/db\")\n\t.connect();\n\n// or by params\nconst db = await new DbConnector()\n\t.params({\n\t\thost: '0.0.0.0',\n\t\tport: 5432,\n\t\tuser: 'user',\n\t\tpassword: 'password',\n\t\tdb: 'optional_db_name'\n\t}).connect();\n```\n## Project structure\n- tables folder\n- migrations folder\n\n## Create tables\n### Users Table\n---\n```typescript\n\nexport const rolesEnum = createEnum({ alias: 'test-enum', values: ['user', 'guest', 'admin'] });\n\nexport default class UsersTable extends AbstractTable<UsersTable> {\n public id = this.serial('id').primaryKey();\n public fullName = this.text('full_name');\n\n public phone = this.varchar('phone', { size: 256 });\n public media = this.jsonb<string[]>('media');\n public decimalField = this.decimal('test', { precision: 100, scale: 2 }).notNull();\n public bigIntField = this.bigint('test1', 'max_bytes_53');\n public role = this.type(rolesEnum, 'name_in_table').notNull();\n\n public createdAt = this.timestamp('created_at').notNull();\n\n public createdAtWithTimezone = this.timestamptz('created_at_time_zone');\n\n public updatedAt = this.timestamp('updated_at').defaultValue(Defaults.CURRENT_TIMESTAMP);\n public isArchived = this.bool('is_archived').defaultValue(false);\n\n public phoneFullNameIndex = this.index([this.phone, this.fullName]);\n public phoneIndex = this.uniqueIndex(this.phone);\n\n public tableName(): string {\n return 'users';\n }\n}\n```\n### Cities Table\n---\n```typescript\ninterface CityMeta {\n population: number,\n connection: string,\n}\n\nexport default class CitiesTable extends AbstractTable<CitiesTable> {\n public id = this.serial('id').primaryKey();\n\n public foundationDate = this.timestamp('name').notNull();\n public location = this.varchar('page', { size: 256 });\n\n public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onUpdate: 'CASCADE' });\n\n public metadata = this.jsonb<CityMeta>('metadata');\n\n public tableName(): string {\n return 'cities';\n }\n}\n```\n### User Groups Table\n---\n```typescript\nexport default class UserGroupsTable extends AbstractTable<UserGroupsTable> {\n public id = this.serial('id').primaryKey();\n\n public name = this.varchar('name');\n public description = this.varchar('description');\n\n public tableName(): string {\n return 'user_groups';\n }\n}\n```\n### User to User Groups Table\n---\n#### Many to many connection between Users and User Groups\n```typescript\nexport default class UsersToUserGroupsTable extends AbstractTable<UsersToUserGroupsTable> {\n public groupId = this.int('city_id').foreignKey(UserGroupsTable, (table) => table.id, { onDelete: 'CASCADE' });\n public userId = this.int('user_id').foreignKey(UsersTable, (table) => table.id, { onDelete: 'CASCADE' });\n\n public manyToManyIndex = this.index([this.groupId, this.userId]);\n\n public tableName(): string {\n return 'users_to_user_groups';\n }\n}\n```\n\n## CRUD\n### **SELECT**\n---\n```typescript\nconst db = await new DbConnector()\n .connectionString('postgresql://postgres@127.0.0.1/drizzle')\n .connect();\n\nconst usersTable = new UsersTable(db);\n\n// select all\nconst allSelect = await usersTable.select().all();\n\n// select first\nconst firstSelect = await usersTable.select().findOne();\n```\n#### **Sorting and Filtering**\n---\n##### Select all records from `Users` where phone is `\"hello\"`\n```typescript\nconst eqSelect = await usersTable.select().where(\n eq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where **both** phone is `\"hello\"` **and** phone is `\"hello\"`\n```typescript\nconst andSelect = await usersTable.select().where(\n and([\n eq(usersTable.phone, 'hello'),\n eq(usersTable.phone, 'hello')\n ]),\n).all();\n```\n##### Select all records from `Users` where **either** phone is `\"hello\"` **or** phone is `\"hello\"`\n```typescript\nconst orSelect = await usersTable.select().where(\n or([eq(usersTable.phone, 'hello')]),\n).all();\n```\n##### Select all records from `Users` using **LIMIT** and **OFFSET**\n```typescript\nconst limitOffsetSelect = await usersTable.select().limit(10).offset(10).all();\n```\n##### Select all records from `Users` where `phone` contains `\"hello\"`\n```typescript\nconst likeSelect = await usersTable.select().where(\n like(usersTable.phone, '%hello%')\n).all();\n```\n##### Select all records from `Users` where `phone` equals to some of values from array\n```typescript\nconst inArraySelect = usersTable.select().where(\n inArray(usersTable.phone, ['hello'])\n).all();\n```\n##### Select all records from `Users` where `phone` greater(**>**) than `\"hello\"`\n```typescript\nconst greaterSelect = usersTable.select().where(\n greater(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` less(**<**) than `\"hello\"`\n```typescript\nconst lessSelect = usersTable.select().where(\n less(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` greater or equals(**>=**) than `\"hello\"`\n```typescript\nconst greaterEqSelect = usersTable.select().where(\n greaterEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` less or equals(**<=**) \n```typescript\nconst lessEqSelect = usersTable.select().where(\n lessEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` where `phone` is **NULL**\n```typescript\nconst isNullSelect = usersTable.select().where(\n isNull(usersTable.phone)\n).all();\n```\n##### Select all records from `Users` where `phone` not equals to `\"hello\"`\n```typescript\nconst notEqSelect = usersTable.select().where(\n notEq(usersTable.phone, 'hello')\n).all();\n```\n##### Select all records from `Users` ordered by `phone` in ascending order\n```typescript\nconst ordered = await usersTable.select().orderBy((table) => table.phone, Order.ASC).all();\n```\n#### **Partial Selecting**\n ```typescript\n const partialSelect = await usersTable.select({\n mappedId: usersTable.id,\n mappedPhone: usersTable.phone,\n }).all();\n\n // Usage\n const { mappedId, mappedPhone } = partialSelect;\n ```\n\n\n### **Update**\n---\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"`\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .execute();\n```\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .all();\n```\n##### Update `fullName` to `newName` in `Users` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.update()\n .where(eq(usersTable.phone, 'hello'))\n .set({ fullName: 'newName' })\n .findOne();\n```\n\n### **Delete**\n##### Delete `user` where phone is `\"hello\"`\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .execute();\n```\n##### Delete `user` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .all();\n```\n##### Delete `user` where phone is `\"hello\"` returning updated `User` model\n```typescript\nawait usersTable.delete()\n .where(eq(usersTable.phone, 'hello'))\n .findOne();\n```\n\n### **Insert**\n##### Insert `user` with required fields\n```typescript\nawait usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).execute();\n```\n##### Insert `user` with required fields and get all rows as array\n```typescript\nconst user = await usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).all();\n```\n##### Insert `user` with required fields and get inserted entity\n```typescript\nconst user = await usersTable.insert({\n test: 1,\n createdAt: new Date(),\n}).findOne();\n```\n##### Insert many `users` with required fields and get all inserted entities\n```typescript\nconst users = await usersTable.insertMany([{\n test: 1,\n createdAt: new Date(),\n }, {\n test: 2,\n createdAt: new Date(),\n }]).all();\n```\n##### Insert many `users` with required fields and get all inserted entities. If such user already exists - update `phone` field\n```typescript\nawait usersTable.insertMany([{\n test: 1,\n createdAt: new Date(),\n }, {\n test: 2,\n createdAt: new Date(),\n }])\n .onConflict(\n (table) => table.phoneIndex,\n { phone: 'confilctUpdate' },\n ).all();\n```\n\n## Joins\n### Join One-To-Many Tables\n##### Join Cities with Users and map to city object with full user\n```typescript\nconst usersTable = new UsersTable(db);\nconst citiesTable = new CitiesTable(db);\n\nconst userWithCities = await citiesTable.select()\n .where(eq(citiesTable.id, 1))\n .leftJoin(usersTable, (cities, users) => onEq(cities.userId, users.id))\n .execute();\n\nconst citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));\n```\n\n### Join Many-To-Many Tables\n##### Join User Groups with Users, using many-to-many table\n```typescript\n const usersWithUserGroups = await usersToUserGroupsTable.select()\n .where(eq(userGroupsTable.id, 1))\n .leftJoin(usersTable, (usersToUserGroups, users) => onEq(usersToUserGroups.userId, users.id))\n .leftJoin(userGroupsTable, (usersToUserGroups, _users, userGroups) => onEq(usersToUserGroups.groupId, userGroups.id))\n .execute();\n```\n### Join using partial field select\n##### Join Cities with Users getting only needed fields form request\n```typescript\n await citiesTable.select({\n id: citiesTable.id,\n userId: citiesTable.userId,\n })\n .where(eq(citiesTable.id, 1))\n .leftJoin(usersTable, (cities, users) => onEq(cities.userId, users.id))\n .execute();\n\nconst citiesWithUserObject = userWithCities.map((city, user) => ({ ...city, user }));\n```\n### Another join examples with different callback ON statements\n```typescript\nawait citiesTable.select()\n .where(eq(citiesTable.location, 'q'))\n .leftJoin(usersTable, (cities, _users) => eq(cities.id, 13))\n .execute();\n// Join statement generated from query\n// LEFT JOIN users AS users_1\n// ON cities.\"id\"=$1\n// WHERE cities.\"page\"=$2\n//\n// Values: [13, 'q']\n\nawait citiesTable.select()\n .leftJoin(usersTable, (cities, _users) => and([\n eq(cities.id, 13), notEq(cities.id, 14),\n ]))\n .execute();\n// Join statement generated from query\n// LEFT JOIN users AS users_1\n// ON (cities.\"id\"=$1 and cities.\"id\"!=$2)\n//\n// Values: [13, 14]\n\nawait citiesTable.select()\n .where(eq(citiesTable.location, 'location'))\n .leftJoin(usersTable, (_cities, _users) => raw('<custom expression after ON statement>'))\n .execute();\n// Join statement generated from query\n// LEFT JOIN users AS users_1\n// ON <custom expression after ON statement>\n// WHERE cities.\"page\"=$1\n// \n// Values: ['location']\n```\n\n\n## Migrations\n#### To run migrations generated by drizzle-kit you could use `Migrator` class\n##### Provide drizzle-kit config path\n```typescript\nawait drizzle.migrator(db).migrate('src/drizzle.config.yaml');\n```\n##### Another possibility is to provide object with path to folder with migrations\n```typescript\nawait drizzle.migrator(db).migrate({ migrationFolder: 'drizzle' });\n```\n\n\n## Raw query usage\n#### If you have some complex queries to execute and drizzle-orm can't handle them yet, then you could use `rawQuery` execution\n\n\n##### Execute custom raw query\n```typescript\nconst res: QueryResult<any> = await db.session().execute('SELECT * FROM users WHERE user.id = $1', [1]);\n```"
|
|
44
44
|
}
|
|
@@ -17,12 +17,13 @@ import PgEnum from '../columns/types/pgEnum';
|
|
|
17
17
|
import DB from '../db/db';
|
|
18
18
|
import { AbstractColumn, Column } from '../columns/column';
|
|
19
19
|
import TableIndex from '../indexes/tableIndex';
|
|
20
|
-
import { ExtractModel } from './inferTypes';
|
|
21
20
|
import Enum, { ExtractEnumValues } from '../types/type';
|
|
22
21
|
import PgSmallInt from '../columns/types/pgSmallInt';
|
|
23
22
|
import PgSerial from '../columns/types/pgSerial';
|
|
24
23
|
import PgTimestamptz from '../columns/types/pgTimestamptz';
|
|
25
24
|
import PgBigSerial53, { PgBigSerial64 } from '../columns/types/pgBigSerial';
|
|
25
|
+
import { ExtractModel } from './inferTypes';
|
|
26
|
+
import { EmptyPartial } from '../builders/highLvlBuilders/joins/selectJoinBuilder';
|
|
26
27
|
export default abstract class AbstractTable<TTable extends AbstractTable<TTable>> {
|
|
27
28
|
db: DB;
|
|
28
29
|
private _session;
|
|
@@ -30,9 +31,7 @@ export default abstract class AbstractTable<TTable extends AbstractTable<TTable>
|
|
|
30
31
|
constructor(db: DB);
|
|
31
32
|
abstract tableName(): string;
|
|
32
33
|
withLogger: (logger: BaseLogger) => void;
|
|
33
|
-
select<
|
|
34
|
-
[name: string]: TColumn;
|
|
35
|
-
} = {}>(partial?: T): SelectTRB<TTable, T>;
|
|
34
|
+
select<T extends EmptyPartial<TTable> = undefined>(partial?: T): SelectTRB<TTable, T>;
|
|
36
35
|
update: () => UpdateTRB<TTable>;
|
|
37
36
|
insert: (value: ExtractModel<TTable>) => InsertTRB<TTable>;
|
|
38
37
|
insertMany: (values: ExtractModel<TTable>[]) => InsertTRB<TTable>;
|
package/tables/abstractTable.js
CHANGED
|
@@ -22,6 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
22
22
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
/* eslint-disable import/no-cycle */
|
|
25
26
|
const pgVarChar_1 = __importDefault(require("../columns/types/pgVarChar"));
|
|
26
27
|
const pgTimestamp_1 = __importDefault(require("../columns/types/pgTimestamp"));
|
|
27
28
|
const pgInteger_1 = __importDefault(require("../columns/types/pgInteger"));
|
package/tables/inferTypes.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { EmptyPartial } from '../builders/highLvlBuilders/joins/selectJoinBuilder';
|
|
1
2
|
import { UpdateCustomExpr } from '../builders/requestBuilders/updates/updates';
|
|
2
3
|
import { AbstractColumn, Column, IndexedColumn } from '../columns/column';
|
|
3
4
|
import ColumnType from '../columns/types/columnType';
|
|
@@ -13,7 +14,7 @@ export declare type ExtractOptionalFieldNames<TTable> = {
|
|
|
13
14
|
export declare type ExtractPartialObjectFromColumns<TTable> = {
|
|
14
15
|
[Key in ExtractFieldNames<TTable>]: TTable[Key];
|
|
15
16
|
} & {
|
|
16
|
-
[Key in ExtractOptionalFieldNames<TTable>]
|
|
17
|
+
[Key in ExtractOptionalFieldNames<TTable>]: TTable[Key];
|
|
17
18
|
};
|
|
18
19
|
export declare type ExtractModel<TTable> = {
|
|
19
20
|
[Key in ExtractFieldNames<TTable>]: ExtractCodeType<TTable[Key]>;
|
|
@@ -32,7 +33,10 @@ export declare type AnyColumn = Column<ColumnType, boolean, boolean> | IndexedCo
|
|
|
32
33
|
export declare type PartialFor<TTable extends AbstractTable<TTable>> = {
|
|
33
34
|
[name: string]: AbstractColumn<ColumnType<any>, boolean, boolean, TTable>;
|
|
34
35
|
};
|
|
35
|
-
export declare type
|
|
36
|
+
export declare type TableAsColumns<TTable extends AbstractTable<TTable>> = {
|
|
37
|
+
[name: string]: AbstractColumn<ColumnType<any>, boolean, boolean, TTable>;
|
|
38
|
+
};
|
|
39
|
+
export declare type FullOrPartial<TTable extends AbstractTable<TTable>, TPartial extends EmptyPartial<TTable>> = [keyof TPartial] extends [never] ? ExtractModel<TTable> : ExtractModel<TPartial>;
|
|
36
40
|
export declare type CheckTwoTypes<TInput, TTable extends AbstractTable<TTable>, TTable1 extends AbstractTable<TTable1>> = TInput extends AbstractTable<TTable> ? TTable : TInput extends AbstractTable<TTable1> ? TTable1 : never;
|
|
37
41
|
export declare type CheckThreeTypes<TInput, TTable extends AbstractTable<TTable>, TTable1 extends AbstractTable<TTable1>, TTable2 extends AbstractTable<TTable2>> = TInput extends AbstractTable<TTable> ? TTable : TInput extends AbstractTable<TTable1> ? TTable1 : TInput extends AbstractTable<TTable2> ? TTable2 : never;
|
|
38
42
|
export declare type CheckFourTypes<TInput, TTable extends AbstractTable<TTable>, TTable1 extends AbstractTable<TTable1>, TTable2 extends AbstractTable<TTable2>, TTable3 extends AbstractTable<TTable3>> = TInput extends AbstractTable<TTable> ? TTable : TInput extends AbstractTable<TTable1> ? TTable1 : TInput extends AbstractTable<TTable2> ? TTable2 : TInput extends AbstractTable<TTable3> ? TTable3 : never;
|
package/test.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/test.js
CHANGED
|
@@ -1,133 +1,166 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
// .
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
//
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
//
|
|
87
|
-
//
|
|
88
|
-
//
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
//
|
|
92
|
-
//
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
//
|
|
96
|
-
//
|
|
97
|
-
//
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
-
//
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
//
|
|
117
|
-
//
|
|
118
|
-
//
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
//
|
|
132
|
-
//
|
|
133
|
-
//
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* eslint-disable max-len */
|
|
7
|
+
const dbConnector_1 = __importDefault(require("./db/dbConnector"));
|
|
8
|
+
const usersTable_1 = __importDefault(require("./docs/tables/usersTable"));
|
|
9
|
+
const consoleLogger_1 = __importDefault(require("./logger/consoleLogger"));
|
|
10
|
+
const citiesTable_1 = __importDefault(require("./docs/tables/citiesTable"));
|
|
11
|
+
const userGroupsTable_1 = __importDefault(require("./docs/tables/userGroupsTable"));
|
|
12
|
+
const static_1 = require("./builders/requestBuilders/where/static");
|
|
13
|
+
const usersToUserGroups_1 = __importDefault(require("./docs/tables/usersToUserGroups"));
|
|
14
|
+
(async () => {
|
|
15
|
+
try {
|
|
16
|
+
console.log('dd');
|
|
17
|
+
const db = await new dbConnector_1.default()
|
|
18
|
+
.connectionString('postgresql://postgres@127.0.0.1/migrator')
|
|
19
|
+
.connect();
|
|
20
|
+
db.useLogger(new consoleLogger_1.default());
|
|
21
|
+
const citiesTable = new citiesTable_1.default(db);
|
|
22
|
+
const usersTable = new usersTable_1.default(db);
|
|
23
|
+
const userGroupsTable = new userGroupsTable_1.default(db);
|
|
24
|
+
const usersToUserGroupsTable = new usersToUserGroups_1.default(db);
|
|
25
|
+
// // group case
|
|
26
|
+
// const usersWithUserGroups = await usersToUserGroupsTable.select()
|
|
27
|
+
// .where(eq(userGroupsTable.id, 1))
|
|
28
|
+
// .leftJoinV1(UsersTable,
|
|
29
|
+
// (userToGroup) => userToGroup.userId,
|
|
30
|
+
// (users) => users.id,
|
|
31
|
+
// {
|
|
32
|
+
// id: usersTable.id,
|
|
33
|
+
// })
|
|
34
|
+
// .leftJoin(UsersToUserGroupsTable, UserGroupsTable,
|
|
35
|
+
// (userToGroup) => userToGroup.groupId,
|
|
36
|
+
// (userGroup) => userGroup.id,
|
|
37
|
+
// {
|
|
38
|
+
// id: userGroupsTable.id,
|
|
39
|
+
// })
|
|
40
|
+
// .execute();
|
|
41
|
+
await citiesTable.select({
|
|
42
|
+
id: citiesTable.id,
|
|
43
|
+
userId: citiesTable.userId,
|
|
44
|
+
})
|
|
45
|
+
.where(static_1.eq(citiesTable.id, 1))
|
|
46
|
+
.leftJoin(usersTable, (cities, users) => static_1.onEq(cities.userId, users.id))
|
|
47
|
+
.execute();
|
|
48
|
+
const usersWithUserGroups = await usersToUserGroupsTable.select()
|
|
49
|
+
.where(static_1.eq(userGroupsTable.id, 1))
|
|
50
|
+
.leftJoin(usersTable, (usersToUserGroups, users) => static_1.onEq(usersToUserGroups.userId, users.id))
|
|
51
|
+
.leftJoin(userGroupsTable, (usersToUserGroups, _users, userGroups) => static_1.onEq(usersToUserGroups.groupId, userGroups.id))
|
|
52
|
+
.execute();
|
|
53
|
+
const userWithCities = await citiesTable.select()
|
|
54
|
+
.where(static_1.eq(citiesTable.id, 1))
|
|
55
|
+
.leftJoin(usersTable, (cities, users) => static_1.onEq(cities.userId, users.id))
|
|
56
|
+
.execute();
|
|
57
|
+
await citiesTable.select()
|
|
58
|
+
.where(static_1.eq(citiesTable.location, 'q'))
|
|
59
|
+
.leftJoin(usersTable, (cities, _users) => static_1.eq(cities.id, 13))
|
|
60
|
+
.execute();
|
|
61
|
+
await citiesTable.select()
|
|
62
|
+
.leftJoin(usersTable, (cities, _users) => static_1.and([
|
|
63
|
+
static_1.eq(cities.id, 13), static_1.notEq(cities.id, 14),
|
|
64
|
+
]))
|
|
65
|
+
.execute();
|
|
66
|
+
await citiesTable.select()
|
|
67
|
+
.where(static_1.eq(citiesTable.location, 'location'))
|
|
68
|
+
.leftJoin(usersTable, (_cities, _users) => static_1.raw('<custom expression after ON statement>'))
|
|
69
|
+
.execute();
|
|
70
|
+
// const res1 = newJoinRes1.map((args, arg2, arg3) => ({ city: args, user: arg2 }));
|
|
71
|
+
// console.log(res1);
|
|
72
|
+
// const newJoinRes1 = await citiesTable.select()
|
|
73
|
+
// .leftJoin(usersTable, (cities, firstJoinUsers) => onEq(cities.userId, firstJoinUsers.id))
|
|
74
|
+
// .execute();
|
|
75
|
+
// const res1 = newJoinRes1.map((args, arg2) => ({ city: args, user: arg2 }));
|
|
76
|
+
// console.log(res1);
|
|
77
|
+
// const newJoinRes2 = await citiesTable.select({ customId: citiesTable.userId })
|
|
78
|
+
// .leftJoin(usersTable, (cities, firstJoinUsers) => onEq(cities.customId, firstJoinUsers.id))
|
|
79
|
+
// .execute();
|
|
80
|
+
// const res2 = newJoinRes2.map((args, arg2) => ({ city: args, user: arg2 }));
|
|
81
|
+
// console.log(res2);
|
|
82
|
+
// const joinsWithPartialObjectResponse = await citiesTable.select({ customUserId: citiesTable.userId })
|
|
83
|
+
// .leftJoin(usersTable,
|
|
84
|
+
// (cities, partialUsers) => onEq(cities.customUserId, partialUsers.customId),
|
|
85
|
+
// { customId: usersTable.id })
|
|
86
|
+
// .leftJoin(usersTable,
|
|
87
|
+
// (_cities, partialUsers, anotherUsersJoin) => onEq(partialUsers.customId, anotherUsersJoin.anotherId),
|
|
88
|
+
// { anotherId: usersTable.id })
|
|
89
|
+
// .execute();
|
|
90
|
+
// const newJoinRes3 = await citiesTable.select()
|
|
91
|
+
// .leftJoin(usersTable, (cities, firstJoinUsers) => onEq(cities.userId, firstJoinUsers.customUserId), { customUserId: usersTable.id })
|
|
92
|
+
// .execute();
|
|
93
|
+
// const res3 = newJoinRes3.map((args, arg2) => ({ city: args, user: arg2 }));
|
|
94
|
+
// console.log(res3);
|
|
95
|
+
// const newJoinRes4 = await citiesTable.select({ customId: citiesTable.userId })
|
|
96
|
+
// .leftJoin(usersTable, (cities, firstJoinUsers) => onEq(cities.customId, firstJoinUsers.customUserId), { customUserId: usersTable.id })
|
|
97
|
+
// .execute();
|
|
98
|
+
// const res4 = newJoinRes4.map((args, arg2) => ({ city: args, user: arg2 }));
|
|
99
|
+
// console.log(res4);
|
|
100
|
+
// const newJoinRes5 = await usersToUserGroupsTable.select()
|
|
101
|
+
// .leftJoin(usersTable, (usersToUserGroup, users) => onEq(usersToUserGroup.userId, users.id))
|
|
102
|
+
// .leftJoin(userGroupsTable, (usersToUserGroup, user, userGroup) => onEq(usersToUserGroup.groupId, userGroup.id))
|
|
103
|
+
// .execute();
|
|
104
|
+
// const res5 = newJoinRes5.map((_u, user, group) => ({
|
|
105
|
+
// user,
|
|
106
|
+
// group,
|
|
107
|
+
// }));
|
|
108
|
+
// console.log(res5);
|
|
109
|
+
// const newJoinRes6 = await usersToUserGroupsTable.select()
|
|
110
|
+
// .leftJoin(usersTable, (usersToUserGroup, users) => onEq(usersToUserGroup.userId, users.id))
|
|
111
|
+
// .leftJoin(userGroupsTable, (usersToUserGroup, userGroup) => onEq(usersToUserGroup.groupId, userGroup.id))
|
|
112
|
+
// .leftJoin(usersTable, (usersToUserGroup, users, usersToUserGroup1, users1) => onEq(usersToUserGroup.userId, users.id))
|
|
113
|
+
// .leftJoin(userGroupsTable, (usersToUserGroup, user, usersToUserGroup1, users1, userGroupsTable2) => onEq(usersToUserGroup.groupId, usersToUserGroup1.id))
|
|
114
|
+
// .leftJoin(usersTable, (usersToUserGroup, user, usersToUserGroup1, users1, userGroupsTable2, users2) => onEq(usersToUserGroup.userId, users1.id))
|
|
115
|
+
// .leftJoin(userGroupsTable, (usersToUserGroup, user, usersToUserGroup1, users1, userGroupsTable2, users2, userGroupsTable3) => onEq(users1.id, userGroupsTable3.id))
|
|
116
|
+
// .execute();
|
|
117
|
+
// const res6 = newJoinRes5.map((_u, user, group) => ({
|
|
118
|
+
// user,
|
|
119
|
+
// group,
|
|
120
|
+
// }));
|
|
121
|
+
// console.log(res6);
|
|
122
|
+
// const res = await citiesTable.update()
|
|
123
|
+
// // .groupBy((table, join1, join2, join3) => [table.id, join1.id, join1.phone])
|
|
124
|
+
// .where(eq(citiesTable.location, 'YR'))
|
|
125
|
+
// .set({
|
|
126
|
+
// metadata: [{
|
|
127
|
+
// fallback_image: true,
|
|
128
|
+
// team_image: 'https://files.slack.com/files-pri/T016CCC3FE3-F03461UR9M5/clinic_team_photo_1.jpg?pub_secret=560c098bfb',
|
|
129
|
+
// image_alt_text: 'Generic Physiotherapy Clinic image 1',
|
|
130
|
+
// logo: '',
|
|
131
|
+
// logo_alt_text: ''
|
|
132
|
+
// }],
|
|
133
|
+
// })
|
|
134
|
+
// // .leftJoin(CitiesTable, UsersTable, (table) => table.userId, (table) => table.id)
|
|
135
|
+
// // .leftJoin(UsersTable, UsersTable, (table) => table.id, (table) => table.id)
|
|
136
|
+
// .execute();
|
|
137
|
+
// console.log(res);
|
|
138
|
+
// const ser = new MigrationSerializer();
|
|
139
|
+
// const res = ser.generate([usersTable as AbstractTable<UsersTable>], []);
|
|
140
|
+
// console.log(JSON.stringify(res, null, 2));
|
|
141
|
+
// const f = {
|
|
142
|
+
// id: count(usersTable.id),
|
|
143
|
+
// };
|
|
144
|
+
// type d = ExtractModel<typeof f>;
|
|
145
|
+
// const res = await usersTable.select()
|
|
146
|
+
// .leftJoin(UsersTable, (table) => table.id, (table) => table.id)
|
|
147
|
+
// .leftJoin(UsersTable, CitiesTable, (table) => table.id, (table) => table.id)
|
|
148
|
+
// .execute();
|
|
149
|
+
// const res = await usersTable.select()
|
|
150
|
+
// // .groupBy((table, join1, join2, join3) => [table.id, join1.id, join1.phone])
|
|
151
|
+
// .leftJoin(UsersTable, (table) => table.foundationDate, (table) => table.id)
|
|
152
|
+
// .leftJoin(UsersTable, UsersTable, (table) => table.role, (table) => table.id)
|
|
153
|
+
// .leftJoin(UsersTable, UsersTable, (table) => table.role, (table) => table.id)
|
|
154
|
+
// // .groupBy({
|
|
155
|
+
// // usersTable: usersTable.id,
|
|
156
|
+
// // firstJoin: [usersTable.id],
|
|
157
|
+
// // secondJoin: usersTable.id,
|
|
158
|
+
// // thirdJoin: usersTable.id,
|
|
159
|
+
// // })
|
|
160
|
+
// .execute();
|
|
161
|
+
// console.log(res);
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
console.log(e);
|
|
165
|
+
}
|
|
166
|
+
})();
|