Libraries
    Preparing search index...

    Function createDb

    • Create a typed database context.

      Type Parameters

      Parameters

      • knex: Knex
      • entities: TMap
      • opts: { tracking: true }

      Returns TrackedDbContext<TMap>

      const TodoEntity = defineEntity(TodoSchema)
      .belongsTo(t => t.author, l => l.userId, r => r.id);

      const UserEntity = defineEntity(UserSchema);

      const db = createDb(knex, { todos: TodoEntity, users: UserEntity });

      const todo = await db.todos
      .where(t => t.id, '=', 42)
      .include(t => t.author)
      .first();

      await db.transaction(async dbTrx => {
      const u = await dbTrx.users.insert({ email: 'a@b.c', role: 'user', authProvider: 'local', createdAt: new Date() });
      await dbTrx.todos.insert({ title: 'Hi', userId: u.id, completed: false, createdAt: new Date(), updatedAt: new Date() });
      });
    • Create a typed database context.

      Type Parameters

      Parameters

      • knex: Knex
      • entities: TMap
      • Optionalopts: { tracking?: false }

      Returns DbContext<TMap>

      const TodoEntity = defineEntity(TodoSchema)
      .belongsTo(t => t.author, l => l.userId, r => r.id);

      const UserEntity = defineEntity(UserSchema);

      const db = createDb(knex, { todos: TodoEntity, users: UserEntity });

      const todo = await db.todos
      .where(t => t.id, '=', 42)
      .include(t => t.author)
      .first();

      await db.transaction(async dbTrx => {
      const u = await dbTrx.users.insert({ email: 'a@b.c', role: 'user', authProvider: 'local', createdAt: new Date() });
      await dbTrx.todos.insert({ title: 'Hi', userId: u.id, completed: false, createdAt: new Date(), updatedAt: new Date() });
      });