drizzle-orm 0.9.7 → 0.9.11

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.
@@ -51,7 +51,7 @@ export declare class Column<T extends ColumnType, TNullable extends boolean = tr
51
51
  constructor(parent: AbstractTable<any>, columnName: string, columnType: T, nullable: TNullable);
52
52
  serial(): Column<T, false, true>;
53
53
  primaryKey(): Column<T, TAutoIncrement extends true ? true : false, TAutoIncrement>;
54
- foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => Column<T, boolean, boolean>, onConstraint: {
54
+ foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => Column<T, boolean, boolean>, onConstraint?: {
55
55
  onDelete?: 'CASCADE' | 'RESTRICT';
56
56
  onUpdate?: 'CASCADE' | 'RESTRICT';
57
57
  }): Column<T, TNullable, TAutoIncrement>;
@@ -61,7 +61,7 @@ export declare class IndexedColumn<T extends ColumnType, TNullable extends boole
61
61
  constructor(parent: AbstractTable<any>, columnName: string, columnType: T, nullable: TNullable);
62
62
  serial(): IndexedColumn<T, false, true>;
63
63
  primaryKey(): IndexedColumn<T, TAutoIncrement extends true ? true : false, TAutoIncrement>;
64
- foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => IndexedColumn<T, boolean, boolean>, onConstraint: {
64
+ foreignKey<ITable extends AbstractTable<ITable>>(table: new (db: DB) => ITable, callback: (table: ITable) => IndexedColumn<T, boolean, boolean>, onConstraint?: {
65
65
  onDelete?: 'CASCADE' | 'RESTRICT';
66
66
  onUpdate?: 'CASCADE' | 'RESTRICT';
67
67
  }): IndexedColumn<T, TNullable, TAutoIncrement>;
package/columns/column.js CHANGED
@@ -59,8 +59,8 @@ class Column extends AbstractColumn {
59
59
  foreignKey(table, callback, onConstraint) {
60
60
  const tableInstance = this.getParent().db.create(table);
61
61
  this.referenced = callback(tableInstance);
62
- this.onDelete = onConstraint.onDelete ? `ON DELETE ${onConstraint.onDelete}` : undefined;
63
- this.onUpdate = onConstraint.onUpdate ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
62
+ this.onDelete = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onDelete) ? `ON DELETE ${onConstraint.onDelete}` : undefined;
63
+ this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
64
64
  return this;
65
65
  }
66
66
  autoIncrement() {
@@ -86,8 +86,8 @@ class IndexedColumn extends AbstractColumn {
86
86
  foreignKey(table, callback, onConstraint) {
87
87
  // eslint-disable-next-line new-cap
88
88
  this.referenced = callback(this.getParent().db.create(table));
89
- this.onDelete = onConstraint.onDelete ? `ON DELETE ${onConstraint.onDelete}` : undefined;
90
- this.onUpdate = onConstraint.onUpdate ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
89
+ this.onDelete = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onDelete) ? `ON DELETE ${onConstraint.onDelete}` : undefined;
90
+ this.onUpdate = (onConstraint === null || onConstraint === void 0 ? void 0 : onConstraint.onUpdate) ? `ON UPDATE ${onConstraint.onUpdate}` : undefined;
91
91
  return this;
92
92
  }
93
93
  autoIncrement() {
@@ -3,5 +3,5 @@ export default abstract class ColumnType<TCodeType = {}> {
3
3
  protected abstract dbName: string;
4
4
  abstract getDbName(): string;
5
5
  abstract insertStrategy(value: TCodeType): string;
6
- abstract selectStrategy(value: any): TCodeType;
6
+ abstract selectStrategy(value: any): TCodeType | undefined;
7
7
  }
@@ -6,5 +6,5 @@ export default class PgBigDecimal extends ColumnType<number> {
6
6
  constructor(precision?: number, scale?: number);
7
7
  getDbName: () => string;
8
8
  insertStrategy: (value: number) => string;
9
- selectStrategy(value: string): number;
9
+ selectStrategy(value: string): number | undefined;
10
10
  }
@@ -22,7 +22,7 @@ class PgBigDecimal extends columnType_1.default {
22
22
  }
23
23
  }
24
24
  selectStrategy(value) {
25
- return parseFloat(value);
25
+ return value ? parseFloat(value) : undefined;
26
26
  }
27
27
  }
28
28
  exports.default = PgBigDecimal;
@@ -4,5 +4,5 @@ export default class PgBigInt extends ColumnType<number> {
4
4
  constructor();
5
5
  getDbName(): string;
6
6
  insertStrategy: (value: number) => string;
7
- selectStrategy(value: string): number;
7
+ selectStrategy(value: string): number | undefined;
8
8
  }
@@ -11,7 +11,7 @@ class PgBigInt extends columnType_1.default {
11
11
  return this.dbName;
12
12
  }
13
13
  selectStrategy(value) {
14
- return parseInt(value, 10);
14
+ return value ? parseInt(value, 10) : undefined;
15
15
  }
16
16
  }
17
17
  exports.default = PgBigInt;
@@ -4,5 +4,5 @@ export default class PgInteger extends ColumnType<number> {
4
4
  constructor();
5
5
  getDbName: () => string;
6
6
  insertStrategy: (value: number) => string;
7
- selectStrategy(value: string): number;
7
+ selectStrategy(value: string): number | undefined;
8
8
  }
@@ -9,7 +9,7 @@ class PgInteger extends columnType_1.default {
9
9
  this.dbName = 'INT';
10
10
  }
11
11
  selectStrategy(value) {
12
- return parseInt(value, 10);
12
+ return value ? parseInt(value, 10) : undefined;
13
13
  }
14
14
  }
15
15
  exports.default = PgInteger;
@@ -0,0 +1,8 @@
1
+ import ColumnType from './columnType';
2
+ export default class PgSmallInt extends ColumnType<number> {
3
+ dbName: string;
4
+ constructor();
5
+ getDbName: () => string;
6
+ insertStrategy: (value: number) => string;
7
+ selectStrategy(value: string): number | undefined;
8
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const columnType_1 = require("./columnType");
4
+ class PgSmallInt extends columnType_1.default {
5
+ constructor() {
6
+ super();
7
+ this.getDbName = () => this.dbName;
8
+ this.insertStrategy = (value) => `${value}`;
9
+ this.dbName = 'SMALLINT';
10
+ }
11
+ selectStrategy(value) {
12
+ return value ? parseInt(value, 10) : undefined;
13
+ }
14
+ }
15
+ exports.default = PgSmallInt;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drizzle-orm",
3
- "version": "0.9.7",
3
+ "version": "0.9.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,6 @@ class MigrationSerializer {
15
15
  const columnToReturn = {};
16
16
  const indexToReturn = {};
17
17
  for (const properties of tableEntries) {
18
- const key = properties[0];
19
18
  const value = properties[1];
20
19
  if (value instanceof tableIndex_1.default) {
21
20
  const columns = value.getColumns();
@@ -33,7 +32,7 @@ class MigrationSerializer {
33
32
  };
34
33
  }
35
34
  if (value instanceof columns_1.Column) {
36
- columnToReturn[key] = {
35
+ columnToReturn[value.getColumnName()] = {
37
36
  name: value.getColumnName(),
38
37
  type: value.isAutoIncrement() ? 'serial' : value.getColumnType().getDbName(),
39
38
  primaryKey: !!value.primaryKeyName,
@@ -43,7 +42,7 @@ class MigrationSerializer {
43
42
  };
44
43
  const referenced = value.getReferenced();
45
44
  if (referenced) {
46
- columnToReturn[key].references = {
45
+ columnToReturn[value.getColumnName()].references = {
47
46
  foreignKeyName: `${value.getParent().tableName()}_${value.getColumnName()}_fk`,
48
47
  table: referenced.getParentName(),
49
48
  column: referenced.getColumnName(),
@@ -63,6 +63,12 @@ export default abstract class AbstractTable<TTable extends AbstractTable<TTable>
63
63
  protected int(name: string, params: {
64
64
  notNull: true;
65
65
  }): Column<PgInteger, false>;
66
+ protected smallInt(name: string, params?: {
67
+ notNull: false;
68
+ }): Column<PgInteger, true>;
69
+ protected smallInt(name: string, params: {
70
+ notNull: true;
71
+ }): Column<PgInteger, false>;
66
72
  protected timestamp(name: string, params?: {
67
73
  notNull: false;
68
74
  }): Column<PgTimestamp, true>;
@@ -16,6 +16,7 @@ const pgBigInt_1 = require("../columns/types/pgBigInt");
16
16
  const pgEnum_1 = require("../columns/types/pgEnum");
17
17
  const column_1 = require("../columns/column");
18
18
  const tableIndex_1 = require("../indexes/tableIndex");
19
+ const pgSmallInt_1 = require("../columns/types/pgSmallInt");
19
20
  class AbstractTable {
20
21
  constructor(db) {
21
22
  this.withLogger = (logger) => {
@@ -79,6 +80,10 @@ class AbstractTable {
79
80
  var _a;
80
81
  return new column_1.Column(this, name, new pgInteger_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
81
82
  }
83
+ smallInt(name, params = {}) {
84
+ var _a;
85
+ return new column_1.Column(this, name, new pgSmallInt_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
86
+ }
82
87
  timestamp(name, params = {}) {
83
88
  var _a;
84
89
  return new column_1.Column(this, name, new pgTimestamp_1.default(), (_a = !(params === null || params === void 0 ? void 0 : params.notNull)) !== null && _a !== void 0 ? _a : false);
package/test.js CHANGED
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const _1 = require(".");
4
4
  const usersTable_1 = require("./docs/tables/usersTable");
5
- const serializer_1 = require("./serializer/serializer");
6
5
  // import { Pool } from 'pg';
7
6
  // import { DB } from '.';
8
7
  // import { DB, DbConnector } from '.';
@@ -20,12 +19,14 @@ const fromTypeFile = (filepath) => {
20
19
  (async () => {
21
20
  try {
22
21
  const db = await new _1.DbConnector()
23
- .connectionString('postgresql://postgres@127.0.0.1/drizzle')
22
+ .connectionString('postgresql://postgres@127.0.0.1/migrator')
24
23
  .connect();
25
- const ser = new serializer_1.default();
26
- const d = db.create(usersTable_1.default);
27
- const f = ser.generate([d], []);
28
- console.log(JSON.stringify(f, null, 2));
24
+ // const ser = new MigrationSerializer();
25
+ // const d = db.create(UsersTable) as unknown as AbstractTable<any>;
26
+ // const f = ser.generate([d], []);
27
+ // console.log(JSON.stringify(f, null, 2));
28
+ // await drizzle.migrator(db).migrate('src/drizzle.config.yaml');
29
+ // drizzle.migrator(db).migrate({ migrationFolder: '' });
29
30
  // const typesFileNames = fs.readdirSync('/Users/andrewsherman/IdeaProjects/datalayer-orm/src/examples/types');
30
31
  // typesFileNames.forEach((filename) => {
31
32
  // const types = fromTypeFile(`./examples/types/${filename.split('.')[0]}`);
@@ -38,9 +39,10 @@ const fromTypeFile = (filepath) => {
38
39
  // const db = await new DbConnector()
39
40
  // .connectionString('postgresql://postgres@127.0.0.1/drizzle')
40
41
  // .connect();
41
- // const res = await db.session().execute(
42
- // Create.table(db.create(UsersTable)).build(),
43
- // );
42
+ const usersTable = new usersTable_1.default(db);
43
+ const res = await db.session().execute(_1.Create.table(db.create(usersTable_1.default)).build());
44
+ // await usersTable.insert({
45
+ // }).execute();
44
46
  // if (res.isLeft()) {
45
47
  // console.log(res.value.reason);
46
48
  // } else {