forj 0.1.8 → 0.1.10

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "forj",
3
3
  "description": "SQLite ORM and Query Builder whitout dependencies",
4
- "version": "0.1.8",
4
+ "version": "0.1.10",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
7
7
  "files": ["src"],
@@ -21,8 +21,9 @@
21
21
  "dependencies": {
22
22
  "@aws-sdk/client-dynamodb": "^3.1003.0",
23
23
  "@aws-sdk/lib-dynamodb": "^3.1003.0",
24
+ "pathe": "^2.0",
24
25
  "pluralize": "^8.0",
25
- "t0n": "^0.1.12",
26
+ "t0n": "^0.1.13",
26
27
  "zod": "^4.3.6"
27
28
  },
28
29
  "devDependencies": {
@@ -3,7 +3,11 @@ import { sqlName, tableSlug } from '../utils'
3
3
  import type { ColumnDefinition, IndexDefinition, ForeignKeyDefinition } from './types'
4
4
 
5
5
  export default class SchemaBuilder {
6
- static create(blueprint: Blueprint, exist: boolean = false): string {
6
+ static create(
7
+ blueprint: Blueprint,
8
+ exist: boolean = false,
9
+ rowId: boolean = true
10
+ ): string {
7
11
  const table = sqlName(blueprint.table)
8
12
  const columns = blueprint.columns
9
13
  const indexes = blueprint.indexes
@@ -19,7 +23,9 @@ export default class SchemaBuilder {
19
23
  ...foreignKeyDefinitions
20
24
  ].filter(Boolean)
21
25
 
22
- return `CREATE TABLE ${exist ? 'IF NOT EXISTS ' : ''}${table} (\n ${allDefinitions.join(',\n ')}\n);`
26
+ return `CREATE TABLE ${exist ? 'IF NOT EXISTS ' : ''}${table} (\n ${allDefinitions.join(',\n ')}\n)${
27
+ rowId ? '' : ' WITHOUT ROWID'
28
+ };`
23
29
  }
24
30
 
25
31
  static alter(blueprint: Blueprint): string[] {
@@ -1,11 +1,12 @@
1
1
  import glob from 'tiny-glob'
2
2
  import { mkdirSync, existsSync, writeFileSync } from 'node:fs'
3
- import { dirname, join, resolve } from 'node:path'
4
- import { Datte, IMPORT } from 't0n'
3
+ import { dirname, join } from 'pathe'
4
+ import { Datte, Envir, IMPORT } from 't0n'
5
5
  import { Schema } from './schema'
6
6
  import { MigrationInfo, MigrationClass, Queue } from './types'
7
7
 
8
- const __root = resolve(dirname(new URL(import.meta.url).pathname), '../../../..')
8
+ export const _forj = join(dirname(new URL(import.meta.url).pathname), '..')
9
+ export const _root = Envir.get('npm_config_local_prefix') || Envir.get('PWD') || join(_forj, '../../../')
9
10
 
10
11
  export class Migrator {
11
12
  static #input: string
@@ -24,17 +25,17 @@ export class Migrator {
24
25
  }
25
26
 
26
27
  static dir(dir?: string) {
27
- this.#input = join(__root, 'migrations', dir || '')
28
+ this.#input = join(_root, 'migrations', dir || '')
28
29
  this.#output = join(this.#input, 'sql')
29
30
  return this
30
31
  }
31
32
 
32
33
  static inputDir(dir: string) {
33
- this.#input = join(__root, dir)
34
+ this.#input = join(_root, dir)
34
35
  return this
35
36
  }
36
37
  static outputDir(dir: string) {
37
- this.#output = join(__root, dir)
38
+ this.#output = join(_root, dir)
38
39
  return this
39
40
  }
40
41
 
@@ -88,7 +89,7 @@ export class Migrator {
88
89
  if (!match) return null
89
90
  const [, year, month, day, hour, minute, second, slugName] = match
90
91
 
91
- const input = join(__root, fileName)
92
+ const input = join(_root, fileName)
92
93
  const output = join(this.#output, name +'.sql')
93
94
  const mod = await IMPORT(input)
94
95
  const handler = mod.default as MigrationClass
@@ -47,8 +47,8 @@ export class Schema {
47
47
  return blueprint
48
48
  }
49
49
 
50
- static create(table: string, fn: BlueprintFn, exist: boolean = false) {
51
- this.#addStatement(Builder.create(this.#blueprint(table, fn), exist))
50
+ static create(table: string, fn: BlueprintFn, exist: boolean = false, rowId: boolean = true) {
51
+ this.#addStatement(Builder.create(this.#blueprint(table, fn), exist, rowId))
52
52
  }
53
53
 
54
54
  static createIfNotExists(table: string, fn: BlueprintFn) {
@@ -66,7 +66,15 @@ export class Schema {
66
66
  this.#addStatement(Builder.create(this.#blueprint(table, fn as BlueprintFn, (table: Blueprint) => {
67
67
  columns.forEach(column => table.foreignId(column))
68
68
  table.primary(table.columns.map(c => c.name))
69
- })).slice(0, -1) + ' WITHOUT ROWID;')
69
+ }), false, false))
70
+ }
71
+
72
+ static createWithoutRowId(table: string, fn: BlueprintFn, exist: boolean = false) {
73
+ this.create(table, fn, false, false)
74
+ }
75
+
76
+ static createIfNotExistsWithoutRowId(table: string, fn: BlueprintFn) {
77
+ this.create(table, fn, true, false)
70
78
  }
71
79
 
72
80
  static table(table: string, fn: BlueprintFn) {
package/src/model.ts CHANGED
@@ -38,7 +38,8 @@ export default abstract class Model<TB extends keyof DB, DB> {
38
38
  M extends typeof Model<TB, DB>,
39
39
  I extends InstanceType<M>,
40
40
  T extends I['$TShape'],
41
- >(this: M, data: Record<keyof T, Value>) {
41
+ C extends keyof T
42
+ >(this: M, data: Record<C, Value>) {
42
43
  return this.builder<I['$DBShape'], T>().insert(data)
43
44
  }
44
45
 
@@ -46,7 +47,8 @@ export default abstract class Model<TB extends keyof DB, DB> {
46
47
  M extends typeof Model<TB, DB>,
47
48
  I extends InstanceType<M>,
48
49
  T extends I['$TShape'],
49
- >(this: M, data: Record<keyof T, Value>) {
50
+ C extends keyof T
51
+ >(this: M, data: Record<C, Value>) {
50
52
  return this.builder<I['$DBShape'], T>().update(data)
51
53
  }
52
54
 
@@ -77,7 +77,7 @@ export default class QueryBuilder<
77
77
  return await this.#pipe?.run(this)
78
78
  }
79
79
 
80
- #setType(type: QueryType, data?: Partial<Record<C, Value>>) {
80
+ #setType<K extends keyof T>(type: QueryType, data?: Partial<Record<K, Value>>) {
81
81
  this.#type = type
82
82
  if (data) {
83
83
  this.#keys = Object.keys(data)
@@ -87,14 +87,14 @@ export default class QueryBuilder<
87
87
  return this
88
88
  }
89
89
 
90
- insert(data: Partial<Record<C, Value>>) {
90
+ insert<K extends keyof T>(data: Partial<Record<K, Value>>) {
91
91
  if (this.#opts.timestamps || this.#opts.createdAt) // @ts-ignore
92
92
  data.created_at = Timestamp.now()
93
93
 
94
94
  return this.#setType(types.INSERT, data)
95
95
  }
96
96
 
97
- update(data: Partial<Record<C, Value>>) {
97
+ update<K extends keyof T>(data: Partial<Record<K, Value>>) {
98
98
  if (this.#opts.timestamps || this.#opts.updatedAt) // @ts-ignore
99
99
  data.updated_at = Timestamp.now()
100
100